def postMessage(user, tags, clubs, text, anon=False, anoncomments=False, format=None, sfrom=None): """!Это дерьмо создает новое сообщение и рассылает его. @param user Объект-пользователь. @param tags Список тегов. @param clubs Список клубов. @param text Текст сообщения. @param anon Отправить от анона. @param anoncom Все комментарии принудительно анонимны. @param format Используемое в сообщении форматирование (Markdown, MoinMoin, plaintext). """ if len(text) == 0: defer.returnValue((False, 'So where is your post?')) if len(text) > 10240: defer.returnValue( (False, 'Message is too long. %d/10240' % (len(text), ))) format = format or user.get('settings', {}).get('default_format') message = { 'user': user['name'], 'tags': tags, 'clubs': clubs, 'id': genid(6), 'date': time.time(), 'replycount': 0, 'text': text, 'anonymous': bool(anon), 'anoncomments': bool(anoncomments), 'recommendations': [], 'format': format, 'html': renderPostOrComment(text, format), } if anon: message['real_user'] = message['user'] message['user'] = '******' stored_message = objs.Message(message) stored_message_id = yield stored_message.save() sub_result = yield subscribe(user, 'sub_message', message['id'], True, sfrom) queries = [{'target': tag, 'type': 'sub_tag'} for tag in tags] queries += [{'target': club, 'type': 'sub_club'} for club in clubs] if ('@' in clubs) or (len(clubs) == 0): queries += [{ 'target': 'anonymous' if anon else user['name'], 'type': 'sub_user' }] qn, recipients = yield send_to_subscribers(queries, stored_message) filtered = stored_message.filter_fields() publish('new_message', filtered) # ALARM publish('new_message_on_user_' + message['user'], filtered) defer.returnValue((True, (message['id'], qn, recipients)))
def postComment(message_id, comment_id, text, user, anon=False, sfrom=None): """!Это дерьмо постит комментарий. @param message_id Id сообщения к которому комментарий. @param comment_id Если ответ - id комментария, на который отвечаем. @param text Текст комментария. @param user Объект-пользователь. @param anon Анонимный ответ. """ if len(text) == 0: defer.returnValue((False, 'So where is your comment?')) if len(text) > 4096: defer.returnValue( (False, 'Comment is too long. %d/4096' % (len(text),))) message = yield objs.Message.find_one({'id': message_id}) if comment_id: old_comment = yield objs.Comment.find_one({'id': comment_id, 'message': message_id}) else: old_comment = None if (not old_comment) and comment_id: defer.returnValue((False, 'No such comment.')) if not message: defer.returnValue((False, 'No such message.')) if message.get('anoncomments'): anon = True comment = {'user': user['name'], 'message': message_id, 'date': time.time(), 'replyto': old_comment['id'] if old_comment else None, 'num': message['replycount'] + 1, 'replytotext': cropstring(old_comment['text'] if comment_id else message['text'], 128), 'text': ('@' + old_comment['user'] + ' 'if comment_id else '') + text, 'anonymous': bool(anon), } # 'depth': old_comment.get('depth',0)+1 if old_comment else 0, if anon: comment['real_user'] = comment['user'] comment['user'] = '******' comment = objs.Comment(comment) for x in range(0, 10): try: comment['id'] = message_id + '/' + genid(3) comment_id = yield comment.save() except mongo_errors.OperationFailure, e: pass # if e['code']!=11000: # raise else: break
def postMessage(user, tags, clubs, text, anon=False, anoncomments=False, format=None, sfrom=None): """!Это дерьмо создает новое сообщение и рассылает его. @param user Объект-пользователь. @param tags Список тегов. @param clubs Список клубов. @param text Текст сообщения. @param anon Отправить от анона. @param anoncom Все комментарии принудительно анонимны. @param format Используемое в сообщении форматирование (Markdown, MoinMoin, plaintext). """ if len(text) == 0: defer.returnValue((False, 'So where is your post?')) if len(text) > 10240: defer.returnValue( (False, 'Message is too long. %d/10240' % (len(text),))) format = format or user.get('settings',{}).get('default_format') message = {'user': user['name'], 'tags': tags, 'clubs': clubs, 'id': genid(6), 'date': time.time(), 'replycount': 0, 'text': text, 'anonymous': bool(anon), 'anoncomments': bool(anoncomments), 'recommendations': [], 'format': format, 'html': renderPostOrComment(text, format), } if anon: message['real_user'] = message['user'] message['user'] = '******' stored_message = objs.Message(message) stored_message_id = yield stored_message.save() sub_result = yield subscribe(user, 'sub_message', message['id'], True, sfrom) queries = [{'target': tag, 'type': 'sub_tag'} for tag in tags] queries += [{'target': club, 'type': 'sub_club'} for club in clubs] if ('@' in clubs) or (len(clubs) == 0): queries += [{'target': 'anonymous' if anon else user[ 'name'], 'type': 'sub_user'}] qn, recipients = yield send_to_subscribers(queries, stored_message) filtered = stored_message.filter_fields() publish('new_message', filtered) # ALARM publish('new_message_on_user_' + message['user'], filtered) defer.returnValue((True, (message['id'], qn, recipients)))
def postComment(message_id, comment_id, text, user, anon=False, sfrom=None): """!Это дерьмо постит комментарий. @param message_id Id сообщения к которому комментарий. @param comment_id Если ответ - id комментария, на который отвечаем. @param text Текст комментария. @param user Объект-пользователь. @param anon Анонимный ответ. """ if len(text) == 0: defer.returnValue((False, 'So where is your comment?')) if len(text) > 4096: defer.returnValue( (False, 'Comment is too long. %d/4096' % (len(text),))) message = yield objs.Message.find_one({'id': message_id}) if comment_id: old_comment = yield objs.Comment.find_one({'id': comment_id, 'message': message_id}) else: old_comment = None if (not old_comment) and comment_id: defer.returnValue((False, 'No such comment.')) if not message: defer.returnValue((False, 'No such message.')) if message.get('anoncomments'): anon = True comment = {'user': user['name'], 'message': message_id, 'date': time.time(), 'replyto': old_comment['id'] if old_comment else None, 'num': message['replycount'] + 1, 'replytotext': cropstring(old_comment['text'] if comment_id else message['text'], 128), 'text': ('@' + old_comment['user'] + ' 'if comment_id else '') + text, 'anonymous': bool(anon), } # 'depth': old_comment.get('depth',0)+1 if old_comment else 0, if anon: comment['real_user'] = comment['user'] comment['user'] = '******' comment = objs.Comment(comment) for x in range(0, 10): try: comment['id'] = message_id + '/' + genid(3) comment_id = yield comment.save() except:# mongo_errors.OperationFailure, e: pass # if e['code']!=11000: # raise else: break else: defer.returnValue( (False, 'Looks like this message has reached its bumplimit.')) sub_result = yield subscribe(user, 'sub_message', message_id, False, sfrom) _ = (yield objs.Message.mupdate({'id': message_id}, {'$inc': {'replycount': 1}})) qn, recipients = yield send_to_subscribers([{'target': message_id, 'type': 'sub_message'}], comment) filtered_comment = comment.filter_fields() # ALARM publish('new_comment', filtered_comment) publish('new_comment_in_' + message_id, filtered_comment) publish('upd_comments_count', message_id, comment['num']) publish('upd_comments_count_in_'+message_id, message_id, comment['num']) defer.returnValue((True, (comment['id'], comment['num'], qn, recipients)))
def postComment(message_id, comment_id, text, user, anon=False, format=None, sfrom=None): """!Это дерьмо постит комментарий. @param message_id Id сообщения к которому комментарий. @param comment_id Если ответ - id комментария, на который отвечаем. @param text Текст комментария. @param user Объект-пользователь. @param anon Анонимный ответ. @param format Используемое в сообщении форматирование (Markdown, MoinMoin, plaintext). """ if len(text) == 0: defer.returnValue((False, 'So where is your comment?')) if len(text) > 4096: defer.returnValue( (False, 'Comment is too long. %d/4096' % (len(text),))) message = yield objs.Message.find_one({'id': message_id}) if comment_id: old_comment = yield objs.Comment.find_one({'id': comment_id, 'message': message_id}) else: old_comment = None if (not old_comment) and comment_id: defer.returnValue((False, 'No such comment.')) if not message: defer.returnValue((False, 'No such message.')) if message.get('anoncomments'): anon = True format = format or user.get('settings',{}).get('default_format') stored_text = ('@' + old_comment['user'] + ' 'if comment_id else '') + text comment = {'user': user['name'], 'message': message_id, 'date': time.time(), 'replyto': old_comment['id'] if old_comment else None, 'num': message['replycount'] + 1, 'replytotext': cropstring(old_comment['text'] if comment_id else message['text'], 128), 'text': stored_text, 'anonymous': bool(anon), 'format': format, 'html': renderPostOrComment(stored_text, format), } # 'depth': old_comment.get('depth',0)+1 if old_comment else 0, if anon: comment['real_user'] = comment['user'] comment['user'] = '******' comment = objs.Comment(comment) for x in range(0, 10): try: comment['id'] = message_id + '/' + genid(3) comment_id = yield comment.save() except:# mongo_errors.OperationFailure, e: pass # if e['code']!=11000: # raise else: break else: defer.returnValue( (False, 'Looks like this message has reached its bumplimit.')) sub_result = yield subscribe(user, 'sub_message', message_id, False, sfrom) _ = (yield objs.Message.mupdate({'id': message_id}, {'$inc': {'replycount': 1}})) qn, recipients = yield send_to_subscribers([{'target': message_id, 'type': 'sub_message'}], comment) filtered_comment = comment.filter_fields() # ALARM publish('new_comment', filtered_comment) publish('new_comment_in_' + message_id, filtered_comment) publish('upd_comments_count', message_id, comment['num']) publish('upd_comments_count_in_'+message_id, message_id, comment['num']) defer.returnValue((True, (comment['id'], comment['num'], qn, recipients)))