Beispiel #1
0
    def _save_attachment(
        self,
        data,
        content_disposition,
        content_type,
        filename,
        content_id,
        namespace_id,
        mid,
    ):
        from inbox.models import Block, Part

        block = Block()
        block.namespace_id = namespace_id
        block.filename = _trim_filename(filename, namespace_id=namespace_id)
        block.content_type = content_type
        part = Part(block=block, message=self)
        if content_id:
            content_id = content_id[:255]
        part.content_id = content_id
        part.content_disposition = content_disposition
        data = data or ""
        if isinstance(data, unicode):
            data = data.encode("utf-8", "strict")
        block.data = data
Beispiel #2
0
def block_to_part(block, message, namespace):
    inline_image_uri = r'cid:{}'.format(block.public_id)
    is_inline = re.search(inline_image_uri, message.body) is not None
    # Create a new Part object to associate to the message object.
    # (You can't just set block.message, because if block is an
    # attachment on an existing message, that would dissociate it from
    # the existing message.)
    part = Part(block=block)
    part.content_id = block.public_id if is_inline else None
    part.namespace_id = namespace.id
    part.content_disposition = 'inline' if is_inline else 'attachment'
    part.is_inboxapp_attachment = True
    return part
 def _save_attachment(self, mimepart, content_disposition, content_type,
                      filename, content_id, namespace_id, mid):
     from inbox.models import Part, Block
     block = Block()
     block.namespace_id = namespace_id
     block.filename = _trim_filename(filename, mid=mid)
     block.content_type = content_type
     part = Part(block=block, message=self)
     part.content_id = content_id
     part.content_disposition = content_disposition
     data = mimepart.body or ''
     if isinstance(data, unicode):
         data = data.encode('utf-8', 'strict')
     block.data = data
Beispiel #4
0
 def _save_attachment(self, mimepart, content_disposition, content_type,
                      filename, content_id, namespace_id, mid):
     from inbox.models import Part, Block
     block = Block()
     block.namespace_id = namespace_id
     block.filename = _trim_filename(filename, mid=mid)
     block.content_type = content_type
     part = Part(block=block, message=self)
     part.content_id = content_id
     part.content_disposition = content_disposition
     data = mimepart.body or ''
     if isinstance(data, unicode):
         data = data.encode('utf-8', 'strict')
     block.data = data
Beispiel #5
0
def fake_attachment(db, default_account, message):
    block = Block()
    namespace_id = default_account.namespace.id
    block.namespace_id = namespace_id
    block.filename = 'zambla.txt'
    block.content_type = 'text/plain'
    block.size = 32
    # This is sha256 of an attachment in our test email.
    block.data_sha256 = '27dc8e801f962098fd4a741ccbd6ca24d42805df0b8035cfb881ad6e5a1bf4b2'
    p = Part(block=block, message=message)
    db.session.add(p)
    db.session.commit()
    return p
Beispiel #6
0
def block_to_part(block, message, namespace):
    inline_image_uri = r"cid:{}".format(block.public_id)
    is_inline = re.search(inline_image_uri, message.body) is not None
    # Create a new Part object to associate to the message object.
    # (You can't just set block.message, because if block is an
    # attachment on an existing message, that would dissociate it from
    # the existing message.)
    part = Part(block=block)
    part.content_id = block.public_id if is_inline else None
    part.namespace_id = namespace.id
    part.content_disposition = "inline" if is_inline else "attachment"
    part.is_inboxapp_attachment = True
    return part
Beispiel #7
0
def create_and_save_draft(db_session,
                          account,
                          to_addr=None,
                          subject=None,
                          body=None,
                          blocks=None,
                          cc_addr=None,
                          bcc_addr=None,
                          new_tags=None,
                          thread=None,
                          is_reply=False,
                          syncback=True):
    """Create a draft object and commit it to the database."""
    with db_session.no_autoflush:
        dt = datetime.utcnow()
        uid = generate_public_id()
        version = generate_public_id()
        to_addr = to_addr or []
        cc_addr = cc_addr or []
        bcc_addr = bcc_addr or []
        blocks = blocks or []
        body = body or ''
        if subject is None and thread is not None:
            # Set subject from thread by default.
            subject = thread.subject
        subject = subject or ''

        message = Message()
        message.namespace = account.namespace
        message.is_created = True
        message.is_draft = True
        message.state = 'draft'
        message.from_addr = [(account.name, account.email_address)]
        # TODO(emfree): we should maybe make received_date nullable, so its
        # value doesn't change in the case of a drafted-and-later-reconciled
        # message.
        message.received_date = dt
        message.subject = subject
        message.sanitized_body = body
        message.to_addr = to_addr
        message.cc_addr = cc_addr
        message.bcc_addr = bcc_addr
        # TODO(emfree): this is different from the normal 'size' value of a
        # message, which is the size of the entire MIME message.
        message.size = len(body)
        message.is_read = True
        message.is_sent = False
        message.is_reply = is_reply
        message.public_id = uid
        message.version = version
        message.inbox_uid = version

        # Set the snippet
        message.snippet = message.calculate_html_snippet(body)

        # Associate attachments to the draft message
        for block in blocks:
            # Create a new Part object to associate to the message object.
            # (You can't just set block.message, because if block is an
            # attachment on an existing message, that would dissociate it from
            # the existing message.)
            part = Part(block=block)
            part.namespace_id = account.namespace.id
            part.content_disposition = 'attachment'
            part.is_inboxapp_attachment = True
            message.parts.append(part)
            db_session.add(part)

        update_contacts_from_message(db_session, message, account.namespace)

        if is_reply:
            message.is_reply = True
            # Construct the in-reply-to and references headers from the last
            # message currently in the thread.
            _set_reply_headers(message, thread)
        if thread is None:
            # Create a new thread object for the draft.
            # We specialize the thread class so that we can, for example, add
            # the g_thrid for Gmail later if we reconcile a synced message with
            # this one. This is a huge hack, but works.
            thread_cls = account.thread_cls
            thread = thread_cls(subject=message.subject,
                                recentdate=message.received_date,
                                namespace=account.namespace,
                                subjectdate=message.received_date)
            db_session.add(thread)

        message.thread = thread
        thread.apply_tag(account.namespace.tags['drafts'])

        if new_tags:
            tags_to_keep = {tag for tag in thread.tags if not tag.user_created}
            thread.tags = new_tags | tags_to_keep

        if syncback:
            schedule_action('save_draft', message, message.namespace.id,
                            db_session)

    db_session.add(message)
    db_session.commit()
    return message
