Skip to content

The open alternative to Salesforce, designed for AI.

4746 create created listener on blocklist for calendar (#5046)

bosiraphael824786ff0425
diff --git a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts
index 5e694a5bba5..899ae234882 100644
--- a/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts
+++ b/packages/twenty-server/src/engine/integrations/message-queue/jobs.module.ts
@@ -15,7 +15,6 @@ import { GoogleAPIRefreshAccessTokenModule } from 'src/modules/connected-account
import { MessageParticipantModule } from 'src/modules/messaging/services/message-participant/message-participant.module';
import { ObjectMetadataRepositoryModule } from 'src/engine/object-metadata-repository/object-metadata-repository.module';
import { ConnectedAccountObjectMetadata } from 'src/modules/connected-account/standard-objects/connected-account.object-metadata';
-import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata';
import { CreateCompanyAndContactJob } from 'src/modules/connected-account/auto-companies-and-contacts-creation/jobs/create-company-and-contact.job';
import { AuditLogObjectMetadata } from 'src/modules/timeline/standard-objects/audit-log.object-metadata';
import { BillingModule } from 'src/engine/core-modules/billing/billing.module';
@@ -46,7 +45,7 @@ import { AutoCompaniesAndContactsCreationModule } from 'src/modules/connected-ac
import { GmailFetchMessagesFromCacheCronJob } from 'src/modules/messaging/crons/jobs/gmail-fetch-messages-from-cache.cron.job';
import { GmailPartialSyncCronJob } from 'src/modules/messaging/crons/jobs/gmail-partial-sync.cron.job';
import { DeleteConnectedAccountAssociatedMessagingDataJob } from 'src/modules/messaging/jobs/delete-connected-account-associated-messaging-data.job';
-import { DeleteMessagesFromHandleJob } from 'src/modules/messaging/jobs/delete-messages-from-handle.job';
+import { BlocklistItemDeleteMessagesJob } from 'src/modules/messaging/jobs/blocklist-item-delete-messages.job';
import { GmailFullSyncJob } from 'src/modules/messaging/jobs/gmail-full-sync.job';
import { GmailPartialSyncJob } from 'src/modules/messaging/jobs/gmail-partial-sync.job';
import { MessagingCreateCompanyAndContactAfterSyncJob } from 'src/modules/messaging/jobs/messaging-create-company-and-contact-after-sync.job';
@@ -58,6 +57,8 @@ import { GmailPartialSyncModule } from 'src/modules/messaging/services/gmail-par
import { ThreadCleanerModule } from 'src/modules/messaging/services/thread-cleaner/thread-cleaner.module';
import { TimelineActivityModule } from 'src/modules/timeline/timeline-activity.module';
import { MessageChannelMessageAssociationObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel-message-association.object-metadata';
+import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata';
+import { BlocklistItemDeleteCalendarEventsJob } from 'src/modules/calendar/jobs/blocklist-item-delete-calendar-events.job';
@Module({
imports: [
@@ -183,8 +184,12 @@ import { MessageChannelMessageAssociationObjectMetadata } from 'src/modules/mess
useClass: GoogleCalendarSyncCronJob,
},
{
- provide: DeleteMessagesFromHandleJob.name,
- useClass: DeleteMessagesFromHandleJob,
+ provide: BlocklistItemDeleteMessagesJob.name,
+ useClass: BlocklistItemDeleteMessagesJob,
+ },
+ {
+ provide: BlocklistItemDeleteCalendarEventsJob.name,
+ useClass: BlocklistItemDeleteCalendarEventsJob,
},
],
})
diff --git a/packages/twenty-server/src/modules/calendar-messaging-participant/utils/__tests__/is-email-blocklisted.util.spec.ts b/packages/twenty-server/src/modules/calendar-messaging-participant/utils/__tests__/is-email-blocklisted.util.spec.ts
new file mode 100644
index 00000000000..0ea2eacb34d
--- /dev/null
+++ b/packages/twenty-server/src/modules/calendar-messaging-participant/utils/__tests__/is-email-blocklisted.util.spec.ts
@@ -0,0 +1,39 @@
+import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util';
+
+describe('isEmailBlocklisted', () => {
+ it('should return true if email is blocklisted', () => {
+ const email = '[email protected]';
+ const blocklist = ['[email protected]', '[email protected]'];
+ const result = isEmailBlocklisted(email, blocklist);
+
+ expect(result).toBe(true);
+ });
+ it('should return false if email is not blocklisted', () => {
+ const email = '[email protected]';
+ const blocklist = ['[email protected]'];
+ const result = isEmailBlocklisted(email, blocklist);
+
+ expect(result).toBe(false);
+ });
+ it('should return false if email is null', () => {
+ const email = null;
+ const blocklist = ['@example.com'];
+ const result = isEmailBlocklisted(email, blocklist);
+
+ expect(result).toBe(false);
+ });
+ it('should return false if email is undefined', () => {
+ const email = undefined;
+ const blocklist = ['@example.com'];
+ const result = isEmailBlocklisted(email, blocklist);
+
+ expect(result).toBe(false);
+ });
+ it('should return true if email ends with blocklisted domain', () => {
+ const email = '[email protected]';
+ const blocklist = ['@example.com'];
+ const result = isEmailBlocklisted(email, blocklist);
+
+ expect(result).toBe(true);
+ });
+});
diff --git a/packages/twenty-server/src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util.ts b/packages/twenty-server/src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util.ts
new file mode 100644
index 00000000000..4231863bfcb
--- /dev/null
+++ b/packages/twenty-server/src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util.ts
@@ -0,0 +1,16 @@
+export const isEmailBlocklisted = (
+ email: string | null | undefined,
+ blocklist: string[],
+): boolean => {
+ if (!email) {
+ return false;
+ }
+
+ return blocklist.some((item) => {
+ if (item.startsWith('@')) {
+ return email.endsWith(item);
+ }
+
+ return email === item;
+ });
+};
diff --git a/packages/twenty-server/src/modules/calendar/calendar.module.ts b/packages/twenty-server/src/modules/calendar/calendar.module.ts
index a41cda848cd..5e7b314e101 100644
--- a/packages/twenty-server/src/modules/calendar/calendar.module.ts
+++ b/packages/twenty-server/src/modules/calendar/calendar.module.ts
@@ -1,10 +1,11 @@
import { Module } from '@nestjs/common';
+import { CalendarBlocklistListener } from 'src/modules/calendar/listeners/calendar-blocklist.listener';
import { CalendarChannelListener } from 'src/modules/calendar/listeners/calendar-channel.listener';
@Module({
imports: [],
- providers: [CalendarChannelListener],
+ providers: [CalendarChannelListener, CalendarBlocklistListener],
exports: [],
})
export class CalendarModule {}
diff --git a/packages/twenty-server/src/modules/calendar/jobs/blocklist-item-delete-calendar-events.job.ts b/packages/twenty-server/src/modules/calendar/jobs/blocklist-item-delete-calendar-events.job.ts
new file mode 100644
index 00000000000..8fe95c736ca
--- /dev/null
+++ b/packages/twenty-server/src/modules/calendar/jobs/blocklist-item-delete-calendar-events.job.ts
@@ -0,0 +1,83 @@
+import { Injectable, Logger } from '@nestjs/common';
+
+import { MessageQueueJob } from 'src/engine/integrations/message-queue/interfaces/message-queue-job.interface';
+
+import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repository/object-metadata-repository.decorator';
+import { CalendarChannelEventAssociationRepository } from 'src/modules/calendar/repositories/calendar-channel-event-association.repository';
+import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository';
+import { CalendarEventCleanerService } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.service';
+import { CalendarChannelEventAssociationObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel-event-association.object-metadata';
+import { CalendarChannelObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel.object-metadata';
+import { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository';
+import { BlocklistObjectMetadata } from 'src/modules/connected-account/standard-objects/blocklist.object-metadata';
+
+export type BlocklistItemDeleteCalendarEventsJobData = {
+ workspaceId: string;
+ blocklistItemId: string;
+};
+
+@Injectable()
+export class BlocklistItemDeleteCalendarEventsJob
+ implements MessageQueueJob<BlocklistItemDeleteCalendarEventsJobData>
+{
+ private readonly logger = new Logger(
+ BlocklistItemDeleteCalendarEventsJob.name,
+ );
+
+ constructor(
+ @InjectObjectMetadataRepository(CalendarChannelObjectMetadata)
+ private readonly calendarChannelRepository: CalendarChannelRepository,
+ @InjectObjectMetadataRepository(
+ CalendarChannelEventAssociationObjectMetadata,
+ )
+ private readonly calendarChannelEventAssociationRepository: CalendarChannelEventAssociationRepository,
+ @InjectObjectMetadataRepository(BlocklistObjectMetadata)
+ private readonly blocklistRepository: BlocklistRepository,
+ private readonly calendarEventCleanerService: CalendarEventCleanerService,
+ ) {}
+
+ async handle(data: BlocklistItemDeleteCalendarEventsJobData): Promise<void> {
+ const { workspaceId, blocklistItemId } = data;
+
+ const blocklistItem = await this.blocklistRepository.getById(
+ blocklistItemId,
+ workspaceId,
+ );
+
+ if (!blocklistItem) {
+ this.logger.log(
+ `Blocklist item with id ${blocklistItemId} not found in workspace ${workspaceId}`,
+ );
+
+ return;
+ }
+
+ const { handle, workspaceMemberId } = blocklistItem;
+
+ this.logger.log(
+ `Deleting calendar events from ${handle} in workspace ${workspaceId} for workspace member ${workspaceMemberId}`,
+ );
+
+ const calendarChannels =
+ await this.calendarChannelRepository.getIdsByWorkspaceMemberId(
+ workspaceMemberId,
+ workspaceId,
+ );
+
+ const calendarChannelIds = calendarChannels.map(({ id }) => id);
+
+ await this.calendarChannelEventAssociationRepository.deleteByCalendarEventParticipantHandleAndCalendarChannelIds(
+ handle,
+ calendarChannelIds,
+ workspaceId,
+ );
+
+ await this.calendarEventCleanerService.cleanWorkspaceCalendarEvents(
+ workspaceId,
+ );
+
+ this.logger.log(
+ `Deleted calendar events from handle ${handle} in workspace ${workspaceId} for workspace member ${workspaceMemberId}`,
+ );
+ }
+}
diff --git a/packages/twenty-server/src/modules/calendar/listeners/calendar-blocklist.listener.ts b/packages/twenty-server/src/modules/calendar/listeners/calendar-blocklist.listener.ts
new file mode 100644
index 00000000000..1520a0dc1fa
--- /dev/null
+++ b/packages/twenty-server/src/modules/calendar/listeners/calendar-blocklist.listener.ts
@@ -0,0 +1,32 @@
+import { Inject, Injectable } from '@nestjs/common';
+import { OnEvent } from '@nestjs/event-emitter';
+
+import { ObjectRecordCreateEvent } from 'src/engine/integrations/event-emitter/types/object-record-create.event';
+import { MessageQueue } from 'src/engine/integrations/message-queue/message-queue.constants';
+import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
+import {
+ BlocklistItemDeleteCalendarEventsJobData,
+ BlocklistItemDeleteCalendarEventsJob,
+} from 'src/modules/calendar/jobs/blocklist-item-delete-calendar-events.job';
+import { BlocklistObjectMetadata } from 'src/modules/connected-account/standard-objects/blocklist.object-metadata';
+
+@Injectable()
+export class CalendarBlocklistListener {
+ constructor(
+ @Inject(MessageQueue.calendarQueue)
+ private readonly messageQueueService: MessageQueueService,
+ ) {}
+
+ @OnEvent('blocklist.created')
+ handleCreatedEvent(
+ payload: ObjectRecordCreateEvent<BlocklistObjectMetadata>,
+ ) {
+ this.messageQueueService.add<BlocklistItemDeleteCalendarEventsJobData>(
+ BlocklistItemDeleteCalendarEventsJob.name,
+ {
+ workspaceId: payload.workspaceId,
+ blocklistItemId: payload.recordId,
+ },
+ );
+ }
+}
diff --git a/packages/twenty-server/src/modules/calendar/listeners/calendar-channel.listener.ts b/packages/twenty-server/src/modules/calendar/listeners/calendar-channel.listener.ts
index bfa9f897364..cc2e5591630 100644
--- a/packages/twenty-server/src/modules/calendar/listeners/calendar-channel.listener.ts
+++ b/packages/twenty-server/src/modules/calendar/listeners/calendar-channel.listener.ts
@@ -14,7 +14,7 @@ import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-obj
@Injectable()
export class CalendarChannelListener {
constructor(
- @Inject(MessageQueue.messagingQueue)
+ @Inject(MessageQueue.calendarQueue)
private readonly messageQueueService: MessageQueueService,
) {}
diff --git a/packages/twenty-server/src/modules/calendar/repositories/calendar-channel-event-association.repository.ts b/packages/twenty-server/src/modules/calendar/repositories/calendar-channel-event-association.repository.ts
index 5162c37bfe2..3e85d4de4d8 100644
--- a/packages/twenty-server/src/modules/calendar/repositories/calendar-channel-event-association.repository.ts
+++ b/packages/twenty-server/src/modules/calendar/repositories/calendar-channel-event-association.repository.ts
@@ -5,7 +5,7 @@ import { EntityManager } from 'typeorm';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
import { CalendarChannelEventAssociationObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-channel-event-association.object-metadata';
-import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/getFlattenedValuesAndValuesStringForBatchRawQuery.util';
+import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
@Injectable()
export class CalendarChannelEventAssociationRepository {
@@ -169,4 +169,28 @@ export class CalendarChannelEventAssociationRepository {
transactionManager,
);
}
+
+ public async deleteByCalendarEventParticipantHandleAndCalendarChannelIds(
+ calendarEventParticipantHandle: string,
+ calendarChannelIds: string[],
+ workspaceId: string,
+ transactionManager?: EntityManager,
+ ) {
+ const dataSourceSchema =
+ this.workspaceDataSourceService.getSchemaName(workspaceId);
+
+ await this.workspaceDataSourceService.executeRawQuery(
+ `DELETE FROM ${dataSourceSchema}."calendarChannelEventAssociation"
+ WHERE "id" IN (
+ SELECT "calendarChannelEventAssociation"."id"
+ FROM ${dataSourceSchema}."calendarChannelEventAssociation" "calendarChannelEventAssociation"
+ JOIN ${dataSourceSchema}."calendarEvent" "calendarEvent" ON "calendarChannelEventAssociation"."calendarEventId" = "calendarEvent"."id"
+ JOIN ${dataSourceSchema}."calendarEventParticipant" "calendarEventParticipant" ON "calendarEvent"."id" = "calendarEventParticipant"."calendarEventId"
+ WHERE "calendarEventParticipant"."handle" = $1 AND "calendarChannelEventAssociation"."calendarChannelId" = ANY($2)
+ )`,
+ [calendarEventParticipantHandle, calendarChannelIds],
+ workspaceId,
+ transactionManager,
+ );
+ }
}
diff --git a/packages/twenty-server/src/modules/calendar/repositories/calendar-channel.repository.ts b/packages/twenty-server/src/modules/calendar/repositories/calendar-channel.repository.ts
index 12a94410df2..c9305ebe31a 100644
--- a/packages/twenty-server/src/modules/calendar/repositories/calendar-channel.repository.ts
+++ b/packages/twenty-server/src/modules/calendar/repositories/calendar-channel.repository.ts
@@ -95,6 +95,27 @@ export class CalendarChannelRepository {
);
}
+ public async getIdsByWorkspaceMemberId(
+ workspaceMemberId: string,
+ workspaceId: string,
+ transactionManager?: EntityManager,
+ ): Promise<ObjectRecord<CalendarChannelObjectMetadata>[]> {
+ const dataSourceSchema =
+ this.workspaceDataSourceService.getSchemaName(workspaceId);
+
+ const calendarChannelIds =
+ await this.workspaceDataSourceService.executeRawQuery(
+ `SELECT "calendarChannel".id FROM ${dataSourceSchema}."calendarChannel" "calendarChannel"
+ JOIN ${dataSourceSchema}."connectedAccount" ON "calendarChannel"."connectedAccountId" = ${dataSourceSchema}."connectedAccount"."id"
+ WHERE ${dataSourceSchema}."connectedAccount"."accountOwnerId" = $1`,
+ [workspaceMemberId],
+ workspaceId,
+ transactionManager,
+ );
+
+ return calendarChannelIds;
+ }
+
public async updateSyncCursor(
syncCursor: string,
calendarChannelId: string,
diff --git a/packages/twenty-server/src/modules/calendar/repositories/calendar-event-participant.repository.ts b/packages/twenty-server/src/modules/calendar/repositories/calendar-event-participant.repository.ts
index b3dc3dad231..a5c0d332cc2 100644
--- a/packages/twenty-server/src/modules/calendar/repositories/calendar-event-participant.repository.ts
+++ b/packages/twenty-server/src/modules/calendar/repositories/calendar-event-participant.repository.ts
@@ -6,7 +6,7 @@ import differenceWith from 'lodash.differencewith';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata';
-import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/getFlattenedValuesAndValuesStringForBatchRawQuery.util';
+import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
import {
CalendarEventParticipant,
CalendarEventParticipantWithId,
diff --git a/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts b/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts
index 8da12f27d55..cefee02fcb9 100644
--- a/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts
+++ b/packages/twenty-server/src/modules/calendar/repositories/calendar-event.repository.ts
@@ -5,7 +5,7 @@ import { EntityManager } from 'typeorm';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
import { ObjectRecord } from 'src/engine/workspace-manager/workspace-sync-metadata/types/object-record';
import { CalendarEventObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event.object-metadata';
-import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/getFlattenedValuesAndValuesStringForBatchRawQuery.util';
+import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
import { CalendarEvent } from 'src/modules/calendar/types/calendar-event';
import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata';
diff --git a/packages/twenty-server/src/modules/calendar/services/calendar-event-participant/calendar-event-participant.service.ts b/packages/twenty-server/src/modules/calendar/services/calendar-event-participant/calendar-event-participant.service.ts
index 6a8c57bb4f9..a9a83f597fd 100644
--- a/packages/twenty-server/src/modules/calendar/services/calendar-event-participant/calendar-event-participant.service.ts
+++ b/packages/twenty-server/src/modules/calendar/services/calendar-event-participant/calendar-event-participant.service.ts
@@ -6,7 +6,7 @@ import { InjectObjectMetadataRepository } from 'src/engine/object-metadata-repos
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
import { PersonObjectMetadata } from 'src/modules/person/standard-objects/person.object-metadata';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
-import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/getFlattenedValuesAndValuesStringForBatchRawQuery.util';
+import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
import { CalendarEventParticipant } from 'src/modules/calendar/types/calendar-event';
import { CalendarEventParticipantRepository } from 'src/modules/calendar/repositories/calendar-event-participant.repository';
import { CalendarEventParticipantObjectMetadata } from 'src/modules/calendar/standard-objects/calendar-event-participant.object-metadata';
diff --git a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync/google-calendar-sync.service.ts b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync/google-calendar-sync.service.ts
index 6fb9bb90b6c..4629fec9d97 100644
--- a/packages/twenty-server/src/modules/calendar/services/google-calendar-sync/google-calendar-sync.service.ts
+++ b/packages/twenty-server/src/modules/calendar/services/google-calendar-sync/google-calendar-sync.service.ts
@@ -12,7 +12,6 @@ import {
FeatureFlagKeys,
} from 'src/engine/core-modules/feature-flag/feature-flag.entity';
import { GoogleCalendarClientProvider } from 'src/modules/calendar/services/providers/google-calendar/google-calendar.provider';
-import { googleCalendarSearchFilterExcludeEmails } from 'src/modules/calendar/utils/google-calendar-search-filter.util';
import { CalendarChannelEventAssociationRepository } from 'src/modules/calendar/repositories/calendar-channel-event-association.repository';
import { CalendarChannelRepository } from 'src/modules/calendar/repositories/calendar-channel.repository';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
@@ -29,6 +28,7 @@ import { BlocklistObjectMetadata } from 'src/modules/connected-account/standard-
import { CalendarEventCleanerService } from 'src/modules/calendar/services/calendar-event-cleaner/calendar-event-cleaner.service';
import { CalendarEventParticipantService } from 'src/modules/calendar/services/calendar-event-participant/calendar-event-participant.service';
import { CalendarEventParticipant } from 'src/modules/calendar/types/calendar-event';
+import { filterOutBlocklistedEvents } from 'src/modules/calendar/utils/filter-out-blocklisted-events.util';
@Injectable()
export class GoogleCalendarSyncService {
@@ -133,7 +133,6 @@ export class GoogleCalendarSyncService {
syncToken,
pageToken: nextPageToken,
showDeleted: true,
- q: googleCalendarSearchFilterExcludeEmails(blocklistedEmails),
});
nextSyncToken = googleCalendarEvents.data.nextSyncToken;
@@ -168,7 +167,12 @@ export class GoogleCalendarSyncService {
return;
}
- const eventExternalIds = events.map((event) => event.id as string);
+ const filteredEvents = filterOutBlocklistedEvents(
+ events,
+ blocklistedEmails,
+ );
+
+ const eventExternalIds = filteredEvents.map((event) => event.id as string);
startTime = Date.now();
@@ -204,7 +208,7 @@ export class GoogleCalendarSyncService {
workspaceId,
);
- const formattedEvents = events
+ const formattedEvents = filteredEvents
.filter((event) => event.status !== 'cancelled')
.map((event) =>
formatGoogleCalendarEvent(event, iCalUIDCalendarEventIdMap),
@@ -218,7 +222,7 @@ export class GoogleCalendarSyncService {
existingEventExternalIds.includes(event.externalId),
);
- const cancelledEventExternalIds = events
+ const cancelledEventExternalIds = filteredEvents
.filter((event) => event.status === 'cancelled')
.map((event) => event.id as string);
@@ -240,7 +244,7 @@ export class GoogleCalendarSyncService {
let newCalendarEventParticipants: CalendarEventParticipant[] = [];
- if (events.length > 0) {
+ if (filteredEvents.length > 0) {
const dataSourceMetadata =
await this.workspaceDataSourceService.connectToWorkspaceDataSource(
workspaceId,
diff --git a/packages/twenty-server/src/modules/calendar/utils/filter-out-blocklisted-events.util.ts b/packages/twenty-server/src/modules/calendar/utils/filter-out-blocklisted-events.util.ts
new file mode 100644
index 00000000000..eafc669b1ae
--- /dev/null
+++ b/packages/twenty-server/src/modules/calendar/utils/filter-out-blocklisted-events.util.ts
@@ -0,0 +1,18 @@
+import { calendar_v3 as calendarV3 } from 'googleapis';
+
+import { isEmailBlocklisted } from 'src/modules/calendar-messaging-participant/utils/is-email-blocklisted.util';
+
+export const filterOutBlocklistedEvents = (
+ events: calendarV3.Schema$Event[],
+ blocklist: string[],
+) => {
+ return events.filter((event) => {
+ if (!event.attendees) {
+ return true;
+ }
+
+ return event.attendees.every(
+ (attendee) => !isEmailBlocklisted(attendee.email, blocklist),
+ );
+ });
+};
diff --git a/packages/twenty-server/src/modules/calendar/utils/getFlattenedValuesAndValuesStringForBatchRawQuery.util.ts b/packages/twenty-server/src/modules/calendar/utils/get-flattened-values-and-values-string-for-batch-raw-query.util.ts
similarity index 100%
rename from packages/twenty-server/src/modules/calendar/utils/getFlattenedValuesAndValuesStringForBatchRawQuery.util.ts
rename to packages/twenty-server/src/modules/calendar/utils/get-flattened-values-and-values-string-for-batch-raw-query.util.ts
diff --git a/packages/twenty-server/src/modules/calendar/utils/google-calendar-search-filter.util.ts b/packages/twenty-server/src/modules/calendar/utils/google-calendar-search-filter.util.ts
index d84f494836e..e9dc788d9f9 100644
--- a/packages/twenty-server/src/modules/calendar/utils/google-calendar-search-filter.util.ts
+++ b/packages/twenty-server/src/modules/calendar/utils/google-calendar-search-filter.util.ts
@@ -5,5 +5,5 @@ export const googleCalendarSearchFilterExcludeEmails = (
return undefined;
}
- return `email=-(${emails.join(', -')})`;
+ return `-(${emails.join(', ')})`;
};
diff --git a/packages/twenty-server/src/modules/messaging/jobs/delete-messages-from-handle.job.ts b/packages/twenty-server/src/modules/messaging/jobs/blocklist-item-delete-messages.job.ts
similarity index 86%
rename from packages/twenty-server/src/modules/messaging/jobs/delete-messages-from-handle.job.ts
rename to packages/twenty-server/src/modules/messaging/jobs/blocklist-item-delete-messages.job.ts
index 46aabf5b98e..0e4cbf88c26 100644
--- a/packages/twenty-server/src/modules/messaging/jobs/delete-messages-from-handle.job.ts
+++ b/packages/twenty-server/src/modules/messaging/jobs/blocklist-item-delete-messages.job.ts
@@ -11,16 +11,16 @@ import { ThreadCleanerService } from 'src/modules/messaging/services/thread-clea
import { MessageChannelMessageAssociationObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel-message-association.object-metadata';
import { MessageChannelObjectMetadata } from 'src/modules/messaging/standard-objects/message-channel.object-metadata';
-export type DeleteMessagesFromHandleJobData = {
+export type BlocklistItemDeleteMessagesJobData = {
workspaceId: string;
blocklistItemId: string;
};
@Injectable()
-export class DeleteMessagesFromHandleJob
- implements MessageQueueJob<DeleteMessagesFromHandleJobData>
+export class BlocklistItemDeleteMessagesJob
+ implements MessageQueueJob<BlocklistItemDeleteMessagesJobData>
{
- private readonly logger = new Logger(DeleteMessagesFromHandleJob.name);
+ private readonly logger = new Logger(BlocklistItemDeleteMessagesJob.name);
constructor(
@InjectObjectMetadataRepository(MessageChannelObjectMetadata)
@@ -34,7 +34,7 @@ export class DeleteMessagesFromHandleJob
private readonly threadCleanerService: ThreadCleanerService,
) {}
- async handle(data: DeleteMessagesFromHandleJobData): Promise<void> {
+ async handle(data: BlocklistItemDeleteMessagesJobData): Promise<void> {
const { workspaceId, blocklistItemId } = data;
const blocklistItem = await this.blocklistRepository.getById(
@@ -64,9 +64,12 @@ export class DeleteMessagesFromHandleJob
const messageChannelIds = messageChannels.map(({ id }) => id);
- await this.messageChannelMessageAssociationRepository.deleteByMessageParticipantHandleAndMessageChannelIds(
+ const rolesToDelete: ('from' | 'to')[] = ['from', 'to'];
+
+ await this.messageChannelMessageAssociationRepository.deleteByMessageParticipantHandleAndMessageChannelIdsAndRoles(
handle,
messageChannelIds,
+ rolesToDelete,
workspaceId,
);
diff --git a/packages/twenty-server/src/modules/messaging/listeners/messaging-blocklist.listener.ts b/packages/twenty-server/src/modules/messaging/listeners/messaging-blocklist.listener.ts
index eb58ae6e56e..22358e1e0b9 100644
--- a/packages/twenty-server/src/modules/messaging/listeners/messaging-blocklist.listener.ts
+++ b/packages/twenty-server/src/modules/messaging/listeners/messaging-blocklist.listener.ts
@@ -6,9 +6,9 @@ import { MessageQueue } from 'src/engine/integrations/message-queue/message-queu
import { MessageQueueService } from 'src/engine/integrations/message-queue/services/message-queue.service';
import { BlocklistObjectMetadata } from 'src/modules/connected-account/standard-objects/blocklist.object-metadata';
import {
- DeleteMessagesFromHandleJobData,
- DeleteMessagesFromHandleJob,
-} from 'src/modules/messaging/jobs/delete-messages-from-handle.job';
+ BlocklistItemDeleteMessagesJobData,
+ BlocklistItemDeleteMessagesJob,
+} from 'src/modules/messaging/jobs/blocklist-item-delete-messages.job';
@Injectable()
export class MessagingBlocklistListener {
@@ -21,8 +21,8 @@ export class MessagingBlocklistListener {
handleCreatedEvent(
payload: ObjectRecordCreateEvent<BlocklistObjectMetadata>,
) {
- this.messageQueueService.add<DeleteMessagesFromHandleJobData>(
- DeleteMessagesFromHandleJob.name,
+ this.messageQueueService.add<BlocklistItemDeleteMessagesJobData>(
+ BlocklistItemDeleteMessagesJob.name,
{
workspaceId: payload.workspaceId,
blocklistItemId: payload.recordId,
diff --git a/packages/twenty-server/src/modules/messaging/repositories/message-channel-message-association.repository.ts b/packages/twenty-server/src/modules/messaging/repositories/message-channel-message-association.repository.ts
index de2b5fcd4e4..c99e88224ea 100644
--- a/packages/twenty-server/src/modules/messaging/repositories/message-channel-message-association.repository.ts
+++ b/packages/twenty-server/src/modules/messaging/repositories/message-channel-message-association.repository.ts
@@ -67,9 +67,10 @@ export class MessageChannelMessageAssociationRepository {
);
}
- public async deleteByMessageParticipantHandleAndMessageChannelIds(
+ public async deleteByMessageParticipantHandleAndMessageChannelIdsAndRoles(
messageParticipantHandle: string,
messageChannelIds: string[],
+ rolesToDelete: ('from' | 'to' | 'cc' | 'bcc')[],
workspaceId: string,
transactionManager?: EntityManager,
) {
@@ -83,7 +84,7 @@ export class MessageChannelMessageAssociationRepository {
JOIN ${dataSourceSchema}."message" ON "messageChannelMessageAssociation"."messageId" = ${dataSourceSchema}."message"."id"
JOIN ${dataSourceSchema}."messageParticipant" "messageParticipant" ON ${dataSourceSchema}."message"."id" = "messageParticipant"."messageId"
WHERE "messageParticipant"."handle" = $1 AND "messageParticipant"."role"= ANY($2) AND "messageChannelMessageAssociation"."messageChannelId" = ANY($3)`,
- [messageParticipantHandle, ['from', 'to'], messageChannelIds],
+ [messageParticipantHandle, rolesToDelete, messageChannelIds],
workspaceId,
transactionManager,
);
diff --git a/packages/twenty-server/src/modules/messaging/services/message-participant/message-participant.service.ts b/packages/twenty-server/src/modules/messaging/services/message-participant/message-participant.service.ts
index 5d65b1c34a2..98b0a348901 100644
--- a/packages/twenty-server/src/modules/messaging/services/message-participant/message-participant.service.ts
+++ b/packages/twenty-server/src/modules/messaging/services/message-participant/message-participant.service.ts
@@ -7,7 +7,7 @@ import { ParticipantWithMessageId } from 'src/modules/messaging/types/gmail-mess
import { PersonRepository } from 'src/modules/person/repositories/person.repository';
import { PersonObjectMetadata } from 'src/modules/person/standard-objects/person.object-metadata';
import { WorkspaceDataSourceService } from 'src/engine/workspace-datasource/workspace-datasource.service';
-import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/getFlattenedValuesAndValuesStringForBatchRawQuery.util';
+import { getFlattenedValuesAndValuesStringForBatchRawQuery } from 'src/modules/calendar/utils/get-flattened-values-and-values-string-for-batch-raw-query.util';
import { MessageParticipantRepository } from 'src/modules/messaging/repositories/message-participant.repository';
import { MessageParticipantObjectMetadata } from 'src/modules/messaging/standard-objects/message-participant.object-metadata';
import { AddPersonIdAndWorkspaceMemberIdService } from 'src/modules/calendar-messaging-participant/services/add-person-id-and-workspace-member-id/add-person-id-and-workspace-member-id.service';