Closed Bug 1646473 Opened 4 years ago Closed 2 years ago

Add a way to add new messages to Thunderbird

Categories

(Thunderbird :: Add-Ons: Extensions API, enhancement)

enhancement

Tracking

(thunderbird_esr102 wontfix)

RESOLVED FIXED
106 Branch
Tracking Status
thunderbird_esr102 --- wontfix

People

(Reporter: TbSync, Assigned: TbSync)

References

(Blocks 2 open bugs)

Details

Attachments

(3 files, 2 obsolete files)


The original subject of this bug was Enhance the message API with a "messageModify" permission to be able modify MessageHeader (for example the subject)

See Comment 14, for why it was changed and why the accepted patch is doing something else.


I am currently converting the "Edit Email Subject" add-on to TB78:
https://addons.thunderbird.net/de/thunderbird/addon/edit-email-subject/

That is a little but very helpful tool, as it allows to modify the Subject of any received Email. This is useful in cases where the Subject is messed up with lots of "AW: RE: AW: " or where the subject is plain wrong. For convenience, one can simply change the subject with this add-on.

From reading the API documentation and by doing some tests, I believe the MessageHeader object is read-only:
https://thunderbird-webextensions.readthedocs.io/en/latest/messages.html#messageheader

I therefore ask, if a new permission "messageModify" could be added, which in turn allows to modify the Header in the received email. If the account is using an IMAP connection, then the modification should be send to the server as well. This is what the AddOn is doing now.

I assume you have existing (non-WE) code that does this. Can you post it? Or better yet, wrap it as an experiment API and post that? That'd help figure out what the new functions should look like and improve the chances of a quick implementation.

Status: UNCONFIRMED → NEW
Ever confirmed: true

I will convert the addon using an experiment and post that here, when done.

Flags: needinfo?(john.bieling)

After working on the conversion of the addon and understanding the code better, I tried to design a few additional API methods for the messages API. It ist just a draft and I am not finished, but I think it has reached a point so we can talk about.

The main addition is (links to github)

addRaw(rawContent, destination, properties)

to add a new message to a folder. The properties parameter should define initial values of "read" and other attributes, so the new email does not "blink" if it should be added with the read attribute set. For the draft, the third parameter is simply an ID of a reference message, where I copy flags and keywords from. I do not know if that 3rd parameter is needed, or if we can use messages.update() to modify the new message directly after creating it (setting it as read) and if that is so fast, that the UI does not trigger its "new email" notifications at all.

The code works with one minor hack-ish line: I need to sleep for a short period of time after the CopyListener has finished, as there is a short period after adding the message, where

let msgHdr = context.extension.messageManager.get(aID);

does not return a valid object. I played around with folderListeners to resolve addRaw() after the message has been added to the folder and not after OnStopCopy() has finished, but I got two events for my added email and I was not able to get this done properly. Do you know what is causing this?

Furthermore I would like to propose the following methods:

  • selectMessage(id) - select a given message in the UI, maybe also pass in the window, if the user has more than one mail:3pain window open

  • getFolderServerType(MailFolder) - I need the server type (e.g. returning "imap"), this could of course also return a more general read-only info object about the folder properties

  • setSubjectOfMessage(id, subject) - This method does not change the email itself, but just the subject used by Thunderbird to display the message. It could be included in messages.update(messageId, newProperties) if the newProperties obj would support a "subject" entry.

Flags: needinfo?(john.bieling)
Attached file implementation.js (obsolete) —
Attached file schema.json (obsolete) —

I added github links in the comment, as I am able to hotlink to individual methods. But I also added the implementation and schema files directly to this bug.

