From 03c485e68d99a2b05205ddad8db448170b69154f Mon Sep 17 00:00:00 2001 From: Zita Szupera Date: Fri, 29 Sep 2023 10:55:22 +0200 Subject: [PATCH] feat: add the option for confirmation before message delete --- .../src/lib/channel.service.spec.ts | 18 ++++++++++++++++++ .../src/lib/channel.service.ts | 15 ++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/projects/stream-chat-angular/src/lib/channel.service.spec.ts b/projects/stream-chat-angular/src/lib/channel.service.spec.ts index ffe239d6..77ab0591 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.spec.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.spec.ts @@ -1247,6 +1247,24 @@ describe('ChannelService', () => { expect(mockChatClient.deleteMessage).toHaveBeenCalledWith(message.id); }); + it(`should call #messageDeleteConfirmationHandler is that's provided`, async () => { + const spy = jasmine.createSpy(); + const message = mockMessage(); + service.messageDeleteConfirmationHandler = spy; + spy.and.resolveTo(false); + await service.deleteMessage(message); + + expect(spy).toHaveBeenCalledWith(message); + expect(mockChatClient.deleteMessage).not.toHaveBeenCalledWith(message.id); + + spy.and.resolveTo(true); + spy.calls.reset(); + await service.deleteMessage(message); + + expect(spy).toHaveBeenCalledWith(message); + expect(mockChatClient.deleteMessage).toHaveBeenCalledWith(message.id); + }); + it('should resend message', async () => { await init(); let latestMessage!: StreamMessage; diff --git a/projects/stream-chat-angular/src/lib/channel.service.ts b/projects/stream-chat-angular/src/lib/channel.service.ts index be86e7f7..81b0818e 100644 --- a/projects/stream-chat-angular/src/lib/channel.service.ts +++ b/projects/stream-chat-angular/src/lib/channel.service.ts @@ -271,6 +271,12 @@ export class ChannelService< url: string, channel: Channel ) => Promise; + /** + * The provided method will be called before deleting a message. If the returned Promise resolves to `true` to deletion will go ahead. If `false` is returned, the message won't be deleted. + */ + messageDeleteConfirmationHandler?: ( + message: StreamMessage + ) => Promise; private channelsSubject = new BehaviorSubject[] | undefined>( undefined ); @@ -749,7 +755,14 @@ export class ChannelService< * @param message Message to be deleted */ async deleteMessage(message: StreamMessage) { - await this.chatClientService.chatClient.deleteMessage(message.id); + if (this.messageDeleteConfirmationHandler) { + const result = await this.messageDeleteConfirmationHandler(message); + if (result) { + await this.chatClientService.chatClient.deleteMessage(message.id); + } + } else { + await this.chatClientService.chatClient.deleteMessage(message.id); + } } /**