stream-chat 8.25.0 → 8.26.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,20 @@ 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,
176
196
  } from './types';
177
197
  import { InsightMetrics, postInsights } from './insights';
178
198
  import { Thread } from './thread';
@@ -1261,19 +1281,6 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
1261
1281
  }
1262
1282
 
1263
1283
  if (event.type === 'notification.channel_mutes_updated' && event.me?.channel_mutes) {
1264
- const currentMutedChannelIds: string[] = [];
1265
- const nextMutedChannelIds: string[] = [];
1266
-
1267
- this.mutedChannels.forEach((mute) => mute.channel && currentMutedChannelIds.push(mute.channel.cid));
1268
- event.me.channel_mutes.forEach((mute) => mute.channel && nextMutedChannelIds.push(mute.channel.cid));
1269
-
1270
- /** Set the unread count of un-muted channels to 0, which is the behaviour of backend */
1271
- currentMutedChannelIds.forEach((cid) => {
1272
- if (!nextMutedChannelIds.includes(cid) && this.activeChannels[cid]) {
1273
- this.activeChannels[cid].state.unreadCount = 0;
1274
- }
1275
- });
1276
-
1277
1284
  this.mutedChannels = event.me.channel_mutes;
1278
1285
  }
1279
1286
 
@@ -3419,4 +3426,190 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
3419
3426
  async commitMessage(id: string) {
3420
3427
  return await this.post<APIResponse & MessageResponse>(this.baseURL + `/messages/${id}/commit`);
3421
3428
  }
