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
lastSyncHistoryIdfrom "messageChannel" - call
users.history.listwithstartHistoryId=lastSyncHistoryId,historyTypes=[messageAdded, messageDeleted] - we get the following response { "history": [ { object (History) } ], "nextPageToken": string, "historyId": string }
- If there is a nextPageToken, call
users.history.listwithpageToken=nextPageTokenstartHistoryId=lastSyncHistoryId,historyTypes=[messageAdded, messageDeleted] - Get all the
messagesAddedandmessagesDeletedidandthreadId - Get threads with
users.threads.getand messages withusers.messages.getin batch requests - Parse the results
- Store the added messages in the db
- Delete the deleted messages from the db
- Store the new
historyIdinlastSyncHistoryIdin 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