@stream-io/feeds-client 0.2.9 → 0.2.10

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.
Files changed (87) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/dist/cjs/index.js +2 -1
  3. package/dist/cjs/index.js.map +1 -1
  4. package/dist/cjs/react-bindings.js +1 -1
  5. package/dist/es/index.mjs +3 -2
  6. package/dist/es/react-bindings.mjs +1 -1
  7. package/dist/{index-B0Mm2xFU.js → index-C49kZoN7.js} +273 -51
  8. package/dist/index-C49kZoN7.js.map +1 -0
  9. package/dist/{index-rSXIDTdA.mjs → index-EeFSq3sq.mjs} +273 -51
  10. package/dist/index-EeFSq3sq.mjs.map +1 -0
  11. package/dist/tsconfig.tsbuildinfo +1 -1
  12. package/dist/types/feed/event-handlers/activity/handle-activity-reaction-added.d.ts +4 -5
  13. package/dist/types/feed/event-handlers/activity/handle-activity-reaction-added.d.ts.map +1 -1
  14. package/dist/types/feed/event-handlers/activity/handle-activity-reaction-deleted.d.ts +1 -2
  15. package/dist/types/feed/event-handlers/activity/handle-activity-reaction-deleted.d.ts.map +1 -1
  16. package/dist/types/feed/event-handlers/activity/handle-activity-updated.d.ts +6 -5
  17. package/dist/types/feed/event-handlers/activity/handle-activity-updated.d.ts.map +1 -1
  18. package/dist/types/feed/event-handlers/comment/handle-comment-added.d.ts +4 -3
  19. package/dist/types/feed/event-handlers/comment/handle-comment-added.d.ts.map +1 -1
  20. package/dist/types/feed/event-handlers/comment/handle-comment-deleted.d.ts +4 -3
  21. package/dist/types/feed/event-handlers/comment/handle-comment-deleted.d.ts.map +1 -1
  22. package/dist/types/feed/event-handlers/comment/handle-comment-reaction-added.d.ts +5 -0
  23. package/dist/types/feed/event-handlers/comment/handle-comment-reaction-added.d.ts.map +1 -0
  24. package/dist/types/feed/event-handlers/comment/handle-comment-reaction-deleted.d.ts +5 -0
  25. package/dist/types/feed/event-handlers/comment/handle-comment-reaction-deleted.d.ts.map +1 -0
  26. package/dist/types/feed/event-handlers/comment/handle-comment-updated.d.ts +4 -3
  27. package/dist/types/feed/event-handlers/comment/handle-comment-updated.d.ts.map +1 -1
  28. package/dist/types/feed/event-handlers/comment/index.d.ts +2 -1
  29. package/dist/types/feed/event-handlers/comment/index.d.ts.map +1 -1
  30. package/dist/types/feed/event-handlers/comment/utils/index.d.ts +2 -0
  31. package/dist/types/feed/event-handlers/comment/utils/index.d.ts.map +1 -0
  32. package/dist/types/feed/event-handlers/comment/utils/update-comment-count.d.ts +8 -0
  33. package/dist/types/feed/event-handlers/comment/utils/update-comment-count.d.ts.map +1 -0
  34. package/dist/types/feed/feed.d.ts.map +1 -1
  35. package/dist/types/feeds-client/feeds-client.d.ts +19 -1
  36. package/dist/types/feeds-client/feeds-client.d.ts.map +1 -1
  37. package/dist/types/types-internal.d.ts +4 -2
  38. package/dist/types/types-internal.d.ts.map +1 -1
  39. package/dist/types/utils/ensure-exhausted.d.ts +2 -0
  40. package/dist/types/utils/ensure-exhausted.d.ts.map +1 -0
  41. package/dist/types/utils/event-triggered-by-connected-user.d.ts +6 -0
  42. package/dist/types/utils/event-triggered-by-connected-user.d.ts.map +1 -0
  43. package/dist/types/utils/index.d.ts +1 -0
  44. package/dist/types/utils/index.d.ts.map +1 -1
  45. package/dist/types/utils/logger.d.ts +10 -1
  46. package/dist/types/utils/logger.d.ts.map +1 -1
  47. package/dist/types/utils/state-update-queue.d.ts +22 -2
  48. package/dist/types/utils/state-update-queue.d.ts.map +1 -1
  49. package/dist/types/utils/type-assertions.d.ts +2 -5
  50. package/dist/types/utils/type-assertions.d.ts.map +1 -1
  51. package/package.json +1 -1
  52. package/src/feed/event-handlers/activity/handle-activity-reaction-added.ts +15 -10
  53. package/src/feed/event-handlers/activity/handle-activity-reaction-deleted.test.ts +1 -1
  54. package/src/feed/event-handlers/activity/handle-activity-reaction-deleted.ts +1 -1
  55. package/src/feed/event-handlers/activity/handle-activity-updated.test.ts +131 -1
  56. package/src/feed/event-handlers/activity/handle-activity-updated.ts +38 -15
  57. package/src/feed/event-handlers/comment/handle-comment-added.test.ts +131 -7
  58. package/src/feed/event-handlers/comment/handle-comment-added.ts +24 -4
  59. package/src/feed/event-handlers/comment/handle-comment-deleted.test.ts +124 -2
  60. package/src/feed/event-handlers/comment/handle-comment-deleted.ts +29 -3
  61. package/src/feed/event-handlers/comment/{handle-comment-reaction.test.ts → handle-comment-reaction-added.test.ts} +152 -138
  62. package/src/feed/event-handlers/comment/handle-comment-reaction-added.ts +72 -0
  63. package/src/feed/event-handlers/comment/handle-comment-reaction-deleted.test.ts +343 -0
  64. package/src/feed/event-handlers/comment/handle-comment-reaction-deleted.ts +74 -0
  65. package/src/feed/event-handlers/comment/handle-comment-updated.test.ts +137 -1
  66. package/src/feed/event-handlers/comment/handle-comment-updated.ts +29 -4
  67. package/src/feed/event-handlers/comment/index.ts +3 -1
  68. package/src/feed/event-handlers/comment/utils/index.ts +1 -0
  69. package/src/feed/event-handlers/comment/utils/update-comment-count.test.ts +320 -0
  70. package/src/feed/event-handlers/comment/utils/update-comment-count.ts +51 -0
  71. package/src/feed/event-handlers/follow/handle-follow-deleted.ts +1 -1
  72. package/src/feed/feed.ts +4 -3
  73. package/src/feeds-client/feeds-client.ts +104 -0
  74. package/src/test-utils/response-generators.ts +18 -1
  75. package/src/types-internal.ts +4 -4
  76. package/src/utils/ensure-exhausted.ts +5 -0
  77. package/src/utils/event-triggered-by-connected-user.test.ts +73 -0
  78. package/src/utils/event-triggered-by-connected-user.ts +15 -0
  79. package/src/utils/index.ts +2 -1
  80. package/src/utils/logger.ts +2 -1
  81. package/src/utils/state-update-queue.ts +89 -25
  82. package/src/utils/type-assertions.ts +2 -3
  83. package/dist/index-B0Mm2xFU.js.map +0 -1
  84. package/dist/index-rSXIDTdA.mjs.map +0 -1
  85. package/dist/types/feed/event-handlers/comment/handle-comment-reaction.d.ts +0 -4
  86. package/dist/types/feed/event-handlers/comment/handle-comment-reaction.d.ts.map +0 -1
  87. package/src/feed/event-handlers/comment/handle-comment-reaction.ts +0 -61