Beispiel #8
0
def create_and_save_draft(
    db_session,
    account,
    to_addr=None,
    subject=None,
    body=None,
    blocks=None,
    cc_addr=None,
    bcc_addr=None,
    new_tags=None,
    thread=None,
    is_reply=False,
    parent_draft=None,
):
    """
    Create a draft object and commit it to the database.
    """
    dt = datetime.utcnow()
    uid = generate_public_id()
    to_addr = to_addr or []
    cc_addr = cc_addr or []
    bcc_addr = bcc_addr or []
    blocks = blocks or []
    body = body or ""
    if subject is None and thread is not None:
        # Set subject from thread by default.
        subject = thread.subject
    subject = subject or ""
    message = SpoolMessage()
    message.from_addr = [(account.sender_name, account.email_address)]
    message.created_date = dt
    # TODO(emfree): we should maybe make received_date nullable, so its value
    # doesn't change in the case of a drafted-and-later-reconciled message.
    message.received_date = dt
    message.is_sent = False
    message.state = "draft"
    if parent_draft is not None:
        message.parent_draft_id = parent_draft.id
    message.subject = subject
    message.sanitized_body = body
    message.to_addr = to_addr
    message.cc_addr = cc_addr
    message.bcc_addr = bcc_addr
    # TODO(emfree): this is different from the normal 'size' value of a
    # message, which is the size of the entire MIME message.
    message.size = len(body)
    message.is_draft = True
    message.is_read = True
    message.inbox_uid = uid
    message.public_id = uid

    # Set the snippet
    message.calculate_html_snippet(body)

    # Associate attachments to the draft message
    for block in blocks:
        # Create a new Part object to associate to the message object.
        # (You can't just set block.message, because if block is an attachment
        # on an existing message, that would dissociate it from the existing
        # message.)
        part = Part()
        part.namespace_id = account.namespace.id
        part.content_disposition = "attachment"
        part.content_type = block.content_type
        part.is_inboxapp_attachment = True
        part.data = block.data
        message.parts.append(part)
        db_session.add(part)

    # TODO(emfree) Update contact data here.

    if is_reply:
        message.is_reply = True
        # If we're updating a draft, copy the in-reply-to and references
        # headers from the parent. Otherwise, construct them from the last
        # message currently in the thread.
        if parent_draft is not None:
            message.in_reply_to = parent_draft.in_reply_to
            message.references = parent_draft.references
        else:
            # Make sure that the headers are constructed from an actual
            # previous message on the thread, not another draft
            non_draft_messages = [m for m in thread.messages if not m.is_draft]
            if non_draft_messages:
                last_message = non_draft_messages[-1]
                message.in_reply_to = last_message.message_id_header
                message.references = last_message.references + "\t" + last_message.message_id_header
    if thread is None:
        # Create a new thread object for the draft.
        thread = Thread(
            subject=message.subject,
            recentdate=message.received_date,
            namespace=account.namespace,
            subjectdate=message.received_date,
        )
        db_session.add(thread)

    message.thread = thread
    # This triggers an autoflush, so we need to execute it after setting
    # message.thread
    thread.apply_tag(account.namespace.tags["drafts"])

    if new_tags:
        tags_to_keep = {tag for tag in thread.tags if not tag.user_created}
        thread.tags = new_tags | tags_to_keep

    schedule_action("save_draft", message, message.namespace.id, db_session)

    db_session.add(message)
    db_session.commit()
    return message
Beispiel #9
0
def update_draft(db_session, account, original_draft, to=None, subject=None,
                 body=None, blocks=None, cc=None, bcc=None, tags=None):
    """
    Update draft.

    To maintain our messages are immutable invariant, we create a new draft
    message object.


    Returns
    -------
    Message
        The new draft message object.

    Notes
    -----
    Messages, including draft messages, are immutable in Inbox.
    So to update a draft, we create a new draft message object and
    return its public_id (which is different than the original's).

    """

    def update(attr, value=None):
        if value is not None:
            setattr(original_draft, attr, value)

            if attr == 'sanitized_body':
                # Update size, snippet too
                original_draft.size = len(value)
                original_draft.calculate_html_snippet(value)

    update('to_addr', _parse_recipients(to) if to else None)
    update('cc_addr', _parse_recipients(cc) if cc else None)
    update('bcc_addr', _parse_recipients(bcc) if bcc else None)
    update('subject', subject if subject else None)
    update('sanitized_body', body if body else None)
    update('received_date', datetime.utcnow())

    # Parts, tags require special handling
    for block in blocks:
        part = Part()
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.content_type = block.content_type
        part.is_inboxapp_attachment = True
        part.data = block.data
        part.filename = block.filename
        original_draft.parts.append(part)

        db_session.add(part)

    thread = original_draft.thread
    if tags:
        tags_to_keep = {tag for tag in thread.tags if not tag.user_created}
        thread.tags = tags | tags_to_keep

    # Delete previous version on remote
    schedule_action('delete_draft', original_draft,
                    original_draft.namespace.id, db_session,
                    inbox_uid=original_draft.inbox_uid)

    # Update version  + inbox_uid, sync to remote
    version = generate_public_id()
    update('version', version)
    update('inbox_uid', version)

    schedule_action('save_draft', original_draft, original_draft.namespace.id,
                    db_session)

    db_session.commit()

    return original_draft
