Beispiel #1
0
    def get_message_list(recipient_id, message_topic=None, read=None,
                         skip=0, limit=None):
        '''得到消息列表'''

        query = MessageDocument.get_query(recipient_id, message_topic)

        if read is not None:
            assert isinstance(read, bool)
            query.update({'read': read})

        cursor = MessageDocument.find(query).sort(
            [('time', pymongo.DESCENDING)]
        ).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        message_list = yield MessageDocument.to_list(cursor)
        for message in message_list:
            if 'sender' in message:
                message['sender'] = yield UserDocument.translate_dbref(
                    message['sender']
                )

            message['recipient'] = yield UserDocument.translate_dbref(
                message['recipient']
            )

            if 'data' not in message:
                continue

            if str(message['data'].collection) == str(
                    StatusDocument.meta['collection']):
                message['data'] = yield StatusDocument.get_status(
                    message['data'].id
                )
                continue

            message['data'] = yield Document.translate_dbref(
                message['data']
            )

            if message['data']:
                message['data'] = yield Document.translate_dbref_in_document(
                    message['data'], depth=2
                )

                if 'status' in message['data']:
                    message['data']['status'] = yield StatusDocument.get_status(
                        message['data']['status']['_id']
                    )

        raise gen.Return(message_list)
Beispiel #2
0
    def get_message_list(recipient_id,
                         message_topic=None,
                         read=None,
                         skip=0,
                         limit=None):
        '''得到消息列表'''

        query = MessageDocument.get_query(recipient_id, message_topic)

        if read is not None:
            assert isinstance(read, bool)
            query.update({'read': read})

        cursor = MessageDocument.find(query).sort([('time', pymongo.DESCENDING)
                                                   ]).skip(skip)

        if limit is not None:
            cursor = cursor.limit(limit)

        message_list = yield MessageDocument.to_list(cursor)
        for message in message_list:
            if 'sender' in message:
                message['sender'] = yield UserDocument.translate_dbref(
                    message['sender'])

            message['recipient'] = yield UserDocument.translate_dbref(
                message['recipient'])

            if 'data' not in message:
                continue

            if str(message['data'].collection) == str(
                    StatusDocument.meta['collection']):
                message['data'] = yield StatusDocument.get_status(
                    message['data'].id)
                continue

            message['data'] = yield Document.translate_dbref(message['data'])

            if message['data']:
                message['data'] = yield Document.translate_dbref_in_document(
                    message['data'], depth=2)

                if 'status' in message['data']:
                    message['data'][
                        'status'] = yield StatusDocument.get_status(
                            message['data']['status']['_id'])

        raise gen.Return(message_list)
Beispiel #3
0
    def get_sidebar_arguments(self):
        '''得到两侧栏的render变量'''

        user_id = self.current_user['_id']

        status_number = yield StatusDocument.get_status_number(user_id)
        topic_number = yield TopicDocument.get_topic_number_by_someone(
            user_id, visitor_id=user_id)

        user_setting = yield UserSettingDocument.find_one({
            'user':
            DBRef(UserDocument.meta['collection'], ObjectId(user_id))
        })
        login_reward_fetched_today = yield UserActivityDocument.login_reward_fetched(
            user_id)
        continuous_login_days = yield UserDocument.get_continuous_login_days(
            user_id)

        kwargs = {
            'status_number': status_number,
            'topic_number': topic_number,
            'MessageTopic': MessageTopic,
            'user_setting': user_setting,
            'login_reward_fetched_today': login_reward_fetched_today,
            'continuous_login_days': continuous_login_days,
            'HOME_SETTINGS': HOME_SETTINGS
        }

        raise gen.Return(kwargs)
Beispiel #4
0
    def get_sidebar_arguments(self):
        '''得到两侧栏的render变量'''

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

        user_setting = yield UserSettingDocument.find_one({
            'user': DBRef(UserDocument.meta['collection'], ObjectId(user_id))
        })

        random_user_list = yield UserDocument.get_random_user_list(
            self.current_user['_id']
        )

        kwargs = {
            'status_number': status_number,
            'topic_number': topic_number,
            'MessageTopic': MessageTopic,
            'user_setting': user_setting,
            'random_user_list': random_user_list,
            'HOME_SETTINGS': HOME_SETTINGS
        }

        raise gen.Return(kwargs)