@@ -1,18 +1,23 @@
1
1
  import { describe, it, expect, beforeEach } from 'vitest';
2
2
  import { Feed } from '../../../feed';
3
3
  import { FeedsClient } from '../../../feeds-client';
4
- import { handleCommentReaction } from './handle-comment-reaction';
4
+ import { handleCommentReactionAdded } from './handle-comment-reaction-added';
5
5
  import {
6
6
  generateCommentResponse,
7
- generateFeedReactionResponse,
8
7
  generateFeedResponse,
9
8
  generateOwnUser,
10
9
  getHumanId,
11
10
  generateCommentReactionAddedEvent,
12
- generateCommentReactionDeletedEvent,
13
- } from '../../../test-utils/response-generators';
11
+ generateFeedReactionResponse,
12
+ } from '../../../test-utils';
13
+ import {
14
+ CommentResponse,
15
+ FeedsReactionResponse,
16
+ } from '../../../gen/models';
17
+ import { shouldUpdateState } from '../../../utils'
18
+ import { EventPayload } from '../../../types-internal';
14
19
 
15
- describe(handleCommentReaction.name, () => {
20
+ describe(handleCommentReactionAdded.name, () => {
16
21
  let feed: Feed;
17
22
  let client: FeedsClient;
18
23
  let currentUserId: string;
@@ -75,7 +80,7 @@ describe(handleCommentReaction.name, () => {
75
80
  ?.own_reactions,
76
81
  ).toHaveLength(0);
77
82
 
78
- handleCommentReaction.call(feed, event);
83
+ handleCommentReactionAdded.call(feed, event);
79
84
 
80
85
  const stateAfter = feed.currentState;
81
86
  const [updated] = stateAfter.comments_by_entity_id[activityId]!.comments!;
@@ -117,7 +122,7 @@ describe(handleCommentReaction.name, () => {
117
122
  },
118
123
  });
119
124
 
120
- handleCommentReaction.call(feed, event);
125
+ handleCommentReactionAdded.call(feed, event);
121
126
  const stateAfter = feed.currentState;
122
127
  const [updated] = stateAfter.comments_by_entity_id[activityId]!.comments!;
123
128
  expect(updated.own_reactions).toHaveLength(0);
@@ -125,101 +130,6 @@ describe(handleCommentReaction.name, () => {
125
130
  expect(updated.reaction_groups).toBe(event.comment.reaction_groups);
126
131
  });
127
132
 
128
- it('removes from own_reactions when the target reaction belongs to the current user', () => {
129
- const commentId = `comment-${getHumanId()}`;
130
- const existingReaction = generateFeedReactionResponse({
131
- type: 'heart',
132
- user: { id: currentUserId },
133
- activity_id: activityId,
134
- comment_id: commentId,
135
- });
136
- const existingComment = generateCommentResponse({
137
- id: commentId,
138
- object_id: activityId,
139
- own_reactions: [existingReaction],
140
- latest_reactions: [],
141
- reaction_groups: {},
142
- });
143
- feed.state.partialNext({
144
- comments_by_entity_id: {
145
- [activityId]: {
146
- comments: [existingComment],
147
- pagination: { sort: 'first' },
148
- },
149
- },
150
- });
151
-
152
- const event = generateCommentReactionDeletedEvent({
153
- comment: {
154
- ...existingComment,
155
- latest_reactions: [],
156
- reaction_groups: {},
157
- },
158
- reaction: {
159
- type: 'heart',
160
- user: { id: currentUserId },
161
- activity_id: activityId,
162
- },
163
- });
164
-
165
- const stateBefore = feed.currentState;
166
- expect(
167
- stateBefore.comments_by_entity_id[activityId]!.comments![0].own_reactions,
168
- ).toHaveLength(1);
169
- handleCommentReaction.call(feed, event);
170
- const stateAfter = feed.currentState;
171
- const [updated] = stateAfter.comments_by_entity_id[activityId]!.comments!;
172
- expect(updated.own_reactions).toHaveLength(0);
173
- expect(updated.latest_reactions).toBe(event.comment.latest_reactions);
174
- expect(updated.reaction_groups).toBe(event.comment.reaction_groups);
175
- });
176
-
177
- it('does not remove from own_reactions when target reaction does not belong to the current user', () => {
178
- const commentId = `comment-${getHumanId()}`;
179
- const ownReaction = generateFeedReactionResponse({
180
- type: 'wow',
181
- user: { id: currentUserId },
182
- activity_id: activityId,
183
- comment_id: commentId,
184
- });
185
- const existingComment = generateCommentResponse({
186
- id: commentId,
187
- object_id: activityId,
188
- own_reactions: [ownReaction],
189
- latest_reactions: [],
190
- reaction_groups: {},
191
- });
192
- feed.state.partialNext({
193
- comments_by_entity_id: {
194
- [activityId]: {
195
- comments: [existingComment],
196
- pagination: { sort: 'first' },
197
- },
198
- },
199
- });
200
-
201
- const event = generateCommentReactionDeletedEvent({
202
- comment: {
203
- ...existingComment,
204
- latest_reactions: [],
205
- reaction_groups: {},
206
- },
207
- reaction: {
208
- type: 'wow',
209
- user: { id: 'other-user' },
210
- comment_id: existingComment.id,
211
- },
212
- });
213
-
214
- handleCommentReaction.call(feed, event);
215
- const stateAfter = feed.currentState;
216
- const [updated] = stateAfter.comments_by_entity_id[activityId]!.comments!;
217
- expect(updated.own_reactions).toHaveLength(1);
218
- expect(updated.own_reactions[0]).toBe(ownReaction);
219
- expect(updated.latest_reactions).toBe(event.comment.latest_reactions);
220
- expect(updated.reaction_groups).toBe(event.comment.reaction_groups);
221
- });
222
-
223
133
  it('does changes to the proper entity state (prefers parent_id)', () => {
224
134
  const parentId = `comment-${getHumanId()}`;
225
135
  const existingComment = generateCommentResponse({
@@ -254,43 +164,18 @@ describe(handleCommentReaction.name, () => {
254
164
  },
255
165
  });
256
166
 
257
- const deletedEvent = generateCommentReactionDeletedEvent({
258
- comment: {
259
- id: existingComment.id,
260
- object_id: activityId,
261
- parent_id: parentId,
262
- latest_reactions: [],
263
- reaction_groups: {},
264
- },
265
- reaction: {
266
- type: 'like',
267
- user: { id: currentUserId },
268
- activity_id: activityId,
269
- comment_id: existingComment.id,
270
- },
271
- });
272
-
273
167
  const stateBefore = feed.currentState;
274
168
  expect(
275
169
  stateBefore.comments_by_entity_id[parentId]?.comments?.[0]?.own_reactions,
276
170
  ).toHaveLength(0);
277
171
 
278
- handleCommentReaction.call(feed, addedEvent);
172
+ handleCommentReactionAdded.call(feed, addedEvent);
279
173
  const stateAfter1 = feed.currentState;
280
174
  const [updated1] = stateAfter1.comments_by_entity_id[parentId]!.comments!;
281
175
  expect(updated1.own_reactions).toHaveLength(1);
282
176
  expect(updated1.own_reactions[0]).toBe(addedEvent.reaction);
283
177
  expect(updated1.latest_reactions).toBe(addedEvent.comment.latest_reactions);
284
178
  expect(updated1.reaction_groups).toBe(addedEvent.comment.reaction_groups);
285
-
286
- handleCommentReaction.call(feed, deletedEvent);
287
- const stateAfter2 = feed.currentState;
288
- const [updated2] = stateAfter2.comments_by_entity_id[parentId]!.comments!;
289
- expect(updated2.own_reactions).toHaveLength(0);
290
- expect(updated2.latest_reactions).toBe(
291
- deletedEvent.comment.latest_reactions,
292
- );
293
- expect(updated2.reaction_groups).toBe(deletedEvent.comment.reaction_groups);
294
179
  });
295
180
 
296
181
  it('does nothing if comment is not found in state', () => {
@@ -298,18 +183,147 @@ describe(handleCommentReaction.name, () => {
298
183
  comment: { object_id: activityId },
299
184
  reaction: { user: { id: currentUserId } },
300
185
  });
301
- const deletedEvent = generateCommentReactionDeletedEvent({
302
- comment: { object_id: activityId },
303
- reaction: { user: { id: currentUserId } },
304
- });
305
186
  const stateBefore = feed.currentState;
306
187
 
307
- handleCommentReaction.call(feed, addedEvent);
308
- const stateAfter1 = feed.currentState;
309
- expect(stateAfter1).toBe(stateBefore);
188
+ handleCommentReactionAdded.call(feed, addedEvent);
189
+ const stateAfter = feed.currentState;
190
+ expect(stateAfter).toBe(stateBefore);
191
+ });
192
+
193
+ describe(`Comment reaction added ${shouldUpdateState.name} integration`, () => {
194
+ let currentUserPayload: EventPayload<'feeds.comment.reaction.added'>;
195
+ let existingComment: CommentResponse;
196
+ let existingReaction: FeedsReactionResponse;
197
+ let newReaction: FeedsReactionResponse;
198
+ let commentId: string;
310
199
 
311
- handleCommentReaction.call(feed, deletedEvent);
312
- const stateAfter2 = feed.currentState;
313
- expect(stateAfter2).toBe(stateBefore);
200
+ beforeEach(() => {
201
+ commentId = `comment-${getHumanId()}`;
202
+ existingReaction = generateFeedReactionResponse({
203
+ type: 'heart',
204
+ user: { id: currentUserId },
205
+ activity_id: activityId,
206
+ comment_id: commentId,
207
+ });
208
+ newReaction = generateFeedReactionResponse({
209
+ type: 'like',
210
+ user: { id: currentUserId },
211
+ activity_id: activityId,
212
+ comment_id: commentId,
213
+ });
214
+ existingComment = generateCommentResponse({
215
+ id: commentId,
216
+ object_id: activityId,
217
+ own_reactions: [existingReaction],
218
+ });
219
+
220
+ currentUserPayload = generateCommentReactionAddedEvent({
221
+ comment: existingComment,
222
+ reaction: newReaction,
223
+ });
224
+
225
+ feed.state.partialNext({
226
+ comments_by_entity_id: {
227
+ [activityId]: {
228
+ comments: [existingComment],
229
+ pagination: { sort: 'first' },
230
+ },
231
+ },
232
+ });
233
+ feed.state.partialNext({ watch: true });
234
+ });
235
+
236
+ it(`skips update if ${shouldUpdateState.name} returns false`, () => {
237
+ // 1. HTTP and then WS
238
+
239
+ handleCommentReactionAdded.call(feed, currentUserPayload, false);
240
+
241
+ let stateBefore = feed.currentState;
242
+
243
+ handleCommentReactionAdded.call(feed, currentUserPayload);
244
+
245
+ let stateAfter = feed.currentState;
246
+
247
+ expect(stateAfter).toBe(stateBefore);
248
+ // @ts-expect-error Using Feed internals for tests only
249
+ expect(feed.stateUpdateQueue.size).toEqual(0);
250
+
251
+ // 2. WS and the HTTP
252
+
253
+ handleCommentReactionAdded.call(feed, currentUserPayload);
254
+
255
+ stateBefore = feed.currentState;
256
+
257
+ handleCommentReactionAdded.call(feed, currentUserPayload, false);
258
+
259
+ stateAfter = feed.currentState;
260
+
261
+ expect(stateAfter).toBe(stateBefore);
262
+ // @ts-expect-error Using Feed internals for tests only
263
+ expect(feed.stateUpdateQueue.size).toEqual(0);
264
+ });
265
+
266
+ it('allows update again from WS after clearing the stateUpdateQueue', () => {
267
+ handleCommentReactionAdded.call(feed, currentUserPayload);
268
+
269
+ // Clear the queue
270
+ (feed as any).stateUpdateQueue.clear();
271
+
272
+ // Now update should be allowed from another WS event
273
+ handleCommentReactionAdded.call(feed, currentUserPayload);
274
+
275
+ const comments =
276
+ feed.currentState.comments_by_entity_id[activityId]?.comments;
277
+ const comment = comments?.find((a) => a.id === commentId);
278
+ const [latestReaction] = (comment?.own_reactions ?? []).toReversed();
279
+
280
+ expect(comment?.own_reactions.length).toEqual(3);
281
+ expect(latestReaction).toMatchObject(newReaction);
282
+ });
283
+
284
+ it('allows update again from HTTP response after clearing the stateUpdateQueue', () => {
285
+ handleCommentReactionAdded.call(feed, currentUserPayload, false);
286
+
287
+ // Clear the queue
288
+ (feed as any).stateUpdateQueue.clear();
289
+
290
+ // Now update should be allowed from another HTTP response
291
+ handleCommentReactionAdded.call(feed, currentUserPayload, false);
292
+
293
+ const comments =
294
+ feed.currentState.comments_by_entity_id[activityId]?.comments;
295
+ const comment = comments?.find((a) => a.id === commentId);
296
+ const [latestReaction] = (comment?.own_reactions ?? []).toReversed();
297
+
298
+ expect(comment?.own_reactions.length).toEqual(3);
299
+ expect(latestReaction).toMatchObject(newReaction);
300
+ });
301
+
302
+ it('should not insert anything into the stateUpdateQueue if the connected_user did not trigger the comment reaction deletion', () => {
303
+ const otherUserPayload = generateCommentReactionAddedEvent({
304
+ comment: existingComment,
305
+ reaction: {
306
+ ...existingReaction,
307
+ user: {
308
+ id: getHumanId(),
309
+ },
310
+ },
311
+ });
312
+
313
+ handleCommentReactionAdded.call(feed, otherUserPayload);
314
+
315
+ expect((feed as any).stateUpdateQueue).toEqual(new Set());
316
+
317
+ handleCommentReactionAdded.call(feed, otherUserPayload);
318
+
319
+ const comments =
320
+ feed.currentState.comments_by_entity_id[activityId]?.comments;
321
+ const comment = comments?.find((a) => a.id === commentId);
322
+ const [latestReaction] = comment?.own_reactions ?? [];
323
+
324
+ expect((feed as any).stateUpdateQueue).toEqual(new Set());
325
+ expect(comment?.own_reactions.length).toEqual(1);
326
+ expect(latestReaction).toMatchObject(existingReaction);
327
+ });
314
328
  });
315
329
  });
@@ -0,0 +1,72 @@
1
+ import { Feed } from '../../feed';
2
+ import { EventPayload, type PartializeAllBut } from '../../../types-internal';
3
+ import { getStateUpdateQueueId, shouldUpdateState } from '../../../utils';
4
+
5
+ export type CommentReactionAddedPayload = PartializeAllBut<
6
+ EventPayload<'feeds.comment.reaction.added'>,
7
+ 'comment' | 'reaction'
8
+ >;
9
+
10
+ export function handleCommentReactionAdded(
11
+ this: Feed,
12
+ payload: CommentReactionAddedPayload,
13
+ fromWs?: boolean,
14
+ ) {
15
+ const { comment, reaction } = payload;
16
+ const connectedUser = this.client.state.getLatestValue().connected_user;
17
+
18
+ const isOwnReaction = reaction.user.id === connectedUser?.id;
19
+
20
+ if (
21
+ !shouldUpdateState({
22
+ stateUpdateQueueId: getStateUpdateQueueId(
23
+ payload,
24
+ 'comment-reaction-created',
25
+ ),
26
+ stateUpdateQueue: this.stateUpdateQueue,
27
+ watch: this.currentState.watch,
28
+ fromWs,
29
+ isTriggeredByConnectedUser: isOwnReaction,
30
+ })
31
+ ) {
32
+ return;
33
+ }
34
+
35
+ this.state.next((currentState) => {
36
+ const commentIndex = this.getCommentIndex(comment, currentState);
37
+
38
+ if (commentIndex === -1) return currentState;
39
+
40
+ const forId = comment.parent_id ?? comment.object_id;
41
+
42
+ const entityState = currentState.comments_by_entity_id[forId];
43
+
44
+ const newComments = entityState?.comments?.concat([]) ?? [];
45
+
46
+ let ownReactions = newComments[commentIndex].own_reactions;
47
+
48
+ if (isOwnReaction) {
49
+ ownReactions = ownReactions.concat(reaction) ?? [reaction];
50
+ }
51
+
52
+ newComments[commentIndex] = {
53
+ ...newComments[commentIndex],
54
+ reaction_count: comment.reaction_count ?? 0,
55
+ // TODO: FIXME this should be handled by the backend
56
+ latest_reactions: comment.latest_reactions ?? [],
57
+ reaction_groups: comment.reaction_groups ?? {},
58
+ own_reactions: ownReactions,
59
+ };
60
+
61
+ return {
62
+ ...currentState,
63
+ comments_by_entity_id: {
64
+ ...currentState.comments_by_entity_id,
65
+ [forId]: {
66
+ ...entityState,
67
+ comments: newComments,
68
+ },
69
+ },
70
+ };
71
+ });
72
+ }