Beispiel #10
0
def create_and_save_draft(db_session, account, to_addr=None, subject=None,
                          body=None, blocks=None, cc_addr=None, bcc_addr=None,
                          new_tags=None, thread=None, is_reply=False,
                          syncback=True):
    """Create a draft object and commit it to the database."""
    dt = datetime.utcnow()
    uid = generate_public_id()
    version = generate_public_id()
    to_addr = to_addr or []
    cc_addr = cc_addr or []
    bcc_addr = bcc_addr or []
    blocks = blocks or []
    body = body or ''
    if subject is None and thread is not None:
        # Set subject from thread by default.
        subject = thread.subject
    subject = subject or ''

    # Sets is_draft = True, state = 'draft'
    message = Message.create_draft_message()

    message.from_addr = [(account.sender_name, account.email_address)]
    # TODO(emfree): we should maybe make received_date nullable, so its value
    # doesn't change in the case of a drafted-and-later-reconciled message.
    message.received_date = dt
    message.subject = subject
    message.sanitized_body = body
    message.to_addr = to_addr
    message.cc_addr = cc_addr
    message.bcc_addr = bcc_addr
    # TODO(emfree): this is different from the normal 'size' value of a
    # message, which is the size of the entire MIME message.
    message.size = len(body)
    message.is_read = True
    message.is_sent = False
    message.is_reply = is_reply
    message.public_id = uid
    message.version = version
    message.inbox_uid = version

    # Set the snippet
    message.calculate_html_snippet(body)

    # Associate attachments to the draft message
    for block in blocks:
        # Create a new Part object to associate to the message object.
        # (You can't just set block.message, because if block is an attachment
        # on an existing message, that would dissociate it from the existing
        # message.)
        part = Part()
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.content_type = block.content_type
        part.is_inboxapp_attachment = True
        part.data = block.data
        part.filename = block.filename
        message.parts.append(part)
        db_session.add(part)

    # TODO(emfree) Update contact data here.

    if is_reply:
        message.is_reply = True
        # Construct the in-reply-to and references headers from the last
        # message currently in the thread.
        _set_reply_headers(message, thread)
    if thread is None:
        # Create a new thread object for the draft.
        thread = Thread(
            subject=message.subject,
            recentdate=message.received_date,
            namespace=account.namespace,
            subjectdate=message.received_date)
        db_session.add(thread)

    message.thread = thread
    # This triggers an autoflush, so we need to execute it after setting
    # message.thread
    thread.apply_tag(account.namespace.tags['drafts'])

    if new_tags:
        tags_to_keep = {tag for tag in thread.tags if not tag.user_created}
        thread.tags = new_tags | tags_to_keep

    if syncback:
        schedule_action('save_draft', message, message.namespace.id,
                        db_session)

    db_session.add(message)
    db_session.commit()
    return message
Beispiel #11
0
def create_draft(data, namespace, db_session, syncback):
    """ Construct a draft object (a Message instance) from `data`, a dictionary
    representing the POST body of an API request. All new objects are added to
    the session, but not committed."""

    # Validate the input and get referenced objects (thread, attachments)
    # as necessary.
    to_addr = get_recipients(data.get('to'), 'to')
    cc_addr = get_recipients(data.get('cc'), 'cc')
    bcc_addr = get_recipients(data.get('bcc'), 'bcc')
    from_addr = get_recipients(data.get('from'), 'from')
    reply_to = get_recipients(data.get('reply_to'), 'reply_to')

    if from_addr and len(from_addr) > 1:
        raise InputError("from_addr field can have at most one item")
    if reply_to and len(reply_to) > 1:
        raise InputError("reply_to field can have at most one item")

    subject = data.get('subject')
    if subject is not None and not isinstance(subject, basestring):
        raise InputError('"subject" should be a string')
    body = data.get('body', '')
    if not isinstance(body, basestring):
        raise InputError('"body" should be a string')
    blocks = get_attachments(data.get('file_ids'), namespace.id, db_session)
    reply_to_thread = get_thread(data.get('thread_id'), namespace.id,
                                 db_session)
    reply_to_message = get_message(data.get('reply_to_message_id'),
                                   namespace.id, db_session)
    if reply_to_message is not None and reply_to_thread is not None:
        if reply_to_message not in reply_to_thread.messages:
            raise InputError('Message {} is not in thread {}'.
                             format(reply_to_message.public_id,
                                    reply_to_thread.public_id))

    with db_session.no_autoflush:
        account = namespace.account
        dt = datetime.utcnow()
        uid = generate_public_id()
        to_addr = to_addr or []
        cc_addr = cc_addr or []
        bcc_addr = bcc_addr or []
        blocks = blocks or []
        if subject is None:
            # If this is a reply with no explicitly specified subject, set the
            # subject from the prior message/thread by default.
            # TODO(emfree): Do we want to allow changing the subject on a reply
            # at all?
            if reply_to_message is not None:
                subject = reply_to_message.subject
            elif reply_to_thread is not None:
                subject = reply_to_thread.subject
        subject = subject or ''

        message = Message()
        message.namespace = namespace
        message.is_created = True
        message.is_draft = True
        message.from_addr = from_addr if from_addr else \
            [(account.name, account.email_address)]
        # TODO(emfree): we should maybe make received_date nullable, so its
        # value doesn't change in the case of a drafted-and-later-reconciled
        # message.
        message.received_date = dt
        message.subject = subject
        message.body = body
        message.to_addr = to_addr
        message.cc_addr = cc_addr
        message.bcc_addr = bcc_addr
        message.reply_to = reply_to
        # TODO(emfree): this is different from the normal 'size' value of a
        # message, which is the size of the entire MIME message.
        message.size = len(body)
        message.is_read = True
        message.is_sent = False
        message.public_id = uid
        message.version = 0
        message.regenerate_inbox_uid()

        # Set the snippet
        message.snippet = message.calculate_html_snippet(body)

        # Associate attachments to the draft message
        for block in blocks:
            # Create a new Part object to associate to the message object.
            # (You can't just set block.message, because if block is an
            # attachment on an existing message, that would dissociate it from
            # the existing message.)
            part = Part(block=block)
            part.namespace_id = namespace.id
            part.content_disposition = 'attachment'
            part.is_inboxapp_attachment = True
            message.parts.append(part)

        update_contacts_from_message(db_session, message, namespace)

        if reply_to_message is not None:
            message.is_reply = True
            _set_reply_headers(message, reply_to_message)
            thread = reply_to_message.thread
            message.reply_to_message = reply_to_message
        elif reply_to_thread is not None:
            message.is_reply = True
            thread = reply_to_thread
            # Construct the in-reply-to and references headers from the last
            # message currently in the thread.
            previous_messages = [m for m in thread.messages if not m.is_draft]
            if previous_messages:
                last_message = previous_messages[-1]
                message.reply_to_message = last_message
                _set_reply_headers(message, last_message)
        else:
            # If this isn't a reply to anything, create a new thread object for
            # the draft.  We specialize the thread class so that we can, for
            # example, add the g_thrid for Gmail later if we reconcile a synced
            # message with this one. This is a huge hack, but works.
            message.is_reply = False
            thread_cls = account.thread_cls
            thread = thread_cls(
                subject=message.subject,
                recentdate=message.received_date,
                namespace=namespace,
                subjectdate=message.received_date)

        message.thread = thread

    db_session.add(message)
    if syncback:
        schedule_action('save_draft', message, namespace.id, db_session,
                        version=message.version)
    db_session.flush()
    return message
