Пример #1
0
def add():
    form = request.form.to_dict()
    author = current_user()
    author_id = author.id
    form['user_id'] = author_id
    new_rep = Reply.new(form)

    # 有新的回复时,更新帖子的last_active_time
    t: Topic = Topic.one(id=new_rep.topic_id)
    t.last_active_time = new_rep.created_time
    t.last_rep_uid = author_id
    t.reps = t.reps + 1
    t.save()

    with data_cache.pipeline(transaction=False) as pipe:
        key = 'topic_id_{}.replies'.format(t.id)
        key2 = 'topic_id_{}.topic_info'.format(t.id)
        pipe.delete(key)
        pipe.delete(key2)
        pipe.execute()

    inform_at(t, new_rep.content, author)
    if author.id != t.user_id:
        # 如果是回复他人的帖子,则对主题主人发系统信息通知
        Info.send(title='{}刚刚回复了您的主题'.format(author.username),
                  receiver_id=t.user_id,
                  content='{}在您的主题{} 中发表了一个新回复,查看:\n\r{}/topic/{}'
                  .format(author.username, t.title, web_domain_name, t.id))
        key = 'user_id_{}.received_info'.format(t.user_id)
        data_cache.delete(key)

    return redirect(url_for('my_topic.detail', topic_id=new_rep.topic_id))
Пример #2
0
def inform_at(topic, checked_content, caller):
    # topic:@ 发生的topic
    # check_content:需要检查 @ 发生的文本
    # caller:发送 @ 的用户

    # 检查当前topic中的正文或评论中是否有用户使用@功能,有的话则发站内信提醒被@的用户。
    called_users = at_users(checked_content)
    with data_cache.pipeline(transaction=False) as pipe:
        for called_user in called_users:
            Info.send(title='{}@了你,快去看看吧。'.format(caller.username),
                      content='用户{}@了你,点击{}/topic/{}查看。'.format(
                          caller.username, web_domain_name, topic.id),
                      receiver_id=called_user.id)
            key = 'user_id_{}.received_info'.format(called_user.id)
            pipe.delete(key)
        pipe.execute()