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

        login_reward_fetched = yield UserActivityDocument.login_reward_fetched(
            self.current_user['_id']
        )

        if login_reward_fetched:
            response_data.update({'error': '你已经领取了今日的登录奖励!'})
        else:
            now = datetime.now()

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

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id']
            )
            quantity = (1 + continuous_login_days / 7) * 5

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

            yield UserDocument.update_wealth(
                self.current_user['_id'], quantity
            )
            yield UserDocument.update(
                {'_id': ObjectId(self.current_user['_id'])},
                {'$inc': {'continuous_login_days': 1}}
            )

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id']
            )

            response_data.update({
                'wealth': self.current_user['wealth'] + quantity,
                'continuous_login_days': continuous_login_days
            })

        self.write_json(response_data)
Example #2
0
    def post(self):
        response_data = {}

        login_reward_fetched = yield UserActivityDocument.login_reward_fetched(
            self.current_user['_id'])

        if login_reward_fetched:
            response_data.update({'error': '你已经领取了今日的登录奖励!'})
        else:
            now = datetime.now()

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

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id'])
            quantity = (1 + continuous_login_days / 7) * 5

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

            yield UserDocument.update_wealth(self.current_user['_id'],
                                             quantity)
            yield UserDocument.update(
                {'_id': ObjectId(self.current_user['_id'])},
                {'$inc': {
                    'continuous_login_days': 1
                }})

            continuous_login_days = yield UserDocument.get_continuous_login_days(
                self.current_user['_id'])

            response_data.update({
                'wealth': self.current_user['wealth'] + quantity,
                'continuous_login_days': continuous_login_days
            })

        self.finish(json.dumps(response_data))
Example #3
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)
Example #4
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)
Example #5
0
    def post(self):
        form = ShareLikeForm(self.request.arguments)
        if not form.validate():
            raise HTTPError(404)

        response_data = {}
        share_id = form.share_id.data

        share = yield ShareDocument.find_one({'_id': ObjectId(share_id)})
        if not share:
            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(share['uploader'].id)):
            response_data.update({'error': '金币不足!'})

        share_dbref = DBRef(ShareDocument.meta['collection'],
                            ObjectId(share_id))
        liker_dbref = DBRef(UserDocument.meta['collection'],
                            ObjectId(self.current_user['_id']))

        document = {'share': share_dbref, 'liker': liker_dbref}

        liked = yield ShareLikeDocument.is_liked(share_id,
                                                 self.current_user['_id'])
        if not liked and not response_data:
            now = datetime.now()

            document.update({'like_time': now})
            like_id = yield ShareLikeDocument.insert_one(document)

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

                # 赞者
                wealth = {
                    '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(wealth)
                yield UserDocument.update_wealth(self.current_user['_id'],
                                                 -WEALTH_SETTINGS['like'])

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

                message = {
                    'sender':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(self.current_user['_id'])),
                    'recipient':
                    DBRef(UserDocument.meta['collection'],
                          ObjectId(share['uploader'].id)),
                    'message_type':
                    'like:share',
                    'time':
                    now,
                    'read':
                    False,
                    'data':
                    DBRef(ShareLikeDocument.meta['collection'],
                          ObjectId(like_id))
                }

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

        like_times = yield ShareLikeDocument.get_like_times(share_id)
        response_data.update({'like_times': like_times})

        self.finish(json.dumps(response_data))
