Exemple #1
0
    def post(self):
        response_data = {}

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

        topic_id = form.topic_id.data

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if not topic:
            raise HTTPError(404)

        can_afford = yield UserDocument.can_afford(self.current_user['_id'],
                                                   WEALTH_SETTINGS['like'])

        if not can_afford and str(self.current_user['_id']) != str(
                topic['author'].id):
            response_data.update({'error': '金币不足!'})

        topic_dbref = DBRef(TopicDocument.meta['collection'],
                            ObjectId(topic_id))
        liker_dbref = DBRef(UserDocument.meta['collection'],
                            ObjectId(self.current_user['_id']))

        document = {'topic': topic_dbref, 'liker': liker_dbref}

        liked = yield TopicLikeDocument.is_liked(topic_id,
                                                 self.current_user['_id'])

        if not liked and not response_data:
            now = datetime.now()
            document.update({'like_time': now})
            like_id = yield TopicLikeDocument.insert_one(document)

            if str(self.current_user['_id']) != str(topic['author'].id):
                document = {
                    'user':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'activity_type':
                    UserActivityDocument.LIKE,
                    'time':
                    now,
                    'data':
                    DBRef(TopicLikeDocument.meta['collection'],
                          ObjectId(like_id))
                }
                activity_id = yield UserActivityDocument.insert(document)

                # 赞者
                document = {
                    'user':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'in_out_type':
                    WealthRecordDocument.OUT,
                    'activity':
                    DBRef(UserActivityDocument.meta['collection'],
                          ObjectId(activity_id)),
                    'quantity':
                    WEALTH_SETTINGS['like'],
                    'time':
                    now
                }
                yield WealthRecordDocument.insert(document)
                yield UserDocument.update_wealth(self.current_user['_id'],
                                                 -WEALTH_SETTINGS['like'])

                # 被赞者
                document = {
                    'user':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(topic['author'].id)),
                    'in_out_type':
                    WealthRecordDocument.IN,
                    'activity':
                    DBRef(UserActivityDocument.meta['collection'],
                          ObjectId(activity_id)),
                    'quantity':
                    WEALTH_SETTINGS['like'],
                    'time':
                    now
                }
                yield WealthRecordDocument.insert(document)
                yield UserDocument.update_wealth(topic['author'].id,
                                                 WEALTH_SETTINGS['like'])

                document = {
                    'sender':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'recipient':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(topic['author'].id)),
                    'message_type':
                    'like:topic',
                    'time':
                    now,
                    'read':
                    False,
                    'data':
                    DBRef(TopicLikeDocument.meta['collection'],
                          ObjectId(like_id))
                }

                message_id = yield MessageDocument.insert(document)
                WriterManager.pub(MessageTopic.LIKE, message_id)

        like_times = yield TopicLikeDocument.get_like_times(topic_id)
        response_data.update({'like_times': like_times})

        self.write_json(response_data)
Exemple #2
0
    def post(self):
        form = TopicCommentNewForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        content = form.content.data
        topic_id = form.topic_id.data
        anonymous = form.anonymous.data
        replyeder_id = form.replyeder_id.data

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

            if anonymous or self.current_user['_id'] == replyeder['_id']:
                raise HTTPError(404)

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if not topic:
            raise HTTPError(404)

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

        existed = yield TopicCommentDocument.find_one(document)
        if existed and (now - existed['comment_time'] < timedelta(minutes=1)):
            response_data.update({'error': '请不要重复评论!'})
        else:
            document.update({'comment_time': now})

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

            comment_id = yield TopicCommentDocument.insert_one(document)

            activity = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(self.current_user['_id'])),
                'activity_type':
                UserActivityDocument.COMMENT,
                'time':
                now,
                'data':
                DBRef(TopicCommentDocument.meta['collection'],
                      ObjectId(comment_id))
            }
            yield UserActivityDocument.insert(activity)

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

            if (str(self.current_user['_id']) != str(recipient_id)
                    and not anonymous
                    and not (message_topic == MessageTopic.COMMENT
                             and topic['anonymous'])):
                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(TopicCommentDocument.meta['collection'],
                          ObjectId(comment_id))
                }
                message_id = yield MessageDocument.insert(message)
                WriterManager.pub(message_topic, message_id)

            comment_times = yield TopicCommentDocument.get_comment_times(
                topic_id)

            document.update({
                '_id': ObjectId(comment_id),
                'author': self.current_user,
                'floor': comment_times
            })

            if replyeder:
                document.update({'replyeder': replyeder})

            item = self.render_string(
                'community/template/topic-comment-list-item.html',
                comment=document)
            response_data.update({'item': item})

        self.write_json(response_data)
