stream-chat 8.25.1 → 8.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/client.ts CHANGED
@@ -117,9 +117,14 @@ import {
117
117
  OGAttachment,
118
118
  OwnUserResponse,
119
119
  PartialMessageUpdate,
120
+ PartialPollUpdate,
120
121
  PartialUserUpdate,
121
122
  PermissionAPIResponse,
122
123
  PermissionsAPIResponse,
124
+ PollData,
125
+ PollOptionData,
126
+ PollVoteData,
127
+ PollVotesAPIResponse,
123
128
  PushProvider,
124
129
  PushProviderConfig,
125
130
  PushProviderID,
@@ -127,6 +132,7 @@ import {
127
132
  PushProviderUpsertResponse,
128
133
  QueryChannelsAPIResponse,
129
134
  QuerySegmentsOptions,
135
+ QueryPollsResponse,
130
136
  ReactionResponse,
131
137
  ReactivateUserOptions,
132
138
  ReactivateUsersOptions,
@@ -173,6 +179,24 @@ import {
173
179
  QuerySegmentTargetsFilter,
174
180
  SortParam,
175
181
  GetMessageOptions,
182
+ QueryVotesFilters,
183
+ VoteSort,
184
+ CreatePollAPIResponse,
185
+ GetPollAPIResponse,
186
+ UpdatePollAPIResponse,
187
+ CreatePollOptionAPIResponse,
188
+ GetPollOptionAPIResponse,
189
+ UpdatePollOptionAPIResponse,
190
+ PollVote,
191
+ CastVoteAPIResponse,
192
+ QueryPollsFilters,
193
+ PollSort,
194
+ QueryPollsOptions,
195
+ QueryVotesOptions,
196
+ ReactionFilters,
197
+ ReactionSort,
198
+ QueryReactionsAPIResponse,
199
+ QueryReactionsOptions,
176
200
  } from './types';
177
201
  import { InsightMetrics, postInsights } from './insights';
178
202
  import { Thread } from './thread';
@@ -1570,6 +1594,37 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
1570
1594
  return this.hydrateActiveChannels(data.channels, stateOptions);
1571
1595
  }
1572
1596
 
1597
+ /**
1598
+ * queryReactions - Query reactions
1599
+ *
1600
+ * @param {ReactionFilters<StreamChatGenerics>} filter object MongoDB style filters
1601
+ * @param {ReactionSort<StreamChatGenerics>} [sort] Sort options, for instance {created_at: -1}.
1602
+ * @param {QueryReactionsOptions} [options] Pagination object
1603
+ *
1604
+ * @return {Promise<{ QueryReactionsAPIResponse } search channels response
1605
+ */
1606
+ async queryReactions(
1607
+ messageID: string,
1608
+ filter: ReactionFilters<StreamChatGenerics>,
1609
+ sort: ReactionSort<StreamChatGenerics> = [],
1610
+ options: QueryReactionsOptions = {},
1611
+ ) {
1612
+ // Make sure we wait for the connect promise if there is a pending one
1613
+ await this.wsPromise;
1614
+
1615
+ // Return a list of channels
1616
+ const payload = {
1617
+ filter,
1618
+ sort: normalizeQuerySort(sort),
1619
+ ...options,
1620
+ };
1621
+
1622
+ return await this.post<QueryReactionsAPIResponse<StreamChatGenerics>>(
1623
+ this.baseURL + '/messages/' + messageID + '/reactions',
1624
+ payload,
1625
+ );
1626
+ }
1627
+
1573
1628
  hydrateActiveChannels(
1574
1629
  channelsFromApi: ChannelAPIResponse<StreamChatGenerics>[] = [],
1575
1630
  stateOptions: ChannelStateOptions = {},
@@ -3406,4 +3461,190 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
3406
3461
  async commitMessage(id: string) {
3407
3462
  return await this.post<APIResponse & MessageResponse>(this.baseURL + `/messages/${id}/commit`);
3408
3463
  }
3464
+
3465
+ /**
3466
+ * Creates a poll
3467
+ * @param params PollData The poll that will be created
3468
+ * @returns {APIResponse & CreatePollAPIResponse} The poll
3469
+ */
3470
+ async createPoll(poll: PollData) {
3471
+ return await this.post<APIResponse & CreatePollAPIResponse>(this.baseURL + `/polls`, poll);
3472
+ }
3473
+
3474
+ /**
3475
+ * Retrieves a poll
3476
+ * @param id string The poll id
3477
+ * @returns {APIResponse & GetPollAPIResponse} The poll
3478
+ */
3479
+ async getPoll(id: string, userId?: string): Promise<APIResponse & GetPollAPIResponse> {
3480
+ return await this.get<APIResponse & GetPollAPIResponse>(this.baseURL + `/polls/${id}`, {
3481
+ ...(userId ? { user_id: userId } : {}),
3482
+ });
3483
+ }
3484
+
3485
+ /**
3486
+ * Updates a poll
3487
+ * @param poll PollData The poll that will be updated
3488
+ * @returns {APIResponse & PollResponse} The poll
3489
+ */
3490
+ async updatePoll(poll: PollData) {
3491
+ return await this.put<APIResponse & UpdatePollAPIResponse>(this.baseURL + `/polls`, poll);
3492
+ }
3493
+
3494
+ /**
3495
+ * Partially updates a poll
3496
+ * @param id string The poll id
3497
+ * @param {PartialPollUpdate<StreamChatGenerics>} partialPollObject which should contain id and any of "set" or "unset" params;
3498
+ * example: {id: "44f26af5-f2be-4fa7-9dac-71cf893781de", set:{field: value}, unset:["field2"]}
3499
+ * @returns {APIResponse & UpdatePollAPIResponse} The poll
3500
+ */
3501
+ async partialUpdatePoll(
3502
+ id: string,
3503
+ partialPollObject: PartialPollUpdate,
3504
+ ): Promise<APIResponse & UpdatePollAPIResponse> {
3505
+ return await this.patch<APIResponse & UpdatePollAPIResponse>(this.baseURL + `/polls/${id}`, partialPollObject);
3506
+ }
3507
+
3508
+ /**
3509
+ * Delete a poll
3510
+ * @param id string The poll id
3511
+ * @param userId string The user id (only serverside)
3512
+ * @returns
3513
+ */
3514
+ async deletePoll(id: string, userId?: string): Promise<APIResponse> {
3515
+ return await this.delete<APIResponse>(this.baseURL + `/polls/${id}`, {
3516
+ ...(userId ? { user_id: userId } : {}),
3517
+ });
3518
+ }
3519
+
3520
+ /**
3521
+ * Close a poll
3522
+ * @param id string The poll id
3523
+ * @returns {APIResponse & UpdatePollAPIResponse} The poll
3524
+ */
3525
+ async closePoll(id: string): Promise<APIResponse & UpdatePollAPIResponse> {
3526
+ return this.partialUpdatePoll(id, {
3527
+ set: {
3528
+ is_closed: true,
3529
+ },
3530
+ });
3531
+ }
3532
+
3533
+ /**
3534
+ * Creates a poll option
3535
+ * @param pollId string The poll id
3536
+ * @param option PollOptionData The poll option that will be created
3537
+ * @returns {APIResponse & PollOptionResponse} The poll option
3538
+ */
3539
+ async createPollOption(pollId: string, option: PollOptionData) {
3540
+ return await this.post<APIResponse & CreatePollOptionAPIResponse>(
3541
+ this.baseURL + `/polls/${pollId}/options`,
3542
+ option,
3543
+ );
3544
+ }
3545
+
3546
+ /**
3547
+ * Retrieves a poll option
3548
+ * @param pollId string The poll id
3549
+ * @param optionId string The poll option id
3550
+ * @returns {APIResponse & PollOptionResponse} The poll option
3551
+ */
3552
+ async getPollOption(pollId: string, optionId: string) {
3553
+ return await this.get<APIResponse & GetPollOptionAPIResponse>(
3554
+ this.baseURL + `/polls/${pollId}/options/${optionId}`,
3555
+ );
3556
+ }
3557
+
3558
+ /**
3559
+ * Updates a poll option
3560
+ * @param pollId string The poll id
3561
+ * @param option PollOptionData The poll option that will be updated
3562
+ * @returns
3563
+ */
3564
+ async updatePollOption(pollId: string, option: PollOptionData) {
3565
+ return await this.put<APIResponse & UpdatePollOptionAPIResponse>(this.baseURL + `/polls/${pollId}/options`, option);
3566
+ }
3567
+
3568
+ /**
3569
+ * Delete a poll option
3570
+ * @param pollId string The poll id
3571
+ * @param optionId string The poll option id
3572
+ * @returns {APIResponse} The poll option
3573
+ */
3574
+ async deletePollOption(pollId: string, optionId: string) {
3575
+ return await this.delete<APIResponse>(this.baseURL + `/polls/${pollId}/options/${optionId}`);
3576
+ }
3577
+
3578
+ /**
3579
+ * Cast vote on a poll
3580
+ * @param messageId string The message id
3581
+ * @param pollId string The poll id
3582
+ * @param vote PollVoteData The vote that will be casted
3583
+ * @returns {APIResponse & CastVoteAPIResponse} The poll vote
3584
+ */
3585
+ async castPollVote(messageId: string, pollId: string, vote: PollVoteData, options = {}) {
3586
+ return await this.post<APIResponse & CastVoteAPIResponse>(
3587
+ this.baseURL + `/messages/${messageId}/polls/${pollId}/vote`,
3588
+ { vote, ...options },
3589
+ );
3590
+ }
3591
+
3592
+ /**
3593
+ * Add a poll answer
3594
+ * @param messageId string The message id
3595
+ * @param pollId string The poll id
3596
+ * @param answerText string The answer text
3597
+ */
3598
+ async addPollAnswer(messageId: string, pollId: string, answerText: string) {
3599
+ return this.castPollVote(messageId, pollId, {
3600
+ answer_text: answerText,
3601
+ });
3602
+ }
3603
+
3604
+ async removePollVote(messageId: string, pollId: string, voteId: string) {
3605
+ return await this.delete<APIResponse & { vote: PollVote }>(
3606
+ this.baseURL + `/messages/${messageId}/polls/${pollId}/vote/${voteId}`,
3607
+ );
3608
+ }
3609
+
3610
+ /**
3611
+ * Queries polls
3612
+ * @param filter
3613
+ * @param sort
3614
+ * @param options Option object, {limit: 10, offset:0}
3615
+ * @returns {APIResponse & QueryPollsResponse} The polls
3616
+ */
3617
+ async queryPolls(
3618
+ filter: QueryPollsFilters = {},
3619
+ sort: PollSort = [],
3620
+ options: QueryPollsOptions = {},
3621
+ ): Promise<APIResponse & QueryPollsResponse> {
3622
+ return await this.post<APIResponse & QueryPollsResponse>(this.baseURL + '/polls/query', {
3623
+ filter,
3624
+ sort: normalizeQuerySort(sort),
3625
+ ...options,
3626
+ });
3627
+ }
3628
+
3629
+ /**
3630
+ * Queries poll votes
3631
+ * @param pollId
3632
+ * @param filter
3633
+ * @param sort
3634
+ * @param options Option object, {limit: 10, offset:0}
3635
+
3636
+ * @returns {APIResponse & PollVotesAPIResponse} The poll votes
3637
+ */
3638
+ async queryPollVotes(
3639
+ pollId: string,
3640
+ filter: QueryVotesFilters = {},
3641
+ sort: VoteSort = [],
3642
+ options: QueryVotesOptions = {},
3643
+ ): Promise<APIResponse & PollVotesAPIResponse> {
3644
+ return await this.post<APIResponse & PollVotesAPIResponse>(this.baseURL + `/polls/${pollId}/votes`, {
3645
+ filter,
3646
+ sort: normalizeQuerySort(sort),
3647
+ ...options,
3648
+ });
3649
+ }
3409
3650
  }
package/src/events.ts CHANGED
@@ -30,6 +30,11 @@ export const EVENT_MAP = {
30
30
  'notification.mutes_updated': true,
31
31
  'notification.removed_from_channel': true,
32
32
  'notification.thread_message_new': true,
33
+ 'poll.closed': true,
34
+ 'poll.updated': true,
35
+ 'poll.vote_casted': true,
36
+ 'poll.vote_changed': true,
37
+ 'poll.vote_removed': true,
33
38
  'reaction.deleted': true,
34
39
  'reaction.new': true,
35
40
  'reaction.updated': true,
package/src/thread.ts CHANGED
@@ -31,24 +31,38 @@ export class Thread<StreamChatGenerics extends ExtendableGenerics = DefaultGener
31
31
  replyCount = 0;
32
32
  _client: StreamChat<StreamChatGenerics>;
33
33
  read: ThreadReadStatus<StreamChatGenerics> = {};
34
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
35
+ data: Record<string, any> = {};
34
36
 
35
37
  constructor(client: StreamChat<StreamChatGenerics>, t: ThreadResponse<StreamChatGenerics>) {
36
- this.id = t.parent_message.id;
37
- this.message = formatMessage(t.parent_message);
38
- this.latestReplies = t.latest_replies.map(formatMessage);
39
- this.participants = t.thread_participants;
40
- this.replyCount = t.reply_count;
41
- this.channel = t.channel;
38
+ const {
39
+ parent_message_id,
40
+ parent_message,
41
+ latest_replies,
42
+ thread_participants,
43
+ reply_count,
44
+ channel,
45
+ read,
46
+ ...data
47
+ } = t;
48
+
49
+ this.id = parent_message_id;
50
+ this.message = formatMessage(parent_message);
51
+ this.latestReplies = latest_replies.map(formatMessage);
52
+ this.participants = thread_participants;
53
+ this.replyCount = reply_count;
54
+ this.channel = channel;
42
55
  this._channel = client.channel(t.channel.type, t.channel.id);
43
56
  this._client = client;
44
- if (t.read) {
45
- for (const r of t.read) {
57
+ if (read) {
58
+ for (const r of read) {
46
59
  this.read[r.user.id] = {
47
60
  ...r,
48
61
  last_read: new Date(r.last_read),
49
62
  };
50
63
  }
51
64
  }
65
+ this.data = data;
52
66
  }
53
67
 
54
68
  getClient(): StreamChat<StreamChatGenerics> {