def test_update_unread_status(db, thread, message, imapuid):
    message.is_read = True
    imapuid.is_seen = False
    update_unread_status(imapuid)

    assert message.is_read is False, "message shouldn't be read"

    tag_names = [tag.name for tag in thread.tags]
    assert 'unread' in tag_names, "thread should be unread"

    imapuid.is_seen = True
    update_unread_status(imapuid)

    assert message.is_read is True, "message should be read"

    tag_names = [tag.name for tag in thread.tags]
    assert 'unread' not in tag_names, "thread should be read"
def test_update_unread_status(db, thread, message, imapuid):
    message.is_read = True
    imapuid.is_seen = False
    update_unread_status(imapuid)

    assert message.is_read is False, "message shouldn't be read"

    tag_names = [tag.name for tag in thread.tags]
    assert 'unread' in tag_names, "thread should be unread"

    imapuid.is_seen = True
    update_unread_status(imapuid)

    assert message.is_read is True, "message should be read"

    tag_names = [tag.name for tag in thread.tags]
    assert 'unread' not in tag_names, "thread should be read"
예제 #3
0
def add_new_imapuids(crispin_client, remote_g_metadata, syncmanager_lock,
                     uids):
    """
    Add ImapUid entries only for (already-downloaded) messages.

    If a message has already been downloaded via another folder, we only need
    to add `ImapUid` accounting for the current folder. `Message` objects
    etc. have already been created.

    """
    flags = crispin_client.flags(uids)

    with syncmanager_lock:
        with mailsync_session_scope() as db_session:
            # Since we prioritize download for messages in certain threads, we
            # may already have ImapUid entries despite calling this method.
            local_folder_uids = {uid for uid, in
                                 db_session.query(ImapUid.msg_uid).join(Folder)
                                 .filter(
                                     ImapUid.account_id ==
                                     crispin_client.account_id,
                                     Folder.name ==
                                     crispin_client.selected_folder_name,
                                     ImapUid.msg_uid.in_(uids))}
            uids = [uid for uid in uids if uid not in local_folder_uids]

            if uids:
                acc = db_session.query(GmailAccount).get(
                    crispin_client.account_id)

                # collate message objects to relate the new imapuids to
                imapuid_for = dict([(metadata.msgid, uid) for (uid, metadata)
                                    in remote_g_metadata.items()
                                    if uid in uids])
                imapuid_g_msgids = [remote_g_metadata[uid].msgid for uid in
                                    uids]
                message_for = dict([(imapuid_for[m.g_msgid], m) for m in
                                    db_session.query(Message).join(ImapThread)
                                    .filter(
                                        Message.g_msgid.in_(imapuid_g_msgids),
                                        ImapThread.namespace_id ==
                                        acc.namespace.id)])

                # Stop Folder.find_or_create()'s query from triggering a flush.
                with db_session.no_autoflush:
                    new_imapuids = [ImapUid(
                        account=acc,
                        folder=Folder.find_or_create(
                            db_session, acc,
                            crispin_client.selected_folder_name),
                        msg_uid=uid, message=message_for[uid]) for uid in uids
                        if uid in message_for]
                    for item in new_imapuids:
                        # skip uids which have disappeared in the meantime
                        if item.msg_uid in flags:
                            item.update_flags_and_labels(
                                flags[item.msg_uid].flags,
                                flags[item.msg_uid].labels)

                            item.message.is_draft = item.is_draft
                            common.update_unread_status(item)
                db_session.add_all(new_imapuids)
                db_session.flush()

                # Don't forget to update thread labels.
                messages = set(message_for.values())
                unique_threads = set([message.thread for message in messages])

                for thread in unique_threads:
                    common.recompute_thread_labels(thread, db_session)
                db_session.commit()
예제 #4
0
def add_new_imapuids(crispin_client, remote_g_metadata, syncmanager_lock,
                     uids):
    """
    Add ImapUid entries only for (already-downloaded) messages.

    If a message has already been downloaded via another folder, we only need
    to add `ImapUid` accounting for the current folder. `Message` objects
    etc. have already been created.

    """
    flags = crispin_client.flags(uids)

    with syncmanager_lock:
        with mailsync_session_scope() as db_session:
            # Since we prioritize download for messages in certain threads, we
            # may already have ImapUid entries despite calling this method.
            local_folder_uids = {
                uid
                for uid, in db_session.query(ImapUid.msg_uid).join(Folder).
                filter(ImapUid.account_id == crispin_client.account_id,
                       Folder.name == crispin_client.selected_folder_name,
                       ImapUid.msg_uid.in_(uids))
            }
            uids = [uid for uid in uids if uid not in local_folder_uids]

            if uids:
                acc = db_session.query(GmailAccount).get(
                    crispin_client.account_id)

                # collate message objects to relate the new imapuids to
                imapuid_for = dict([
                    (metadata.msgid, uid)
                    for (uid, metadata) in remote_g_metadata.items()
                    if uid in uids
                ])
                imapuid_g_msgids = [
                    remote_g_metadata[uid].msgid for uid in uids
                ]
                message_for = dict([
                    (imapuid_for[m.g_msgid], m)
                    for m in db_session.query(Message).join(ImapThread).filter(
                        Message.g_msgid.in_(imapuid_g_msgids),
                        ImapThread.namespace_id == acc.namespace.id)
                ])

                # Stop Folder.find_or_create()'s query from triggering a flush.
                with db_session.no_autoflush:
                    new_imapuids = [
                        ImapUid(account=acc,
                                folder=Folder.find_or_create(
                                    db_session, acc,
                                    crispin_client.selected_folder_name),
                                msg_uid=uid,
                                message=message_for[uid]) for uid in uids
                        if uid in message_for
                    ]
                    for item in new_imapuids:
                        # skip uids which have disappeared in the meantime
                        if item.msg_uid in flags:
                            item.update_flags_and_labels(
                                flags[item.msg_uid].flags,
                                flags[item.msg_uid].labels)

                            item.message.is_draft = item.is_draft
                            common.update_unread_status(item)
                db_session.add_all(new_imapuids)
                db_session.flush()

                # Don't forget to update thread labels.
                messages = set(message_for.values())
                unique_threads = set([message.thread for message in messages])

                for thread in unique_threads:
                    common.recompute_thread_labels(thread, db_session)
                db_session.commit()