def new_message_from_synced(db): received_date = datetime.datetime(2014, 9, 22, 17, 25, 46) new_msg = Message.create_from_synced(default_account(db), 139219, '[Gmail]/All Mail', received_date, raw_message()) assert new_msg.received_date == received_date return new_msg
def create_imap_message(db_session, account, folder, msg): """ IMAP-specific message creation logic. Returns ------- imapuid : inbox.models.backends.imap.ImapUid New db object, which links to new Message and Block objects through relationships. All new objects are uncommitted. """ new_message = Message.create_from_synced( account=account, mid=msg.uid, folder_name=folder.name, received_date=msg.internaldate, body_string=msg.body ) # Check to see if this is a copy of a message that was first created # by the Nylas API. If so, don't create a new object; just use the old one. existing_copy = reconcile_message(new_message, db_session) if existing_copy is not None: new_message = existing_copy imapuid = ImapUid(account=account, folder=folder, msg_uid=msg.uid, message=new_message) imapuid.update_flags(msg.flags) if msg.g_labels is not None: imapuid.update_labels(msg.g_labels) # Update the message's metadata with db_session.no_autoflush: is_draft = imapuid.is_draft and (folder.canonical_name == "drafts" or folder.canonical_name == "all") update_message_metadata(db_session, account, new_message, is_draft) update_contacts_from_message(db_session, new_message, account.namespace) return imapuid
def test_sanitize_subject(default_account): from inbox.log import configure_logging configure_logging() # Raw message with encoded null bytes in subject header. raw_message_with_wonky_subject = \ '''From: "UPS My Choice" <*****@*****.**> To: [email protected] Subject: =?UTF-8?B?WW91ciBVUFMgUGFja2FnZSB3YXMgZGVsaXZlcmVkAAAA?= Content-Type: text/html; charset=UTF-8 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary=--==_mimepart_553921a23aa2c_3aee3fe2e442b2b815347 --==_mimepart_553921a23aa2c_3aee3fe2e442b2b815347 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable --==_mimepart_553921a23aa2c_3aee3fe2e442b2b815347 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable --==_mimepart_553921a23aa2c_3aee3fe2e442b2b815347 ''' m = Message.create_from_synced(default_account, 22, '[Gmail]/All Mail', datetime.datetime.utcnow(), raw_message_with_wonky_subject) assert m.subject == u'Your UPS Package was delivered'
def create_imap_message(db_session, log, account, folder, msg): """ IMAP-specific message creation logic. This is the one function in this file that gets to take an account object instead of an account_id, because we need to relate the account to ImapUids for versioning to work, since it needs to look up the namespace. Returns ------- imapuid : inbox.models.tables.imap.ImapUid New db object, which links to new Message and Block objects through relationships. All new objects are uncommitted. """ new_msg = Message.create_from_synced(account=account, mid=msg.uid, folder_name=folder.name, received_date=msg.internaldate, body_string=msg.body) # Check to see if this is a copy of a message that was first created # by the Inbox API. If so, don't create a new object; just use the old one. existing_copy = reconcile_message(new_msg, db_session) if existing_copy is not None: new_msg = existing_copy imapuid = ImapUid(account=account, folder=folder, msg_uid=msg.uid, message=new_msg) imapuid.update_flags_and_labels(msg.flags, msg.g_labels) new_msg.is_draft = imapuid.is_draft new_msg.is_read = imapuid.is_seen update_contacts_from_message(db_session, new_msg, account.namespace) return imapuid
def create_imap_message(db_session, log, account, folder, msg): """ IMAP-specific message creation logic. This is the one function in this file that gets to take an account object instead of an account_id, because we need to relate the account to ImapUids for versioning to work, since it needs to look up the namespace. Returns ------- imapuid : inbox.models.tables.imap.ImapUid New db object, which links to new Message and Block objects through relationships. All new objects are uncommitted. """ new_msg = Message.create_from_synced(account=account, mid=msg.uid, folder_name=folder.name, received_date=msg.internaldate, body_string=msg.body) # Check to see if this is a copy of a message that was first created # by the Inbox API. If so, don't create a new object; just use the old one. existing_copy = reconcile_message(new_msg, db_session) if existing_copy is not None: new_msg = existing_copy imapuid = ImapUid(account=account, folder=folder, msg_uid=msg.uid, message=new_msg) imapuid.update_flags_and_labels(msg.flags, msg.g_labels) new_msg.is_draft = imapuid.is_draft new_msg.is_read = imapuid.is_seen update_contacts_from_message(db_session, new_msg, account.namespace) return imapuid
def test_store_full_body_on_parse_error(default_account, mime_message_with_bad_date): received_date = None m = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, mime_message_with_bad_date.to_string()) assert get_from_blockstore(m.data_sha256)
def test_store_full_body_on_parse_error( default_account, mime_message_with_bad_date): received_date = None m = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, mime_message_with_bad_date.to_string()) assert m.full_body
def test_sanitize_subject(default_account): from inbox.log import configure_logging configure_logging() # Raw message with encoded null bytes in subject header. raw_message_with_wonky_subject = \ '''From: "UPS My Choice" <*****@*****.**> To: [email protected] Subject: =?UTF-8?B?WW91ciBVUFMgUGFja2FnZSB3YXMgZGVsaXZlcmVkAAAA?= Content-Type: text/html; charset=UTF-8 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary=--==_mimepart_553921a23aa2c_3aee3fe2e442b2b815347 --==_mimepart_553921a23aa2c_3aee3fe2e442b2b815347 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable --==_mimepart_553921a23aa2c_3aee3fe2e442b2b815347 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable --==_mimepart_553921a23aa2c_3aee3fe2e442b2b815347 ''' m = Message.create_from_synced(default_account, 22, '[Gmail]/All Mail', datetime.datetime.utcnow(), raw_message_with_wonky_subject) assert m.subject == u'Your UPS Package was delivered'
def test_parse_body_on_bad_attachment( default_account, raw_message_with_bad_attachment): received_date = None m = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, raw_message_with_bad_attachment) assert m.decode_error assert 'dingy blue carpet' in m.body
def test_parse_body_on_bad_attachment( default_account, raw_message_with_bad_attachment): received_date = None m = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, raw_message_with_bad_attachment) assert m.decode_error assert 'dingy blue carpet' in m.body
def test_store_full_body_on_parse_error( default_account, default_namespace, raw_message_with_bad_date): received_date = None m = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, raw_message_with_bad_date) assert m.full_body
def test_truncate_recipients(db, default_account, default_namespace, thread, raw_message_with_many_recipients): received_date = datetime.datetime(2014, 9, 22, 17, 25, 46) m = Message.create_from_synced( default_account, 139219, "[Gmail]/All Mail", received_date, raw_message_with_many_recipients ) m.thread = thread db.session.add(m) # Check that no database error is raised. db.session.commit()
def test_sanitize_subject(default_account, mime_message): # Parse a raw message with encoded null bytes in subject header; # check that we strip the null bytes. mime_message.headers['Subject'] = \ '=?UTF-8?B?WW91ciBVUFMgUGFja2FnZSB3YXMgZGVsaXZlcmVkAAAA?=' m = Message.create_from_synced( default_account, 22, '[Gmail]/All Mail', datetime.datetime.utcnow(), mime_message.to_string()) assert m.subject == u'Your UPS Package was delivered'
def create_from_synced(db, account, raw_message): thread = add_fake_thread(db.session, account.namespace.id) received_date = datetime.datetime.utcnow() m = Message.create_from_synced(account, 22, "[Gmail]/All Mail", received_date, raw_message) m.thread = thread db.session.add(m) db.session.commit() return m
def test_sanitize_subject(default_account, mime_message): # Parse a raw message with encoded null bytes in subject header; # check that we strip the null bytes. mime_message.headers['Subject'] = \ '=?UTF-8?B?WW91ciBVUFMgUGFja2FnZSB3YXMgZGVsaXZlcmVkAAAA?=' m = Message.create_from_synced(default_account, 22, '[Gmail]/All Mail', datetime.datetime.utcnow(), mime_message.to_string()) assert m.subject == u'Your UPS Package was delivered'
def new_message_from_synced(db): received_date = datetime.datetime(2014, 9, 22, 17, 25, 46) new_msg = Message.create_from_synced(default_account(db), 139219, '[Gmail]/All Mail', received_date, raw_message()) assert new_msg.received_date == received_date return new_msg
def create_from_synced(db, account, raw_message): thread = add_fake_thread(db.session, account.namespace.id) received_date = datetime.datetime.utcnow() m = Message.create_from_synced(account, 22, '[Gmail]/All Mail', received_date, raw_message) m.thread = thread db.session.add(m) db.session.commit() return m
def new_message_from_synced(db, default_account, mime_message): from inbox.models import Message received_date = datetime(2014, 9, 22, 17, 25, 46) new_msg = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, mime_message.to_string()) assert new_msg.received_date == received_date return new_msg
def test_truncate_recipients(db, default_account, default_namespace, thread, raw_message_with_many_recipients): received_date = datetime.datetime(2014, 9, 22, 17, 25, 46) m = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, raw_message_with_many_recipients) m.thread = thread db.session.add(m) # Check that no database error is raised. db.session.commit()
def new_message_from_synced(db, default_account, mime_message): from inbox.models import Message received_date = datetime(2014, 9, 22, 17, 25, 46) new_msg = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, mime_message.to_string()) assert new_msg.received_date == received_date new_msg.is_read = True new_msg.is_starred = False return new_msg
def test_parse_body_on_bad_attachment(default_account, default_namespace, raw_message_with_bad_attachment): received_date = None m = Message.create_from_synced( default_account, 139219, "[Gmail]/All Mail", received_date, raw_message_with_bad_attachment ) assert m.decode_error plain_part, html_part = m.body_parts assert "dingy blue carpet" in plain_part assert "dingy blue carpet" in html_part assert "dingy blue carpet" in m.body
def add_fake_msg_with_calendar_part(db_session, account, ics_str): parsed = mime.create.multipart('mixed') parsed.append( mime.create.attachment('text/calendar', ics_str, disposition=None)) msg = Message.create_from_synced(account, 22, '[Gmail]/All Mail', datetime.utcnow(), parsed.to_string()) msg.thread = add_fake_thread(db_session, account.namespace.id) assert msg.has_attached_events return msg
def test_truncate_recipients(db, raw_message_with_many_recipients): account = db.session.query(Account).get(ACCOUNT_ID) assert account.namespace.id == NAMESPACE_ID received_date = datetime.datetime(2014, 9, 22, 17, 25, 46), m = Message.create_from_synced(account, 139219, '[Gmail]/All Mail', received_date, raw_message_with_many_recipients) m.thread_id = 1 db.session.add(m) # Check that no database error is raised. db.session.commit()
def test_truncate_recipients(db, raw_message_with_many_recipients): account = db.session.query(Account).get(ACCOUNT_ID) assert account.namespace.id == NAMESPACE_ID received_date = datetime.datetime(2014, 9, 22, 17, 25, 46), m = Message.create_from_synced(account, 139219, '[Gmail]/All Mail', received_date, raw_message_with_many_recipients) m.thread_id = 1 db.session.add(m) # Check that no database error is raised. db.session.commit()
def add_fake_msg_with_calendar_part(db_session, account, ics_str): parsed = mime.create.multipart('mixed') parsed.append( mime.create.attachment('text/calendar', ics_str, disposition=None) ) msg = Message.create_from_synced( account, 22, '[Gmail]/All Mail', datetime.utcnow(), parsed.to_string()) msg.thread = add_fake_thread(db_session, account.namespace.id) assert msg.has_attached_events return msg
def test_message_from_synced(db, raw_message): account = db.session.query(Account).get(ACCOUNT_ID) assert account.namespace.id == NAMESPACE_ID received_date = datetime.datetime(2014, 9, 22, 17, 25, 46), m = Message.create_from_synced(account, 139219, '[Gmail]/All Mail', received_date, raw_message) assert m.namespace_id == NAMESPACE_ID assert sorted(m.to_addr) == [(u'', u'*****@*****.**'), (u'', u'*****@*****.**'), (u'', u'*****@*****.**')] assert len(m.parts) == 4 assert 'Attached Message Part' in [part.block.filename for part in m.parts] assert m.received_date == received_date assert all(part.block.namespace_id == m.namespace_id for part in m.parts)
def test_handle_bad_content_disposition( default_account, default_namespace, raw_message_with_bad_content_disposition): received_date = datetime.datetime(2014, 9, 22, 17, 25, 46) m = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, raw_message_with_bad_content_disposition) assert m.namespace_id == default_namespace.id assert sorted(m.to_addr) == [(u'', u'*****@*****.**'), (u'', u'*****@*****.**'), (u'', u'*****@*****.**')] assert len(m.parts) == 3 assert m.received_date == received_date assert all(part.block.namespace_id == m.namespace_id for part in m.parts)
def test_message_from_synced(db, raw_message): account = db.session.query(Account).get(ACCOUNT_ID) assert account.namespace.id == NAMESPACE_ID received_date = datetime.datetime(2014, 9, 22, 17, 25, 46), m = Message.create_from_synced(account, 139219, '[Gmail]/All Mail', received_date, raw_message) assert m.namespace_id == NAMESPACE_ID assert sorted(m.to_addr) == [(u'', u'*****@*****.**'), (u'', u'*****@*****.**'), (u'', u'*****@*****.**')] assert len(m.parts) == 4 assert 'Attached Message Part' in [part.block.filename for part in m.parts] assert m.received_date == received_date assert all(part.block.namespace_id == m.namespace_id for part in m.parts)
def test_handle_bad_content_disposition( default_account, default_namespace, raw_message_with_bad_content_disposition): received_date = datetime.datetime(2014, 9, 22, 17, 25, 46) m = Message.create_from_synced(default_account, 139219, '[Gmail]/All Mail', received_date, raw_message_with_bad_content_disposition) assert m.namespace_id == default_namespace.id assert sorted(m.to_addr) == [(u'', u'*****@*****.**'), (u'', u'*****@*****.**'), (u'', u'*****@*****.**')] assert len(m.parts) == 3 assert m.received_date == received_date assert all(part.block.namespace_id == m.namespace_id for part in m.parts)
def add_fake_msg_with_calendar_part(db_session, account, ics_str, thread=None): from inbox.models import Message parsed = mime.create.multipart("mixed") parsed.append(mime.create.attachment("text/calendar", ics_str, disposition=None)) msg = Message.create_from_synced(account, 22, "[Gmail]/All Mail", datetime.utcnow(), parsed.to_string()) msg.from_addr = [("Ben Bitdiddle", "*****@*****.**")] if thread is None: msg.thread = add_fake_thread(db_session, account.namespace.id) else: msg.thread = thread assert msg.has_attached_events return msg
def add_fake_msg_with_calendar_part(db_session, account, ics_str, thread=None): from inbox.models import Message parsed = mime.create.multipart('mixed') parsed.append( mime.create.attachment('text/calendar', ics_str, disposition=None)) msg = Message.create_from_synced(account, 22, '[Gmail]/All Mail', datetime.utcnow(), parsed.to_string()) msg.from_addr = [('Ben Bitdiddle', '*****@*****.**')] if thread is None: msg.thread = add_fake_thread(db_session, account.namespace.id) else: msg.thread = thread assert msg.has_attached_events return msg
def create_imap_message(db_session, account, folder, msg): """ IMAP-specific message creation logic. Returns ------- imapuid : inbox.models.backends.imap.ImapUid New db object, which links to new Message and Block objects through relationships. All new objects are uncommitted. """ log.debug("creating message", account_id=account.id, folder_name=folder.name, mid=msg.uid) new_message = Message.create_from_synced( account=account, mid=msg.uid, folder_name=folder.name, received_date=msg.internaldate, body_string=msg.body, ) # Check to see if this is a copy of a message that was first created # by the Nylas API. If so, don't create a new object; just use the old one. existing_copy = reconcile_message(new_message, db_session) if existing_copy is not None: new_message = existing_copy imapuid = ImapUid(account=account, folder=folder, msg_uid=msg.uid, message=new_message) imapuid.update_flags(msg.flags) if msg.g_labels is not None: imapuid.update_labels(msg.g_labels) # Update the message's metadata with db_session.no_autoflush: is_draft = imapuid.is_draft and (folder.canonical_name == "drafts" or folder.canonical_name == "all") update_message_metadata(db_session, account, new_message, is_draft) update_contacts_from_message(db_session, new_message, account.namespace.id) return imapuid
def create_draft_from_mime(account, raw_mime, db_session): our_uid = generate_public_id() # base-36 encoded string new_headers = ('X-INBOX-ID: {0}-0\r\n' 'Message-Id: <{0}[email protected]>\r\n' 'User-Agent: NylasMailer/{1}\r\n').format(our_uid, VERSION) new_body = new_headers + raw_mime with db_session.no_autoflush: msg = Message.create_from_synced(account, '', '', datetime.utcnow(), new_body) if msg.from_addr and len(msg.from_addr) > 1: raise InputError("from_addr field can have at most one item") if msg.reply_to and len(msg.reply_to) > 1: raise InputError("reply_to field can have at most one item") if msg.subject is not None and not \ isinstance(msg.subject, basestring): raise InputError('"subject" should be a string') if not isinstance(msg.body, basestring): raise InputError('"body" should be a string') if msg.references or msg.in_reply_to: msg.is_reply = True thread_cls = account.thread_cls msg.thread = thread_cls( subject=msg.subject, recentdate=msg.received_date, namespace=account.namespace, subjectdate=msg.received_date) if msg.attachments: attachment_tag = account.namespace.tags['attachment'] msg.thread.apply_tag(attachment_tag) msg.is_created = True msg.is_sent = True msg.is_draft = False msg.is_read = True db_session.add(msg) db_session.flush() return msg
def create_draft_from_mime(account, raw_mime, db_session): our_uid = generate_public_id() # base-36 encoded string new_headers = ('X-INBOX-ID: {0}-0\r\n' 'Message-Id: <{0}[email protected]>\r\n' 'User-Agent: NylasMailer/{1}\r\n').format(our_uid, VERSION) new_body = new_headers + raw_mime with db_session.no_autoflush: msg = Message.create_from_synced(account, '', '', datetime.utcnow(), new_body) if msg.from_addr and len(msg.from_addr) > 1: raise InputError("from_addr field can have at most one item") if msg.reply_to and len(msg.reply_to) > 1: raise InputError("reply_to field can have at most one item") if msg.subject is not None and not \ isinstance(msg.subject, basestring): raise InputError('"subject" should be a string') if not isinstance(msg.body, basestring): raise InputError('"body" should be a string') if msg.references or msg.in_reply_to: msg.is_reply = True thread_cls = account.thread_cls msg.thread = thread_cls(subject=msg.subject, recentdate=msg.received_date, namespace=account.namespace, subjectdate=msg.received_date) if msg.attachments: attachment_tag = account.namespace.tags['attachment'] msg.thread.apply_tag(attachment_tag) msg.is_created = True msg.is_sent = True msg.is_draft = False msg.is_read = True db_session.add(msg) db_session.flush() return msg
def test_store_full_body_on_parse_error(default_account, default_namespace, raw_message_with_bad_date): received_date = None m = Message.create_from_synced( default_account, 139219, "[Gmail]/All Mail", received_date, raw_message_with_bad_date ) assert m.full_body
def create_from_synced(account, raw_message): received_date = datetime.datetime.utcnow() return Message.create_from_synced(account, 22, '[Gmail]/All Mail', received_date, raw_message)
def create_from_synced(account, raw_message): received_date = datetime.datetime.utcnow() return Message.create_from_synced(account, 22, '[Gmail]/All Mail', received_date, raw_message)