twenty
twenty copied to clipboard
Create a service to do a partial sync with the gmail API using the historyId
Scope & Context
After doing the full sync when creating a connected account, we need to call a cron job regularly to sync all the emails.
Technical inputs
Create a new service in packages/twenty-server/src/workspace/messaging which uses the users.history.list
method from the gmail API to get all the changes since the last historyId and update the database.
- get the
lastSyncHistoryId
from "messageChannel" - call
users.history.list
withstartHistoryId=lastSyncHistoryId
,historyTypes=[messageAdded, messageDeleted]
- we get the following response { "history": [ { object (History) } ], "nextPageToken": string, "historyId": string }
- If there is a nextPageToken, call
users.history.list
withpageToken=nextPageToken
startHistoryId=lastSyncHistoryId
,historyTypes=[messageAdded, messageDeleted]
- Get all the
messagesAdded
andmessagesDeleted
id
andthreadId
- Get threads with
users.threads.get
and messages withusers.messages.get
in batch requests - Parse the results
- Store the added messages in the db
- Delete the deleted messages from the db
- Store the new
historyId
inlastSyncHistoryId
in themessageChannel
- Maybe store a timestamp of the last sync
- To check if we want to import SPAM
- To check what to do with TRASH messages -> If we want them, we need to add historyType for changed labels