Пример #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
        })
Пример #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
        })

        self.finish()
Пример #3
0
    def can_see(status, user_id):
        '''判断某人能否看到某状态.

        :Parameters:
          - `status`: 状态, 是id或者document
          - `user_id`: 相关用户
        '''

        can = False

        if isinstance(status, (str, ObjectId)):
            status = yield StatusDocument.find_one({'_id': ObjectId(status)})

        if status:
            if isinstance(status['author'], DBRef):
                author = status['author'].id
            elif isinstance(status['author'], dict):
                author = status['author']['_id']
            else:
                raise gen.Return(can)

            is_friend = yield FriendDocument.is_friend(user_id, author)
            user_setting = yield UserSettingDocument.get_user_setting(author)

            if (str(author) == str(user_id) or is_friend
                    or user_setting['allow_stranger_visiting_profile']):
                can = True

        raise gen.Return(can)
Пример #4
0
    def can_see(status, user_id):
        '''判断某人能否看到某状态.

        :Parameters:
          - `status`: 状态, 是id或者document
          - `user_id`: 相关用户
        '''

        can = False

        if isinstance(status, (str, ObjectId)):
            status = yield StatusDocument.find_one({'_id': ObjectId(status)})

        if status:
            if isinstance(status['author'], DBRef):
                author = status['author'].id
            elif isinstance(status['author'], dict):
                author = status['author']['_id']
            else:
                raise gen.Return(can)

            is_friend = yield FriendDocument.is_friend(user_id, author)
            user_setting = yield UserSettingDocument.get_user_setting(author)

            if (str(author) == str(user_id) or is_friend or
                    user_setting['allow_stranger_visiting_profile']):
                can = True

        raise gen.Return(can)
Пример #5
0
    def get_header_arguments(self, user_id=None):
        user_id = user_id or self.current_user["_id"]

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

        status_number = yield StatusDocument.get_status_number(user_id)
        topic_number = yield TopicDocument.get_topic_number_by_someone(
            user_id, visitor_id=self.current_user['_id']
        )

        user = yield UserDocument.translate_dbref_in_document(user)

        can_seen = yield UserDocument.can_seen(
            user_id, self.current_user['_id']
        )
        is_friend = yield FriendDocument.is_friend(
            user_id, self.current_user['_id']
        )
        user_setting = yield UserSettingDocument.get_user_setting(
            user_id
        )
        profile_cover = yield UserSettingDocument.get_profile_cover(
            user_id
        )

        kwargs = {
            'user': user,
            'can_seen': can_seen,
            'is_friend': is_friend,
            'user_setting': user_setting,
            'status_number': status_number,
            'topic_number': topic_number,
            'profile_cover': profile_cover,
            'PROFILE_SETTINGS': PROFILE_SETTINGS
        }

        if not can_seen:
            messages_func = LeaveMessageDocument.get_leave_message_list
            leave_message_list = yield messages_func(
                user_id, self.current_user['_id'],
                limit=PROFILE_SETTINGS['leave_message_number_per_page']
            )

            kwargs.update({
                'leave_message_list': leave_message_list
            })

        raise gen.Return(kwargs)
Пример #6
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)
Пример #7
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))
Пример #8
0
    def get_header_arguments(self, user_id=None):
        user_id = user_id or self.current_user["_id"]

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

        status_number = yield StatusDocument.get_status_number(user_id)
        topic_number = yield TopicDocument.get_topic_number_by_someone(
            user_id, visitor_id=self.current_user['_id'])

        user = yield UserDocument.translate_dbref_in_document(user)

        can_seen = yield UserDocument.can_seen(user_id,
                                               self.current_user['_id'])
        is_friend = yield FriendDocument.is_friend(user_id,
                                                   self.current_user['_id'])
        user_setting = yield UserSettingDocument.get_user_setting(user_id)
        profile_cover = yield UserSettingDocument.get_profile_cover(user_id)

        kwargs = {
            'user': user,
            'can_seen': can_seen,
            'is_friend': is_friend,
            'user_setting': user_setting,
            'status_number': status_number,
            'topic_number': topic_number,
            'profile_cover': profile_cover,
            'PROFILE_SETTINGS': PROFILE_SETTINGS
        }

        if not can_seen:
            messages_func = LeaveMessageDocument.get_leave_message_list
            leave_message_list = yield messages_func(
                user_id,
                self.current_user['_id'],
                limit=PROFILE_SETTINGS['leave_message_number_per_page'])

            kwargs.update({'leave_message_list': leave_message_list})

        raise gen.Return(kwargs)
