Exemple #1
0
 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
Exemple #2
0
 def post(self, **kwargs):
     """  接口: 1. 标记消息为已读-read
               2. 设置系统消息-sys
     """
     type = kwargs.get('type')
     msg_id = kwargs.get('msg_id')
    # self.write({'read msg_id':msg_id})
    # return
     if type == 'read' and msg_id:
         result = Message.mark_read(user_id=kwargs['uid'], msg_id=msg_id)
     elif type == 'sys' and (kwargs['uid'] == 'admin' or configs['test_mode']):  # todo 系统用户才能设置系统消息
         data = json_decode(self.request.body)
         content = data.get('content')
         if content:
             result = Message.set_message(type=Message._type_sys, content=content)
     else:
         raise HTTPError(404, 'not support post method or user not support')
     self.write(result.dictify())
     self.set_status(201)
Exemple #3
0
    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'))