6657 Refactor and fix blocklist (#6803)
Closes #6657 - Fix listeners - Refactor jobs to take array of events - Fix calendar events and messages deletion --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -55,20 +55,20 @@ export class MessagingOngoingStaleJob {
|
||||
`Sync for message channel ${messageChannel.id} and workspace ${workspaceId} is stale. Setting sync stage to MESSAGES_IMPORT_PENDING`,
|
||||
);
|
||||
|
||||
await this.messageChannelSyncStatusService.resetSyncStageStartedAt(
|
||||
await this.messageChannelSyncStatusService.resetSyncStageStartedAt([
|
||||
messageChannel.id,
|
||||
);
|
||||
]);
|
||||
|
||||
switch (messageChannel.syncStage) {
|
||||
case MessageChannelSyncStage.MESSAGE_LIST_FETCH_ONGOING:
|
||||
await this.messageChannelSyncStatusService.schedulePartialMessageListFetch(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
);
|
||||
break;
|
||||
case MessageChannelSyncStage.MESSAGES_IMPORT_ONGOING:
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport(
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport([
|
||||
messageChannel.id,
|
||||
);
|
||||
]);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
@ -79,7 +79,7 @@ export class MessageImportExceptionHandlerService {
|
||||
): Promise<void> {
|
||||
if (messageChannel.throttleFailureCount >= CALENDAR_THROTTLE_MAX_ATTEMPTS) {
|
||||
await this.messageChannelSyncStatusService.markAsFailedUnknownAndFlushMessagesToImport(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
@ -92,9 +92,7 @@ export class MessageImportExceptionHandlerService {
|
||||
);
|
||||
|
||||
await messageChannelRepository.increment(
|
||||
{
|
||||
id: messageChannel.id,
|
||||
},
|
||||
{ id: messageChannel.id },
|
||||
'throttleFailureCount',
|
||||
1,
|
||||
);
|
||||
@ -102,20 +100,20 @@ export class MessageImportExceptionHandlerService {
|
||||
switch (syncStep) {
|
||||
case MessageImportSyncStep.FULL_MESSAGE_LIST_FETCH:
|
||||
await this.messageChannelSyncStatusService.scheduleFullMessageListFetch(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
);
|
||||
break;
|
||||
|
||||
case MessageImportSyncStep.PARTIAL_MESSAGE_LIST_FETCH:
|
||||
await this.messageChannelSyncStatusService.schedulePartialMessageListFetch(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
);
|
||||
break;
|
||||
|
||||
case MessageImportSyncStep.MESSAGES_IMPORT:
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport(
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport([
|
||||
messageChannel.id,
|
||||
);
|
||||
]);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -128,7 +126,7 @@ export class MessageImportExceptionHandlerService {
|
||||
workspaceId: string,
|
||||
): Promise<void> {
|
||||
await this.messageChannelSyncStatusService.markAsFailedInsufficientPermissionsAndFlushMessagesToImport(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
workspaceId,
|
||||
);
|
||||
}
|
||||
@ -139,7 +137,7 @@ export class MessageImportExceptionHandlerService {
|
||||
workspaceId: string,
|
||||
): Promise<void> {
|
||||
await this.messageChannelSyncStatusService.markAsFailedUnknownAndFlushMessagesToImport(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
@ -159,7 +157,7 @@ export class MessageImportExceptionHandlerService {
|
||||
}
|
||||
|
||||
await this.messageChannelSyncStatusService.resetAndScheduleFullMessageListFetch(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
workspaceId,
|
||||
);
|
||||
}
|
||||
|
||||
@ -34,7 +34,7 @@ export class MessagingFullMessageListFetchService {
|
||||
) {
|
||||
try {
|
||||
await this.messageChannelSyncStatusService.markAsMessagesListFetchOngoing(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
);
|
||||
|
||||
const { messageExternalIds, nextSyncCursor } =
|
||||
@ -95,9 +95,9 @@ export class MessagingFullMessageListFetchService {
|
||||
},
|
||||
);
|
||||
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport(
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport([
|
||||
messageChannel.id,
|
||||
);
|
||||
]);
|
||||
} catch (error) {
|
||||
await this.messageImportErrorHandlerService.handleDriverException(
|
||||
error,
|
||||
|
||||
@ -1,7 +1,5 @@
|
||||
import { Injectable, Logger } from '@nestjs/common';
|
||||
|
||||
import { FeatureFlagKey } from 'src/engine/core-modules/feature-flag/enums/feature-flag-key.enum';
|
||||
import { FeatureFlagService } from 'src/engine/core-modules/feature-flag/services/feature-flag.service';
|
||||
import { CacheStorageService } from 'src/engine/integrations/cache-storage/cache-storage.service';
|
||||
import { InjectCacheStorage } from 'src/engine/integrations/cache-storage/decorators/cache-storage.decorator';
|
||||
import { CacheStorageNamespace } from 'src/engine/integrations/cache-storage/types/cache-storage-namespace.enum';
|
||||
@ -44,7 +42,6 @@ export class MessagingMessagesImportService {
|
||||
@InjectObjectMetadataRepository(BlocklistWorkspaceEntity)
|
||||
private readonly blocklistRepository: BlocklistRepository,
|
||||
private readonly emailAliasManagerService: EmailAliasManagerService,
|
||||
private readonly isFeatureEnabledService: FeatureFlagService,
|
||||
private readonly twentyORMManager: TwentyORMManager,
|
||||
private readonly messagingGetMessagesService: MessagingGetMessagesService,
|
||||
private readonly messageImportErrorHandlerService: MessageImportExceptionHandlerService,
|
||||
@ -76,9 +73,9 @@ export class MessagingMessagesImportService {
|
||||
`Messaging import for workspace ${workspaceId} and account ${connectedAccount.id} starting...`,
|
||||
);
|
||||
|
||||
await this.messageChannelSyncStatusService.markAsMessagesImportOngoing(
|
||||
await this.messageChannelSyncStatusService.markAsMessagesImportOngoing([
|
||||
messageChannel.id,
|
||||
);
|
||||
]);
|
||||
|
||||
try {
|
||||
connectedAccount.accessToken =
|
||||
@ -111,17 +108,10 @@ export class MessagingMessagesImportService {
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
await this.isFeatureEnabledService.isFeatureEnabled(
|
||||
FeatureFlagKey.IsMessagingAliasFetchingEnabled,
|
||||
workspaceId,
|
||||
)
|
||||
) {
|
||||
await this.emailAliasManagerService.refreshHandleAliases(
|
||||
connectedAccount,
|
||||
workspaceId,
|
||||
);
|
||||
}
|
||||
await this.emailAliasManagerService.refreshHandleAliases(
|
||||
connectedAccount,
|
||||
workspaceId,
|
||||
);
|
||||
|
||||
messageIdsToFetch = await this.cacheStorage.setPop(
|
||||
`messages-to-import:${workspaceId}:gmail:${messageChannel.id}`,
|
||||
@ -130,7 +120,7 @@ export class MessagingMessagesImportService {
|
||||
|
||||
if (!messageIdsToFetch?.length) {
|
||||
await this.messageChannelSyncStatusService.markAsCompletedAndSchedulePartialMessageListFetch(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
);
|
||||
|
||||
return await this.trackMessageImportCompleted(
|
||||
@ -151,7 +141,7 @@ export class MessagingMessagesImportService {
|
||||
);
|
||||
|
||||
const messagesToSave = filterEmails(
|
||||
messageChannel.handle,
|
||||
[messageChannel.handle, ...connectedAccount.handleAliases.split(',')],
|
||||
allMessages,
|
||||
blocklist.map((blocklistItem) => blocklistItem.handle),
|
||||
);
|
||||
@ -167,12 +157,12 @@ export class MessagingMessagesImportService {
|
||||
messageIdsToFetch.length < MESSAGING_GMAIL_USERS_MESSAGES_GET_BATCH_SIZE
|
||||
) {
|
||||
await this.messageChannelSyncStatusService.markAsCompletedAndSchedulePartialMessageListFetch(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
);
|
||||
} else {
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport(
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport([
|
||||
messageChannel.id,
|
||||
);
|
||||
]);
|
||||
}
|
||||
|
||||
const messageChannelRepository =
|
||||
|
||||
@ -38,7 +38,7 @@ export class MessagingPartialMessageListFetchService {
|
||||
): Promise<void> {
|
||||
try {
|
||||
await this.messageChannelSyncStatusService.markAsMessagesListFetchOngoing(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
);
|
||||
|
||||
const messageChannelRepository =
|
||||
@ -70,7 +70,7 @@ export class MessagingPartialMessageListFetchService {
|
||||
);
|
||||
|
||||
await this.messageChannelSyncStatusService.markAsCompletedAndSchedulePartialMessageListFetch(
|
||||
messageChannel.id,
|
||||
[messageChannel.id],
|
||||
);
|
||||
|
||||
return;
|
||||
@ -110,9 +110,9 @@ export class MessagingPartialMessageListFetchService {
|
||||
);
|
||||
}
|
||||
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport(
|
||||
await this.messageChannelSyncStatusService.scheduleMessagesImport([
|
||||
messageChannel.id,
|
||||
);
|
||||
]);
|
||||
} catch (error) {
|
||||
await this.messageImportErrorHandlerService.handleDriverException(
|
||||
error,
|
||||
|
||||
@ -3,19 +3,19 @@ import { MessageWithParticipants } from 'src/modules/messaging/message-import-ma
|
||||
|
||||
// Todo: refactor this into several utils
|
||||
export const filterEmails = (
|
||||
messageChannelHandle: string,
|
||||
messageChannelHandles: string[],
|
||||
messages: MessageWithParticipants[],
|
||||
blocklist: string[],
|
||||
) => {
|
||||
return filterOutBlocklistedMessages(
|
||||
messageChannelHandle,
|
||||
messageChannelHandles,
|
||||
filterOutIcsAttachments(messages),
|
||||
blocklist,
|
||||
);
|
||||
};
|
||||
|
||||
const filterOutBlocklistedMessages = (
|
||||
messageChannelHandle: string,
|
||||
messageChannelHandles: string[],
|
||||
messages: MessageWithParticipants[],
|
||||
blocklist: string[],
|
||||
) => {
|
||||
@ -27,7 +27,7 @@ const filterOutBlocklistedMessages = (
|
||||
return message.participants.every(
|
||||
(participant) =>
|
||||
!isEmailBlocklisted(
|
||||
messageChannelHandle,
|
||||
messageChannelHandles,
|
||||
participant.handle,
|
||||
blocklist,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user