def serve_messages_for(board_id, request_data, db_source): """ Reply-to api client request to receive new messages. """ # pylint: disable=R0914 # Too many local variables (16/15) from x84.bbs import DBProxy, msgbase from x84.bbs.msgbase import to_utctime log = logging.getLogger(__name__) # log.error(msg) db_tags = DBProxy(msgbase.TAGDB, use_session=False) db_messages = DBProxy(msgbase.MSGDB, use_session=False) def message_owned_by(msg_id, board_id): """ Whether given message is owned by specified board. """ return (msg_id in db_source and db_source[msg_id] == board_id) def msgs_after(idx=None): """ Generator of network messages following index ``idx```. If ``idx`` is None, all messages are returned. """ for msg_id in db_tags.get(request_data['network'], []): if idx is None: yield db_messages[idx] elif (int(msg_id) > int(idx) and not message_owned_by(msg_id, board_id)): yield db_messages[msg_id] last_seen = request_data.get('last', None) pending_messages = msgs_after(last_seen) return_messages = list() num_sent = 0 for num_sent, msg in enumerate(pending_messages, start=1): return_messages.append({ u'id': msg.idx, u'author': msg.author, u'recipient': msg.recipient, u'parent': msg.parent, u'subject': msg.subject, u'tags': list(msg.tags ^ set([request_data['network']])), u'ctime': to_utctime(msg.ctime), u'body': msg.body }) if num_sent >= BATCH_MSGS: log.warn('[{request_data[network]}] Batch limit reached for ' 'board {board_id}; halting' .format(request_data=request_data, board_id=board_id)) break if num_sent > 0: log.info('[{request_data[network]}] {num_sent} messages ' 'served to {board_id}'.format(request_data=request_data, num_sent=num_sent, board_id=board_id)) return {u'response': True, u'messages': return_messages}
def serve_messages_for(board_id, request_data, db_source): " Reply-to api client request to receive new messages. " from x84.bbs import DBProxy, msgbase from x84.bbs.msgbase import to_utctime log = logging.getLogger(__name__) #log.error(msg) db_tags = DBProxy(msgbase.TAGDB, use_session=False) db_messages = DBProxy(msgbase.MSGDB, use_session=False) def message_owned_by(msg_id, board_id): return (msg_id in db_source and db_source[msg_id] == board_id) def msgs_after(idx=None): for msg_id in db_tags.get(request_data['network'], []): if idx is None: yield db_messages[idx] elif (int(msg_id) > int(idx) and not message_owned_by(msg_id, board_id)): yield db_messages[msg_id] last_seen = request_data.get('last', None) pending_messages = msgs_after(last_seen) return_messages = list() num_sent = 0 for num_sent, msg in enumerate(pending_messages, start=1): return_messages.append({ u'id': msg.idx, u'author': msg.author, u'recipient': msg.recipient, u'parent': msg.parent, u'subject': msg.subject, u'tags': list(msg.tags ^ set([request_data['network']])), u'ctime': to_utctime(msg.ctime), u'body': msg.body }) if num_sent >= BATCH_MSGS: log.warn('[{request_data[network]}] Batch limit reached for ' 'board {board_id}; halting'.format( request_data=request_data, board_id=board_id)) break if num_sent > 0: log.info('[{request_data[network]}] {num_sent} messages ' 'served to {board_id}'.format(request_data=request_data, num_sent=num_sent, board_id=board_id)) return {u'response': True, u'messages': return_messages}
def prepare_message(msg, network, parent): """ turn a Msg object into a dict for transfer """ from x84.bbs.msgbase import format_origin_line, to_utctime return { 'author': msg.author, 'subject': msg.subject, 'recipient': msg.recipient, 'parent': parent, 'tags': [tag for tag in msg.tags if tag != network['name']], 'body': u''.join((msg.body, format_origin_line())), 'ctime': to_utctime(msg.ctime) }