def _msg_data(self, msg): """ 统一返回格式 - 带上 comment, item """ data = Message._msg_data(msg) if data['link_id']: topic = Topic.get(data['link_id']) topic = Topic._topic_data(topic) data['topic'] = topic tmp_user = model.user.get_user(user_id=topic['user_id']) # 消息来源用户 if not tmp_user: return None data['topic_user'] = { 'id': tmp_user['id'], 'nick': tmp_user['nick'], 'portrait': tmp_user['portrait'], 'type': tmp_user['user_type'], } if tmp_user else {} if data['from_uid']: tmp_user = model.user.get_user(user_id=data['from_uid']) # 消息来源用户 if not tmp_user: return None data['from_user'] = { 'id': tmp_user['id'], 'nick': tmp_user['nick'], 'portrait': tmp_user['portrait'], 'type': tmp_user['user_type'], } if tmp_user else {} data['class'] = 'message' return data
def create_msg(self, msg_type, user, item): #建立消息 to_uid = item['user_id'] uid = user['_id'] if uid == to_uid: #如果赞的是自己 不存消息 return {} if msg_type == 'TL': m_type = "心得" content = user['nick'] + "赞了你的"+m_type + " \"%s\""%item['content'] if msg_type == 'CL': m_type = "评论" if item['c_type'] == 'audio': content = user['nick'] + "赞了你的语音"+m_type else: content = user['nick'] + "赞了你的"+m_type + " \"%s\""%item['content'] msg = Message.set_message(to_uid, msg_type, uid, content, item['topic_id']) msg = Message._msg_data(msg) return msg
def post(self, **kwargs): """ 新建一个评论 需要用户登录 """ token = self._get_token() uid = token['_id'] user = hqby.user.get_user_info(uid=uid) if not user: raise HTTPError(403, 'can not found this user') try: params = json_decode(self.request.body) except ValueError: params = self.get_form() comment_type = params.get('comment_type', False) self.check_comment_type(comment_type) self.check_topic(params['topic_id']) params['user_id'] = uid # 添加一个新的评论 comm = self.create_comment(params,comment_type) # self.write('haha = %s'%comm) #return data = self._comment_data(comm) data['is_liked'] = 0 topic = Topic.update_topic(data['topic_id'],False,True) topic_data = Topic._topic_data(topic) if data['to_user_id']: to_uid = data['to_user_id'] msg_type = 'CC' content = user['nick'] + '回复了你的评论 ' else: to_uid = topic_data['user_id'] msg_type = 'TC' content = user['nick'] + "评论了你的心得 " + " \"%s\""%topic_data['content'] if uid != to_uid: msg = Message.set_message(to_uid, msg_type, uid, content, topic_data['topic_id']) #link_id 是topic的id msg = Message._msg_data(msg) else: msg = 'you reply yourself' #给被回复者发送通知 baidu_apiKey = baidu_push_configs['apiKey'] baidu_secretKey = baidu_push_configs['secretKey'] bind_info = hqby.user.get_bind_info(to_uid) baidu_uid, baidu_cid = bind_info.get('baidu_uid'), bind_info.get('baidu_cid') if baidu_uid and baidu_cid and uid != to_uid: message = { 'title': '读经', 'description': '%s回复了你,快去看看吧' % user['nick'].encode('utf8'), 'open_type': 2, "aps": { "alert": '%s回复了你,快去看看吧' % user['nick'].encode('utf8'), "sound":"", "badge":0 }, } message = json.dumps(message) message_key = "sys" c = Channel(baidu_apiKey, baidu_secretKey, arr_curlOpts=dict(TIMEOUT=3, CONNECTTIMEOUT=5)) push_type = 1 # 1-单个人, 2-一群人, 3-全部人 optional = dict() optional[Channel.USER_ID] = baidu_uid optional[Channel.CHANNEL_ID] = int(baidu_cid) optional[Channel.MESSAGE_TYPE] = 1 # 0-消息, 1-通知 optional['device_types'] = [3, 4] # 4-ios, 3-安卓, 5-wp设备, 2-pc, 1-浏览器 optional['deploy_status'] = 1 if configs['debug'] else 2 # 1-开发, 2-生产 #job = c.pushMessage(push_type, message, message_key, optional) job = rq_client.default_queue.enqueue(c.pushMessage, push_type, message, message_key, optional) #logging.info('log for baidu pusher: %s', str(job)) self.write({'comment':data,'topic':topic_data,'msg':msg}) self.set_status(200) self.set_header('Content-Type', self._ct('json'))