Beispiel #12
0
def update_draft(db_session, account, draft, to_addr=None,
                 subject=None, body=None, blocks=None, cc_addr=None,
                 bcc_addr=None, from_addr=None, reply_to=None):
    """
    Update draft with new attributes.
    """

    def update(attr, value=None):
        if value is not None:
            setattr(draft, attr, value)

            if attr == 'body':
                # Update size, snippet too
                draft.size = len(value)
                draft.snippet = draft.calculate_html_snippet(
                    value)

    update('to_addr', to_addr)
    update('cc_addr', cc_addr)
    update('bcc_addr', bcc_addr)
    update('reply_to', reply_to)
    update('from_addr', from_addr)
    update('subject', subject if subject else None)
    update('body', body if body else None)
    update('received_date', datetime.utcnow())

    # Remove any attachments that aren't specified
    new_block_ids = [b.id for b in blocks]
    for part in filter(lambda x: x.block_id not in new_block_ids,
                       draft.parts):
        draft.parts.remove(part)
        db_session.delete(part)

    # Parts require special handling
    for block in blocks:
        # Don't re-add attachments that are already attached
        if block.id in [p.block_id for p in draft.parts]:
            continue
        part = Part(block=block)
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.is_inboxapp_attachment = True
        draft.parts.append(part)

    thread = draft.thread
    if len(thread.messages) == 1:
        # If there are no prior messages on the thread, update its subject and
        # dates to match the draft.
        thread.subject = draft.subject
        thread.subjectdate = draft.received_date
        thread.recentdate = draft.received_date

    # Remove previous message-contact associations, and create new ones.
    draft.contacts = []
    update_contacts_from_message(db_session, draft, account.namespace)

    prior_inbox_uid = draft.inbox_uid
    prior_message_id_header = draft.message_id_header

    # Update version  + inbox_uid (is_created is already set)
    draft.version += 1
    draft.regenerate_inbox_uid()

    # Sync to remote
    schedule_action('save_draft', draft, draft.namespace.id, db_session,
                    version=draft.version)
    # Delete previous version on remote
    schedule_action('delete_draft', draft,
                    draft.namespace.id, db_session,
                    inbox_uid=prior_inbox_uid,
                    message_id_header=prior_message_id_header)

    db_session.commit()
    return draft
Beispiel #13
0
def update_draft(db_session, account, original_draft, to_addr=None,
                 subject=None, body=None, blocks=None, cc_addr=None,
                 bcc_addr=None, tags=None):
    """
    Update draft.

    To maintain our messages are immutable invariant, we create a new draft
    message object.


    Returns
    -------
    Message
        The new draft message object.

    Notes
    -----
    Messages, including draft messages, are immutable in Inbox.
    So to update a draft, we create a new draft message object and
    return its public_id (which is different than the original's).

    """

    def update(attr, value=None):
        if value is not None:
            setattr(original_draft, attr, value)

            if attr == 'sanitized_body':
                # Update size, snippet too
                original_draft.size = len(value)
                original_draft.snippet = original_draft.calculate_html_snippet(
                    value)

    update('to_addr', to_addr)
    update('cc_addr', cc_addr)
    update('bcc_addr', bcc_addr)
    update('subject', subject if subject else None)
    update('sanitized_body', body if body else None)
    update('received_date', datetime.utcnow())

    # Remove any attachments that aren't specified
    new_block_ids = [b.id for b in blocks]
    for part in filter(lambda x: x.block_id not in new_block_ids,
                       original_draft.parts):
        original_draft.parts.remove(part)
        db_session.delete(part)

    # Parts, tags require special handling
    for block in blocks:
        # Don't re-add attachments that are already attached
        if block.id in [p.block_id for p in original_draft.parts]:
            continue
        part = Part(block=block)
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.is_inboxapp_attachment = True
        original_draft.parts.append(part)

        db_session.add(part)

    thread = original_draft.thread
    if tags:
        tags_to_keep = {tag for tag in thread.tags if not tag.user_created}
        thread.tags = tags | tags_to_keep

    # Remove previous message-contact associations, and create new ones.
    original_draft.contacts = []
    update_contacts_from_message(db_session, original_draft, account.namespace)

    # Delete previous version on remote
    schedule_action('delete_draft', original_draft,
                    original_draft.namespace.id, db_session,
                    inbox_uid=original_draft.inbox_uid,
                    message_id_header=original_draft.message_id_header)

    # Update version  + inbox_uid (is_created is already set)
    version = generate_public_id()
    update('version', version)
    update('inbox_uid', version)

    # Sync to remote
    schedule_action('save_draft', original_draft, original_draft.namespace.id,
                    db_session)

    db_session.commit()

    return original_draft
Beispiel #14
0
def update_draft(db_session, account, draft, to_addr=None,
                 subject=None, body=None, blocks=None, cc_addr=None,
                 bcc_addr=None, from_addr=None, reply_to=None):
    """
    Update draft with new attributes.
    """

    def update(attr, value=None):
        if value is not None:
            setattr(draft, attr, value)

            if attr == 'body':
                # Update size, snippet too
                draft.size = len(value)
                draft.snippet = draft.calculate_html_snippet(
                    value)

    update('to_addr', to_addr)
    update('cc_addr', cc_addr)
    update('bcc_addr', bcc_addr)
    update('reply_to', reply_to)
    update('from_addr', from_addr)
    update('subject', subject if subject else None)
    update('body', body if body else None)
    update('received_date', datetime.utcnow())

    # Remove any attachments that aren't specified
    new_block_ids = [b.id for b in blocks]
    for part in filter(lambda x: x.block_id not in new_block_ids,
                       draft.parts):
        draft.parts.remove(part)
        db_session.delete(part)

    # Parts require special handling
    for block in blocks:
        # Don't re-add attachments that are already attached
        if block.id in [p.block_id for p in draft.parts]:
            continue
        part = Part(block=block)
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.is_inboxapp_attachment = True
        draft.parts.append(part)

    thread = draft.thread
    if len(thread.messages) == 1:
        # If there are no prior messages on the thread, update its subject and
        # dates to match the draft.
        thread.subject = draft.subject
        thread.subjectdate = draft.received_date
        thread.recentdate = draft.received_date

    # Remove previous message-contact associations, and create new ones.
    draft.contacts = []
    update_contacts_from_message(db_session, draft, account.namespace)

    # The draft we're updating may or may not be one authored through the API:
    # - Ours: is_created = True, Message-Id = public_id+version
    # - Not Ours: is_created = False, Message-Id = ???

    # Mark that the draft is now created by us
    draft.is_created = True

    # Save the current Message-Id so we know which draft to delete in syncback
    old_message_id_header = draft.message_id_header

    # Increment version and rebuild the message ID header.
    draft.version += 1
    draft.regenerate_nylas_uid()

    # Sync to remote
    schedule_action('update_draft', draft, draft.namespace.id, db_session,
                    version=draft.version,
                    old_message_id_header=old_message_id_header)
    db_session.commit()
    return draft