Example #6
0
    def get(self, share_id):
        share = yield ShareDocument.get_share(share_id)

        if not share or not share['passed'] or 'origin_file' not in share:
            raise HTTPError(404)

        self.set_header('Content-Type', self.get_content_type(share))
        self.set_header('Content-Disposition',
                        'attachment; filename=%s' % self.get_filename(share))

        fs = self.get_gridfs()
        gridout = yield fs.get(ObjectId(share['origin_file']))
        if gridout.length <= 0:
            raise HTTPError(404)

        if (self.current_user['_id'] != share['uploader']['_id']
                and self.current_user['wealth'] < share['cost']):
            raise HTTPError(404)

        size = 0
        while size < gridout.length:
            content = yield gridout.read(gridout.chunk_size)
            size += len(content)
            self.write(content)

        yield ShareDocument.update({'_id': ObjectId(share_id)},
                                   {'$inc': {
                                       'download_times': 1
                                   }})

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

        document = {
            'share':
            DBRef(ShareDocument.meta['collection'], ObjectId(share_id)),
            'downloader':
            DBRef(UserDocument.meta['collection'],
                  ObjectId(self.current_user['_id'])),
            'download_time':
            now
        }
        yield ShareDownloadDocument.insert(document)

        if (share['cost'] > 0
                and self.current_user['_id'] != share['uploader']['_id']):
            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':
                share['cost'],
                'time':
                now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(self.current_user['_id'],
                                             -share['cost'])

            document = {
                'user':
                DBRef(UserDocument.meta['collection'],
                      ObjectId(share['uploader']['_id'])),
                'in_out_type':
                WealthRecordDocument.IN,
                'activity':
                DBRef(UserActivityDocument.meta['collection'],
                      ObjectId(activity_id)),
                'quantity':
                share['cost'],
                'time':
                now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(share['uploader']['_id'],
                                             share['cost'])

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

        response_data = {}
        share_id = form.share_id.data

        share = yield ShareDocument.find_one({
            '_id': ObjectId(share_id)
        })
        if not share:
            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(share['uploader'].id)):
            response_data.update({'error': '金币不足!'})

        share_dbref = DBRef(
            ShareDocument.meta['collection'],
            ObjectId(share_id)
        )
        liker_dbref = DBRef(
            UserDocument.meta['collection'],
            ObjectId(self.current_user['_id'])
        )

        document = {
            'share': share_dbref,
            'liker': liker_dbref
        }

        liked = yield ShareLikeDocument.is_liked(
            share_id, self.current_user['_id']
        )
        if not liked and not response_data:
            now = datetime.now()

            document.update({
                'like_time': now
            })
            like_id = yield ShareLikeDocument.insert_one(document)

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

                # 赞者
                wealth = {
                    '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(wealth)
                yield UserDocument.update_wealth(
                    self.current_user['_id'], -WEALTH_SETTINGS['like']
                )

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

                message = {
                    'sender': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(self.current_user['_id'])
                    ),
                    'recipient': DBRef(
                        UserDocument.meta['collection'],
                        ObjectId(share['uploader'].id)
                    ),
                    'message_type': 'like:share',
                    'time': now,
                    'read': False,
                    'data': DBRef(
                        ShareLikeDocument.meta['collection'],
                        ObjectId(like_id)
                    )
                }

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

        like_times = yield ShareLikeDocument.get_like_times(share_id)
        response_data.update({'like_times': like_times})

        self.write_json(response_data)
Example #8
0
    def get(self, share_id):
        share = yield ShareDocument.get_share(share_id)

        if not share or not share['passed'] or 'origin_file' not in share:
            raise HTTPError(404)

        self.set_header('Content-Type', self.get_content_type(share))
        self.set_header(
            'Content-Disposition',
            'attachment; filename=%s' % self.get_filename(share)
        )

        fs = self.get_gridfs()
        gridout = yield fs.get(ObjectId(share['origin_file']))
        if gridout.length <= 0:
            raise HTTPError(404)

        if (self.current_user['_id'] != share['uploader']['_id']
                and self.current_user['wealth'] < share['cost']):
            raise HTTPError(404)

        size = 0
        while size < gridout.length:
            content = yield gridout.read(gridout.chunk_size)
            size += len(content)
            self.write(content)

        yield ShareDocument.update(
            {'_id': ObjectId(share_id)},
            {'$inc': {'download_times': 1}}
        )

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

        document = {
            'share': DBRef(
                ShareDocument.meta['collection'],
                ObjectId(share_id)
            ),
            'downloader': DBRef(
                UserDocument.meta['collection'],
                ObjectId(self.current_user['_id'])
            ),
            'download_time': now
        }
        yield ShareDownloadDocument.insert(document)

        if (share['cost'] > 0 and
                self.current_user['_id'] != share['uploader']['_id']):
            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': share['cost'],
                'time': now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(
                self.current_user['_id'], -share['cost']
            )

            document = {
                'user': DBRef(
                    UserDocument.meta['collection'],
                    ObjectId(share['uploader']['_id'])
                ),
                'in_out_type': WealthRecordDocument.IN,
                'activity': DBRef(
                    UserActivityDocument.meta['collection'],
                    ObjectId(activity_id)
                ),
                'quantity': share['cost'],
                'time': now
            }
            yield WealthRecordDocument.insert(document)
            yield UserDocument.update_wealth(
                share['uploader']['_id'], share['cost']
            )

        self.finish()
Example #9
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)
Example #10
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)