def update_draft(crispin_client, account_id, message_id, args): """ Sync an updated draft back to the remote backend. """ with session_scope(account_id) as db_session: message = db_session.query(Message).get(message_id) version = args.get("version") old_message_id_header = args.get("old_message_id_header") if message is None: log.info( "tried to save nonexistent message as draft", message_id=message_id, account_id=account_id, ) return if not message.is_draft: log.warning( "tried to save non-draft message as draft", message_id=message_id, account_id=account_id, ) return if version != message.version: log.warning("tried to save outdated version of draft") return remote_update_draft(crispin_client, account_id, message_id, old_message_id_header)
def test_draft_updates(db, default_account, mock_imapclient): # Set up folder list mock_imapclient._data['Drafts'] = {} mock_imapclient._data['Trash'] = {} mock_imapclient.list_folders = lambda: [ (('\\HasNoChildren', '\\Drafts'), '/', 'Drafts'), (('\\HasNoChildren', '\\Trash'), '/', 'Trash') ] pool = writable_connection_pool(default_account.id) draft = create_message_from_json({'subject': 'Test draft'}, default_account.namespace, db.session, True) draft.is_draft = True draft.version = 0 remote_save_draft(default_account, draft, db.session) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that draft is not resaved if already synced. remote_update_draft(default_account, draft, db.session) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) assert len(conn.all_uids()) == 1 # Check that an older version is deleted draft.version = 4 update_draft(db.session, default_account, draft, from_addr=draft.from_addr, subject='New subject', blocks=[]) remote_update_draft(default_account, draft, db.session) with pool.get() as conn: conn.select_folder('Drafts', lambda *args: True) all_uids = conn.all_uids() assert len(all_uids) == 1 data = conn.uids(all_uids)[0] parsed = mime.from_string(data.body) expected_message_id = '<{}-{}@mailer.nylas.com>'.format( draft.public_id, draft.version) assert parsed.headers.get('Message-Id') == expected_message_id
def update_draft(account_id, message_id, args): """ Sync an updated draft back to the remote backend. """ with session_scope(account_id) as db_session: message = db_session.query(Message).get(message_id) version = args.get('version') if message is None: log.info('tried to save nonexistent message as draft', message_id=message_id, account_id=account_id) return if not message.is_draft: log.warning('tried to save non-draft message as draft', message_id=message_id, account_id=account_id) return if version != message.version: log.warning('tried to save outdated version of draft') return remote_update_draft(account_id, message_id)