@stream-io/feeds-client 0.2.15 → 0.2.17

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 (43) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/dist/cjs/index.js +1 -1
  3. package/dist/cjs/react-bindings.js +1 -1
  4. package/dist/es/index.mjs +2 -2
  5. package/dist/es/react-bindings.mjs +1 -1
  6. package/dist/{index-BSzSBlMh.mjs → index-BZL77zNq.mjs} +184 -27
  7. package/dist/index-BZL77zNq.mjs.map +1 -0
  8. package/dist/{index-DRX66SIx.js → index-nq6SDtbt.js} +184 -27
  9. package/dist/index-nq6SDtbt.js.map +1 -0
  10. package/dist/tsconfig.tsbuildinfo +1 -1
  11. package/dist/types/feed/event-handlers/activity/handle-activity-reaction-updated.d.ts +14 -0
  12. package/dist/types/feed/event-handlers/activity/handle-activity-reaction-updated.d.ts.map +1 -0
  13. package/dist/types/feed/event-handlers/activity/index.d.ts +1 -0
  14. package/dist/types/feed/event-handlers/activity/index.d.ts.map +1 -1
  15. package/dist/types/feed/event-handlers/comment/handle-comment-reaction-added.d.ts.map +1 -1
  16. package/dist/types/feed/event-handlers/comment/handle-comment-reaction-updated.d.ts +6 -0
  17. package/dist/types/feed/event-handlers/comment/handle-comment-reaction-updated.d.ts.map +1 -0
  18. package/dist/types/feed/event-handlers/comment/index.d.ts +1 -0
  19. package/dist/types/feed/event-handlers/comment/index.d.ts.map +1 -1
  20. package/dist/types/feed/feed.d.ts.map +1 -1
  21. package/dist/types/feeds-client/feeds-client.d.ts.map +1 -1
  22. package/dist/types/gen/feeds/FeedsApi.d.ts +4 -1
  23. package/dist/types/gen/feeds/FeedsApi.d.ts.map +1 -1
  24. package/dist/types/gen/models/index.d.ts +7 -0
  25. package/dist/types/gen/models/index.d.ts.map +1 -1
  26. package/dist/types/utils/state-update-queue.d.ts +5 -1
  27. package/dist/types/utils/state-update-queue.d.ts.map +1 -1
  28. package/package.json +1 -1
  29. package/src/feed/event-handlers/activity/handle-activity-reaction-updated.test.ts +282 -0
  30. package/src/feed/event-handlers/activity/handle-activity-reaction-updated.ts +140 -0
  31. package/src/feed/event-handlers/activity/index.ts +1 -0
  32. package/src/feed/event-handlers/comment/handle-comment-reaction-added.ts +1 -2
  33. package/src/feed/event-handlers/comment/handle-comment-reaction-updated.test.ts +350 -0
  34. package/src/feed/event-handlers/comment/handle-comment-reaction-updated.ts +72 -0
  35. package/src/feed/event-handlers/comment/index.ts +1 -1
  36. package/src/feed/feed.ts +4 -2
  37. package/src/feeds-client/feeds-client.ts +15 -3
  38. package/src/gen/feeds/FeedsApi.ts +28 -0
  39. package/src/gen/models/index.ts +10 -0
  40. package/src/test-utils/response-generators.ts +52 -0
  41. package/src/utils/state-update-queue.ts +14 -2
  42. package/dist/index-BSzSBlMh.mjs.map +0 -1
  43. package/dist/index-DRX66SIx.js.map +0 -1
