Skip to content

Commit

Permalink
feat: added reaction endpoint to thread
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalnarkhede committed Jan 22, 2024
1 parent d0a6309 commit c5aa578
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2591,8 +2591,8 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
watch?: boolean;
}) {
const opts = {
limit: 2,
participant_limit: 100,
limit: 10,
participant_limit: 10,
reply_limit: 3,
watch: true,
...(options || {}),
Expand Down
36 changes: 35 additions & 1 deletion src/thread.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
ThreadResponse,
ChannelResponse,
FormatMessageResponse,
ReactionResponse,
} from './types';
import { addToMessageList, formatMessage } from './utils';

Expand All @@ -15,17 +16,20 @@ export class Thread<StreamChatGenerics extends ExtendableGenerics = DefaultGener
participants: ThreadResponse['thread_participants'] = [];
message: FormatMessageResponse<StreamChatGenerics>;
channel: ChannelResponse<StreamChatGenerics>;
_channel: ReturnType<StreamChat<StreamChatGenerics>['channel']>;
replyCount = 0;
_client: StreamChat<StreamChatGenerics>;

read: ThreadResponse['read'] = [];
constructor(client: StreamChat<StreamChatGenerics>, t: ThreadResponse<StreamChatGenerics>) {
this.id = t.parent_message.id;
this.message = formatMessage(t.parent_message);
this.latestReplies = t.latest_replies.map(formatMessage);
this.participants = t.thread_participants;
this.replyCount = t.reply_count;
this.channel = t.channel;
this._channel = client.channel(t.channel.type, t.channel.id);
this._client = client;
this.read = t.read;
}

getClient(): StreamChat<StreamChatGenerics> {
Expand Down Expand Up @@ -62,4 +66,34 @@ export class Thread<StreamChatGenerics extends ExtendableGenerics = DefaultGener
this.message = formatMessage(message);
}
}

addReaction(
reaction: ReactionResponse<StreamChatGenerics>,
message?: MessageResponse<StreamChatGenerics>,
enforce_unique?: boolean,
) {
if (!message) return;

this.latestReplies = this.latestReplies.map((m) => {
if (m.id === message.id) {
return formatMessage(
this._channel.state.addReaction(reaction, message, enforce_unique) as MessageResponse<StreamChatGenerics>,
);
}
return m;
});
}

removeReaction(reaction: ReactionResponse<StreamChatGenerics>, message?: MessageResponse<StreamChatGenerics>) {
if (!message) return;

this.latestReplies = this.latestReplies.map((m) => {
if (m.id === message.id) {
return formatMessage(
this._channel.state.removeReaction(reaction, message) as MessageResponse<StreamChatGenerics>,
);
}
return m;
});
}
}
14 changes: 14 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,12 @@ export type ThreadResponse<StreamChatGenerics extends ExtendableGenerics = Defau
latest_replies: MessageResponse<StreamChatGenerics>[];
parent_message: MessageResponse<StreamChatGenerics>;
parent_message_id: string;
read: {
last_read: string;
last_read_message_id: string;
unread_messages: number;
user: UserResponse<StreamChatGenerics>;
}[];
reply_count: number;
thread_participants: {
created_at: string;
Expand Down Expand Up @@ -536,7 +542,14 @@ export type GetUnreadCountAPIResponse = APIResponse & {
last_read: string;
unread_count: number;
}[];
threads: {
last_read: string;
last_read_message_id: string;
parent_message_id: string;
unread_count: number;
}[];
total_unread_count: number;
total_unread_threads_count: number;
};

export type ListChannelResponse<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = APIResponse & {
Expand Down Expand Up @@ -902,6 +915,7 @@ export type MarkChannelsReadOptions<StreamChatGenerics extends ExtendableGeneric
export type MarkReadOptions<StreamChatGenerics extends ExtendableGenerics = DefaultGenerics> = {
client_id?: string;
connection_id?: string;
thread_id?: string;
user?: UserResponse<StreamChatGenerics>;
user_id?: string;
};
Expand Down

0 comments on commit c5aa578

Please sign in to comment.