Beispiel #5
0
    def post(self):
        '''加载更多状态'''

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

        page = form.page.data

        skip = HOME_SETTINGS['status_number_per_page'] * page
        limit = HOME_SETTINGS['status_number_per_page']

        status_list = yield StatusDocument.get_friends_status_list(
            self.current_user['_id'], skip=skip, limit=limit
        )

        html = ''.join(
            self.render_string(
                'home/template/status/status-list-item.html',
                status=status
            ) for status in status_list
        )
        response_data = json.dumps({'html': html, 'page': page + 1})

        self.finish(response_data)
Beispiel #6
0
    def get(self, user_id=None):
        user_id = user_id or self.current_user['_id']

        kwargs = yield self.get_header_arguments(user_id)
        if not kwargs['can_seen']:
            self.render('profile/template/profile-visitor.html', **kwargs)
        else:
            status_list = yield StatusDocument.get_status_list(
                user_id,
                self.current_user['_id'],
                limit=PROFILE_SETTINGS['status_number_per_page'])

            recommend_friend_list = yield UserDocument.get_random_user_list(
                self.current_user['_id'])

            if kwargs['user']['user_type'] == 'league':
                member_list = yield LeagueMemberDocument.get_member(user_id)
                kwargs.update({'member_list': member_list})

            kwargs.update({
                'status_list': status_list,
                'recommend_friend_list': recommend_friend_list,
                'PROFILE_SETTINGS': PROFILE_SETTINGS
            })

            self.render('profile/template/status/status.html', **kwargs)
Beispiel #7
0
    def get_sidebar_arguments(self):
        '''得到两侧栏的render变量'''

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

        user_setting = yield UserSettingDocument.find_one({
            'user': DBRef(UserDocument.meta['collection'], ObjectId(user_id))
        })

        random_user_list = yield UserDocument.get_random_user_list(
            self.current_user['_id']
        )

        kwargs = {
            'status_number': status_number,
            'topic_number': topic_number,
            'MessageTopic': MessageTopic,
            'user_setting': user_setting,
            'random_user_list': random_user_list,
            'HOME_SETTINGS': HOME_SETTINGS
        }

        raise gen.Return(kwargs)
Beispiel #8
0
    def get_sidebar_arguments(self):
        '''得到两侧栏的render变量'''

        user_id = self.current_user['_id']

        status_number = yield StatusDocument.get_status_number(user_id)
        topic_number = yield TopicDocument.get_topic_number_by_someone(
            user_id, visitor_id=user_id
        )

        user_setting = yield UserSettingDocument.find_one({
            'user': DBRef(UserDocument.meta['collection'], ObjectId(user_id))
        })
        login_reward_fetched_today = yield UserActivityDocument.login_reward_fetched(
            user_id
        )
        continuous_login_days = yield UserDocument.get_continuous_login_days(
            user_id
        )

        kwargs = {
            'status_number': status_number,
            'topic_number': topic_number,
            'MessageTopic': MessageTopic,
            'user_setting': user_setting,
            'login_reward_fetched_today': login_reward_fetched_today,
            'continuous_login_days': continuous_login_days,
            'HOME_SETTINGS': HOME_SETTINGS
        }

        raise gen.Return(kwargs)
Beispiel #9
0
    def get(self, user_id=None):
        user_id = user_id or self.current_user['_id']

        kwargs = yield self.get_header_arguments(user_id)
        if not kwargs['can_seen']:
            self.render('profile/template/profile-visitor.html', **kwargs)
        else:
            status_list = yield StatusDocument.get_status_list(
                user_id,
                self.current_user['_id'],
                limit=PROFILE_SETTINGS['status_number_per_page']
            )

            recommend_friend_list = yield UserDocument.get_random_user_list(
                self.current_user['_id']
            )

            if kwargs['user']['user_type'] == 'league':
                member_list = yield LeagueMemberDocument.get_member(user_id)
                kwargs.update({'member_list': member_list})

            kwargs.update({
                'status_list': status_list,
                'recommend_friend_list': recommend_friend_list,
                'PROFILE_SETTINGS': PROFILE_SETTINGS
            })

            self.render('profile/template/status/status.html', **kwargs)
