Skip to content

Commit

Permalink
Merge branch 'test-implementation-channels-relevant-to-user-csk-164' of
Browse files Browse the repository at this point in the history
https://github.com/pubnub/js-chat into test-implementation-channels-relevant-to-user-csk-164
  • Loading branch information
anton-pub committed Oct 1, 2023
2 parents 9525eaa + a2f21ed commit cdd8b34
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 11 deletions.
24 changes: 18 additions & 6 deletions lib/src/entities/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,23 +433,35 @@ 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"
}

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),
])
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/src/entities/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
5 changes: 1 addition & 4 deletions lib/src/entities/thread-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cdd8b34

Please sign in to comment.