Exemple #3
0
    def post(self):
        form = TopicNewForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        title = form.title.data
        content = form.content.data
        nodes = form.nodes.data.split(',')
        anonymous = form.anonymous.data

        nodes = list(set(nodes))

        if len(nodes) > 3:
            response_data = {'error': '节点请不要超过3个!'}

        can_afford = yield UserDocument.can_afford(
            self.current_user['_id'], WEALTH_SETTINGS['topic_new'])
        if not can_afford:
            response_data = {'error': '金币不足!'}

        new_nodes = []
        for node in nodes:
            existed = yield NodeDocument.find_one({'name': node})
            if existed:
                node_id = existed['_id']
            else:
                node_id = yield NodeDocument.insert({'name': node})

            new_nodes.append(
                DBRef(NodeDocument.meta['collection'], ObjectId(node_id)))

        now = datetime.now()
        document = {
            'author':
            DBRef(UserDocument.meta['collection'],
                  ObjectId(self.current_user['_id'])),
            'title':
            title,
            'anonymous':
            anonymous,
            'nodes':
            new_nodes,
        }

        existed = yield TopicDocument.find_one(document)
        if existed and (now - existed['publish_time'] < timedelta(minutes=1)):
            response_data.update({'error': '你已经发布了一个相同的帖子!'})
        else:
            document.update({'publish_time': now, 'last_update_time': now})

        if not response_data:
            if content:
                document.update({'content': content})

                images = yield self.get_images(content)
                if images:
                    document.update({'images': images})

            topic_id = yield TopicDocument.insert_one(document)

            document = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(self.current_user['_id'])),
                'activity_type':
                UserActivityDocument.TOPIC_NEW,
                'time':
                now,
                'data':
                DBRef(TopicDocument.meta['collection'], ObjectId(topic_id))
            }
            activity_id = yield UserActivityDocument.insert(document)

            document = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(self.current_user['_id'])),
                'in_out_type':
                WealthRecordDocument.OUT,
                'activity':
                DBRef(UserActivityDocument.meta['collection'],
                      ObjectId(activity_id)),
                'quantity':
                WEALTH_SETTINGS['topic_new'],
                'time':
                now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(self.current_user['_id'],
                                             -WEALTH_SETTINGS['topic_new'])

            if not anonymous:
                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.TOPIC_NEW,
                        'time':
                        now,
                        'read':
                        False,
                        'data':
                        DBRef(TopicDocument.meta['collection'],
                              ObjectId(topic_id))
                    }
                    message_id = yield MessageDocument.insert(document)
                    message_list.append(str(message_id))

                if message_list:
                    try:
                        WriterManager.mpub(MessageTopic.TOPIC_NEW,
                                           message_list)
                    except:
                        pass

        self.write_json(response_data)
Exemple #4
0
    def post(self):
        response_data = {}

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

        topic_id = form.topic_id.data

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if not topic:
            raise HTTPError(404)

        can_afford = yield UserDocument.can_afford(
            self.current_user['_id'], WEALTH_SETTINGS['like']
        )

        if not can_afford and str(
                self.current_user['_id']) != str(topic['author'].id):
            response_data.update({'error': '金币不足!'})

        topic_dbref = DBRef(
            TopicDocument.meta['collection'],
            ObjectId(topic_id)
        )
        liker_dbref = DBRef(
            UserDocument.meta['collection'],
            ObjectId(self.current_user['_id'])
        )

        document = {'topic': topic_dbref, 'liker': liker_dbref}

        liked = yield TopicLikeDocument.is_liked(
            topic_id, self.current_user['_id']
        )

        if not liked and not response_data:
            now = datetime.now()
            document.update({'like_time': now})
            like_id = yield TopicLikeDocument.insert_one(document)

            if str(self.current_user['_id']) != str(topic['author'].id):
                document = {
                    'user': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'activity_type': UserActivityDocument.LIKE,
                    'time': now,
                    'data': DBRef(
                        TopicLikeDocument.meta['collection'],
                        ObjectId(like_id)
                    )
                }
                activity_id = yield UserActivityDocument.insert(document)

                # 赞者
                document = {
                    'user': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'in_out_type': WealthRecordDocument.OUT,
                    'activity': DBRef(
                        UserActivityDocument.meta['collection'],
                        ObjectId(activity_id)
                    ),
                    'quantity': WEALTH_SETTINGS['like'],
                    'time': now
                }
                yield WealthRecordDocument.insert(document)
                yield UserDocument.update_wealth(
                    self.current_user['_id'], -WEALTH_SETTINGS['like']
                )

                # 被赞者
                document = {
                    'user': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(topic['author'].id)
                    ),
                    'in_out_type': WealthRecordDocument.IN,
                    'activity': DBRef(
                        UserActivityDocument.meta['collection'],
                        ObjectId(activity_id)
                    ),
                    'quantity': WEALTH_SETTINGS['like'],
                    'time': now
                }
                yield WealthRecordDocument.insert(document)
                yield UserDocument.update_wealth(
                    topic['author'].id, WEALTH_SETTINGS['like']
                )

                document = {
                    'sender': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'recipient': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(topic['author'].id)
                    ),
                    'message_type': 'like:topic',
                    'time': now,
                    'read': False,
                    'data': DBRef(
                        TopicLikeDocument.meta['collection'],
                        ObjectId(like_id)
                    )
                }

                message_id = yield MessageDocument.insert(document)
                WriterManager.pub(MessageTopic.LIKE, message_id)

        like_times = yield TopicLikeDocument.get_like_times(topic_id)
        response_data.update({'like_times': like_times})

        self.write_json(response_data)
