def update_contacts(db_session, account_id, message): """Add new contacts from the given message's to/from/cc fields, and update ranking scores for all contacts in those fields.""" msg_timestamp = calendar.timegm(message.received_date.utctimetuple()) for field in ['to_addr', 'cc_addr', 'bcc_addr', 'from_addr']: addresses = getattr(message, field) contacts = get_contact_objects(db_session, account_id, addresses) for contact in contacts: search_util.increment_signal(contact, SIGNAL_NAME_MAPPING[field]) search_util.update_timestamp_signal(contact, msg_timestamp) update_association(db_session, message, contact, field) search_util.score(contact)
def update_contacts_from_message(account_id, message): """Add new contacts from the given message's to/from/cc fields, and update ranking scores for all contacts in those fields.""" with session_scope() as db_session: from_contacts = get_contact_objects( account_id, message.from_addr, db_session) to_contacts = get_contact_objects( account_id, message.to_addr, db_session) cc_contacts = get_contact_objects( account_id, message.cc_addr, db_session) bcc_contacts = get_contact_objects( account_id, message.bcc_addr, db_session) msg_timestamp = calendar.timegm(message.received_date.utctimetuple()) for contact in to_contacts: search_util.increment_signal(contact, 'to_count') search_util.update_timestamp_signal(contact, msg_timestamp) for contact in cc_contacts: search_util.increment_signal(contact, 'cc_count') search_util.update_timestamp_signal(contact, msg_timestamp) for contact in bcc_contacts: search_util.increment_signal(contact, 'bcc_count') search_util.update_timestamp_signal(contact, msg_timestamp) for contact in from_contacts: search_util.increment_signal(contact, 'from_count') search_util.update_timestamp_signal(contact, msg_timestamp) for contact in to_contacts + cc_contacts + from_contacts: # TODO(emfree): We may be recomputing the score many more times # than needed. If this has performance implications, hoist the # score computation. search_util.score(contact) db_session.commit()