Пример #9
0
    def post(self, action):
        '''屏蔽/拉黑/删除某一个朋友'''

        form = FriendActionForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        friend_id = form.friend_id.data

        is_friend = yield FriendDocument.is_friend(
            self.current_user['_id'], friend_id
        )

        if not is_friend:
            response_data.update({'error': '你们不是朋友'})
        else:
            owner = DBRef(
                UserDocument.meta['collection'],
                self.current_user['_id']
            )
            friend = DBRef(
                UserDocument.meta['collection'],
                ObjectId(friend_id)
            )

            friend_document = yield FriendDocument.find_one({
                'owner': owner, 'friend': friend
            })

            if action == 'shield':
                shielded = True
                if friend_document['shielded']:
                    shielded = False

                yield FriendDocument.update(
                    {'_id': friend_document['_id']},
                    {'$set': {'shielded': shielded}}
                )

                response_data.update({'shielded': shielded})

            elif action == 'block':
                blocked = True
                if friend_document['blocked']:
                    blocked = False

                yield FriendDocument.update(
                    {'_id': friend_document['_id']},
                    {'$set': {'blocked': blocked}}
                )

                response_data.update({'blocked': blocked})

            elif action == 'delete':
                yield FriendDocument.remove({
                    '$or': [
                        {'owner': owner, 'friend': friend},
                        {'owner': friend, 'friend': owner}
                    ]
                })
            else:
                raise HTTPError(404)

        self.finish(json.dumps(response_data))
Пример #10
0
    def post(self):
        '''评论状态'''

        response_data ={}

        form = StatusCommentNewForm(self.request.arguments)
        if form.validate():
            status_id = form.status_id.data
            content = form.content.data
            replyeder_id = form.replyeder_id.data

            status = yield StatusDocument.find_one({
                '_id': ObjectId(status_id)
            })
            if not status:
                raise HTTPError(404)

            can_see = yield StatusDocument.can_see(
                status, self.current_user['_id']
            )
            if not can_see:
                raise HTTPError(404)

            replyeder = None
            if replyeder_id:
                replyeder = yield UserDocument.find_one({
                    '_id': ObjectId(replyeder_id)
                })
                if not replyeder:
                    raise HTTPError(404)

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

            now = datetime.now()
            document = {
                'status': DBRef(
                    StatusDocument.meta['collection'],
                    ObjectId(status['_id'])
                ),
                'author': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'comment_time': now,
                'content': content
            }

            if replyeder:
                document.update({
                    'replyeder': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(replyeder_id)
                    )
                })

            comment_id = yield StatusCommentDocument.insert_one(document)

            if replyeder:
                recipient_id = replyeder_id
                message_type = 'reply:status'
                message_topic = MessageTopic.REPLY
            else:
                recipient_id = status['author'].id
                message_type = 'comment:status'
                message_topic = MessageTopic.COMMENT

            if ObjectId(self.current_user['_id']) != ObjectId(recipient_id):
                message = {
                    'sender': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'recipient': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(recipient_id)
                    ),
                    'message_type': message_type,
                    'time': now,
                    'read': False,
                    'data': DBRef(
                        StatusCommentDocument.meta['collection'],
                        ObjectId(comment_id)
                    )
                }
                message_id = yield MessageDocument.insert(message)
                WriterManager.pub(message_topic, message_id)

            comment = yield StatusCommentDocument.get_comment(comment_id)
            html = self.render_string(
                'home/template/status/status-comment-list-item.html',
                status=status,
                status_comment=comment
            )
            response_data.update({'html': html})
        else:
            for field in form.errors:
                response_data.update({'error': form.errors[field][0]})
                break

        self.finish(json.dumps(response_data))