Exemple #5
0
    def post(self):
        form = TopicCommentNewForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        content = form.content.data
        topic_id = form.topic_id.data
        anonymous = form.anonymous.data
        replyeder_id = form.replyeder_id.data

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

            if anonymous or self.current_user['_id'] == replyeder['_id']:
                raise HTTPError(404)

        topic = yield TopicDocument.find_one({'_id': ObjectId(topic_id)})
        if not topic:
            raise HTTPError(404)

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

        existed = yield TopicCommentDocument.find_one(document)
        if existed and (now - existed['comment_time'] < timedelta(minutes=1)):
            response_data.update({'error': '请不要重复评论!'})
        else:
            document.update({'comment_time': now})

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

            comment_id = yield TopicCommentDocument.insert_one(document)

            activity = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'activity_type': UserActivityDocument.COMMENT,
                'time': now,
                'data': DBRef(
                    TopicCommentDocument.meta['collection'],
                    ObjectId(comment_id)
                )
            }
            yield UserActivityDocument.insert(activity)

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

            if (str(self.current_user['_id']) != str(recipient_id) and
                    not anonymous and
                    not (message_topic == MessageTopic.COMMENT and
                         topic['anonymous'])):
                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(
                        TopicCommentDocument.meta['collection'],
                        ObjectId(comment_id)
                    )
                }
                message_id = yield MessageDocument.insert(message)
                WriterManager.pub(message_topic, message_id)

            comment_times = yield TopicCommentDocument.get_comment_times(
                topic_id
            )

            document.update({
                '_id': ObjectId(comment_id),
                'author': self.current_user,
                'floor': comment_times
            })

            if replyeder:
                document.update({'replyeder': replyeder})

            item = self.render_string(
                'community/template/topic-comment-list-item.html',
                comment=document
            )
            response_data.update({'item': item})

        self.write_json(response_data)
Exemple #6
0
    def post(self):
        form = TopicNewForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}

        title = form.title.data
        content = form.content.data
        nodes = form.nodes.data.split(',')
        anonymous = form.anonymous.data

        nodes = list(set(nodes))

        if len(nodes) > 3:
            response_data = {'error': '节点请不要超过3个!'}

        can_afford = yield UserDocument.can_afford(
            self.current_user['_id'], WEALTH_SETTINGS['topic_new']
        )
        if not can_afford:
            response_data = {'error': '金币不足!'}

        new_nodes = []
        for node in nodes:
            existed = yield NodeDocument.find_one({'name': node})
            if existed:
                node_id = existed['_id']
            else:
                node_id = yield NodeDocument.insert({'name': node})

            new_nodes.append(
                DBRef(NodeDocument.meta['collection'], ObjectId(node_id))
            )

        now = datetime.now()
        document = {
            'author': DBRef(
                UserDocument.meta['collection'],
                ObjectId(self.current_user['_id'])
            ),
            'title': title,
            'anonymous': anonymous,
            'nodes': new_nodes,
        }

        existed = yield TopicDocument.find_one(document)
        if existed and (now - existed['publish_time'] < timedelta(minutes=1)):
            response_data.update({'error': '你已经发布了一个相同的帖子!'})
        else:
            document.update({'publish_time': now, 'last_update_time': now})

        if not response_data:
            if content:
                document.update({'content': content})

                images = yield self.get_images(content)
                if images:
                    document.update({'images': images})

            topic_id = yield TopicDocument.insert_one(document)

            document = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'activity_type': UserActivityDocument.TOPIC_NEW,
                'time': now,
                'data': DBRef(
                    TopicDocument.meta['collection'], ObjectId(topic_id)
                )
            }
            activity_id = yield UserActivityDocument.insert(document)

            document = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(self.current_user['_id'])
                ),
                'in_out_type': WealthRecordDocument.OUT,
                'activity': DBRef(
                    UserActivityDocument.meta['collection'],
                    ObjectId(activity_id)
                ),
                'quantity': WEALTH_SETTINGS['topic_new'],
                'time': now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(
                self.current_user['_id'], -WEALTH_SETTINGS['topic_new']
            )

            if not anonymous:
                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.TOPIC_NEW,
                        'time': now,
                        'read': False,
                        'data': DBRef(
                            TopicDocument.meta['collection'],
                            ObjectId(topic_id)
                        )
                    }
                    message_id = yield MessageDocument.insert(document)
                    message_list.append(str(message_id))

                if message_list:
                    try:
                        WriterManager.mpub(MessageTopic.TOPIC_NEW, message_list)
                    except:
                        pass

        self.write_json(response_data)