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_imap_flags(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. """ msg_class = SpoolMessage if msg.created else Message new_msg = msg_class.create(account=account, mid=msg.uid, folder_name=folder.name, received_date=msg.internaldate, flags=msg.flags, body_string=msg.body) if new_msg: imapuid = ImapUid(account=account, folder=folder, msg_uid=msg.uid, message=new_msg) imapuid.update_imap_flags(msg.flags) new_msg.is_draft = imapuid.is_draft new_msg.is_read = imapuid.is_seen # NOTE: This might be a good place to add FolderItem entries for # non-Gmail backends. return imapuid
def add_new_imapuid(db_session, log, gmessage, folder_name, acc): """ Add ImapUid object for this GMessage if we don't already have one. Parameters ---------- message : GMessage Message to add ImapUid for. folder_name : str Which folder to add the ImapUid in. acc : GmailAccount Which account to associate the message with. (Not looking this up within this function is a db access optimization.) """ if not db_session.query(ImapUid.msg_uid).join(Folder).filter( Folder.name == folder_name, ImapUid.msg_uid == gmessage.uid).all(): message = db_session.query(Message).filter_by( g_msgid=gmessage.g_metadata.msgid).one() new_imapuid = ImapUid( account=acc, folder=Folder.find_or_create(db_session, acc, folder_name), msg_uid=gmessage.uid, message=message) new_imapuid.update_imap_flags(gmessage.flags, gmessage.labels) db_session.add(new_imapuid) db_session.commit() else: log.debug('skipping imapuid creation', uid=gmessage.uid)
def add_new_imapuid(db_session, gmessage, folder_name, acc): """ Add ImapUid object for this GMessage if we don't already have one. Parameters ---------- message : GMessage Message to add ImapUid for. folder_name : str Which folder to add the ImapUid in. acc : GmailAccount Which account to associate the message with. (Not looking this up within this function is a db access optimization.) """ if not db_session.query(ImapUid.msg_uid).join(Folder).filter( Folder.name == folder_name, ImapUid.account_id == acc.id, ImapUid.msg_uid == gmessage.uid).all(): try: message = db_session.query(Message).join(ImapThread).filter( ImapThread.g_thrid == gmessage.g_metadata.thrid, Message.g_thrid == gmessage.g_metadata.thrid, Message.g_msgid == gmessage.g_metadata.msgid, ImapThread.namespace_id == acc.namespace.id).one() except NoResultFound: # this may occur when a thread is expanded and those messages are # downloaded and committed, then new messages on that thread arrive # and get added to the download queue before this code is run log.debug('no Message object found, skipping imapuid creation', uid=gmessage.uid, g_msgid=gmessage.g_metadata.msgid) return new_imapuid = ImapUid(account=acc, folder=Folder.find_or_create( db_session, acc, folder_name), msg_uid=gmessage.uid, message=message) new_imapuid.update_imap_flags(gmessage.flags, gmessage.labels) new_imapuid.g_labels = [label for label in gmessage.labels] db_session.add(new_imapuid) db_session.commit() else: log.debug('skipping imapuid creation', uid=gmessage.uid, g_msgid=gmessage.g_metadata.msgid)
def add_new_imapuid(db_session, gmessage, folder_name, acc): """ Add ImapUid object for this GMessage if we don't already have one. Parameters ---------- message : GMessage Message to add ImapUid for. folder_name : str Which folder to add the ImapUid in. acc : GmailAccount Which account to associate the message with. (Not looking this up within this function is a db access optimization.) """ if not db_session.query(ImapUid.msg_uid).join(Folder).filter( Folder.name == folder_name, ImapUid.account_id == acc.id, ImapUid.msg_uid == gmessage.uid).all(): try: message = db_session.query(Message).join(ImapThread).filter( ImapThread.g_thrid == gmessage.g_metadata.thrid, Message.g_thrid == gmessage.g_metadata.thrid, Message.g_msgid == gmessage.g_metadata.msgid, ImapThread.namespace_id == acc.namespace.id).one() except NoResultFound: # this may occur when a thread is expanded and those messages are # downloaded and committed, then new messages on that thread arrive # and get added to the download queue before this code is run log.debug('no Message object found, skipping imapuid creation', uid=gmessage.uid, g_msgid=gmessage.g_metadata.msgid) return new_imapuid = ImapUid( account=acc, folder=Folder.find_or_create(db_session, acc, folder_name), msg_uid=gmessage.uid, message=message) new_imapuid.update_imap_flags(gmessage.flags, gmessage.labels) new_imapuid.g_labels = [label for label in gmessage.labels] db_session.add(new_imapuid) db_session.commit() else: log.debug('skipping imapuid creation', uid=gmessage.uid, g_msgid=gmessage.g_metadata.msgid)
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(account=account, mid=msg.uid, folder_name=folder.name, received_date=msg.internaldate, flags=msg.flags, body_string=msg.body) imapuid = ImapUid(account=account, folder=folder, msg_uid=msg.uid, message=new_msg) imapuid.update_imap_flags(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.id) # NOTE: This might be a good place to add FolderItem entries for # non-Gmail backends. return imapuid