@@ -0,0 +1,282 @@
1
+ import { describe, it, expect, beforeEach } from 'vitest';
2
+ import { Feed, handleActivityReactionUpdated } from '../../../feed';
3
+ import { FeedsClient } from '../../../feeds-client';
4
+ import {
5
+ generateActivityPinResponse,
6
+ generateActivityResponse,
7
+ generateFeedResponse,
8
+ generateOwnUser,
9
+ getHumanId,
10
+ generateActivityReactionUpdatedEvent,
11
+ generateFeedReactionResponse,
12
+ } from '../../../test-utils';
13
+ import { shouldUpdateState } from '../../../utils';
14
+ import type { EventPayload } from '../../../types-internal';
15
+
16
+ describe(handleActivityReactionUpdated.name, () => {
17
+ let feed: Feed;
18
+ let client: FeedsClient;
19
+ let currentUserId: string;
20
+ let activityId: string;
21
+
22
+ beforeEach(() => {
23
+ client = new FeedsClient('mock-api-key');
24
+ currentUserId = getHumanId();
25
+ activityId = getHumanId();
26
+ client.state.partialNext({
27
+ connected_user: generateOwnUser({ id: currentUserId }),
28
+ });
29
+ const feedResponse = generateFeedResponse({
30
+ id: 'main',
31
+ group_id: 'user',
32
+ created_by: { id: currentUserId },
33
+ });
34
+ feed = new Feed(
35
+ client,
36
+ feedResponse.group_id,
37
+ feedResponse.id,
38
+ feedResponse,
39
+ );
40
+ });
41
+
42
+ it('updates the reaction in the correct activity for current user & updates activities with event.activity', () => {
43
+ const event = generateActivityReactionUpdatedEvent({
44
+ reaction: {
45
+ user: { id: currentUserId },
46
+ type: 'downvote',
47
+ activity_id: activityId,
48
+ },
49
+ activity: {
50
+ reaction_count: 1,
51
+ latest_reactions: [],
52
+ reaction_groups: {},
53
+ },
54
+ });
55
+
56
+ const existingReaction = generateFeedReactionResponse({
57
+ user: { id: currentUserId },
58
+ type: 'like',
59
+ activity_id: activityId,
60
+ });
61
+
62
+ const activity = generateActivityResponse({
63
+ id: event.activity.id,
64
+ reaction_count: 1,
65
+ own_reactions: [existingReaction],
66
+ latest_reactions: [],
67
+ reaction_groups: {},
68
+ });
69
+ const activityPin = generateActivityPinResponse({
70
+ activity: { ...activity },
71
+ });
72
+ feed.state.partialNext({
73
+ activities: [activity],
74
+ pinned_activities: [activityPin],
75
+ });
76
+
77
+ const stateBefore = feed.currentState;
78
+
79
+ expect(stateBefore.activities![0].reaction_count).toBe(1);
80
+ expect(stateBefore.pinned_activities![0].activity.reaction_count).toBe(1);
81
+
82
+ handleActivityReactionUpdated.call(feed, event);
83
+
84
+ const stateAfter = feed.currentState;
85
+
86
+ expect(stateAfter.activities![0].own_reactions).toContain(event.reaction);
87
+ expect(stateAfter.pinned_activities![0].activity.own_reactions).toContain(
88
+ event.reaction,
89
+ );
90
+ expect(stateAfter.activities![0].own_bookmarks).toBe(
91
+ stateBefore.activities![0].own_bookmarks,
92
+ );
93
+ expect(stateAfter.pinned_activities![0].activity.own_bookmarks).toBe(
94
+ stateBefore.pinned_activities![0].activity.own_bookmarks,
95
+ );
96
+ expect(stateAfter.activities![0].reaction_count).toBe(1);
97
+ expect(stateAfter.pinned_activities![0].activity.reaction_count).toBe(1);
98
+ });
99
+
100
+ it('does not update own_reactions if the reaction is from another user but still updates activity', () => {
101
+ const event = generateActivityReactionUpdatedEvent({
102
+ reaction: {
103
+ user: { id: 'other-user-id' },
104
+ type: 'downvote',
105
+ activity_id: activityId,
106
+ },
107
+ activity: {
108
+ reaction_count: 2,
109
+ },
110
+ });
111
+
112
+ const existingReaction = generateFeedReactionResponse({
113
+ user: { id: currentUserId },
114
+ type: 'like',
115
+ activity_id: activityId,
116
+ });
117
+
118
+ const activity = generateActivityResponse({
119
+ id: event.activity.id,
120
+ reaction_count: 1,
121
+ own_reactions: [existingReaction],
122
+ latest_reactions: [],
123
+ reaction_groups: {},
124
+ });
125
+ const activityPin = generateActivityPinResponse({
126
+ activity: { ...activity },
127
+ });
128
+ feed.state.partialNext({
129
+ activities: [activity],
130
+ pinned_activities: [activityPin],
131
+ });
132
+
133
+ const stateBefore = feed.currentState;
134
+
135
+ expect(stateBefore.activities![0].reaction_count).toBe(1);
136
+ expect(stateBefore.pinned_activities![0].activity.reaction_count).toBe(1);
137
+
138
+ handleActivityReactionUpdated.call(feed, event);
139
+
140
+ const stateAfter = feed.currentState;
141
+
142
+ expect(stateAfter.activities![0].own_reactions).toHaveLength(1);
143
+ expect(
144
+ stateAfter.pinned_activities![0].activity.own_reactions,
145
+ ).toHaveLength(1);
146
+ expect(stateAfter.activities![0].reaction_count).toBe(2);
147
+ expect(stateAfter.pinned_activities![0].activity.reaction_count).toBe(2);
148
+ expect(stateAfter.activities![0].own_bookmarks).toBe(
149
+ stateBefore.activities![0].own_bookmarks,
150
+ );
151
+ expect(stateAfter.pinned_activities![0].activity.own_bookmarks).toBe(
152
+ stateBefore.pinned_activities![0].activity.own_bookmarks,
153
+ );
154
+ expect(stateAfter.activities![0].own_reactions).toBe(
155
+ stateBefore.activities![0].own_reactions,
156
+ );
157
+ expect(stateAfter.pinned_activities![0].activity.own_reactions).toBe(
158
+ stateBefore.pinned_activities![0].activity.own_reactions,
159
+ );
160
+ });
161
+
162
+ it('does nothing if activity is not found', () => {
163
+ const event = generateActivityReactionUpdatedEvent({
164
+ reaction: { user: { id: currentUserId } },
165
+ });
166
+ const activity = generateActivityResponse({
167
+ id: 'unrelated',
168
+ });
169
+ const activityPin = generateActivityPinResponse({
170
+ activity: { ...activity },
171
+ });
172
+ feed.state.partialNext({
173
+ activities: [activity],
174
+ pinned_activities: [activityPin],
175
+ });
176
+
177
+ const stateBefore = feed.currentState;
178
+
179
+ handleActivityReactionUpdated.call(feed, event);
180
+
181
+ const stateAfter = feed.currentState;
182
+
183
+ expect(stateAfter).toBe(stateBefore);
184
+ });
185
+
186
+ describe(`Activity reaction updated ${shouldUpdateState.name} integration`, () => {
187
+ let currentUserPayload: EventPayload<'feeds.activity.reaction.updated'>;
188
+
189
+ beforeEach(() => {
190
+ currentUserPayload = generateActivityReactionUpdatedEvent({
191
+ reaction: { user: { id: currentUserId }, activity_id: activityId },
192
+ activity: { id: activityId },
193
+ });
194
+
195
+ feed.state.partialNext({ activities: [currentUserPayload.activity] });
196
+ feed.state.partialNext({ watch: true });
197
+ });
198
+
199
+ it(`skips update if ${shouldUpdateState.name} returns false`, () => {
200
+ // 1. HTTP and then WS
201
+
202
+ handleActivityReactionUpdated.call(feed, currentUserPayload, false);
203
+
204
+ let stateBefore = feed.currentState;
205
+
206
+ handleActivityReactionUpdated.call(feed, currentUserPayload);
207
+
208
+ let stateAfter = feed.currentState;
209
+
210
+ expect(stateAfter).toBe(stateBefore);
211
+ // @ts-expect-error Using Feed internals for tests only
212
+ expect(feed.stateUpdateQueue.size).toEqual(0);
213
+
214
+ // 2. WS and the HTTP
215
+
216
+ handleActivityReactionUpdated.call(feed, currentUserPayload);
217
+
218
+ stateBefore = feed.currentState;
219
+
220
+ handleActivityReactionUpdated.call(feed, currentUserPayload, false);
221
+
222
+ stateAfter = feed.currentState;
223
+
224
+ expect(stateAfter).toBe(stateBefore);
225
+ // @ts-expect-error Using Feed internals for tests only
226
+ expect(feed.stateUpdateQueue.size).toEqual(0);
227
+ });
228
+
229
+ it('allows update again from WS after clearing the stateUpdateQueue', () => {
230
+ handleActivityReactionUpdated.call(feed, currentUserPayload);
231
+
232
+ // Clear the queue
233
+ (feed as any).stateUpdateQueue.clear();
234
+
235
+ // Now update should be allowed from another WS event
236
+ handleActivityReactionUpdated.call(feed, currentUserPayload);
237
+
238
+ const activities = feed.currentState.activities!;
239
+ const activity = activities.find((a) => a.id === activityId);
240
+ const [latestReaction] = activity?.own_reactions ?? [];
241
+
242
+ expect(activity?.own_reactions.length).toEqual(1);
243
+ expect(latestReaction).toMatchObject(currentUserPayload.reaction);
244
+ });
245
+
246
+ it('allows update again from HTTP response after clearing the stateUpdateQueue', () => {
247
+ handleActivityReactionUpdated.call(feed, currentUserPayload, false);
248
+
249
+ // Clear the queue
250
+ (feed as any).stateUpdateQueue.clear();
251
+
252
+ // Now update should be allowed from another HTTP response
253
+ handleActivityReactionUpdated.call(feed, currentUserPayload, false);
254
+
255
+ const activities = feed.currentState.activities!;
256
+ const activity = activities.find((a) => a.id === activityId);
257
+ const [latestReaction] = activity?.own_reactions ?? [];
258
+
259
+ expect(activity?.own_reactions.length).toEqual(1);
260
+ expect(latestReaction).toMatchObject(currentUserPayload.reaction);
261
+ });
262
+
263
+ it('should not insert anything into the stateUpdateQueue if the connected_user did not trigger the reaction', () => {
264
+ const otherUserPayload = generateActivityReactionUpdatedEvent({
265
+ reaction: { user: { id: getHumanId() }, activity_id: activityId },
266
+ activity: { id: activityId },
267
+ });
268
+
269
+ handleActivityReactionUpdated.call(feed, otherUserPayload);
270
+
271
+ expect((feed as any).stateUpdateQueue).toEqual(new Set());
272
+
273
+ handleActivityReactionUpdated.call(feed, otherUserPayload);
274
+
275
+ const activities = feed.currentState.activities!;
276
+ const activity = activities.find((a) => a.id === activityId);
277
+
278
+ expect((feed as any).stateUpdateQueue).toEqual(new Set());
279
+ expect(activity?.own_reactions.length).toEqual(0);
280
+ });
281
+ });
282
+ });
@@ -0,0 +1,140 @@
1
+ import type { Feed } from '../../feed';
2
+ import type {
3
+ ActivityPinResponse,
4
+ ActivityResponse,
5
+ } from '../../../gen/models';
6
+ import type { EventPayload, PartializeAllBut } from '../../../types-internal';
7
+ import {
8
+ getStateUpdateQueueId,
9
+ shouldUpdateState,
10
+ updateEntityInArray,
11
+ } from '../../../utils';
12
+
13
+ export type ActivityReactionUpdatedPayload = PartializeAllBut<
14
+ EventPayload<'feeds.activity.reaction.updated'>,
15
+ 'activity' | 'reaction'
16
+ >;
17
+
18
+ // shared function to update the activity with the new reaction
19
+ const sharedUpdateActivity = ({
20
+ payload,
21
+ currentActivity,
22
+ eventBelongsToCurrentUser,
23
+ }: {
24
+ payload: ActivityReactionUpdatedPayload;
25
+ currentActivity: ActivityResponse;
26
+ eventBelongsToCurrentUser: boolean;
27
+ }) => {
28
+ const { activity: newActivity, reaction: newReaction } = payload;
29
+ let ownReactions = currentActivity.own_reactions;
30
+
31
+ if (eventBelongsToCurrentUser) {
32
+ ownReactions = [newReaction];
33
+ }
34
+
35
+ return {
36
+ ...currentActivity,
37
+ latest_reactions: newActivity.latest_reactions,
38
+ reaction_groups: newActivity.reaction_groups,
39
+ reaction_count: newActivity.reaction_count,
40
+ own_reactions: ownReactions,
41
+ };
42
+ };
43
+
44
+ export const updateReactionInActivities = (
45
+ payload: ActivityReactionUpdatedPayload,
46
+ activities: ActivityResponse[] | undefined,
47
+ eventBelongsToCurrentUser: boolean,
48
+ ) =>
49
+ updateEntityInArray({
50
+ entities: activities,
51
+ matcher: (activity) => activity.id === payload.activity.id,
52
+ updater: (matchedActivity) =>
53
+ sharedUpdateActivity({
54
+ payload,
55
+ currentActivity: matchedActivity,
56
+ eventBelongsToCurrentUser,
57
+ }),
58
+ });
59
+
60
+ export const updateReactionInPinnedActivities = (
61
+ payload: ActivityReactionUpdatedPayload,
62
+ pinnedActivities: ActivityPinResponse[] | undefined,
63
+ eventBelongsToCurrentUser: boolean,
64
+ ) =>
65
+ updateEntityInArray({
66
+ entities: pinnedActivities,
67
+ matcher: (pinnedActivity) =>
68
+ pinnedActivity.activity.id === payload.activity.id,
69
+ updater: (matchedPinnedActivity) => {
70
+ const updatedActivity = sharedUpdateActivity({
71
+ payload,
72
+ currentActivity: matchedPinnedActivity.activity,
73
+ eventBelongsToCurrentUser,
74
+ });
75
+
76
+ // this should never happen, but just in case
77
+ if (updatedActivity === matchedPinnedActivity.activity) {
78
+ return matchedPinnedActivity;
79
+ }
80
+
81
+ return {
82
+ ...matchedPinnedActivity,
83
+ activity: updatedActivity,
84
+ };
85
+ },
86
+ });
87
+
88
+ export function handleActivityReactionUpdated(
89
+ this: Feed,
90
+ payload: ActivityReactionUpdatedPayload,
91
+ fromWs?: boolean,
92
+ ) {
93
+ const connectedUser = this.client.state.getLatestValue().connected_user;
94
+
95
+ const eventBelongsToCurrentUser =
96
+ typeof connectedUser !== 'undefined' &&
97
+ payload.reaction.user.id === connectedUser.id;
98
+
99
+ if (
100
+ !shouldUpdateState({
101
+ stateUpdateQueueId: getStateUpdateQueueId(
102
+ payload,
103
+ 'activity-reaction-updated',
104
+ ),
105
+ stateUpdateQueue: this.stateUpdateQueue,
106
+ watch: this.currentState.watch,
107
+ fromWs,
108
+ isTriggeredByConnectedUser: eventBelongsToCurrentUser,
109
+ })
110
+ ) {
111
+ return;
112
+ }
113
+
114
+ const {
115
+ activities: currentActivities,
116
+ pinned_activities: currentPinnedActivities,
117
+ } = this.currentState;
118
+
119
+ const [result1, result2] = [
120
+ this.hasActivity(payload.activity.id)
121
+ ? updateReactionInActivities(
122
+ payload,
123
+ currentActivities,
124
+ eventBelongsToCurrentUser,
125
+ )
126
+ : undefined,
127
+ updateReactionInPinnedActivities(
128
+ payload,
129
+ currentPinnedActivities,
130
+ eventBelongsToCurrentUser,
131
+ ),
132
+ ];
133
+
134
+ if (result1?.changed || result2.changed) {
135
+ this.state.partialNext({
136
+ ...(result1 ? { activities: result1.entities } : {}),
137
+ pinned_activities: result2.entities,
138
+ });
139
+ }
140
+ }
@@ -4,4 +4,5 @@ export * from './handle-activity-removed-from-feed';
4
4
  export * from './handle-activity-updated';
5
5
  export * from './handle-activity-reaction-added';
6
6
  export * from './handle-activity-reaction-deleted';
7
+ export * from './handle-activity-reaction-updated';
7
8
  export * from './handle-activity-marked';
@@ -1,5 +1,5 @@
1
1
  import type { Feed } from '../../feed';
2
- import type { EventPayload} from '../../../types-internal';
2
+ import type { EventPayload } from '../../../types-internal';
3
3
  import { type PartializeAllBut } from '../../../types-internal';
4
4
  import { getStateUpdateQueueId, shouldUpdateState } from '../../../utils';
5
5
 
@@ -53,7 +53,6 @@ export function handleCommentReactionAdded(
53
53
  newComments[commentIndex] = {
54
54
  ...newComments[commentIndex],
55
55
  reaction_count: comment.reaction_count ?? 0,
56
- // TODO: FIXME this should be handled by the backend
57
56
  latest_reactions: comment.latest_reactions ?? [],
58
57
  reaction_groups: comment.reaction_groups ?? {},
59
58
  own_reactions: ownReactions,