コード例 #1
0
 def _push_notify(self,from_jid,gid,action,members=[]):
     '''
     推送群事件
     :param from_jid: 发送人jid
     :param gid: 群id
     :param action: 事件名称包括 create / add / del
     :return:
     '''
     packet = {
         "envelope": {
             "id": uuid.uuid4().hex,
             "type": 2,
             "gid": str(gid),
             "from": from_jid,
             "ack": 1
         },
         "payload": {
             "attrs": {
                 "message_type": "notify",
                 "action": action
             }
         }
     }
     if members :
         packet['payload']['attrs']['members'] = members
     packet_str = json.dumps(packet)
     logger.info("group_push_notify = %s" % packet_str)
     emsg_client.process(packet_str)
コード例 #2
0
    def _reject_contact(self,body):
        '''
        拒绝添加好友请求
        :param body:
        :return:
        '''
        try:
            sn, token, params = self._get_sn_token_params(body)
            auth_user, user_info = self._get_user_by_token(token)
            userid = auth_user.id
            contact_id = params.get('contact_id')
            with transaction.atomic():
                # 将对方添加我为好友的数据状态改为 拒绝
                for contact in UserContact.objects.filter(userid=contact_id, contactid=userid):
                    contact.status = 'reject'
                    contact.et = int(time.time())
                    contact.save()
                packet = {
                    "envelope": {
                        "id": uuid.uuid4().hex,
                        "type": 1,
                        "from": self._jid(userid),
                        "to": self._jid(contact_id),
                        "ack": 1
                    },
                    "vsn": "0.0.1",
                    "payload": {
                        "attrs": {
                            "action": "reject",
                            "message_type": "contact",
                            "contact_icon": user_info.icon,
                            "contact_nickname": user_info.nickname,
                            "contact_id": str(userid),

                            "messageType": "contact",
                            "contactIcon": user_info.icon,
                            "contactNickname": user_info.nickname,
                            "contactId": str(userid)
                        }
                    }
                }

                packet_str = json.dumps(packet)
                logger.info("reject_contact_packet = %s" % packet_str)
                emsg_client.process(packet_str)
            return self._success(sn=sn, success=True)
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())
            return self._success(sn=sn, success=False, code='1007', reason=errors.error_1007)
コード例 #3
0
    def _delete_contact(self,body):
        '''
        删除联系人
        :param body:
        :return:
        '''
        try:
            sn, token, params = self._get_sn_token_params(body)
            auth_user, user_info = self._get_user_by_token(token)
            userid = auth_user.id
            contact_id = params.get('contact_id')
            with transaction.atomic():
                for contact in UserContact.objects.filter(userid=contact_id, contactid=userid):
                    contact.delete()
                for user_contact in UserContact.objects.filter(userid=userid,contactid=contact_id) :
                    user_contact.delete()
                packet = {
                    "envelope": {
                        "id": uuid.uuid4().hex,
                        "type": 1,
                        "from": self._jid(userid),
                        "to": self._jid(contact_id),
                        "ack": 1
                    },
                    "vsn": "0.0.1",
                    "payload": {
                        "attrs": {
                            "action": "delete",
                            "message_type": "contact",
                            "contact_id": str(userid),

                            "messageType": "contact",
                            "contactId": str(userid),
                        }
                    }
                }
                packet_str = json.dumps(packet)
                logger.info("delete_contact_packet = %s" % packet_str)
                emsg_client.process(packet_str)
        except Exception as e :
            logger.error(e)
            logger.error(traceback.format_exc())
            return self._success(sn=sn, success=False, code='1007', reason=errors.error_1007)
        return self._success(sn=sn, success=True)
コード例 #4
0
    def _add_contact(self,body):
        '''
        添加好友
        :param body:
        :return:
        '''
        try:
            sn, token, params = self._get_sn_token_params(body)
            auth_user, user_info = self._get_user_by_token(token)
            userid = auth_user.id
            contact_id = params.get('contact_id')
            with transaction.atomic():
                for contact in UserContact.objects.filter(userid=userid, contactid=contact_id):
                    if 'accept' == contact.status:
                        # 已添加,不能重复添加
                        return self._success(sn=sn, success=False, code='1007', reason=errors.error_1007_1)
                    else :
                        # 如果拒绝过,或者正在等待对方应答,则删除这个记录,重新添加
                        contact.delete()

                # 在数据库中创建联系人记录
                user_contact = UserContact()
                user_contact.userid = userid
                user_contact.status = 'add'
                user_contact.contactid = contact_id
                ts = int(time.time())
                user_contact.ct = ts
                user_contact.et = ts
                user_contact.save()
                # 并且判断对方是否添加过自己,如果没有添加过,则发加好友推送
                if not UserContact.objects.filter(userid=contact_id, contactid=userid):
                    # 对方没有加过我,需要推送
                    packet = {
                        "envelope": {
                            "id": uuid.uuid4().hex,
                            "type": 1,
                            "from": self._jid(userid),
                            "to": self._jid(contact_id),
                            "ack": 1
                        },
                        "vsn": "0.0.1",
                        "payload": {
                            "attrs": {
                                "action": "add",
                                # TODO 为了兼容一个错误的命名,稍后修改
                                "message_type": "contact",
                                "contact_icon": user_info.icon,
                                "contact_nickname": user_info.nickname,
                                "contact_id": str(userid),
                                # -------------------------------------
                                "messageType": "contact",
                                "contactIcon": user_info.icon,
                                "contactNickname": user_info.nickname,
                                "contactId": str(userid)
                            }
                        }
                    }
                    packet_str = json.dumps(packet)
                    logger.info("add_contact_packet = %s" % packet_str)
                    emsg_client.process(packet_str)
            return self._success(sn=sn, success=True)
        except Exception as e:
            logger.error(e)
            logger.error(traceback.format_exc())
            return self._success(sn=sn, success=False, code='1007', reason=errors.error_1007_2)