Beispiel #10
0
    def post(self):
        '''点赞'''

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

        status_id = form.status_id.data

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

        status_dbref = DBRef(StatusDocument.meta['collection'],
                             ObjectId(status_id))
        liker_dbref = DBRef(UserDocument.meta['collection'],
                            ObjectId(self.current_user['_id']))

        document = {'status': status_dbref, 'liker': liker_dbref}

        liked = yield StatusLikeDocument.is_liked(status_id,
                                                  self.current_user['_id'])
        if not liked:
            now = datetime.now()
            document.update({'like_time': now})
            like_id = yield StatusLikeDocument.insert_one(document)

            if str(self.current_user['_id']) != str(status['author'].id):
                document = {
                    'sender':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'recipient':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(status['author'].id)),
                    'message_type':
                    'like:status',
                    'time':
                    now,
                    'read':
                    False,
                    'data':
                    DBRef(StatusLikeDocument.meta['collection'],
                          ObjectId(like_id))
                }
                message_id = yield MessageDocument.insert(document)
                WriterManager.pub(MessageTopic.LIKE, message_id)

        like_times = yield StatusLikeDocument.get_like_times(status_id)
        like_list = yield StatusLikeDocument.get_like_list(
            status['_id'], self.current_user['_id'])
        likers = '、'.join([like['liker']['name'] for like in like_list])

        self.write_json({'like_times': like_times, 'likers': likers})
Beispiel #11
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)
Beispiel #12
0
    def get(self):
        kwargs = yield self.get_sidebar_arguments()

        user_id = self.current_user['_id']
        status_list = yield StatusDocument.get_friends_status_list(
            user_id, limit=HOME_SETTINGS['status_number_per_page'])

        kwargs.update({
            'status_list': status_list,
            'HOME_SETTINGS': HOME_SETTINGS
        })

        self.render('home/template/status/status.html', **kwargs)
Beispiel #13
0
    def get(self):
        kwargs = yield self.get_sidebar_arguments()

        user_id = self.current_user['_id']
        status_list = yield StatusDocument.get_friends_status_list(
            user_id, limit=HOME_SETTINGS['status_number_per_page']
        )

        kwargs.update({
            'status_list': status_list,
            'HOME_SETTINGS': HOME_SETTINGS
        })

        self.render('home/template/status/status.html', **kwargs)
Beispiel #14
0
    def post(self):
        form = StatusMoreForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        page = form.page.data

        skip = PROFILE_SETTINGS['status_number_per_page'] * page
        limit = PROFILE_SETTINGS['status_number_per_page']
        status_list = yield StatusDocument.get_status_list(
            self.current_user['_id'],
            self.current_user['_id'],
            skip=skip,
            limit=limit)

        html = ''.join(
            self.render_string('profile/template/status/status-list-item.html',
                               status=status) for status in status_list)

        self.finish(json.dumps({'html': html, 'page': page + 1}))
Beispiel #15
0
    def post(self):
        form = StatusCommentsForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        status_id = form.status_id.data

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

        status_comment_list = yield StatusCommentDocument.get_comment_list(
            status_id, self.current_user['_id'])

        html = self.render_string(
            'home/template/status/status-comment-list.html',
            status=status,
            status_comment_list=status_comment_list)

        self.write_json({'html': html})
Beispiel #16
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)
Beispiel #17
0
    def post(self):
        form = StatusMoreForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        page = form.page.data

        skip = PROFILE_SETTINGS['status_number_per_page'] * page
        limit = PROFILE_SETTINGS['status_number_per_page']
        status_list = yield StatusDocument.get_status_list(
            self.current_user['_id'],
            self.current_user['_id'],
            skip=skip, limit=limit
        )

        html = ''.join(
            self.render_string(
                'profile/template/status/status-list-item.html',
                status=status
            ) for status in status_list
        )
        self.write_json({'html': html, 'page': page + 1})
Beispiel #18
0
    def post(self):
        form = StatusCommentsForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        status_id = form.status_id.data

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

        status_comment_list = yield StatusCommentDocument.get_comment_list(
            status_id, self.current_user['_id']
        )

        html = self.render_string(
            'home/template/status/status-comment-list.html',
            status=status,
            status_comment_list=status_comment_list
        )

        self.write_json({'html': html})