Beispiel #15
0
def update_draft(db_session,
                 account,
                 draft,
                 to_addr=None,
                 subject=None,
                 body=None,
                 blocks=None,
                 cc_addr=None,
                 bcc_addr=None,
                 from_addr=None,
                 reply_to=None):
    """
    Update draft with new attributes.
    """
    def update(attr, value=None):
        if value is not None:
            setattr(draft, attr, value)

            if attr == 'body':
                # Update size, snippet too
                draft.size = len(value)
                draft.snippet = draft.calculate_html_snippet(value)

    update('to_addr', to_addr)
    update('cc_addr', cc_addr)
    update('bcc_addr', bcc_addr)
    update('reply_to', reply_to)
    update('from_addr', from_addr)
    update('subject', subject if subject else None)
    update('body', body if body else None)
    update('received_date', datetime.utcnow())

    # Remove any attachments that aren't specified
    new_block_ids = [b.id for b in blocks]
    for part in filter(lambda x: x.block_id not in new_block_ids, draft.parts):
        draft.parts.remove(part)
        db_session.delete(part)

    # Parts require special handling
    for block in blocks:
        # Don't re-add attachments that are already attached
        if block.id in [p.block_id for p in draft.parts]:
            continue
        part = Part(block=block)
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.is_inboxapp_attachment = True
        draft.parts.append(part)

    thread = draft.thread
    if len(thread.messages) == 1:
        # If there are no prior messages on the thread, update its subject and
        # dates to match the draft.
        thread.subject = draft.subject
        thread.subjectdate = draft.received_date
        thread.recentdate = draft.received_date

    # Remove previous message-contact associations, and create new ones.
    draft.contacts = []
    update_contacts_from_message(db_session, draft, account.namespace)

    # The draft we're updating may or may not be one authored through the API:
    # - Ours: is_created = True, Message-Id = public_id+version
    # - Not Ours: is_created = False, Message-Id = ???

    # Mark that the draft is now created by us
    draft.is_created = True

    # Save the current Message-Id so we know which draft to delete in syncback
    old_message_id_header = draft.message_id_header

    # Increment version and rebuild the message ID header.
    draft.version += 1
    draft.regenerate_nylas_uid()

    # Sync to remote
    schedule_action('update_draft',
                    draft,
                    draft.namespace.id,
                    db_session,
                    version=draft.version,
                    old_message_id_header=old_message_id_header)
    db_session.commit()
    return draft
Beispiel #16
0
def update_draft(db_session,
                 account,
                 draft,
                 to_addr=None,
                 subject=None,
                 body=None,
                 blocks=None,
                 cc_addr=None,
                 bcc_addr=None,
                 from_addr=None,
                 reply_to=None):
    """
    Update draft with new attributes.
    """
    def update(attr, value=None):
        if value is not None:
            setattr(draft, attr, value)

            if attr == 'body':
                # Update size, snippet too
                draft.size = len(value)
                draft.snippet = draft.calculate_html_snippet(value)

    update('to_addr', to_addr)
    update('cc_addr', cc_addr)
    update('bcc_addr', bcc_addr)
    update('reply_to', reply_to)
    update('from_addr', from_addr)
    update('subject', subject if subject else None)
    update('body', body if body else None)
    update('received_date', datetime.utcnow())

    # Remove any attachments that aren't specified
    new_block_ids = [b.id for b in blocks]
    for part in filter(lambda x: x.block_id not in new_block_ids, draft.parts):
        draft.parts.remove(part)
        db_session.delete(part)

    # Parts require special handling
    for block in blocks:
        # Don't re-add attachments that are already attached
        if block.id in [p.block_id for p in draft.parts]:
            continue
        part = Part(block=block)
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.is_inboxapp_attachment = True
        draft.parts.append(part)

    thread = draft.thread
    if len(thread.messages) == 1:
        # If there are no prior messages on the thread, update its subject and
        # dates to match the draft.
        thread.subject = draft.subject
        thread.subjectdate = draft.received_date
        thread.recentdate = draft.received_date

    # Remove previous message-contact associations, and create new ones.
    draft.contacts = []
    update_contacts_from_message(db_session, draft, account.namespace)

    # Update version  + inbox_uid (is_created is already set)
    draft.version += 1
    draft.regenerate_inbox_uid()

    # Sync to remote
    schedule_action('update_draft',
                    draft,
                    draft.namespace.id,
                    db_session,
                    version=draft.version)
    db_session.commit()
    return draft
