-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
4746 create created listener on blocklist (#5031)
Closes #4746 for messaging. I will create another PR to implement the listener on calendar.
- Loading branch information
1 parent
c42fcf4
commit 8702c71
Showing
9 changed files
with
202 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/twenty-server/src/modules/messaging/jobs/delete-messages-from-handle.job.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
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 { BlocklistRepository } from 'src/modules/connected-account/repositories/blocklist.repository'; | ||
import { BlocklistObjectMetadata } from 'src/modules/connected-account/standard-objects/blocklist.object-metadata'; | ||
import { MessageChannelMessageAssociationRepository } from 'src/modules/messaging/repositories/message-channel-message-association.repository'; | ||
import { MessageChannelRepository } from 'src/modules/messaging/repositories/message-channel.repository'; | ||
import { ThreadCleanerService } from 'src/modules/messaging/services/thread-cleaner/thread-cleaner.service'; | ||
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 = { | ||
workspaceId: string; | ||
blocklistItemId: string; | ||
}; | ||
|
||
@Injectable() | ||
export class DeleteMessagesFromHandleJob | ||
implements MessageQueueJob<DeleteMessagesFromHandleJobData> | ||
{ | ||
private readonly logger = new Logger(DeleteMessagesFromHandleJob.name); | ||
|
||
constructor( | ||
@InjectObjectMetadataRepository(MessageChannelObjectMetadata) | ||
private readonly messageChannelRepository: MessageChannelRepository, | ||
@InjectObjectMetadataRepository( | ||
MessageChannelMessageAssociationObjectMetadata, | ||
) | ||
private readonly messageChannelMessageAssociationRepository: MessageChannelMessageAssociationRepository, | ||
@InjectObjectMetadataRepository(BlocklistObjectMetadata) | ||
private readonly blocklistRepository: BlocklistRepository, | ||
private readonly threadCleanerService: ThreadCleanerService, | ||
) {} | ||
|
||
async handle(data: DeleteMessagesFromHandleJobData): 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 messages from ${handle} in workspace ${workspaceId} for workspace member ${workspaceMemberId}`, | ||
); | ||
|
||
const messageChannels = | ||
await this.messageChannelRepository.getIdsByWorkspaceMemberId( | ||
workspaceMemberId, | ||
workspaceId, | ||
); | ||
|
||
const messageChannelIds = messageChannels.map(({ id }) => id); | ||
|
||
await this.messageChannelMessageAssociationRepository.deleteByMessageParticipantHandleAndMessageChannelIds( | ||
handle, | ||
messageChannelIds, | ||
workspaceId, | ||
); | ||
|
||
await this.threadCleanerService.cleanWorkspaceThreads(workspaceId); | ||
|
||
this.logger.log( | ||
`Deleted messages from handle ${handle} in workspace ${workspaceId} for workspace member ${workspaceMemberId}`, | ||
); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
packages/twenty-server/src/modules/messaging/listeners/messaging-blocklist.listener.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { BlocklistObjectMetadata } from 'src/modules/connected-account/standard-objects/blocklist.object-metadata'; | ||
import { | ||
DeleteMessagesFromHandleJobData, | ||
DeleteMessagesFromHandleJob, | ||
} from 'src/modules/messaging/jobs/delete-messages-from-handle.job'; | ||
|
||
@Injectable() | ||
export class MessagingBlocklistListener { | ||
constructor( | ||
@Inject(MessageQueue.messagingQueue) | ||
private readonly messageQueueService: MessageQueueService, | ||
) {} | ||
|
||
@OnEvent('blocklist.created') | ||
handleCreatedEvent( | ||
payload: ObjectRecordCreateEvent<BlocklistObjectMetadata>, | ||
) { | ||
this.messageQueueService.add<DeleteMessagesFromHandleJobData>( | ||
DeleteMessagesFromHandleJob.name, | ||
{ | ||
workspaceId: payload.workspaceId, | ||
blocklistItemId: payload.recordId, | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters