Exemplo n.º 1
0
def work():
    sql_execute('drop table "wiki_history";')
    sql_execute('drop table "wiki_item";')
    sql_execute('drop table "wiki_article";')
    sql_execute('drop table "statistic24h";')
    sql_execute('drop table "statistic24h_log";')
    sql_execute('ALTER TABLE statistic RENAME TO post_stats;')
    sql_execute('ALTER TABLE post_stats ADD edit_count int DEFAULT 0 NULL;')
    sql_execute('ALTER TABLE post_stats DROP viewed_users;')
    sql_execute('ALTER TABLE post_stats DROP edited_users;')
    sql_execute('ALTER TABLE post_stats DROP commented_users;')
    sql_execute('ALTER TABLE post_stats DROP bookmarked_users;')
    sql_execute('ALTER TABLE post_stats DROP upvoted_users;')
    sql_execute('ALTER TABLE post_stats DROP downvoted_users;')
    sql_execute('ALTER TABLE post_stats DROP thanked_users;')
    sql_execute(
        'ALTER TABLE post_stats ADD last_edit_time bigint DEFAULT NULL NULL;')
    sql_execute(
        'ALTER TABLE post_stats ADD last_edit_user_id BYTEA DEFAULT NULL NULL;'
    )
    sql_execute(
        'CREATE INDEX post_stats_last_edit_time_index ON post_stats (last_edit_time);'
    )
    sql_execute(
        'ALTER TABLE post_stats ADD update_time bigint DEFAULT NULL NULL;')
    sql_execute('CREATE INDEX update_time_index ON post_stats (update_time);')
    sql_execute(
        r"ALTER TABLE user_notif_last_info ADD last_manage_log_id bytea DEFAULT '\x00' NULL;"
    )
    sql_execute('ALTER TABLE notif ALTER COLUMN from_post_type DROP NOT NULL;')
    sql_execute('ALTER TABLE notif ALTER COLUMN from_post_id DROP NOT NULL;')

    sql_execute(
        'ALTER TABLE "user" ADD is_wiki_editor BOOLEAN DEFAULT FALSE NOT NULL;'
    )
    sql_execute(
        'ALTER TABLE "user" ADD is_board_moderator BOOLEAN DEFAULT FALSE  NOT NULL;'
    )
    sql_execute(
        'ALTER TABLE "user" ADD is_forum_master BOOLEAN DEFAULT FALSE NOT NULL;'
    )
    sql_execute(
        'CREATE INDEX user_is_wiki_editor_index ON "user" (is_wiki_editor);')
    sql_execute(
        'CREATE INDEX user_is_board_moderator_index ON "user" (is_board_moderator);'
    )
    sql_execute(
        'CREATE INDEX user_is_forum_master_index ON "user" (is_forum_master);')

    # 移除 MOP.TOPIC_TITLE_CHANGE 300
    ManageLog.update(operation=MOP.POST_TITLE_CHANGE).where(
        ManageLog.operation == 300).execute()
    # 移除 MOP.TOPIC_CONTENT_CHANGE 301
    ManageLog.update(operation=MOP.POST_CONTENT_CHANGE).where(
        ManageLog.operation == 301).execute()
    # 移除 MOP.COMMENT_STATE_CHANGE 500
    ManageLog.update(operation=MOP.POST_STATE_CHANGE).where(
        ManageLog.operation == 500).execute()

    # BOARD_NEW 200
    for i in ManageLog.select().where(ManageLog.operation == 200):
        i.operation = MOP.POST_CREATE
        i.value = {'title': i.value}
        i.save()

    for i in ManageLog.select():
        if i and i.value:
            if i.operation == MOP.POST_CREATE:
                # wiki 改动
                if not isinstance(i.value, dict):
                    i.value = {'title': i.value}
                    i.save()
            elif isinstance(i.value, list):
                i.value = {'change': i.value}
                i.save()
            elif isinstance(i.value, dict):
                pass
            else:
                if i.operation == MOP.POST_CONTENT_CHANGE:
                    i.value = {'change': i.value}
                    i.save()