Beispiel #17
0
def upgrade():
    from inbox.models.session import session_scope, Session
    from inbox.ignition import main_engine
    engine = main_engine(pool_size=1, max_overflow=0)

    from inbox.models import (Part, Namespace, Message, Thread)
    from inbox.sqlalchemy_ext.util import JSON

    print 'Creating table for parts...'
    op.create_table(
        'part', sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('message_id', sa.Integer(), nullable=True),
        sa.Column('walk_index', sa.Integer(), nullable=True),
        sa.Column('content_disposition',
                  sa.Enum('inline', 'attachment'),
                  nullable=True),
        sa.Column('content_id', sa.String(length=255), nullable=True),
        sa.Column('misc_keyval', JSON(), nullable=True),
        sa.Column('is_inboxapp_attachment',
                  sa.Boolean(),
                  server_default=sa.sql.expression.false(),
                  nullable=True),
        sa.ForeignKeyConstraint(['id'], ['block.id'], ondelete='CASCADE'),
        sa.ForeignKeyConstraint(['message_id'], ['message.id'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('message_id', 'walk_index'))

    print 'Reflecting old block table schema'
    Base = declarative_base()
    Base.metadata.reflect(engine)

    class Block_(Base):  # old schema, reflected from database table
        __table__ = Base.metadata.tables['block']

    print 'Adding namespace_id column to blocks ',
    op.add_column(u'block',
                  sa.Column('namespace_id', sa.Integer(), nullable=False))

    print 'Migrating from blocks to parts'
    new_parts = []
    with session_scope() as db_session:
        for block in db_session.query(Block_).yield_per(chunk_size):

            # Move relevant fields
            p = Part()
            p.size = block.size
            p.data_sha256 = block.data_sha256
            p.message_id = block.message_id
            p.walk_index = block.walk_index
            p.content_disposition = block.content_disposition
            p.content_id = block.content_id
            p.misc_keyval = block.misc_keyval
            p.is_inboxapp_attachment

            old_namespace = db_session.query(Namespace) \
                .join(Message.thread, Thread.namespace) \
                .filter(Message.id == block.message_id).one()
            p.namespace_id = old_namespace.id

            # Commit after column modifications
            new_parts.append(p)

        print 'Deleting old blocks (now parts)... ',
        db_session.query(Block_).delete()
        db_session.commit()
        print 'Done!'

    print 'Removing `message_id` constraint from block'
    op.drop_constraint('block_ibfk_1', 'block', type_='foreignkey')

    print 'Creating foreign key for block -> namespace on block'
    op.create_foreign_key('block_ibfk_1',
                          'block',
                          'namespace', ['namespace_id'], ['id'],
                          ondelete='CASCADE')

    print 'Dropping old block columns which are now in part'
    op.drop_column(u'block', u'walk_index')
    op.drop_column(u'block', u'content_disposition')
    op.drop_column(u'block', u'misc_keyval')
    op.drop_column(u'block', u'content_id')
    op.drop_column(u'block', u'is_inboxapp_attachment')
    op.drop_constraint(u'message_id', 'block', type_='unique')
    op.drop_column(u'block', u'message_id')

    # Note: here we use the regular database session, since the transaction
    # log requires the `namespace` property on objects. We've set the
    # `namespace_id` foreign key, but need to commit the object before the
    # SQLalchemy reference is valid
    no_tx_session = Session(autoflush=True, autocommit=False)
    no_tx_session.add_all(new_parts)
    no_tx_session.commit()

    print 'Done migration blocks to parts!'
Beispiel #18
0
def upgrade():
    from inbox.models.session import session_scope, Session
    from inbox.ignition import engine

    from inbox.models import Part, Namespace, Message, Thread
    from inbox.sqlalchemy_ext.util import JSON

    print "Creating table for parts..."
    op.create_table(
        "part",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("message_id", sa.Integer(), nullable=True),
        sa.Column("walk_index", sa.Integer(), nullable=True),
        sa.Column("content_disposition", sa.Enum("inline", "attachment"), nullable=True),
        sa.Column("content_id", sa.String(length=255), nullable=True),
        sa.Column("misc_keyval", JSON(), nullable=True),
        sa.Column("is_inboxapp_attachment", sa.Boolean(), server_default=sa.sql.expression.false(), nullable=True),
        sa.ForeignKeyConstraint(["id"], ["block.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["message_id"], ["message.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("message_id", "walk_index"),
    )

    print "Reflecting old block table schema"
    Base = declarative_base()
    Base.metadata.reflect(engine)

    class Block_(Base):  # old schema, reflected from database table
        __table__ = Base.metadata.tables["block"]

    print "Adding namespace_id column to blocks ",
    op.add_column(u"block", sa.Column("namespace_id", sa.Integer(), nullable=False))

    print "Migrating from blocks to parts"
    new_parts = []
    with session_scope() as db_session:
        for block in db_session.query(Block_).yield_per(chunk_size):

            # Move relevant fields
            p = Part()
            p.size = block.size
            p.data_sha256 = block.data_sha256
            p.message_id = block.message_id
            p.walk_index = block.walk_index
            p.content_disposition = block.content_disposition
            p.content_id = block.content_id
            p.misc_keyval = block.misc_keyval
            p.is_inboxapp_attachment

            old_namespace = (
                db_session.query(Namespace)
                .join(Message.thread, Thread.namespace)
                .filter(Message.id == block.message_id)
                .one()
            )
            p.namespace_id = old_namespace.id

            # Commit after column modifications
            new_parts.append(p)

        print "Deleting old blocks (now parts)... ",
        db_session.query(Block_).delete()
        db_session.commit()
        print "Done!"

    print "Removing `message_id` constraint from block"
    op.drop_constraint("block_ibfk_1", "block", type_="foreignkey")

    print "Creating foreign key for block -> namespace on block"
    op.create_foreign_key("block_ibfk_1", "block", "namespace", ["namespace_id"], ["id"], ondelete="CASCADE")

    print "Dropping old block columns which are now in part"
    op.drop_column(u"block", u"walk_index")
    op.drop_column(u"block", u"content_disposition")
    op.drop_column(u"block", u"misc_keyval")
    op.drop_column(u"block", u"content_id")
    op.drop_column(u"block", u"is_inboxapp_attachment")
    op.drop_constraint(u"message_id", "block", type_="unique")
    op.drop_column(u"block", u"message_id")

    # Note: here we use the regular database session, since the transaction
    # log requires the `namespace` property on objects. We've set the
    # `namespace_id` foreign key, but need to commit the object before the
    # SQLalchemy reference is valid
    no_tx_session = Session(autoflush=True, autocommit=False)
    no_tx_session.add_all(new_parts)
    no_tx_session.commit()

    print "Done migration blocks to parts!"
Beispiel #19
0
def upgrade():
    from inbox.ignition import main_engine
    from inbox.models.session import Session, session_scope

    engine = main_engine(pool_size=1, max_overflow=0)

    from inbox.models import Message, Namespace, Part, Thread
    from inbox.sqlalchemy_ext.util import JSON

    print("Creating table for parts...")
    op.create_table(
        "part",
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("message_id", sa.Integer(), nullable=True),
        sa.Column("walk_index", sa.Integer(), nullable=True),
        sa.Column("content_disposition",
                  sa.Enum("inline", "attachment"),
                  nullable=True),
        sa.Column("content_id", sa.String(length=255), nullable=True),
        sa.Column("misc_keyval", JSON(), nullable=True),
        sa.Column(
            "is_inboxapp_attachment",
            sa.Boolean(),
            server_default=sa.sql.expression.false(),
            nullable=True,
        ),
        sa.ForeignKeyConstraint(["id"], ["block.id"], ondelete="CASCADE"),
        sa.ForeignKeyConstraint(["message_id"], ["message.id"],
                                ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("message_id", "walk_index"),
    )

    print("Reflecting old block table schema")
    Base = declarative_base()
    Base.metadata.reflect(engine)

    class Block_(Base):  # old schema, reflected from database table
        __table__ = Base.metadata.tables["block"]

    print("Adding namespace_id column to blocks ", end=" ")
    op.add_column(u"block",
                  sa.Column("namespace_id", sa.Integer(), nullable=False))

    print("Migrating from blocks to parts")
    new_parts = []
    with session_scope() as db_session:
        for block in db_session.query(Block_).yield_per(chunk_size):

            # Move relevant fields
            p = Part()
            p.size = block.size
            p.data_sha256 = block.data_sha256
            p.message_id = block.message_id
            p.walk_index = block.walk_index
            p.content_disposition = block.content_disposition
            p.content_id = block.content_id
            p.misc_keyval = block.misc_keyval
            p.is_inboxapp_attachment

            old_namespace = (db_session.query(Namespace).join(
                Message.thread,
                Thread.namespace).filter(Message.id == block.message_id).one())
            p.namespace_id = old_namespace.id

            # Commit after column modifications
            new_parts.append(p)

        print("Deleting old blocks (now parts)... ", end=" ")
        db_session.query(Block_).delete()
        db_session.commit()
        print("Done!")

    print("Removing `message_id` constraint from block")
    op.drop_constraint("block_ibfk_1", "block", type_="foreignkey")

    print("Creating foreign key for block -> namespace on block")
    op.create_foreign_key(
        "block_ibfk_1",
        "block",
        "namespace",
        ["namespace_id"],
        ["id"],
        ondelete="CASCADE",
    )

    print("Dropping old block columns which are now in part")
    op.drop_column(u"block", u"walk_index")
    op.drop_column(u"block", u"content_disposition")
    op.drop_column(u"block", u"misc_keyval")
    op.drop_column(u"block", u"content_id")
    op.drop_column(u"block", u"is_inboxapp_attachment")
    op.drop_constraint(u"message_id", "block", type_="unique")
    op.drop_column(u"block", u"message_id")

    # Note: here we use the regular database session, since the transaction
    # log requires the `namespace` property on objects. We've set the
    # `namespace_id` foreign key, but need to commit the object before the
    # SQLalchemy reference is valid
    no_tx_session = Session(autoflush=True, autocommit=False)
    no_tx_session.add_all(new_parts)
    no_tx_session.commit()

    print("Done migration blocks to parts!")
Beispiel #20
0
def create_and_save_draft(db_session,
                          account,
                          to_addr=None,
                          subject=None,
                          body=None,
                          blocks=None,
                          cc_addr=None,
                          bcc_addr=None,
                          new_tags=None,
                          thread=None,
                          is_reply=False,
                          parent_draft=None,
                          syncback=True):
    """
    Create a draft object and commit it to the database.
    """
    dt = datetime.utcnow()
    uid = generate_public_id()
    to_addr = to_addr or []
    cc_addr = cc_addr or []
    bcc_addr = bcc_addr or []
    blocks = blocks or []
    body = body or ''
    if subject is None and thread is not None:
        # Set subject from thread by default.
        subject = thread.subject
    subject = subject or ''

    # Sets is_draft = True, state = 'draft'
    message = Message.create_draft_message()

    message.from_addr = [(account.sender_name, account.email_address)]
    # TODO(emfree): we should maybe make received_date nullable, so its value
    # doesn't change in the case of a drafted-and-later-reconciled message.
    message.received_date = dt
    if parent_draft is not None:
        message.parent_draft_id = parent_draft.id
    message.subject = subject
    message.sanitized_body = body
    message.to_addr = to_addr
    message.cc_addr = cc_addr
    message.bcc_addr = bcc_addr
    # TODO(emfree): this is different from the normal 'size' value of a
    # message, which is the size of the entire MIME message.
    message.size = len(body)
    message.is_read = True
    message.is_sent = False
    message.is_reply = is_reply
    message.inbox_uid = uid
    message.public_id = uid

    # Set the snippet
    message.calculate_html_snippet(body)

    # Associate attachments to the draft message
    for block in blocks:
        # Create a new Part object to associate to the message object.
        # (You can't just set block.message, because if block is an attachment
        # on an existing message, that would dissociate it from the existing
        # message.)
        part = Part()
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.content_type = block.content_type
        part.is_inboxapp_attachment = True
        part.data = block.data
        part.filename = block.filename
        message.parts.append(part)
        db_session.add(part)

    # TODO(emfree) Update contact data here.

    if is_reply:
        message.is_reply = True
        # If we're updating a draft, copy the in-reply-to and references
        # headers from the parent. Otherwise, construct them from the last
        # message currently in the thread.
        if parent_draft is not None:
            message.in_reply_to = parent_draft.in_reply_to
            message.references = parent_draft.references
        else:
            _set_reply_headers(message, thread)
    if thread is None:
        # Create a new thread object for the draft.
        thread = Thread(subject=message.subject,
                        recentdate=message.received_date,
                        namespace=account.namespace,
                        subjectdate=message.received_date)
        db_session.add(thread)

    message.thread = thread
    # This triggers an autoflush, so we need to execute it after setting
    # message.thread
    thread.apply_tag(account.namespace.tags['drafts'])

    if new_tags:
        tags_to_keep = {tag for tag in thread.tags if not tag.user_created}
        thread.tags = new_tags | tags_to_keep

    if syncback:
        schedule_action('save_draft', message, message.namespace.id,
                        db_session)

    db_session.add(message)
    db_session.commit()
    return message
Beispiel #21
0
def create_message_from_json(data, namespace, db_session, is_draft):
    """ Construct a Message instance from `data`, a dictionary representing the
    POST body of an API request. All new objects are added to the session, but
    not committed."""

    # Validate the input and get referenced objects (thread, attachments)
    # as necessary.
    to_addr = get_recipients(data.get('to'), 'to')
    cc_addr = get_recipients(data.get('cc'), 'cc')
    bcc_addr = get_recipients(data.get('bcc'), 'bcc')
    from_addr = get_recipients(data.get('from'), 'from')
    reply_to = get_recipients(data.get('reply_to'), 'reply_to')

    if from_addr and len(from_addr) > 1:
        raise InputError("from_addr field can have at most one item")
    if reply_to and len(reply_to) > 1:
        raise InputError("reply_to field can have at most one item")

    subject = data.get('subject')
    if subject is not None and not isinstance(subject, basestring):
        raise InputError('"subject" should be a string')
    body = data.get('body', '')
    if not isinstance(body, basestring):
        raise InputError('"body" should be a string')
    blocks = get_attachments(data.get('file_ids'), namespace.id, db_session)
    reply_to_thread = get_thread(data.get('thread_id'), namespace.id,
                                 db_session)
    reply_to_message = get_message(data.get('reply_to_message_id'),
                                   namespace.id, db_session)
    if reply_to_message is not None and reply_to_thread is not None:
        if reply_to_message not in reply_to_thread.messages:
            raise InputError('Message {} is not in thread {}'.format(
                reply_to_message.public_id, reply_to_thread.public_id))

    with db_session.no_autoflush:
        account = namespace.account
        dt = datetime.utcnow()
        uid = generate_public_id()
        to_addr = to_addr or []
        cc_addr = cc_addr or []
        bcc_addr = bcc_addr or []
        blocks = blocks or []
        if subject is None:
            # If this is a reply with no explicitly specified subject, set the
            # subject from the prior message/thread by default.
            # TODO(emfree): Do we want to allow changing the subject on a reply
            # at all?
            if reply_to_message is not None:
                subject = reply_to_message.subject
            elif reply_to_thread is not None:
                subject = reply_to_thread.subject
        subject = subject or ''

        message = Message()
        message.namespace = namespace
        message.is_created = True
        message.is_draft = is_draft
        message.from_addr = from_addr if from_addr else \
            [(account.name, account.email_address)]
        # TODO(emfree): we should maybe make received_date nullable, so its
        # value doesn't change in the case of a drafted-and-later-reconciled
        # message.
        message.received_date = dt
        message.subject = subject
        message.body = body
        message.to_addr = to_addr
        message.cc_addr = cc_addr
        message.bcc_addr = bcc_addr
        message.reply_to = reply_to
        # TODO(emfree): this is different from the normal 'size' value of a
        # message, which is the size of the entire MIME message.
        message.size = len(body)
        message.is_read = True
        message.is_sent = False
        message.public_id = uid
        message.version = 0
        message.regenerate_inbox_uid()

        # Set the snippet
        message.snippet = message.calculate_html_snippet(body)

        # Associate attachments to the draft message
        for block in blocks:
            # Create a new Part object to associate to the message object.
            # (You can't just set block.message, because if block is an
            # attachment on an existing message, that would dissociate it from
            # the existing message.)
            part = Part(block=block)
            part.namespace_id = namespace.id
            part.content_disposition = 'attachment'
            part.is_inboxapp_attachment = True
            message.parts.append(part)

        update_contacts_from_message(db_session, message, namespace)

        if reply_to_message is not None:
            message.is_reply = True
            _set_reply_headers(message, reply_to_message)
            thread = reply_to_message.thread
            message.reply_to_message = reply_to_message
        elif reply_to_thread is not None:
            message.is_reply = True
            thread = reply_to_thread
            # Construct the in-reply-to and references headers from the last
            # message currently in the thread.
            previous_messages = [m for m in thread.messages if not m.is_draft]
            if previous_messages:
                last_message = previous_messages[-1]
                message.reply_to_message = last_message
                _set_reply_headers(message, last_message)
        else:
            # If this isn't a reply to anything, create a new thread object for
            # the draft.  We specialize the thread class so that we can, for
            # example, add the g_thrid for Gmail later if we reconcile a synced
            # message with this one. This is a huge hack, but works.
            message.is_reply = False
            thread_cls = account.thread_cls
            thread = thread_cls(subject=message.subject,
                                recentdate=message.received_date,
                                namespace=namespace,
                                subjectdate=message.received_date)

        message.thread = thread

    db_session.add(message)
    if is_draft:
        schedule_action('save_draft',
                        message,
                        namespace.id,
                        db_session,
                        version=message.version)
    db_session.flush()
    return message
Beispiel #22
0
def update_draft(db_session,
                 account,
                 original_draft,
                 to_addr=None,
                 subject=None,
                 body=None,
                 blocks=None,
                 cc_addr=None,
                 bcc_addr=None,
                 tags=None):
    """
    Update draft.

    To maintain our messages are immutable invariant, we create a new draft
    message object.


    Returns
    -------
    Message
        The new draft message object.

    Notes
    -----
    Messages, including draft messages, are immutable in Inbox.
    So to update a draft, we create a new draft message object and
    return its public_id (which is different than the original's).

    """
    def update(attr, value=None):
        if value is not None:
            setattr(original_draft, attr, value)

            if attr == 'sanitized_body':
                # Update size, snippet too
                original_draft.size = len(value)
                original_draft.snippet = original_draft.calculate_html_snippet(
                    value)

    update('to_addr', to_addr)
    update('cc_addr', cc_addr)
    update('bcc_addr', bcc_addr)
    update('subject', subject if subject else None)
    update('sanitized_body', body if body else None)
    update('received_date', datetime.utcnow())

    # Remove any attachments that aren't specified
    new_block_ids = [b.id for b in blocks]
    for part in filter(lambda x: x.block_id not in new_block_ids,
                       original_draft.parts):
        original_draft.parts.remove(part)
        db_session.delete(part)

    # Parts, tags require special handling
    for block in blocks:
        # Don't re-add attachments that are already attached
        if block.id in [p.block_id for p in original_draft.parts]:
            continue
        part = Part(block=block)
        part.namespace_id = account.namespace.id
        part.content_disposition = 'attachment'
        part.is_inboxapp_attachment = True
        original_draft.parts.append(part)

        db_session.add(part)

    thread = original_draft.thread
    if tags:
        tags_to_keep = {tag for tag in thread.tags if not tag.user_created}
        thread.tags = tags | tags_to_keep

    # Remove previous message-contact associations, and create new ones.
    original_draft.contacts = []
    update_contacts_from_message(db_session, original_draft, account.namespace)

    # Delete previous version on remote
    schedule_action('delete_draft',
                    original_draft,
                    original_draft.namespace.id,
                    db_session,
                    inbox_uid=original_draft.inbox_uid,
                    message_id_header=original_draft.message_id_header)

    # Update version  + inbox_uid (is_created is already set)
    version = generate_public_id()
    update('version', version)
    update('inbox_uid', version)

    # Sync to remote
    schedule_action('save_draft', original_draft, original_draft.namespace.id,
                    db_session)

    db_session.commit()

    return original_draft