Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log email attachments #4108

Open
FelixMalfait opened this issue Feb 21, 2024 · 10 comments
Open

Log email attachments #4108

FelixMalfait opened this issue Feb 21, 2024 · 10 comments
Assignees
Labels
good for experienced contributor scope: backend Issues that are affecting the backend side only

Comments

@FelixMalfait
Copy link
Member

FelixMalfait commented Feb 21, 2024

Context

We've built an integration with gmail which allows workspace members to connect their accounts and import all emails. Those emails are then automatically attached to company/contacts.

The gmail API exposes attachments for every message but we ignore them.

The goal of this issue would be to import attachments in our database to then display them in the Files tab and in email threads.

Backend

The steps on the backend side should roughly look like this:

  1. STORAGE_DRIVER env variable is now used to store the default driver only
  2. Add new StorageDriverType for gmail
  3. Adapt fileService/fileStorageService to stream from Gmail's API instead of s3
  4. Add a new field storageDriver on Attachments
  5. Add new relation to messages on Attachments
  6. Adapt the gmail fetch service to create attachments along with company and people during import

This is just a general guideline, this a complex issue that will require investigation!

Frontend

  1. Because we use the existing Attachment object it should automatically appear in Files tabs (nothing to do?)

  2. We should also add a clipper icon on the threads list

Screenshot 2024-02-23 at 13 56 45
  1. We should show it on thread level (we display it as a field/fieldValue like for the properties of a task for example)
Screenshot 2024-02-23 at 13 58 24
  1. We should show it on message level
Screenshot 2024-02-23 at 13 59 33

Link to Figma

@FelixMalfait FelixMalfait added scope: backend Issues that are affecting the backend side only good for experienced contributor labels Feb 21, 2024
@tatethurston
Copy link
Contributor

@quest-bot embark

Copy link

quest-bot bot commented Mar 8, 2024

⚠️ There's no active Quest for this Issue.

Check the docs for more info.

@tatethurston
Copy link
Contributor

tatethurston commented Mar 8, 2024

@FelixMalfait do you all use any centralized access for shared secrets? Specifically here I needed AUTH_GOOGLE_CLIENT_ID and AUTH_GOOGLE_CLIENT_SECRET. I went through the process of spinning up a personal google integration, but I'm curious about this for future PRs.

@FelixMalfait
Copy link
Member Author

@tatethurston sorry for the delayed reply! We don't really have better process no, every just setup their own. I don't know if it would be possible to do otherwise 🤔

@tatethurston
Copy link
Contributor

No worries. I have limited capacity and don’t think I’ll be able to push this one through for a few weeks. Happy to let someone else take this on.

@varunKT001
Copy link

I would like to work on this issue.

@rostaklein
Copy link
Contributor

rostaklein commented Apr 2, 2024

Hey guys, i just started looking into this one. Please @FelixMalfait assign this issue to me. I was able to set up my own Google credentials and sync a few emails. Is there any preferred way to work with the email sync other than calling a manual command e.g. yarn nx command twenty-server workspace:gmail-partial-sync -w 20202020-1c25-4d02-bf25-6aeccf7ea419? 🤔 Any more tips/tricks on how to get emails visible in the UI and so (any reference PR)? 😊

@FelixMalfait
Copy link
Member Author

@varunKT001 thanks! Since this issue is a very complex one, you might want to start with smaller issues first. @rostaklein has some experience on the code base so I'm assigning it to him, but feel free to take another one!

cc @bosiraphael any tip to give to @rostaklein? thanks

@varunKT001
Copy link

@FelixMalfait sure no issues. I'll pick another issue 👍

@bosiraphael
Copy link
Contributor

bosiraphael commented Apr 3, 2024

Hello @rostaklein, thank you for taking this issue :) Here are some insights:

Currently, we're querying the raw email from gmail API using mailparser to parse the email content and headers.
The raw email contains the whole attachment but does not contain the gmail api attachment id.
Since we do not want to store the attachment but a way to get the attachment from the gmail api, we have two solutions:

Make a second query for files with attachments (Simpler)

  • Query the email in raw format
  • If the message has an attachment, make a second query to get the attachmentId
  • Save attachements after saving the messages and store gmailAttachmentId in the attachment table
  • We will be able to get the attachments using users.messages.attachments.get

This solution is simpler but we're wasting bandwidth because we're getting the whole attachment and doing nothing with it, and we also have to do a second query.

Change the query format to full instead of raw and modify the parsing logic (Better)

  • Change query format to full instead of raw in users.messages.get in fetch-messages-by-batches.service
  • Use adressParser instead of simpleParser to parse address objects (from, to, cc, bcc) and update the way we save messages in formatBatchResponseAsGmailMessage in fetch-messages-by-batches.service
  • Since we're not using mailParser simpleParser anymore, implement a function to parse the different message parts and get the whole message plain text
  • Save attachements information after saving the messages and store gmailAttachmentId in the attachment table
  • We will be able to get the attachments using users.messages.attachments.get

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good for experienced contributor scope: backend Issues that are affecting the backend side only
Projects
Status: 🆕 New
Development

Successfully merging a pull request may close this issue.

5 participants