Efficient way of syncing Gmail Inbox messages usin

2019-06-12 12:10发布

问题:

A web application sends an email on behalf of a UserA to UserB, using the new Gmail API (Users.messages: send).

The synchronous response contains threadId, messageId which are stored in the database.

We then query the history API for any changes in user's inbox (Users.history: list).

Is there an efficient way to get all the updates since last sync (new replies, read/unread changes)?

One implementation that we tried was to filter the history API results through a custom label. Unfortunately, we noticed that once a thread/message is tagged with a specific label any subsequent responses are not labeled automatically and new replies are not included in the history API response.

A second approach was to query threads using gmail advanced search for a particular label and date (e.g. after:2014/08/29 label:MY_LABEL). The problem was that gmail does not return threads that were created before 2014/08/29 but had a reply on that date.

Any scalable suggestions would be greatly appreciated.

回答1:

Not sure I understand here, users.history.list was made exactly for this. Given a previous historyId, you can then call history.list(previousHistoryid), iterate through the results to find all the message Ids that have been updated since the previous historyId. Then call messages.get() on all of those--for any messages you already knew about you can just call format=MINIMAL (to see label updates), and for new messages you can use a different format to get the message content if you need it.