def test_threading(): Message = namedtuple('Message', ['message_id_header', 'references', 'received_date']) messages = [ Message("aaa_header", [], datetime(1999, 1, 23)), Message("bbb_header", ["aaa_header"], datetime(2000, 12, 23)), Message("ccc_header", ["bbb_header"], datetime(2000, 12, 24)) ] assert thread_messages(messages) == messages messages2 = [ Message("aaa_header", [], datetime(1999, 1, 23)), Message("bbb_header", ["aaa_header"], datetime(2000, 12, 23)), Message("ccc_header", ["aaa_header"], datetime(2000, 12, 24)), Message("ddd_header", ["bbb_header"], datetime(2000, 12, 25)) ] assert thread_messages(messages2) == messages2 messages3 = [ Message("aaa_header", [], datetime(1999, 1, 23)), Message("bbb_header", ["aaa_header"], datetime(2000, 12, 23)), Message("ccc_header", [], datetime(2000, 12, 23)), Message("ddd_header", ["bbb_header"], datetime(2000, 12, 23)) ] assert thread_messages(messages3) == messages3
def add_message_attrs(self, db_session, new_uid, msg): """ Post-create-message bits.""" with db_session.no_autoflush: parent_threads = self.fetch_similar_threads(db_session, new_uid) construct_new_thread = True if parent_threads: # If there's a parent thread that isn't too long already, # add to it. Otherwise create a new thread. parent_thread = parent_threads[0] parent_message_count, = db_session.query( func.count(Message.id)). \ filter(Message.thread_id == parent_thread.id).one() if parent_message_count < MAX_THREAD_LENGTH: construct_new_thread = False if construct_new_thread: new_uid.message.thread = ImapThread.from_imap_message( db_session, new_uid.account.namespace, new_uid.message) new_uid.message.thread_order = 0 else: parent_thread = parent_threads[0] parent_thread.messages.append(new_uid.message) constructed_thread = thread_messages(parent_thread.messages) for index, message in enumerate(constructed_thread): message.thread_order = index db_session.flush() # Make sure this thread has all the correct labels common.add_any_new_thread_labels(new_uid.message.thread, new_uid, db_session) new_uid.update_flags_and_labels(msg.flags) return new_uid
def add_message_attrs(self, db_session, new_uid, msg, folder): """ Post-create-message bits.""" with db_session.no_autoflush: parent_threads = self.fetch_similar_threads(db_session, new_uid) construct_new_thread = True if parent_threads: # If there's a parent thread that isn't too long already, # add to it. Otherwise create a new thread. parent_thread = parent_threads[0] parent_message_count, = db_session.query( func.count(Message.id)). \ filter(Message.thread_id == parent_thread.id).one() if parent_message_count < MAX_THREAD_LENGTH: construct_new_thread = False if construct_new_thread: new_uid.message.thread = ImapThread.from_imap_message( db_session, new_uid.account.namespace, new_uid.message) new_uid.message.thread_order = 0 else: parent_thread = parent_threads[0] parent_thread.messages.append(new_uid.message) constructed_thread = thread_messages(parent_thread.messages) for index, message in enumerate(constructed_thread): message.thread_order = index db_session.flush() # Make sure this thread has all the correct labels common.add_any_new_thread_labels(new_uid.message.thread, new_uid, db_session) new_uid.update_imap_flags(msg.flags) return new_uid
def add_message_attrs(self, db_session, new_uid, msg, folder): """ Post-create-message bits.""" with db_session.no_autoflush: clean_subject = cleanup_subject(new_uid.message.subject) parent_threads = db_session.query(ImapThread).filter( ImapThread.subject.like(clean_subject)).all() if parent_threads == []: new_uid.message.thread = ImapThread.from_imap_message( db_session, new_uid.account.namespace, new_uid.message) new_uid.message.thread_order = 0 else: # FIXME: arbitrarily select the first thread. This shouldn't # be a problem now but it could become one if we choose # to implement thread-splitting. parent_thread = parent_threads[0] parent_thread.messages.append(new_uid.message) constructed_thread = thread_messages(parent_thread.messages) for index, message in enumerate(constructed_thread): message.thread_order = index # Make sure this thread has all the correct labels common.update_thread_labels(new_uid.message.thread, folder.name, [folder.canonical_name], db_session) new_uid.update_imap_flags(msg.flags) return new_uid
def test_threading(): Message = namedtuple('Message', ['message_id_header', 'references', 'received_date']) messages = [Message("aaa_header", [], datetime(1999, 1, 23)), Message("bbb_header", ["aaa_header"], datetime(2000, 12, 23)), Message("ccc_header", ["bbb_header"], datetime(2000, 12, 24))] assert thread_messages(messages) == messages messages2 = [Message("aaa_header", [], datetime(1999, 1, 23)), Message("bbb_header", ["aaa_header"], datetime(2000, 12, 23)), Message("ccc_header", ["aaa_header"], datetime(2000, 12, 24)), Message("ddd_header", ["bbb_header"], datetime(2000, 12, 25))] assert thread_messages(messages2) == messages2 messages3 = [Message("aaa_header", [], datetime(1999, 1, 23)), Message("bbb_header", ["aaa_header"], datetime(2000, 12, 23)), Message("ccc_header", [], datetime(2000, 12, 23)), Message("ddd_header", ["bbb_header"], datetime(2000, 12, 23))] assert thread_messages(messages3) == messages3
def add_attrs(db_session, log, new_uid, flags, folder, created): """ Post-create-message bits.""" with db_session.no_autoflush: clean_subject = cleanup_subject(new_uid.message.subject) parent_threads = db_session.query(ImapThread).\ filter(ImapThread.subject.like(clean_subject)).all() if parent_threads == []: new_uid.message.thread = ImapThread.from_imap_message( db_session, new_uid.account.namespace, new_uid.message) new_uid.message.thread_order = 0 else: # FIXME: arbitrarily select the first thread. This shouldn't # be a problem now but it could become one if we choose # to implement thread-splitting. parent_thread = parent_threads[0] parent_thread.messages.append(new_uid.message) constructed_thread = thread_messages(parent_thread.messages) for index, message in enumerate(constructed_thread): message.thread_order = index # FIXME: refactor 'new_labels' name. This is generic IMAP, not # gmail. # make sure this thread has all the correct labels new_labels = account.update_thread_labels(new_uid.message.thread, folder.name, [folder.canonical_name], db_session) # Reconciliation for Drafts, Sent Mail folders: if (('draft' in new_labels or '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) new_uid.update_imap_flags(flags) return new_uid
def add_attrs(db_session, log, new_uid, flags, folder, created): """ Post-create-message bits.""" with db_session.no_autoflush: clean_subject = cleanup_subject(new_uid.message.subject) parent_threads = db_session.query(ImapThread).filter( ImapThread.subject.like(clean_subject)).all() if parent_threads == []: new_uid.message.thread = ImapThread.from_imap_message( db_session, new_uid.account.namespace, new_uid.message) new_uid.message.thread_order = 0 else: # FIXME: arbitrarily select the first thread. This shouldn't # be a problem now but it could become one if we choose # to implement thread-splitting. parent_thread = parent_threads[0] parent_thread.messages.append(new_uid.message) constructed_thread = thread_messages(parent_thread.messages) for index, message in enumerate(constructed_thread): message.thread_order = index # FIXME: refactor 'new_labels' name. This is generic IMAP, not # gmail. # make sure this thread has all the correct labels new_labels = account.update_thread_labels(new_uid.message.thread, folder.name, [folder.canonical_name], db_session) # Reconciliation for Drafts, Sent Mail folders: if (('draft' in new_labels or '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) new_uid.update_imap_flags(flags) return new_uid
def add_message_attrs(self, db_session, new_uid, msg, folder): """ Post-create-message bits.""" with db_session.no_autoflush: parent_threads = self.fetch_similar_threads(db_session, new_uid) construct_new_thread = True if parent_threads: # If there's a parent thread that isn't too long already, # add to it. Otherwise create a new thread. parent_thread = parent_threads[0] parent_message_count, = db_session.query( func.count(Message.id)). \ filter(Message.thread_id == parent_thread.id).one() if parent_message_count < MAX_THREAD_LENGTH: construct_new_thread = False if construct_new_thread: new_uid.message.thread = ImapThread.from_imap_message( db_session, new_uid.account.namespace, new_uid.message) new_uid.message.thread_order = 0 else: # FIXME: arbitrarily select the first thread. This shouldn't # be a problem now but it could become one if we choose # to implement thread-splitting. parent_thread = parent_threads[0] parent_thread.messages.append(new_uid.message) constructed_thread = thread_messages(parent_thread.messages) for index, message in enumerate(constructed_thread): message.thread_order = index # Make sure this thread has all the correct labels common.update_thread_labels(new_uid.message.thread, folder.name, [folder.canonical_name], db_session) new_uid.update_imap_flags(msg.flags) return new_uid