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

5015 make gmail filters work for partial sync #5695

Merged
merged 15 commits into from
May 31, 2024

Conversation

bosiraphael
Copy link
Contributor

Closes #5015

@bosiraphael bosiraphael linked an issue May 31, 2024 that may be closed by this pull request
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

  • Simplified regex pattern in is-person-email.util.ts for identifying non-personal emails.
  • Added attachment handling in fetch-messages-by-batches.service.ts.
  • Introduced gmailSearchFilter in gmail-full-message-list-fetch-v2.service.ts for refined message retrieval.
  • Updated Gmail filter usage in gmail-full-message-list-fetch.service.ts for partial sync.
  • Added blocklist filtering in gmail-messages-import-v2.service.ts.

export const isPersonEmail = (email: string | undefined): boolean => {
if (!email) return false;

export const isPersonEmail = (email: string): boolean => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the check for undefined email input may lead to runtime errors if the function is called with an undefined value.

@@ -186,6 +186,8 @@ export class FetchMessagesByBatchesService {
const bodyData = this.getBodyData(message);
const text = bodyData ? Buffer.from(bodyData, 'base64').toString() : '';

const attachments = this.getAttachmentData(message);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure that getAttachmentData handles cases where message.payload or message.payload.parts might be undefined.

Comment on lines +219 to +229
private getAttachmentData(message: gmail_v1.Schema$Message) {
return (
message.payload?.parts
?.filter((part) => part.filename && part.body?.attachmentId)
.map((part) => ({
filename: part.filename || '',
id: part.body?.attachmentId || '',
mimeType: part.mimeType || '',
size: part.body?.size || 0,
})) || []
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a check to handle cases where message.payload or message.payload.parts might be undefined to avoid potential runtime errors.

Comment on lines 36 to 39
.map((history) => history.messagesAdded)
.flat()
.map((message) => message?.message?.id)
.filter((id) => id) as string[];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Combine the map and filter operations to improve readability and performance.

);

this.logger.log(
`Added ${messagesAdded.length} messages to import for workspace ${workspaceId} and account ${connectedAccount.id}`,
`Added ${messagesAddedFiltered.length} messages to import for workspace ${workspaceId} and account ${connectedAccount.id}`,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update log message to reflect filtering: 'Added ${messagesAddedFiltered.length} of ${messagesAdded.length} messages...'

Comment on lines +4 to +9
export const filterEmails = (messages: GmailMessage[], blocklist: string[]) => {
return filterOutBlocklistedMessages(
filterOutIcsAttachments(filterOutNonPersonalEmails(messages)),
blocklist,
);
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding JSDoc comments to filterEmails to explain its purpose and parameters.

Comment on lines +20 to +23
return message.participants.every(
(participant) => !isEmailBlocklisted(participant.handle, blocklist),
);
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential performance issue: every method will check all participants even if one is blocklisted. Consider short-circuiting.

Comment on lines +32 to +34
return message.attachments.every(
(attachment) => !attachment.filename.endsWith('.ics'),
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential performance issue: every method will check all attachments even if one ends with .ics. Consider short-circuiting.


const isPersonEmail = (email: string): boolean => {
const nonPersonalPattern =
/noreply|no-reply|do_not_reply|no\.reply|^(info@|contact@|hello@|support@|feedback@|service@|help@)/;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider moving the regex pattern to a constant for better readability and reusability.

Comment on lines +51 to +53
return message.participants.every((participant) =>
isPersonEmail(participant.handle),
);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential performance issue: every method will check all participants even if one is non-personal. Consider short-circuiting.

Copy link
Member

@charlesBochet charlesBochet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!
Still want to re-work file organization and interfaces but we will do that once the old jobs are removed

@charlesBochet charlesBochet merged commit e0103bb into main May 31, 2024
4 checks passed
@charlesBochet charlesBochet deleted the 5015-make-gmail-filters-work-for-partial-sync branch May 31, 2024 21:20
arnavsaxena17 pushed a commit to arnavsaxena17/twenty that referenced this pull request Oct 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make gmail filters work for partial sync
2 participants