Beispiel #19
0
    def post(self):
        response_data = {}

        form = StatusNewForm(self.request.arguments)
        if form.validate():
            content = form.content.data

            n = len(content)
            if n > HOME_SETTINGS['status_max_length']:
                response_data.update({
                    'error': (
                        '状态内容不能超过%s字' %
                        HOME_SETTINGS['status_max_length']
                    )
                })
            elif n <= 0 and not (
                    self.request.files and 'picture' in self.request.files):
                response_data.update({'error': '请输入文字内容或者照片'})
            else:
                picture = None
                if self.request.files and 'picture' in self.request.files:
                    picture = self.request.files['picture'][0]
                    image_types = [
                        'image/jpg', 'image/png', 'image/jpeg', 'image/gif'
                    ]
                    if picture['content_type'].lower() not in image_types:
                        response_data.update({
                            'error': '请上传jpg/png/gif格式的图片'
                        })

                if 'error' not in response_data:
                    now = datetime.now()

                    document = {
                        'author': DBRef(
                            UserDocument.meta['collection'],
                            self.current_user['_id']
                        ),
                        'publish_time': now,
                        'content': content
                    }
                    status_id = yield StatusDocument.insert(document)
                    friends = yield FriendDocument.get_reached_friends(
                        self.current_user['_id']
                    )

                    message_list = []
                    for friend in friends:
                        document = {
                            'sender': DBRef(
                                UserDocument.meta['collection'],
                                ObjectId(self.current_user['_id'])
                            ),
                            'recipient': DBRef(
                                UserDocument.meta['collection'],
                                ObjectId(friend['_id'])
                            ),
                            'message_type': MessageTopic.STATUS_NEW,
                            'time': now,
                            'read': False,
                            'data': DBRef(
                                StatusDocument.meta['collection'],
                                ObjectId(status_id)
                            )
                        }
                        message_id = yield MessageDocument.insert(document)
                        message_list.append(str(message_id))

                    if message_list:
                        WriterManager.mpub(
                            MessageTopic.STATUS_NEW, message_list
                        )

                    if picture is not None:
                        try:
                            image = Image.open(StringIO(picture['body']))
                        except:
                            raise HTTPError(404)

                        try:
                            content_type = picture[
                                'content_type'
                            ].split('/')[-1].upper()
                        except:
                            content_type = 'JPEG'

                        document = {
                            'status': DBRef(
                                StatusDocument.meta['collection'],
                                ObjectId(status_id)
                            ),
                            'name': picture['filename'],
                            'content_type': content_type,
                            'upload_time': datetime.now()
                        }

                        width = 1024
                        if image.size[0] > width:
                            height = width * 1.0 * image.size[1] / image.size[0]
                            image = image.resize(
                                map(int, (width, height)), Image.ANTIALIAS
                            )

                        output = StringIO()
                        image.save(output, content_type, quality=100)
                        document.update({'body': Binary(output.getvalue())})
                        output.close()

                        thumbnail_width = 200
                        thumbnail_height = (
                            thumbnail_width * 1.0 * image.size[1] / image.size[0]
                        )

                        output = StringIO()
                        image = image.resize(
                            map(int, (thumbnail_width, thumbnail_height)),
                            Image.ANTIALIAS
                        )
                        image.save(output, content_type, quality=100)
                        document.update({
                            'thumbnail': Binary(output.getvalue())
                        })
                        output.close()

                        yield StatusPhotoDocument.insert(document)

                    status = yield StatusDocument.get_status(
                        status_id, self.current_user['_id']
                    )
                    html = self.render_string(
                        'home/template/status/status-list-item.html',
                        status=status
                    )
                    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))