Пример #11
0
    def post(self, action):
        '''屏蔽/拉黑/删除某一个朋友'''

        form = FriendActionForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        friend_id = form.friend_id.data

        is_friend = yield FriendDocument.is_friend(
            self.current_user['_id'], friend_id
        )

        if not is_friend:
            response_data.update({'error': '你们不是朋友'})
        else:
            owner = DBRef(
                UserDocument.meta['collection'],
                self.current_user['_id']
            )
            friend = DBRef(
                UserDocument.meta['collection'],
                ObjectId(friend_id)
            )

            friend_document = yield FriendDocument.find_one({
                'owner': owner, 'friend': friend
            })

            if action == 'shield':
                shielded = True
                if friend_document['shielded']:
                    shielded = False

                yield FriendDocument.update(
                    {'_id': friend_document['_id']},
                    {'$set': {'shielded': shielded}}
                )

                response_data.update({'shielded': shielded})

            elif action == 'block':
                blocked = True
                if friend_document['blocked']:
                    blocked = False

                yield FriendDocument.update(
                    {'_id': friend_document['_id']},
                    {'$set': {'blocked': blocked}}
                )

                response_data.update({'blocked': blocked})

            elif action == 'delete':
                yield FriendDocument.remove({
                    '$or': [
                        {'owner': owner, 'friend': friend},
                        {'owner': friend, 'friend': owner}
                    ]
                })
            else:
                raise HTTPError(404)

        self.write_json(response_data)
Пример #12
0
    def post(self):
        '''评论状态'''

        response_data ={}

        form = StatusCommentNewForm(self.request.arguments)
        if form.validate():
            status_id = form.status_id.data
            content = form.content.data
            replyeder_id = form.replyeder_id.data

            status = yield StatusDocument.find_one({
                '_id': ObjectId(status_id)
            })
            if not status:
                raise HTTPError(404)

            can_see = yield StatusDocument.can_see(
                status, self.current_user['_id']
            )
            if not can_see:
                raise HTTPError(404)

            replyeder = None
            if replyeder_id:
                replyeder = yield UserDocument.find_one({
                    '_id': ObjectId(replyeder_id)
                })
                if not replyeder:
                    raise HTTPError(404)

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

            now = datetime.now()
            document = {
                'status': DBRef(
                    StatusDocument.meta['collection'],
                    ObjectId(status['_id'])
                ),
                'author': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'comment_time': now,
                'content': content
            }

            if replyeder:
                document.update({
                    'replyeder': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(replyeder_id)
                    )
                })

            comment_id = yield StatusCommentDocument.insert_one(document)

            if replyeder:
                recipient_id = replyeder_id
                message_type = 'reply:status'
                message_topic = MessageTopic.REPLY
            else:
                recipient_id = status['author'].id
                message_type = 'comment:status'
                message_topic = MessageTopic.COMMENT

            if ObjectId(self.current_user['_id']) != ObjectId(recipient_id):
                message = {
                    'sender': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'recipient': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(recipient_id)
                    ),
                    'message_type': message_type,
                    'time': now,
                    'read': False,
                    'data': DBRef(
                        StatusCommentDocument.meta['collection'],
                        ObjectId(comment_id)
                    )
                }
                message_id = yield MessageDocument.insert(message)
                WriterManager.pub(message_topic, message_id)

            comment = yield StatusCommentDocument.get_comment(comment_id)
            html = self.render_string(
                'home/template/status/status-comment-list-item.html',
                status=status,
                status_comment=comment
            )
            response_data.update({'html': html})
        else:
            for field in form.errors:
                response_data.update({'error': form.errors[field][0]})
                break

        self.write_json(response_data)