def add_gmail_attrs(db_session, log, new_uid, flags, folder_name, g_thrid, g_msgid, g_labels, created): """ Gmail-specific post-create-message bits.""" new_uid.message.g_msgid = g_msgid # NOTE: g_thrid == g_msgid on the first message in the thread :) new_uid.message.g_thrid = g_thrid new_uid.update_imap_flags(flags, g_labels) # NOTE: This code _requires_ autoflush=True, otherwise duplicate # threads may attempt to be created and crash. thread = new_uid.message.thread = ImapThread.from_gmail_message( db_session, new_uid.imapaccount.namespace, new_uid.message) # make sure this thread has all the correct labels existing_labels = {l.folder_name.lower() for l in thread.folders} # convert things like \Inbox -> Inbox, \Important -> Important # also, gmail labels are case-insensitive new_labels = {l.lstrip('\\').lower() for l in g_labels} | \ {folder_name.lower()} # remove labels that have been deleted -- note that the \Sent label is # per-message, not per-thread, but since we always work at the thread # level, _we_ apply the label to the whole thread. same goes for # \Important. thread.folders = [l for l in thread.folders if l.folder_name in new_labels or l.folder_name in ('sent', 'important')] # canonicalize "All Mail" to be "archive" in Inbox's data representation if new_uid.imapaccount.archive_folder_name: archive_folder_name = new_uid.imapaccount.archive_folder_name.lower() if archive_folder_name in new_labels: new_labels.remove(archive_folder_name) new_labels.add('archive') # add new labels for label in new_labels: if label not in existing_labels: item = FolderItem(thread=thread, folder_name=label) db_session.add(item) # Reconciliation for Sent Mail folder: if 'sent' in new_labels and not created and new_uid.message.inbox_uid: reconcile_message(db_session, log, new_uid.message.inbox_uid, new_uid.message) return new_uid
def reconcile_gmail_message(db_session, log, inbox_uid, new_msg, thread_id, g_thrid): spool_message = reconcile_message(db_session, log, inbox_uid, new_msg, thread_id) if spool_message: spool_message.g_thrid = g_thrid