Beispiel #20
0
    def post(self):
        response_data = {}

        form = StatusNewForm(self.request.arguments)
        if form.validate():
            content = form.content.data

            n = len(content)
            if n > HOME_SETTINGS['status_max_length']:
                response_data.update({
                    'error': (
                        '状态内容不能超过%s字' %
                        HOME_SETTINGS['status_max_length']
                    )
                })
            elif n <= 0 and not (
                    self.request.files and 'picture' in self.request.files):
                response_data.update({'error': '请输入文字内容或者照片'})
            else:
                picture = None
                if self.request.files and 'picture' in self.request.files:
                    picture = self.request.files['picture'][0]
                    image_types = [
                        'image/jpg', 'image/png', 'image/jpeg', 'image/gif'
                    ]
                    if picture['content_type'].lower() not in image_types:
                        response_data.update({
                            'error': '请上传jpg/png/gif格式的图片'
                        })

                if 'error' not in response_data:
                    now = datetime.now()

                    document = {
                        'author': DBRef(
                            UserDocument.meta['collection'],
                            self.current_user['_id']
                        ),
                        'publish_time': now,
                        'content': content
                    }
                    status_id = yield StatusDocument.insert(document)
                    friends = yield FriendDocument.get_reached_friends(
                        self.current_user['_id']
                    )

                    message_list = []
                    for friend in friends:
                        document = {
                            'sender': DBRef(
                                UserDocument.meta['collection'],
                                ObjectId(self.current_user['_id'])
                            ),
                            'recipient': DBRef(
                                UserDocument.meta['collection'],
                                ObjectId(friend['_id'])
                            ),
                            'message_type': MessageTopic.STATUS_NEW,
                            'time': now,
                            'read': False,
                            'data': DBRef(
                                StatusDocument.meta['collection'],
                                ObjectId(status_id)
                            )
                        }
                        message_id = yield MessageDocument.insert(document)
                        message_list.append(str(message_id))

                    if message_list:
                        WriterManager.mpub(
                            MessageTopic.STATUS_NEW, message_list
                        )

                    if picture is not None:
                        try:
                            image = Image.open(StringIO(picture['body']))
                        except:
                            raise HTTPError(404)

                        try:
                            content_type = picture[
                                'content_type'
                            ].split('/')[-1].upper()
                        except:
                            content_type = 'JPEG'

                        document = {
                            'status': DBRef(
                                StatusDocument.meta['collection'],
                                ObjectId(status_id)
                            ),
                            'name': picture['filename'],
                            'content_type': content_type,
                            'upload_time': datetime.now()
                        }

                        width = 1024
                        if image.size[0] > width:
                            height = width * 1.0 * image.size[1] / image.size[0]
                            image = image.resize(
                                map(int, (width, height)), Image.ANTIALIAS
                            )

                        output = StringIO()
                        image.save(output, content_type, quality=100)
                        document.update({'body': Binary(output.getvalue())})
                        output.close()

                        thumbnail_width = 200
                        thumbnail_height = (
                            thumbnail_width * 1.0 * image.size[1] / image.size[0]
                        )

                        output = StringIO()
                        image = image.resize(
                            map(int, (thumbnail_width, thumbnail_height)),
                            Image.ANTIALIAS
                        )
                        image.save(output, content_type, quality=100)
                        document.update({
                            'thumbnail': Binary(output.getvalue())
                        })
                        output.close()

                        yield StatusPhotoDocument.insert(document)

                    status = yield StatusDocument.get_status(
                        status_id, self.current_user['_id']
                    )
                    html = self.render_string(
                        'home/template/status/status-list-item.html',
                        status=status
                    )
                    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)
Beispiel #21
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)
Beispiel #22
0
    def post(self):
        '''点赞'''

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

        status_id = form.status_id.data

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

        status_dbref = DBRef(
            StatusDocument.meta['collection'],
            ObjectId(status_id)
        )
        liker_dbref = DBRef(
            UserDocument.meta['collection'],
            ObjectId(self.current_user['_id'])
        )

        document = {
            'status': status_dbref,
            'liker': liker_dbref
        }

        liked = yield StatusLikeDocument.is_liked(
            status_id, self.current_user['_id']
        )
        if not liked:
            now = datetime.now()
            document.update({'like_time': now})
            like_id = yield StatusLikeDocument.insert_one(document)

            if str(self.current_user['_id']) != str(status['author'].id):
                document = {
                    'sender': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'recipient': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(status['author'].id)
                    ),
                    'message_type': 'like:status',
                    'time': now,
                    'read': False,
                    'data': DBRef(
                        StatusLikeDocument.meta['collection'],
                        ObjectId(like_id)
                    )
                }
                message_id = yield MessageDocument.insert(document)
                WriterManager.pub(MessageTopic.LIKE, message_id)

        like_times = yield StatusLikeDocument.get_like_times(status_id)
        like_list = yield StatusLikeDocument.get_like_list(
            status['_id'], self.current_user['_id']
        )
        likers = '、'.join([like['liker']['name'] for like in like_list])

        self.write_json({
            'like_times': like_times,
            'likers': likers
        })
Beispiel #23
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))