def post(self): form = FriendRequestForm(self.request.arguments) if not form.validate(): raise HTTPError(404) user_id = form.user_id.data user = yield UserDocument.find_one({'_id': ObjectId(user_id)}) if not user or user['_id'] == self.current_user['_id']: raise HTTPError(404) is_friend = yield FriendDocument.is_friend( user_id, self.current_user['_id'] ) if not is_friend: yield FriendDocument.add_friend( user_id, self.current_user['_id'] ) yield MessageDocument.remove({ 'sender': DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), 'recipient': DBRef( UserDocument.meta['collection'], ObjectId(self.current_user['_id']) ), 'message_type': MessageTopic.FRIEND_REQUEST_NEW })
def post(self): form = FriendRequestForm(self.request.arguments) if not form.validate(): raise HTTPError(404) user_id = form.user_id.data user = yield UserDocument.find_one({'_id': ObjectId(user_id)}) if not user or user['_id'] == self.current_user['_id']: raise HTTPError(404) is_friend = yield FriendDocument.is_friend(user_id, self.current_user['_id']) if not is_friend: yield FriendDocument.add_friend(user_id, self.current_user['_id']) yield MessageDocument.remove({ 'sender': DBRef(UserDocument.meta['collection'], ObjectId(user_id)), 'recipient': DBRef(UserDocument.meta['collection'], ObjectId(self.current_user['_id'])), 'message_type': MessageTopic.FRIEND_REQUEST_NEW }) self.finish()
def get_friends_status_list(user_id, skip=0, limit=None): '''得到user_id的朋友的状态, 包括自己. :Parameters: - `user_id`: 用户id - `skip`: 默认0 - `limit`: 默认None ''' friend_list = yield FriendDocument.get_friend_list(user_id) shielded_friend_list = yield FriendDocument.get_shielded_friends( user_id ) blocked_friend_list = yield FriendDocument.get_blocked_friends( user_id ) all_friend_dbref_list = [ DBRef(UserDocument.meta['collection'], ObjectId(friend['_id'])) for friend in friend_list ] shielded_friend_dbref_list = [ DBRef(UserDocument.meta['collection'], ObjectId(friend['_id'])) for friend in shielded_friend_list ] blocked_friend_dbref_list = [ DBRef(UserDocument.meta['collection'], ObjectId(friend['_id'])) for friend in blocked_friend_list ] friend_dbref_list = [ DBRef(UserDocument.meta['collection'], ObjectId(user_id)) ] for friend in all_friend_dbref_list: if (friend not in shielded_friend_dbref_list and friend not in blocked_friend_dbref_list): friend_dbref_list.append(friend) cursor = StatusDocument.find({ 'author': {'$in': friend_dbref_list} }).sort([('publish_time', pymongo.DESCENDING)]).skip(skip) if limit is not None: cursor = cursor.limit(limit) status_list = yield StatusDocument.to_list(cursor) status_list = yield StatusDocument.translate_dbref_in_document_list( status_list ) status_list = yield StatusDocument._extend_status_list( status_list, user_id ) raise gen.Return(status_list)
def get_friends_status_list(user_id, skip=0, limit=None): '''得到user_id的朋友的状态, 包括自己. :Parameters: - `user_id`: 用户id - `skip`: 默认0 - `limit`: 默认None ''' friend_list = yield FriendDocument.get_friend_list(user_id) shielded_friend_list = yield FriendDocument.get_shielded_friends( user_id) blocked_friend_list = yield FriendDocument.get_blocked_friends(user_id) all_friend_dbref_list = [ DBRef(UserDocument.meta['collection'], ObjectId(friend['_id'])) for friend in friend_list ] shielded_friend_dbref_list = [ DBRef(UserDocument.meta['collection'], ObjectId(friend['_id'])) for friend in shielded_friend_list ] blocked_friend_dbref_list = [ DBRef(UserDocument.meta['collection'], ObjectId(friend['_id'])) for friend in blocked_friend_list ] friend_dbref_list = [ DBRef(UserDocument.meta['collection'], ObjectId(user_id)) ] for friend in all_friend_dbref_list: if (friend not in shielded_friend_dbref_list and friend not in blocked_friend_dbref_list): friend_dbref_list.append(friend) cursor = StatusDocument.find({ 'author': { '$in': friend_dbref_list } }).sort([('publish_time', pymongo.DESCENDING)]).skip(skip) if limit is not None: cursor = cursor.limit(limit) status_list = yield StatusDocument.to_list(cursor) status_list = yield StatusDocument.translate_dbref_in_document_list( status_list) status_list = yield StatusDocument._extend_status_list( status_list, user_id) raise gen.Return(status_list)
def post(self): response_data = {} form = FriendRequestForm(self.request.arguments) if not form.validate(): raise HTTPError(404) user_id = form.user_id.data user = yield UserDocument.find_one({'_id': ObjectId(user_id)}) if not user or user['_id'] == self.current_user['_id']: raise HTTPError(404) is_friend = yield FriendDocument.is_friend( user_id, self.current_user['_id'] ) if is_friend: raise HTTPError(404) document = { 'sender': DBRef( UserDocument.meta['collection'], ObjectId(self.current_user['_id']) ), 'recipient': DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), 'message_type': MessageTopic.FRIEND_REQUEST_NEW, } message = yield MessageDocument.find_one(document) if message: response_data = {'error': '你已经发送了好友请求!'} else: user_setting = yield UserSettingDocument.get_user_setting(user_id) if not user_setting['require_verify_when_add_friend']: yield FriendDocument.add_friend( user_id, self.current_user['_id'] ) response_data.update({'ok': 1}) document.update({'time': datetime.now()}) message_id = yield MessageDocument.insert(document) WriterManager.pub(MessageTopic.FRIEND_REQUEST_NEW, message_id) self.write_json(response_data)
def can_see(status, user_id): '''判断某人能否看到某状态. :Parameters: - `status`: 状态, 是id或者document - `user_id`: 相关用户 ''' can = False if isinstance(status, (str, ObjectId)): status = yield StatusDocument.find_one({'_id': ObjectId(status)}) if status: if isinstance(status['author'], DBRef): author = status['author'].id elif isinstance(status['author'], dict): author = status['author']['_id'] else: raise gen.Return(can) is_friend = yield FriendDocument.is_friend(user_id, author) user_setting = yield UserSettingDocument.get_user_setting(author) if (str(author) == str(user_id) or is_friend or user_setting['allow_stranger_visiting_profile']): can = True raise gen.Return(can)
def _get_status_liker_cursor(status, user_id): friend_ids = yield FriendDocument.get_same_friends( status['author']['_id'], user_id ) friend_ids += [ DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), DBRef( UserDocument.meta['collection'], ObjectId(status['author']['_id']) ) ] cursor = StatusLikeDocument.find({ 'status': DBRef( StatusDocument.meta['collection'], ObjectId(status['_id']) ), 'liker': {'$in': friend_ids} }) raise gen.Return(cursor)
def can_see(status, user_id): '''判断某人能否看到某状态. :Parameters: - `status`: 状态, 是id或者document - `user_id`: 相关用户 ''' can = False if isinstance(status, (str, ObjectId)): status = yield StatusDocument.find_one({'_id': ObjectId(status)}) if status: if isinstance(status['author'], DBRef): author = status['author'].id elif isinstance(status['author'], dict): author = status['author']['_id'] else: raise gen.Return(can) is_friend = yield FriendDocument.is_friend(user_id, author) user_setting = yield UserSettingDocument.get_user_setting(author) if (str(author) == str(user_id) or is_friend or user_setting['allow_stranger_visiting_profile']): can = True raise gen.Return(can)
def get(self): '''朋友首页''' kwargs = yield self.get_sidebar_arguments() friend_list = yield FriendDocument.get_friend_list( self.current_user['_id']) kwargs.update({'friend_list': friend_list}) self.render('home/template/friend/friend.html', **kwargs)
def post(self): response_data = {} form = FriendRequestForm(self.request.arguments) if not form.validate(): raise HTTPError(404) user_id = form.user_id.data user = yield UserDocument.find_one({'_id': ObjectId(user_id)}) if not user or user['_id'] == self.current_user['_id']: raise HTTPError(404) is_friend = yield FriendDocument.is_friend(user_id, self.current_user['_id']) if is_friend: raise HTTPError(404) document = { 'sender': DBRef(UserDocument.meta['collection'], ObjectId(self.current_user['_id'])), 'recipient': DBRef(UserDocument.meta['collection'], ObjectId(user_id)), 'message_type': MessageTopic.FRIEND_REQUEST_NEW, } message = yield MessageDocument.find_one(document) if message: response_data = {'error': '你已经发送了好友请求!'} else: user_setting = yield UserSettingDocument.get_user_setting(user_id) if not user_setting['require_verify_when_add_friend']: yield FriendDocument.add_friend(user_id, self.current_user['_id']) response_data.update({'ok': 1}) document.update({'time': datetime.now()}) message_id = yield MessageDocument.insert(document) WriterManager.pub(MessageTopic.FRIEND_REQUEST_NEW, message_id) self.finish(json.dumps(response_data))
def get(self): '''朋友首页''' kwargs = yield self.get_sidebar_arguments() friend_list = yield FriendDocument.get_friend_list( self.current_user['_id'] ) kwargs.update({ 'friend_list': friend_list }) self.render('home/template/friend/friend.html', **kwargs)
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)
def get_comment_times_can_seen(status_id, user_id): '''得到某一个人能够看到的评论次数''' status = yield StatusDocument.get_status(status_id) if not status: raise gen.Return(0) if ObjectId(status['author']['_id']) == ObjectId(user_id): cnt = yield StatusCommentDocument.get_comment_times(status_id) raise gen.Return(cnt) can_see = yield StatusDocument.can_see(status, user_id) if not can_see: raise gen.Return(0) friend_ids = yield FriendDocument.get_same_friend_ids( status['author']['_id'], user_id ) friend_ids += [ DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), DBRef( UserDocument.meta['collection'], ObjectId(status['author']['_id']) ) ] cnt = yield StatusCommentDocument.find({ 'status': DBRef( StatusDocument.meta['collection'], ObjectId(status['_id']) ), 'author': {'$in': friend_ids}, '$or': [ {'replyeder': {'$exists': False}}, {'replyeder': {'$exists': True, '$in': friend_ids}} ] }).count() raise gen.Return(cnt)
def _get_status_liker_cursor(status, user_id): friend_ids = yield FriendDocument.get_same_friends( status['author']['_id'], user_id) friend_ids += [ DBRef(UserDocument.meta['collection'], ObjectId(user_id)), DBRef(UserDocument.meta['collection'], ObjectId(status['author']['_id'])) ] cursor = StatusLikeDocument.find({ 'status': DBRef(StatusDocument.meta['collection'], ObjectId(status['_id'])), 'liker': { '$in': friend_ids } }) raise gen.Return(cursor)
def get_comment_times_can_seen(status_id, user_id): '''得到某一个人能够看到的评论次数''' status = yield StatusDocument.get_status(status_id) if not status: raise gen.Return(0) if ObjectId(status['author']['_id']) == ObjectId(user_id): cnt = yield StatusCommentDocument.get_comment_times(status_id) raise gen.Return(cnt) can_see = yield StatusDocument.can_see(status, user_id) if not can_see: raise gen.Return(0) friend_ids = yield FriendDocument.get_same_friend_ids( status['author']['_id'], user_id) friend_ids += [ DBRef(UserDocument.meta['collection'], ObjectId(user_id)), DBRef(UserDocument.meta['collection'], ObjectId(status['author']['_id'])) ] cnt = yield StatusCommentDocument.find({ 'status': DBRef(StatusDocument.meta['collection'], ObjectId(status['_id'])), 'author': { '$in': friend_ids }, '$or': [{ 'replyeder': { '$exists': False } }, { 'replyeder': { '$exists': True, '$in': friend_ids } }] }).count() raise gen.Return(cnt)
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)
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))
def get_comment_list(status_id, user_id, skip=0, limit=None): '''得到某一状态的评论, 只能看到共同好友和自己的评论. :Parameters: - `status_id`: 相关的状态 - `user_id`: 查看评论者 - `skip`: 默认0 - `limit`: 默认None ''' comment_list = [] status = yield StatusDocument.find_one({'_id': ObjectId(status_id)}) if status: status = yield StatusDocument.translate_dbref_in_document(status) if status['author']['_id'] == ObjectId(user_id): cursor = StatusCommentDocument.find({ 'status': DBRef( StatusDocument.meta['collection'], ObjectId(status['_id']) ) }) else: can_see = yield StatusDocument.can_see(status, user_id) if not can_see: raise gen.Return(comment_list) friend_ids = yield FriendDocument.get_same_friend_ids( status['author']['_id'], user_id ) friend_ids += [ DBRef( UserDocument.meta['collection'], ObjectId(user_id) ), DBRef( UserDocument.meta['collection'], ObjectId(status['author']['_id']) ) ] cursor = StatusCommentDocument.find({ 'status': DBRef( StatusDocument.meta['collection'], ObjectId(status['_id']) ), 'author': {'$in': friend_ids}, '$or': [ {'replyeder': {'$exists': False}}, {'replyeder': {'$exists': True, '$in': friend_ids}} ] }) cursor = cursor.sort( [('comment_time', pymongo.ASCENDING)] ).skip(skip) if limit is not None: cursor = cursor.limit(limit) translate = StatusCommentDocument.translate_dbref_in_document_list comment_list = yield StatusCommentDocument.to_list(cursor) comment_list = yield translate(comment_list) raise gen.Return(comment_list)
def get_comment_list(status_id, user_id, skip=0, limit=None): '''得到某一状态的评论, 只能看到共同好友和自己的评论. :Parameters: - `status_id`: 相关的状态 - `user_id`: 查看评论者 - `skip`: 默认0 - `limit`: 默认None ''' comment_list = [] status = yield StatusDocument.find_one({'_id': ObjectId(status_id)}) if status: status = yield StatusDocument.translate_dbref_in_document(status) if status['author']['_id'] == ObjectId(user_id): cursor = StatusCommentDocument.find({ 'status': DBRef(StatusDocument.meta['collection'], ObjectId(status['_id'])) }) else: can_see = yield StatusDocument.can_see(status, user_id) if not can_see: raise gen.Return(comment_list) friend_ids = yield FriendDocument.get_same_friend_ids( status['author']['_id'], user_id) friend_ids += [ DBRef(UserDocument.meta['collection'], ObjectId(user_id)), DBRef(UserDocument.meta['collection'], ObjectId(status['author']['_id'])) ] cursor = StatusCommentDocument.find({ 'status': DBRef(StatusDocument.meta['collection'], ObjectId(status['_id'])), 'author': { '$in': friend_ids }, '$or': [{ 'replyeder': { '$exists': False } }, { 'replyeder': { '$exists': True, '$in': friend_ids } }] }) cursor = cursor.sort([('comment_time', pymongo.ASCENDING) ]).skip(skip) if limit is not None: cursor = cursor.limit(limit) translate = StatusCommentDocument.translate_dbref_in_document_list comment_list = yield StatusCommentDocument.to_list(cursor) comment_list = yield translate(comment_list) raise gen.Return(comment_list)
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)
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)
def post(self, action): '''屏蔽/拉黑/删除某一个朋友''' form = FriendActionForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} friend_id = form.friend_id.data is_friend = yield FriendDocument.is_friend( self.current_user['_id'], friend_id ) if not is_friend: response_data.update({'error': '你们不是朋友'}) else: owner = DBRef( UserDocument.meta['collection'], self.current_user['_id'] ) friend = DBRef( UserDocument.meta['collection'], ObjectId(friend_id) ) friend_document = yield FriendDocument.find_one({ 'owner': owner, 'friend': friend }) if action == 'shield': shielded = True if friend_document['shielded']: shielded = False yield FriendDocument.update( {'_id': friend_document['_id']}, {'$set': {'shielded': shielded}} ) response_data.update({'shielded': shielded}) elif action == 'block': blocked = True if friend_document['blocked']: blocked = False yield FriendDocument.update( {'_id': friend_document['_id']}, {'$set': {'blocked': blocked}} ) response_data.update({'blocked': blocked}) elif action == 'delete': yield FriendDocument.remove({ '$or': [ {'owner': owner, 'friend': friend}, {'owner': friend, 'friend': owner} ] }) else: raise HTTPError(404) self.write_json(response_data)
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))
def post(self, action): '''屏蔽/拉黑/删除某一个朋友''' form = FriendActionForm(self.request.arguments) if not form.validate(): raise HTTPError(404) response_data = {} friend_id = form.friend_id.data is_friend = yield FriendDocument.is_friend( self.current_user['_id'], friend_id ) if not is_friend: response_data.update({'error': '你们不是朋友'}) else: owner = DBRef( UserDocument.meta['collection'], self.current_user['_id'] ) friend = DBRef( UserDocument.meta['collection'], ObjectId(friend_id) ) friend_document = yield FriendDocument.find_one({ 'owner': owner, 'friend': friend }) if action == 'shield': shielded = True if friend_document['shielded']: shielded = False yield FriendDocument.update( {'_id': friend_document['_id']}, {'$set': {'shielded': shielded}} ) response_data.update({'shielded': shielded}) elif action == 'block': blocked = True if friend_document['blocked']: blocked = False yield FriendDocument.update( {'_id': friend_document['_id']}, {'$set': {'blocked': blocked}} ) response_data.update({'blocked': blocked}) elif action == 'delete': yield FriendDocument.remove({ '$or': [ {'owner': owner, 'friend': friend}, {'owner': friend, 'friend': owner} ] }) else: raise HTTPError(404) self.finish(json.dumps(response_data))
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)
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)