Exemplo n.º 2
0
def fetch_notif_of_log(user_id, last_manage_log_id=b'\x00'):
    from model.manage_log import ManageLog, MOP

    if last_manage_log_id is None:
        last_manage_log_id = b'\x00'
    item_lst = ManageLog.select().where(
        ManageLog.related_user_id == user_id,
        ManageLog.id > last_manage_log_id,
        ManageLog.operation.in_(
            (MOP.POST_STATE_CHANGE, MOP.USER_PASSWORD_CHANGE,
            MOP.USER_PASSWORD_RESET, MOP.USER_KEY_RESET, MOP.USER_GROUP_CHANGE, MOP.USER_CREDIT_CHANGE,
            MOP.USER_REPUTE_CHANGE, MOP.USER_NICKNAME_CHANGE,
            MOP.TOPIC_BOARD_MOVE, MOP.TOPIC_AWESOME_CHANGE, MOP.TOPIC_STICKY_WEIGHT_CHANGE)
        )
    ).order_by(ManageLog.id.desc())

    moves = []

    def wrap(item: ManageLog):
        # 总不能把MANAGE_OPERATION的内容换个号码,抄一遍写在上面。
        # 因此选择过滤掉一些,其他全部归为一类。
        if item.operation == MOP.USER_CREDIT_CHANGE:
            if item.note == '每日签到':
                # 签到得分忽略
                return
        elif item.operation == MOP.USER_EXP_CHANGE:
            if item.note == '每日登录':
                # 签到得分忽略
                return

        if item.operation == MOP.TOPIC_BOARD_MOVE:
            moves.append([POST_TYPES.BOARD, to_bin(item.value['change'][0])])
            moves.append([POST_TYPES.BOARD, to_bin(item.value['change'][1])])

        return {
            'type': NOTIF_TYPE.MANAGE_INFO_ABOUT_ME,
            'time': item.time,

            'loc_post_type': item.related_type,
            'loc_post_id': item.related_id,
            'loc_post_title': None,

            'sender_ids': (item.user_id,),
            'receiver_id': user_id,

            'from_post_type': None,  # 来源为managelog,但并非post
            'from_post_id': item.id,

            'related_type': item.related_type,  # 提醒类型较为特殊
            'related_id': item.related_id,

            'data': {
                'op': item.operation,
                'role': item.role,
                'value': item.value,
                'title': None  # 进行二次填充
            }
        }

    ret_items = list(filter(lambda x: x, map(wrap, item_lst)))
    info = POST_TYPES.get_post_title_by_list(*[[i['related_type'], i['related_id']] for i in ret_items])
    info2 = POST_TYPES.get_post_title_by_list(*moves)

    for i in ret_items:
        t = info.get(get_bytes_from_blob(i['related_id']), None)
        if t: i['data']['title'] = t

        if i['related_type'] == POST_TYPES.COMMENT:
            # 这里不是批量,可能要付出较大代价
            c: Comment = Comment.get_by_id(i['related_id'])
            if not c: continue
            p = POST_TYPES.get_post(c.related_type, c.related_id)
            if not p: continue
            i['loc_post_type'] = c.related_type
            i['loc_post_id'] = c.related_id
            i['loc_post_title'] = p.get_title()

            i['data']['comment'] = {
                'related_type': c.related_type,
                'related_id': c.related_id,
                'related_title': p.get_title(),
                'post_number': c.post_number,
                'content': c.content
            }

        if i['data']['op'] == MOP.TOPIC_BOARD_MOVE:
            val = i['data']['value']['change']
            i['data']['move_info'] = [
                info2.get(to_bin(val[0]), None),
                info2.get(to_bin(val[1]), None)
            ]

    return ret_items