Пример #1
0
    def create(self, validated_data):
        ns = validated_data['ns']
        chat = validated_data['chat']
        users = [encode_ns_user(ns, user) for user in validated_data['users']]

        chat_update_users(chat, users)
        return True
Пример #2
0
def new_user_jwt(ns, name, exp_delta):
    exp = datetime.utcnow() + exp_delta
    payload = dict(ns=ns,
                   name=name,
                   username=encode_ns_user(ns, name),
                   exp=exp)

    return jwt.encode(payload,
                      config.XChat.KEY).decode('utf-8'), exp.timestamp()
Пример #3
0
    def put(self, request, chat_id, format=None):
        """替换会话成员"""
        chat = self.get_chat(chat_id)
        # 只能从群组会话删除成员
        if chat.type not in [ChatType.GROUP]:
            return Response({'ok': False}, status=status.HTTP_403_FORBIDDEN)

        serializer = MembersSerializer(data=request.data)
        if serializer.is_valid():
            ns = request.user.ns
            users = serializer.validated_data['users']
            users = [encode_ns_user(ns, user) for user in users]

            update_chat_members(chat, users)
            return Response({'ok': True}, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #4
0
    def delete(self, request, chat_id, format=None):
        """删除会话成员"""
        chat = self.get_chat(chat_id)
        # 只能从群组会话删除成员
        if chat.type not in [ChatType.GROUP]:
            return Response({'ok': False}, status=status.HTTP_403_FORBIDDEN)

        serializer = MembersSerializer(data=request.data)
        if serializer.is_valid():
            ns = request.user.ns
            users = serializer.validated_data['users']
            users = [encode_ns_user(ns, user) for user in users]
            # TODO: 标记is_exited.
            deleted, _ = chat.members.filter(user__in=users).delete()
            if deleted > 0:
                # update
                chat.update_members_updated()
            return Response({'ok': True}, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Пример #5
0
    def create(self, validated_data):
        ns = validated_data['ns']
        chat = validated_data['chat']
        start_msg_id = validated_data['start_msg_id']
        start_delta = validated_data['start_delta']

        msgs = [
            dict(uid=encode_ns_user(ns, msg.get('uid')),
                 ts=arrow.get(msg.get('ts')).datetime,
                 msg=msg.get('msg'),
                 domain=msg.get('domain')) for msg in validated_data['msgs']
        ]

        if len(msgs) <= 0:
            return 0

        return chat_insert_msgs(chat,
                                msgs,
                                start_msg_id=start_msg_id,
                                start_delta=start_delta)
Пример #6
0
    def create(self, validated_data):
        ns = validated_data['ns']
        t = validated_data['type']
        users = [encode_ns_user(ns, user) for user in validated_data['users']]
        chat_id = validated_data.get('chat_id')
        id = None
        if chat_id:
            # 指定chat_id, 则优先使用这里的t
            t, id = decode_chat_id(chat_id)

        key = None
        owner = None
        tag = validated_data.get('tag')
        if t == ChatType.SELF:
            # 用户建会话
            tag = 'user'
            owner = users[0]
            # 自聊唯一
            key = owner
        elif t == ChatType.USER:
            # 用户建会话
            tag = 'user'
            # 创建者
            owner = users[0]
            # 单聊唯一
            key = ','.join(sorted(users))
        elif t == ChatType.USERS:
            # 用户建会话
            tag = 'user'
            # 创建者
            owner = users[0]
        elif t == ChatType.CS:
            # TODO: 客服通过tag区别不同的客服团队
            tag = '_cs'
            owner = users[0]
            # 同一tag的user客服唯一
            key = '%s|%s' % (tag, owner)

        app_id = validated_data.get('app_id')
        start_msg_id = validated_data.get('start_msg_id')
        title = validated_data.get('title')
        ext = validated_data.get('ext')
        chat = None
        if id:
            chat = Chat.objects.filter(id=id, type=t).first()
        elif t in [ChatType.SELF, ChatType.USER, ChatType.CS]:
            chat = Chat.objects.filter(type=t, key=key).first()

        biz_id = validated_data.get('biz_id')
        if t != ChatType.GROUP:
            biz_id = None

        if not chat and biz_id:
            # biz id唯一
            chat = Chat.objects.filter(biz_id=biz_id).first()

        if chat is not None:
            updated_fields = set_update_chat(chat,
                                             biz_id,
                                             app_id,
                                             title,
                                             tag,
                                             ext,
                                             is_deleted=False)
            # update
            if updated_fields:
                chat.update_updated(fields=updated_fields)
        else:
            chat = Chat(type=t,
                        key=key,
                        biz_id=biz_id,
                        owner=owner,
                        start_msg_id=start_msg_id,
                        msg_id=start_msg_id)
            set_update_chat(chat, biz_id, app_id, title, tag, ext)
            chat.save()

        update_chat_members(chat, users)
        return chat