def add(): """发送新私信""" form = request.form.to_dict() u = current_user() receiver_id = form.get('receiver_id', None) if receiver_id is None: r_name = form.get('receiver_name', None) receiver = User.one(username=r_name) else: # receiver = User.one(id=receiver_id) receiver = cached_user_id2user(receiver_id) if receiver is None: flash('收件人不存在') elif receiver.id == u.id or receiver.username == u.username: flash('私信是发给其他人的哦,请重新输入收件人') else: Messages.send( title=form['title'], content=form['content'], sender_id=u.id, receiver_id=receiver.id, ) flash('发送成功') with data_cache.pipeline(transaction=False) as pipe: key = 'user_id_{}.sent_message'.format(u.id) key2 = 'user_id_{}.received_message'.format(receiver_id) pipe.delete(key) pipe.delete(key2) pipe.execute() return redirect(url_for('.index'))
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))
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()
def sweep(): """清理所有已读信息""" u = current_user() owner_id = int(request.form.get('owner_id', -1)) owner = cached_user_id2user(owner_id) if owner is not None: if owner_id == u.id: read_infos = Info.all(receiver_id=owner_id, been_read=True) with data_cache.pipeline(transaction=False) as pipe: for i in read_infos: Info.delete(i) key = 'user_id_{}.received_info'.format(u.id) pipe.delete(key) pipe.execute() return redirect(url_for('.info'))
def set_read(): """一键标记所有信息为已读""" u = current_user() owner_id = int(request.form.get('owner_id', -1)) owner = cached_user_id2user(owner_id) if owner is not None: if owner_id == u.id: unread_info = Info.all(receiver_id=owner_id, been_read=False) with data_cache.pipeline(transaction=False) as pipe: for i in unread_info: i.been_read = True i.save() key = 'info_id_{}.info'.format(i.id) pipe.delete(key) pipe.execute() return redirect(url_for('.info'))
def set_read(): """标记所有未读私信为已读""" u = current_user() receiver_id = int(request.form.get('receiver_id', -1)) receiver = cached_user_id2user(receiver_id) if receiver is not None: if receiver_id == u.id: unread_messages = Messages.all(receiver_id=receiver_id, been_read=False) with data_cache.pipeline(transaction=False) as pipe: for message in unread_messages: key = 'message_id_{}.message'.format(message.id) message.been_read = True message.save() pipe.delete(key) pipe.execute() return redirect(url_for('.inbox'))
def delete(): i = int(request.form.get('reply_id', -1)) rep: Reply = Reply.one(id=i) if rep is None: return abort(404) topic: Topic = Topic.one(id=rep.topic_id) Reply.delete(rep) with data_cache.pipeline(transaction=False) as pipe: key = 'topic_id_{}.replies'.format(topic.id) key2 = 'topic_id_{}.topic_info'.format(topic.id) pipe.delete(key) pipe.delete(key2) pipe.execute() # 更新帖子的last_active_time last_rep = topic.last_reply() topic.last_active_time = last_rep.created_time if last_rep is not None else topic.created_time topic.reps = topic.reps - 1 topic.save() flash('回复删除成功') return redirect(url_for('my_topic.detail', topic_id=topic.id))
def delete(): """删除某个话题,话题id从form中获取""" user_id = current_user().id i = int(request.form.get('topic_id', -1)) topic = Topic.one(id=i) if topic is not None: # 删帖后,清理该帖子相关的缓存 with data_cache.pipeline(transaction=False) as pipe: key = 'topic_id_{}.topic_info'.format(i) key2 = 'user_id_{}.created_topics'.format(user_id) key3 = 'topic_id_{}.replies'.format(topic.id) pipe.delete(key) pipe.delete(key2) pipe.delete(key3) delete_rep_id_list = Topic.delete(topic) for i in delete_rep_id_list: key = 'reply_id_{}.reply_info'.format(i) data_cache.delete(key) pipe.execute() flash('帖子删除成功') return redirect(url_for('my_user.user_created_topics', user_id=user_id))