Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lib): add removing threads #118

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/src/entities/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ export class Chat {
})
return ThreadChannel.fromDTO(this, {
...response.data,
parentMessage: message,
parentChannelId: message.channelId,
})
} catch (error) {
Expand Down Expand Up @@ -381,6 +382,7 @@ export class Chat {
const data = await Promise.all([
ThreadChannel.fromDTO(this, {
...response.data,
parentMessage: message,
parentChannelId: message.channelId,
}),
this.sdk.addMessageAction({
Expand All @@ -400,6 +402,26 @@ export class Chat {
}
}

/** @internal */
async removeThreadChannel(message: Message) {
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),
})
}

/**
* Channels
*/
Expand Down Expand Up @@ -885,6 +907,7 @@ export class Chat {
threadChannel: ThreadChannel.fromDTO(this, {
id: (channel as Channel).id,
parentChannelId: parentChannel.id,
parentMessage: message,
}),
message: message as Message,
user: user as User,
Expand Down
4 changes: 4 additions & 0 deletions lib/src/entities/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ export class Message {
return this.chat.createThreadChannel(this)
}

removeThread() {
return this.chat.removeThreadChannel(this)
}

/** @internal */
private async deleteThread(params: DeleteParameters = {}) {
if (this.hasThread) {
Expand Down
24 changes: 22 additions & 2 deletions lib/src/entities/thread-channel.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
import { ObjectCustom, SetChannelMetadataResponse } from "pubnub"
import { Channel, ChannelFields } from "./channel"
import { Chat } from "./chat"
import { ThreadChannelDTOParams } from "../types"
import { DeleteParameters, ThreadChannelDTOParams } from "../types"
import { ThreadMessage } from "./thread-message"
import { getErrorProxiedEntity } from "../error-logging"
import { MESSAGE_THREAD_ID_PREFIX } from "../constants"
import { Message } from "./message"

export class ThreadChannel extends Channel {
readonly parentChannelId: string
/** @internal */
readonly parentMessage: Message

/** @internal */
constructor(chat: Chat, params: ChannelFields & { parentChannelId: string }) {
constructor(
chat: Chat,
params: ChannelFields & { parentChannelId: string; parentMessage: Message }
) {
super(chat, params)
this.parentChannelId = params.parentChannelId
this.parentMessage = params.parentMessage
}

/** @internal */
static override fromDTO(chat: Chat, params: ThreadChannelDTOParams) {
const data = {
id: params.id,
parentChannelId: params.parentChannelId,
parentMessage: params.parentMessage,
name: params.name || undefined,
custom: params.custom || undefined,
description: params.description || undefined,
Expand All @@ -37,6 +46,7 @@ export class ThreadChannel extends Channel {
)) as SetChannelMetadataResponse<ObjectCustom>
return ThreadChannel.fromDTO(this.chat, {
...response.data,
parentMessage: this.parentMessage,
parentChannelId: this.parentChannelId,
})
}
Expand All @@ -45,6 +55,7 @@ export class ThreadChannel extends Channel {
const response = await this.chat.pinMessageToChannel(null, this)
return ThreadChannel.fromDTO(this.chat, {
...response.data,
parentMessage: this.parentMessage,
parentChannelId: this.parentChannelId,
})
}
Expand Down Expand Up @@ -89,6 +100,15 @@ export class ThreadChannel extends Channel {
}
}

override async delete(options: DeleteParameters = {}) {
const data = await Promise.all([
this.chat.removeThreadChannel(this.parentMessage),
super.delete(options),
])

return data[1]
}

/** @internal */
protected emitUserMention({
userId,
Expand Down
5 changes: 4 additions & 1 deletion lib/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export type ChannelDTOParams = OptionalAllBut<ChannelMetadataObject<ObjectCustom
type?: ChannelType
}

export type ThreadChannelDTOParams = ChannelDTOParams & { parentChannelId: string }
export type ThreadChannelDTOParams = ChannelDTOParams & {
parentChannelId: string
parentMessage: Message
}

export type MessageDraftConfig = {
userSuggestionSource: "channel" | "global"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ <h1>Message List</h1>
<div *ngIf="message.hasThread">
<span>Thread pinned message: </span>
<span>{{ pinnedMessages[threadMessages[message.timetoken][0].channelId] }}</span>
<button (click)="removeThread(message)">Remove this thread</button>
</div>
<div *ngFor="let threadMessage of threadMessages[message.timetoken]">
<div class="message-row">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ export class MessageListComponentChat {
}

const thread = await message.getThread()

const threadMessages = await thread!.getHistory()
await this.getPinnedMessage(thread!)
this.threadMessages[message.timetoken] = threadMessages.messages
Expand Down Expand Up @@ -170,4 +171,8 @@ export class MessageListComponentChat {

this.threadInputOpen = message.timetoken
}

async removeThread(message: Message) {
message.removeThread()
}
}
Loading