3429
+
3430
+ /**
3431
+ * Creates a poll
3432
+ * @param params PollData The poll that will be created
3433
+ * @returns {APIResponse & CreatePollAPIResponse} The poll
3434
+ */
3435
+ async createPoll(poll: PollData) {
3436
+ return await this.post<APIResponse & CreatePollAPIResponse>(this.baseURL + `/polls`, poll);
3437
+ }
3438
+
3439
+ /**
3440
+ * Retrieves a poll
3441
+ * @param id string The poll id
3442
+ * @returns {APIResponse & GetPollAPIResponse} The poll
3443
+ */
3444
+ async getPoll(id: string, userId?: string): Promise<APIResponse & GetPollAPIResponse> {
3445
+ return await this.get<APIResponse & GetPollAPIResponse>(this.baseURL + `/polls/${id}`, {
3446
+ ...(userId ? { user_id: userId } : {}),
3447
+ });
3448
+ }
3449
+
3450
+ /**
3451
+ * Updates a poll
3452
+ * @param poll PollData The poll that will be updated
3453
+ * @returns {APIResponse & PollResponse} The poll
3454
+ */
3455
+ async updatePoll(poll: PollData) {
3456
+ return await this.put<APIResponse & UpdatePollAPIResponse>(this.baseURL + `/polls`, poll);
3457
+ }
3458
+
3459
+ /**
3460
+ * Partially updates a poll
3461
+ * @param id string The poll id
3462
+ * @param {PartialPollUpdate<StreamChatGenerics>} partialPollObject which should contain id and any of "set" or "unset" params;
3463
+ * example: {id: "44f26af5-f2be-4fa7-9dac-71cf893781de", set:{field: value}, unset:["field2"]}
3464
+ * @returns {APIResponse & UpdatePollAPIResponse} The poll
3465
+ */
3466
+ async partialUpdatePoll(
3467
+ id: string,
3468
+ partialPollObject: PartialPollUpdate,
3469
+ ): Promise<APIResponse & UpdatePollAPIResponse> {
3470
+ return await this.patch<APIResponse & UpdatePollAPIResponse>(this.baseURL + `/polls/${id}`, partialPollObject);
3471
+ }
3472
+
3473
+ /**
3474
+ * Delete a poll
3475
+ * @param id string The poll id
3476
+ * @param userId string The user id (only serverside)
3477
+ * @returns
3478
+ */
3479
+ async deletePoll(id: string, userId?: string): Promise<APIResponse> {
3480
+ return await this.delete<APIResponse>(this.baseURL + `/polls/${id}`, {
3481
+ ...(userId ? { user_id: userId } : {}),
3482
+ });
3483
+ }
3484
+
3485
+ /**
3486
+ * Close a poll
3487
+ * @param id string The poll id
3488
+ * @returns {APIResponse & UpdatePollAPIResponse} The poll
3489
+ */
3490
+ async closePoll(id: string): Promise<APIResponse & UpdatePollAPIResponse> {
3491
+ return this.partialUpdatePoll(id, {
3492
+ set: {
3493
+ is_closed: true,
3494
+ },
3495
+ });
3496
+ }
3497
+
3498
+ /**
3499
+ * Creates a poll option
3500
+ * @param pollId string The poll id
3501
+ * @param option PollOptionData The poll option that will be created
3502
+ * @returns {APIResponse & PollOptionResponse} The poll option
3503
+ */
3504
+ async createPollOption(pollId: string, option: PollOptionData) {
3505
+ return await this.post<APIResponse & CreatePollOptionAPIResponse>(
3506
+ this.baseURL + `/polls/${pollId}/options`,
3507
+ option,
3508
+ );
3509
+ }
3510
+
3511
+ /**
3512
+ * Retrieves a poll option
3513
+ * @param pollId string The poll id
3514
+ * @param optionId string The poll option id
3515
+ * @returns {APIResponse & PollOptionResponse} The poll option
3516
+ */
3517
+ async getPollOption(pollId: string, optionId: string) {
3518
+ return await this.get<APIResponse & GetPollOptionAPIResponse>(
3519
+ this.baseURL + `/polls/${pollId}/options/${optionId}`,
3520
+ );
3521
+ }
3522
+
3523
+ /**
3524
+ * Updates a poll option
3525
+ * @param pollId string The poll id
3526
+ * @param option PollOptionData The poll option that will be updated
3527
+ * @returns
3528
+ */
3529
+ async updatePollOption(pollId: string, option: PollOptionData) {
3530
+ return await this.put<APIResponse & UpdatePollOptionAPIResponse>(this.baseURL + `/polls/${pollId}/options`, option);
3531
+ }
3532
+
3533
+ /**
3534
+ * Delete a poll option
3535
+ * @param pollId string The poll id
3536
+ * @param optionId string The poll option id
3537
+ * @returns {APIResponse} The poll option
3538
+ */
3539
+ async deletePollOption(pollId: string, optionId: string) {
3540
+ return await this.delete<APIResponse>(this.baseURL + `/polls/${pollId}/options/${optionId}`);
3541
+ }
3542
+
3543
+ /**
3544
+ * Cast vote on a poll
3545
+ * @param messageId string The message id
3546
+ * @param pollId string The poll id
3547
+ * @param vote PollVoteData The vote that will be casted
3548
+ * @returns {APIResponse & CastVoteAPIResponse} The poll vote
3549
+ */
3550
+ async castPollVote(messageId: string, pollId: string, vote: PollVoteData, options = {}) {
3551
+ return await this.post<APIResponse & CastVoteAPIResponse>(
3552
+ this.baseURL + `/messages/${messageId}/polls/${pollId}/vote`,
3553
+ { vote, ...options },
3554
+ );
3555
+ }
3556
+
3557
+ /**
3558
+ * Add a poll answer
3559
+ * @param messageId string The message id
3560
+ * @param pollId string The poll id
3561
+ * @param answerText string The answer text
3562
+ */
3563
+ async addPollAnswer(messageId: string, pollId: string, answerText: string) {
3564
+ return this.castPollVote(messageId, pollId, {
3565
+ answer_text: answerText,
3566
+ });
3567
+ }
3568
+
3569
+ async removePollVote(messageId: string, pollId: string, voteId: string) {
3570
+ return await this.delete<APIResponse & { vote: PollVote }>(
3571
+ this.baseURL + `/messages/${messageId}/polls/${pollId}/vote/${voteId}`,
3572
+ );
3573
+ }
3574
+
3575
+ /**
3576
+ * Queries polls
3577
+ * @param filter
3578
+ * @param sort
3579
+ * @param options Option object, {limit: 10, offset:0}
3580
+ * @returns {APIResponse & QueryPollsResponse} The polls
3581
+ */
3582
+ async queryPolls(
3583
+ filter: QueryPollsFilters = {},
3584
+ sort: PollSort = [],
3585
+ options: QueryPollsOptions = {},
3586
+ ): Promise<APIResponse & QueryPollsResponse> {
3587
+ return await this.post<APIResponse & QueryPollsResponse>(this.baseURL + '/polls/query', {
3588
+ filter,
3589
+ sort: normalizeQuerySort(sort),
3590
+ ...options,
3591
+ });
3592
+ }
3593
+
3594
+ /**
3595
+ * Queries poll votes
3596
+ * @param pollId
3597
+ * @param filter
3598
+ * @param sort
3599
+ * @param options Option object, {limit: 10, offset:0}
3600
+
3601
+ * @returns {APIResponse & PollVotesAPIResponse} The poll votes
3602
+ */
3603
+ async queryPollVotes(
3604
+ pollId: string,
3605
+ filter: QueryVotesFilters = {},
3606
+ sort: VoteSort = [],
3607
+ options: QueryVotesOptions = {},
3608
+ ): Promise<APIResponse & PollVotesAPIResponse> {
3609
+ return await this.post<APIResponse & PollVotesAPIResponse>(this.baseURL + `/polls/${pollId}/votes`, {
3610
+ filter,
3611
+ sort: normalizeQuerySort(sort),
3612
+ ...options,
3613
+ });
3614
+ }
3422
3615
  }
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> {