(In reply to John Bieling (TbSync) from comment #3)

The code works with one minor hack-ish line: I need to sleep for a short period of time after the CopyListener has finished, as there is a short period after adding the message, where

If you change that to Services.tm.dispatchToMainThread(() => { ...remaining code ...}); does that work?

Sometimes you just need to let any other listeners complete and the stack unwind.

  • selectMessage(id) - select a given message in the UI, maybe also pass in the window, if the user has more than one mail:3pain window open

Snap! Conversations has almost exactly the same code. I've just called mine switchToFolderAndMsg.

  • getFolderServerType(MailFolder) - I need the server type (e.g. returning "imap"), this could of course also return a more general read-only info object about the folder properties

I think you can just do (await browser.accounts.get(mailFolder.accountId)).type (with appropriate checks etc).

Thanks for the pointer to the accounts API, that did the trick. Once you see it it is obvious, but I was only looking in the folders API. So the method getFolderServerType(MailFolder) can be dropped from this bug entirely.

Regarding Services.tm.dispatchToMainThread(() I have to check. I will probably ask you via matrix about this.

Attached file implementation.js
Attachment #9161186 - Attachment is obsolete: true
Attached file schema.json
Attachment #9161187 - Attachment is obsolete: true

In the first draft, where addRaw() resolved after OnStopCopy() from the copyListener, only messages in IMAP folders (and probably NEWS folders, but did not check) needed the extra sleep to get around the issue described in the description. Messages in local folders where not affected.

As suggested by Standard8 in matrix chat, I retried to work with a folderListener for IMAP and NEWS. As reported in the description, I already observed that the OnItemAdded() callback function is called TWICE and if I resolve after the first call, the issue remains. I now tried to resolve after the second call and it looks like the issue is gone.

I would still call this a hack. Is there a regression somewhere else?

Depends on D155145

Assignee: nobody → john
Attachment #9290783 - Attachment description: WIP: Bug 1646473 - Add messages.import() and add support for messages.update() to specify a file to replace its content. → Bug 1646473 - Add messages.import(). r=mkmelin
Status: NEW → ASSIGNED

New strings so wontfix for 102.

Blocks: 1787104

I was the creator of this bug and even though the patch is not fully addressing my original request, this is good for me. We currently cannot directly alter a message without a lot of glue code to make it look like we altered it. What we can do is to delete a message and add a new one.

The second request in this bug was to alter the subject stored in the database. It turns out that this feature actually confused my users, because some IMAP servers occasionally re-push messages which will overwrite the database and the changed subject is lost. A subject changed just in the database is also not propagated to other Thunderbird instances or other mail clients, so I will remove that feature from my add-on altogether.

Summary: Enhance the message API with a "messageModify" permission to be able modify MessageHeader (for example the subject) → Add a way to add new messages to Thunderbird
Blocks: 1787107
Target Milestone: --- → 106 Branch

The patch doesn't seem to apply

Rebased and fixed merge conflicts.

Pushed by mkmelin@iki.fi:
https://hg.mozilla.org/comm-central/rev/f4c8260d7edd
Add messages.import(). r=mkmelin

Status: ASSIGNED → RESOLVED
Closed: 2 years ago
Resolution: --- → FIXED

Looks like this is crashing comm/mailnews/compose/test/unit/test_sendBackground.js (I also get that locally, but not with this backed out)

Flags: needinfo?(john)

Ok, thanks, I will have a look. Let's back it out for now.

Flags: needinfo?(john)
Backout by mkmelin@iki.fi:
https://hg.mozilla.org/comm-central/rev/21cb14215be6
Backed out changeset f4c8260d7edd for test failures. rs=backout
Status: RESOLVED → REOPENED
Resolution: FIXED → ---

My try has issues with bct5 on Linux, but locally the entire set of comm/mail/test/browser/composition runs fine.

Pushed by mkmelin@iki.fi:
https://hg.mozilla.org/comm-central/rev/448f4d89f48f
Add messages.import(). r=mkmelin

Status: REOPENED → RESOLVED
Closed: 2 years ago2 years ago
Resolution: --- → FIXED
Regressions: 1852428
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: