Skip to content

Commit

Permalink
Merge pull request #485 from GetStream/delete-confirmation
Browse files Browse the repository at this point in the history
feat: add the option for confirmation before message delete
  • Loading branch information
szuperaz authored Sep 29, 2023
2 parents a14fcec + 03c485e commit c2f03ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
18 changes: 18 additions & 0 deletions projects/stream-chat-angular/src/lib/channel.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 14 additions & 1 deletion projects/stream-chat-angular/src/lib/channel.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ export class ChannelService<
url: string,
channel: Channel<T>
) => Promise<void>;
/**
* 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<T>
) => Promise<boolean>;
private channelsSubject = new BehaviorSubject<Channel<T>[] | undefined>(
undefined
);
Expand Down Expand Up @@ -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);
}
}

/**
Expand Down

0 comments on commit c2f03ed

Please sign in to comment.