From a2f21ed23ca8130f9c87e1d51b388bc7172c7677 Mon Sep 17 00:00:00 2001 From: piotr-suwala <112620304+piotr-suwala@users.noreply.github.com> Date: Tue, 26 Sep 2023 14:42:31 +0200 Subject: [PATCH] feat(lib): fix removing threads (#133) * feat(lib): fix removing threads * feat(lib): rm logs * feat(lib): add delete options * feat(lib): add delete options * feat(lib): add delete options --- lib/src/entities/chat.ts | 24 ++++++++++++++----- lib/src/entities/message.ts | 3 ++- lib/src/entities/thread-channel.ts | 5 +--- .../components/message-text/MessageText.tsx | 11 +++++++++ 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/lib/src/entities/chat.ts b/lib/src/entities/chat.ts index 7990109..a97db98 100644 --- a/lib/src/entities/chat.ts +++ b/lib/src/entities/chat.ts @@ -433,7 +433,7 @@ export class Chat { } /** @internal */ - async removeThreadChannel(message: Message) { + async removeThreadChannel(message: Message, options: DeleteParameters = {}) { if (!message.hasThread) { throw "There is no thread to be deleted" } @@ -441,15 +441,27 @@ export class Chat { const actionTimetoken = message.actions?.threadRootId[this.getThreadId(message.channelId, message.timetoken)][0] .actionTimetoken + if (!actionTimetoken) { throw "There is no action timetoken corresponding to the thread" } - return this.sdk.removeMessageAction({ - channel: message.channelId, - messageTimetoken: message.timetoken, - actionTimetoken: String(actionTimetoken), - }) + const threadId = this.getThreadId(message.channelId, message.timetoken) + + const threadChannel = await this.getChannel(threadId) + + if (!threadChannel) { + throw `There is no thread with id: ${threadId}` + } + + return Promise.all([ + this.sdk.removeMessageAction({ + channel: message.channelId, + messageTimetoken: message.timetoken, + actionTimetoken: String(actionTimetoken), + }), + threadChannel.delete(options), + ]) } /** diff --git a/lib/src/entities/message.ts b/lib/src/entities/message.ts index c34e915..00263d0 100644 --- a/lib/src/entities/message.ts +++ b/lib/src/entities/message.ts @@ -31,8 +31,9 @@ export class Message { if (!this.actions?.["threadRootId"]) { return false } + const key = Object.keys(this.actions["threadRootId"])[0] - return !!Object.keys(this.actions["threadRootId"])[0] + return !!key && !!this.actions["threadRootId"][key].length } get mentionedUsers() { diff --git a/lib/src/entities/thread-channel.ts b/lib/src/entities/thread-channel.ts index 24edb04..a831922 100644 --- a/lib/src/entities/thread-channel.ts +++ b/lib/src/entities/thread-channel.ts @@ -101,10 +101,7 @@ export class ThreadChannel extends Channel { } override async delete(options: DeleteParameters = {}) { - const data = await Promise.all([ - this.chat.removeThreadChannel(this.parentMessage), - super.delete(options), - ]) + const data = await this.chat.removeThreadChannel(this.parentMessage, options) return data[1] } diff --git a/samples/react-native-group-chat/components/message-text/MessageText.tsx b/samples/react-native-group-chat/components/message-text/MessageText.tsx index 5195a65..6bef5b1 100644 --- a/samples/react-native-group-chat/components/message-text/MessageText.tsx +++ b/samples/react-native-group-chat/components/message-text/MessageText.tsx @@ -33,6 +33,17 @@ export function MessageText({ onGoToMessage, messageProps }: MessageTextProps) { navigation.navigate("Chat") } + async function removeThread() { + if (messageProps.currentMessage?.originalPnMessage.hasThread) { + const thread = await messageProps.currentMessage?.originalPnMessage.getThread() + const messagesObject = await thread.getHistory() + for (let i = 0; i < messagesObject.messages.length; i++) { + await messagesObject.messages[i].delete({ soft: false }) + } + await thread.delete() + } + } + const renderEmojis = useCallback(() => { if (!messageProps.currentMessage?.originalPnMessage.reactions) { return null