Example #1
0
    def post(self):
        form = FriendRequestForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        user_id = form.user_id.data

        user = yield UserDocument.find_one({'_id': ObjectId(user_id)})
        if not user or user['_id'] == self.current_user['_id']:
            raise HTTPError(404)

        is_friend = yield FriendDocument.is_friend(user_id,
                                                   self.current_user['_id'])
        if not is_friend:
            yield FriendDocument.add_friend(user_id, self.current_user['_id'])

        yield MessageDocument.remove({
            'sender':
            DBRef(UserDocument.meta['collection'], ObjectId(user_id)),
            'recipient':
            DBRef(UserDocument.meta['collection'],
                  ObjectId(self.current_user['_id'])),
            'message_type':
            MessageTopic.FRIEND_REQUEST_NEW
        })

        self.finish()
Example #2
0
    def post(self):
        form = FriendRequestForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        user_id = form.user_id.data

        user = yield UserDocument.find_one({'_id': ObjectId(user_id)})
        if not user or user['_id'] == self.current_user['_id']:
            raise HTTPError(404)

        is_friend = yield FriendDocument.is_friend(
            user_id, self.current_user['_id']
        )
        if not is_friend:
            yield FriendDocument.add_friend(
                user_id, self.current_user['_id']
            )

        yield MessageDocument.remove({
            'sender': DBRef(
                UserDocument.meta['collection'],
                ObjectId(user_id)
            ),
            'recipient': DBRef(
                UserDocument.meta['collection'],
                ObjectId(self.current_user['_id'])
            ),
            'message_type': MessageTopic.FRIEND_REQUEST_NEW
        })
Example #3
0
    def post(self):
        response_data = {}
        form = FriendRequestForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        user_id = form.user_id.data

        user = yield UserDocument.find_one({'_id': ObjectId(user_id)})
        if not user or user['_id'] == self.current_user['_id']:
            raise HTTPError(404)

        is_friend = yield FriendDocument.is_friend(
            user_id, self.current_user['_id']
        )
        if is_friend:
            raise HTTPError(404)

        document = {
            'sender': DBRef(
                UserDocument.meta['collection'],
                ObjectId(self.current_user['_id'])
            ),
            'recipient': DBRef(
                UserDocument.meta['collection'],
                ObjectId(user_id)
            ),
            'message_type': MessageTopic.FRIEND_REQUEST_NEW,
        }

        message = yield MessageDocument.find_one(document)
        if message:
            response_data = {'error': '你已经发送了好友请求!'}
        else:
            user_setting = yield UserSettingDocument.get_user_setting(user_id)
            if not user_setting['require_verify_when_add_friend']:
                yield FriendDocument.add_friend(
                    user_id,
                    self.current_user['_id']
                )
                response_data.update({'ok': 1})

            document.update({'time': datetime.now()})
            message_id = yield MessageDocument.insert(document)

            WriterManager.pub(MessageTopic.FRIEND_REQUEST_NEW, message_id)

        self.write_json(response_data)
Example #4
0
    def post(self):
        response_data = {}
        form = FriendRequestForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        user_id = form.user_id.data

        user = yield UserDocument.find_one({'_id': ObjectId(user_id)})
        if not user or user['_id'] == self.current_user['_id']:
            raise HTTPError(404)

        is_friend = yield FriendDocument.is_friend(user_id,
                                                   self.current_user['_id'])
        if is_friend:
            raise HTTPError(404)

        document = {
            'sender':
            DBRef(UserDocument.meta['collection'],
                  ObjectId(self.current_user['_id'])),
            'recipient':
            DBRef(UserDocument.meta['collection'], ObjectId(user_id)),
            'message_type':
            MessageTopic.FRIEND_REQUEST_NEW,
        }

        message = yield MessageDocument.find_one(document)
        if message:
            response_data = {'error': '你已经发送了好友请求!'}
        else:
            user_setting = yield UserSettingDocument.get_user_setting(user_id)
            if not user_setting['require_verify_when_add_friend']:
                yield FriendDocument.add_friend(user_id,
                                                self.current_user['_id'])
                response_data.update({'ok': 1})

            document.update({'time': datetime.now()})
            message_id = yield MessageDocument.insert(document)

            WriterManager.pub(MessageTopic.FRIEND_REQUEST_NEW, message_id)

        self.finish(json.dumps(response_data))