@stream-io/feeds-client 0.2.16 → 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 (40) hide show
  1. package/CHANGELOG.md +13 -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-J3MkoYPN.mjs → index-BZL77zNq.mjs} +166 -27
  7. package/dist/index-BZL77zNq.mjs.map +1 -0
  8. package/dist/{index-CaFrpjpl.js → index-nq6SDtbt.js} +166 -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/models/index.d.ts +1 -1
  23. package/dist/types/gen/models/index.d.ts.map +1 -1
  24. package/dist/types/utils/state-update-queue.d.ts +5 -1
  25. package/dist/types/utils/state-update-queue.d.ts.map +1 -1
  26. package/package.json +1 -1
  27. package/src/feed/event-handlers/activity/handle-activity-reaction-updated.test.ts +282 -0
  28. package/src/feed/event-handlers/activity/handle-activity-reaction-updated.ts +140 -0
  29. package/src/feed/event-handlers/activity/index.ts +1 -0
  30. package/src/feed/event-handlers/comment/handle-comment-reaction-added.ts +1 -2
  31. package/src/feed/event-handlers/comment/handle-comment-reaction-updated.test.ts +350 -0
  32. package/src/feed/event-handlers/comment/handle-comment-reaction-updated.ts +72 -0
  33. package/src/feed/event-handlers/comment/index.ts +1 -1
  34. package/src/feed/feed.ts +4 -2
  35. package/src/feeds-client/feeds-client.ts +15 -3
  36. package/src/gen/models/index.ts +1 -1
  37. package/src/test-utils/response-generators.ts +52 -0
  38. package/src/utils/state-update-queue.ts +14 -2
  39. package/dist/index-CaFrpjpl.js.map +0 -1
  40. package/dist/index-J3MkoYPN.mjs.map +0 -1
@@ -3740,7 +3740,7 @@ const getRateLimitFromResponseHeader = (response_headers) => {
3740
3740
  };
3741
3741
  return result;
3742
3742
  };
3743
- const version = "0.2.16";
3743
+ const version = "0.2.17";
3744
3744
  class ApiClient {
3745
3745
  constructor(apiKey, tokenManager, connectionIdManager, options) {
3746
3746
  this.apiKey = apiKey;
@@ -4549,14 +4549,16 @@ function getStateUpdateQueueId(...args) {
4549
4549
  return toJoin.concat([data.activity.id]).join("-");
4550
4550
  }
4551
4551
  case "activity-reaction-created":
4552
- case "activity-reaction-deleted": {
4552
+ case "activity-reaction-deleted":
4553
+ case "activity-reaction-updated": {
4553
4554
  return toJoin.concat([
4554
4555
  data.activity.id,
4555
4556
  data.reaction.type
4556
4557
  ]).join("-");
4557
4558
  }
4558
4559
  case "comment-reaction-created":
4559
- case "comment-reaction-deleted": {
4560
+ case "comment-reaction-deleted":
4561
+ case "comment-reaction-updated": {
4560
4562
  return toJoin.concat([
4561
4563
  data.comment.id,
4562
4564
  data.reaction.type
@@ -4916,7 +4918,6 @@ function handleCommentReactionAdded(payload, fromWs) {
4916
4918
  newComments[commentIndex] = {
4917
4919
  ...newComments[commentIndex],
4918
4920
  reaction_count: comment.reaction_count ?? 0,
4919
- // TODO: FIXME this should be handled by the backend
4920
4921
  latest_reactions: comment.latest_reactions ?? [],
4921
4922
  reaction_groups: comment.reaction_groups ?? {},
4922
4923
  own_reactions: ownReactions
@@ -4981,6 +4982,51 @@ function handleCommentReactionDeleted(payload, fromWs) {
4981
4982
  };
4982
4983
  });
4983
4984
  }
4985
+ function handleCommentReactionUpdated(payload, fromWs) {
4986
+ const { comment, reaction } = payload;
4987
+ const connectedUser = this.client.state.getLatestValue().connected_user;
4988
+ const isOwnReaction = reaction.user.id === connectedUser?.id;
4989
+ if (!shouldUpdateState({
4990
+ stateUpdateQueueId: getStateUpdateQueueId(
4991
+ payload,
4992
+ "comment-reaction-updated"
4993
+ ),
4994
+ stateUpdateQueue: this.stateUpdateQueue,
4995
+ watch: this.currentState.watch,
4996
+ fromWs,
4997
+ isTriggeredByConnectedUser: isOwnReaction
4998
+ })) {
4999
+ return;
5000
+ }
5001
+ this.state.next((currentState) => {
5002
+ const commentIndex = this.getCommentIndex(comment, currentState);
5003
+ if (commentIndex === -1) return currentState;
5004
+ const forId = comment.parent_id ?? comment.object_id;
5005
+ const entityState = currentState.comments_by_entity_id[forId];
5006
+ const newComments = entityState?.comments?.concat([]) ?? [];
5007
+ let ownReactions = newComments[commentIndex].own_reactions;
5008
+ if (isOwnReaction) {
5009
+ ownReactions = [reaction];
5010
+ }
5011
+ newComments[commentIndex] = {
5012
+ ...newComments[commentIndex],
5013
+ reaction_count: comment.reaction_count ?? 0,
5014
+ latest_reactions: comment.latest_reactions ?? [],
5015
+ reaction_groups: comment.reaction_groups ?? {},
5016
+ own_reactions: ownReactions
5017
+ };
5018
+ return {
5019
+ ...currentState,
5020
+ comments_by_entity_id: {
5021
+ ...currentState.comments_by_entity_id,
5022
+ [forId]: {
5023
+ ...entityState,
5024
+ comments: newComments
5025
+ }
5026
+ }
5027
+ };
5028
+ });
5029
+ }
4984
5030
  function handleFeedMemberAdded(event) {
4985
5031
  const { connected_user: connectedUser } = this.client.state.getLatestValue();
4986
5032
  this.state.next((currentState) => {
@@ -5044,7 +5090,7 @@ function handleFeedMemberRemoved(event) {
5044
5090
  return newState ?? currentState;
5045
5091
  });
5046
5092
  }
5047
- const sharedUpdateActivity$5 = ({
5093
+ const sharedUpdateActivity$6 = ({
5048
5094
  currentActivity,
5049
5095
  event,
5050
5096
  eventBelongsToCurrentUser
@@ -5062,7 +5108,7 @@ const sharedUpdateActivity$5 = ({
5062
5108
  const addBookmarkToActivities = (event, activities, eventBelongsToCurrentUser) => updateEntityInArray({
5063
5109
  entities: activities,
5064
5110
  matcher: (activity) => activity.id === event.bookmark.activity.id,
5065
- updater: (matchedActivity) => sharedUpdateActivity$5({
5111
+ updater: (matchedActivity) => sharedUpdateActivity$6({
5066
5112
  currentActivity: matchedActivity,
5067
5113
  event,
5068
5114
  eventBelongsToCurrentUser
@@ -5072,7 +5118,7 @@ const addBookmarkToPinnedActivities = (event, pinnedActivities, eventBelongsToCu
5072
5118
  entities: pinnedActivities,
5073
5119
  matcher: (pinnedActivity) => pinnedActivity.activity.id === event.bookmark.activity.id,
5074
5120
  updater: (matchedPinnedActivity) => {
5075
- const newActivity = sharedUpdateActivity$5({
5121
+ const newActivity = sharedUpdateActivity$6({
5076
5122
  currentActivity: matchedPinnedActivity.activity,
5077
5123
  event,
5078
5124
  eventBelongsToCurrentUser
@@ -5115,7 +5161,7 @@ function handleBookmarkAdded(event) {
5115
5161
  const isSameBookmark = (bookmark1, bookmark2) => {
5116
5162
  return bookmark1.user.id === bookmark2.user.id && bookmark1.activity.id === bookmark2.activity.id && bookmark1.folder?.id === bookmark2.folder?.id;
5117
5163
  };
5118
- const sharedUpdateActivity$4 = ({
5164
+ const sharedUpdateActivity$5 = ({
5119
5165
  currentActivity,
5120
5166
  event,
5121
5167
  eventBelongsToCurrentUser
@@ -5135,7 +5181,7 @@ const sharedUpdateActivity$4 = ({
5135
5181
  const removeBookmarkFromActivities = (event, activities, eventBelongsToCurrentUser) => updateEntityInArray({
5136
5182
  entities: activities,
5137
5183
  matcher: (activity) => activity.id === event.bookmark.activity.id,
5138
- updater: (matchedActivity) => sharedUpdateActivity$4({
5184
+ updater: (matchedActivity) => sharedUpdateActivity$5({
5139
5185
  currentActivity: matchedActivity,
5140
5186
  event,
5141
5187
  eventBelongsToCurrentUser
@@ -5145,7 +5191,7 @@ const removeBookmarkFromPinnedActivities = (event, pinnedActivities, eventBelong
5145
5191
  entities: pinnedActivities,
5146
5192
  matcher: (pinnedActivity) => pinnedActivity.activity.id === event.bookmark.activity.id,
5147
5193
  updater: (matchedPinnedActivity) => {
5148
- const newActivity = sharedUpdateActivity$4({
5194
+ const newActivity = sharedUpdateActivity$5({
5149
5195
  currentActivity: matchedPinnedActivity.activity,
5150
5196
  event,
5151
5197
  eventBelongsToCurrentUser
@@ -5185,7 +5231,7 @@ function handleBookmarkDeleted(event) {
5185
5231
  });
5186
5232
  }
5187
5233
  }
5188
- const sharedUpdateActivity$3 = ({
5234
+ const sharedUpdateActivity$4 = ({
5189
5235
  currentActivity,
5190
5236
  event,
5191
5237
  eventBelongsToCurrentUser
@@ -5209,7 +5255,7 @@ const sharedUpdateActivity$3 = ({
5209
5255
  const updateBookmarkInActivities = (event, activities, eventBelongsToCurrentUser) => updateEntityInArray({
5210
5256
  entities: activities,
5211
5257
  matcher: (activity) => activity.id === event.bookmark.activity.id,
5212
- updater: (matchedActivity) => sharedUpdateActivity$3({
5258
+ updater: (matchedActivity) => sharedUpdateActivity$4({
5213
5259
  currentActivity: matchedActivity,
5214
5260
  event,
5215
5261
  eventBelongsToCurrentUser
@@ -5219,7 +5265,7 @@ const updateBookmarkInPinnedActivities = (event, pinnedActivities, eventBelongsT
5219
5265
  entities: pinnedActivities,
5220
5266
  matcher: (pinnedActivity) => pinnedActivity.activity.id === event.bookmark.activity.id,
5221
5267
  updater: (matchedPinnedActivity) => {
5222
- const newActivity = sharedUpdateActivity$3({
5268
+ const newActivity = sharedUpdateActivity$4({
5223
5269
  currentActivity: matchedPinnedActivity.activity,
5224
5270
  event,
5225
5271
  eventBelongsToCurrentUser
@@ -5350,7 +5396,7 @@ function handleActivityRemovedFromFeed(event) {
5350
5396
  }
5351
5397
  }
5352
5398
  }
5353
- const sharedUpdateActivity$2 = ({
5399
+ const sharedUpdateActivity$3 = ({
5354
5400
  currentActivity,
5355
5401
  event
5356
5402
  }) => {
@@ -5363,7 +5409,7 @@ const sharedUpdateActivity$2 = ({
5363
5409
  const updateActivityInState = (event, activities) => updateEntityInArray({
5364
5410
  entities: activities,
5365
5411
  matcher: (activity) => activity.id === event.activity.id,
5366
- updater: (matchedActivity) => sharedUpdateActivity$2({
5412
+ updater: (matchedActivity) => sharedUpdateActivity$3({
5367
5413
  currentActivity: matchedActivity,
5368
5414
  event
5369
5415
  })
@@ -5372,7 +5418,7 @@ const updatePinnedActivityInState = (event, pinnedActivities) => updateEntityInA
5372
5418
  entities: pinnedActivities,
5373
5419
  matcher: (pinnedActivity) => pinnedActivity.activity.id === event.activity.id,
5374
5420
  updater: (matchedPinnedActivity) => {
5375
- const newActivity = sharedUpdateActivity$2({
5421
+ const newActivity = sharedUpdateActivity$3({
5376
5422
  currentActivity: matchedPinnedActivity.activity,
5377
5423
  event
5378
5424
  });
@@ -5414,7 +5460,7 @@ function handleActivityUpdated(payload, fromWs) {
5414
5460
  });
5415
5461
  }
5416
5462
  }
5417
- const sharedUpdateActivity$1 = ({
5463
+ const sharedUpdateActivity$2 = ({
5418
5464
  payload,
5419
5465
  currentActivity,
5420
5466
  eventBelongsToCurrentUser
@@ -5435,7 +5481,7 @@ const sharedUpdateActivity$1 = ({
5435
5481
  const addReactionToActivities = (payload, activities, eventBelongsToCurrentUser) => updateEntityInArray({
5436
5482
  entities: activities,
5437
5483
  matcher: (activity) => activity.id === payload.activity.id,
5438
- updater: (matchedActivity) => sharedUpdateActivity$1({
5484
+ updater: (matchedActivity) => sharedUpdateActivity$2({
5439
5485
  payload,
5440
5486
  currentActivity: matchedActivity,
5441
5487
  eventBelongsToCurrentUser
@@ -5445,7 +5491,7 @@ const addReactionToPinnedActivities = (payload, pinnedActivities, eventBelongsTo
5445
5491
  entities: pinnedActivities,
5446
5492
  matcher: (pinnedActivity) => pinnedActivity.activity.id === payload.activity.id,
5447
5493
  updater: (matchedPinnedActivity) => {
5448
- const updatedActivity = sharedUpdateActivity$1({
5494
+ const updatedActivity = sharedUpdateActivity$2({
5449
5495
  payload,
5450
5496
  currentActivity: matchedPinnedActivity.activity,
5451
5497
  eventBelongsToCurrentUser
@@ -5497,7 +5543,7 @@ function handleActivityReactionAdded(payload, fromWs) {
5497
5543
  });
5498
5544
  }
5499
5545
  }
5500
- const sharedUpdateActivity = ({
5546
+ const sharedUpdateActivity$1 = ({
5501
5547
  currentActivity,
5502
5548
  payload,
5503
5549
  eventBelongsToCurrentUser
@@ -5520,7 +5566,7 @@ const sharedUpdateActivity = ({
5520
5566
  const removeReactionFromActivities = (payload, activities, eventBelongsToCurrentUser) => updateEntityInArray({
5521
5567
  entities: activities,
5522
5568
  matcher: (activity) => activity.id === payload.activity.id,
5523
- updater: (matchedActivity) => sharedUpdateActivity({
5569
+ updater: (matchedActivity) => sharedUpdateActivity$1({
5524
5570
  currentActivity: matchedActivity,
5525
5571
  payload,
5526
5572
  eventBelongsToCurrentUser
@@ -5530,7 +5576,7 @@ const removeReactionFromPinnedActivities = (payload, activities, eventBelongsToC
5530
5576
  entities: activities,
5531
5577
  matcher: (pinnedActivity) => pinnedActivity.activity.id === payload.activity.id,
5532
5578
  updater: (matchedPinnedActivity) => {
5533
- const newActivity = sharedUpdateActivity({
5579
+ const newActivity = sharedUpdateActivity$1({
5534
5580
  currentActivity: matchedPinnedActivity.activity,
5535
5581
  payload,
5536
5582
  eventBelongsToCurrentUser
@@ -5582,6 +5628,89 @@ function handleActivityReactionDeleted(payload, fromWs) {
5582
5628
  });
5583
5629
  }
5584
5630
  }
5631
+ const sharedUpdateActivity = ({
5632
+ payload,
5633
+ currentActivity,
5634
+ eventBelongsToCurrentUser
5635
+ }) => {
5636
+ const { activity: newActivity, reaction: newReaction } = payload;
5637
+ let ownReactions = currentActivity.own_reactions;
5638
+ if (eventBelongsToCurrentUser) {
5639
+ ownReactions = [newReaction];
5640
+ }
5641
+ return {
5642
+ ...currentActivity,
5643
+ latest_reactions: newActivity.latest_reactions,
5644
+ reaction_groups: newActivity.reaction_groups,
5645
+ reaction_count: newActivity.reaction_count,
5646
+ own_reactions: ownReactions
5647
+ };
5648
+ };
5649
+ const updateReactionInActivities = (payload, activities, eventBelongsToCurrentUser) => updateEntityInArray({
5650
+ entities: activities,
5651
+ matcher: (activity) => activity.id === payload.activity.id,
5652
+ updater: (matchedActivity) => sharedUpdateActivity({
5653
+ payload,
5654
+ currentActivity: matchedActivity,
5655
+ eventBelongsToCurrentUser
5656
+ })
5657
+ });
5658
+ const updateReactionInPinnedActivities = (payload, pinnedActivities, eventBelongsToCurrentUser) => updateEntityInArray({
5659
+ entities: pinnedActivities,
5660
+ matcher: (pinnedActivity) => pinnedActivity.activity.id === payload.activity.id,
5661
+ updater: (matchedPinnedActivity) => {
5662
+ const updatedActivity = sharedUpdateActivity({
5663
+ payload,
5664
+ currentActivity: matchedPinnedActivity.activity,
5665
+ eventBelongsToCurrentUser
5666
+ });
5667
+ if (updatedActivity === matchedPinnedActivity.activity) {
5668
+ return matchedPinnedActivity;
5669
+ }
5670
+ return {
5671
+ ...matchedPinnedActivity,
5672
+ activity: updatedActivity
5673
+ };
5674
+ }
5675
+ });
5676
+ function handleActivityReactionUpdated(payload, fromWs) {
5677
+ const connectedUser = this.client.state.getLatestValue().connected_user;
5678
+ const eventBelongsToCurrentUser = typeof connectedUser !== "undefined" && payload.reaction.user.id === connectedUser.id;
5679
+ if (!shouldUpdateState({
5680
+ stateUpdateQueueId: getStateUpdateQueueId(
5681
+ payload,
5682
+ "activity-reaction-updated"
5683
+ ),
5684
+ stateUpdateQueue: this.stateUpdateQueue,
5685
+ watch: this.currentState.watch,
5686
+ fromWs,
5687
+ isTriggeredByConnectedUser: eventBelongsToCurrentUser
5688
+ })) {
5689
+ return;
5690
+ }
5691
+ const {
5692
+ activities: currentActivities,
5693
+ pinned_activities: currentPinnedActivities
5694
+ } = this.currentState;
5695
+ const [result1, result2] = [
5696
+ this.hasActivity(payload.activity.id) ? updateReactionInActivities(
5697
+ payload,
5698
+ currentActivities,
5699
+ eventBelongsToCurrentUser
5700
+ ) : void 0,
5701
+ updateReactionInPinnedActivities(
5702
+ payload,
5703
+ currentPinnedActivities,
5704
+ eventBelongsToCurrentUser
5705
+ )
5706
+ ];
5707
+ if (result1?.changed || result2.changed) {
5708
+ this.state.partialNext({
5709
+ ...result1 ? { activities: result1.entities } : {},
5710
+ pinned_activities: result2.entities
5711
+ });
5712
+ }
5713
+ }
5585
5714
  const updateNotificationStatusFromActivityMarked = (event, currentNotificationStatus, aggregatedActivities = []) => {
5586
5715
  if (!currentNotificationStatus) {
5587
5716
  return {
@@ -5778,7 +5907,7 @@ const _Feed = class _Feed extends FeedApi {
5778
5907
  "feeds.activity.deleted": handleActivityDeleted.bind(this),
5779
5908
  "feeds.activity.reaction.added": handleActivityReactionAdded.bind(this),
5780
5909
  "feeds.activity.reaction.deleted": handleActivityReactionDeleted.bind(this),
5781
- "feeds.activity.reaction.updated": _Feed.noop,
5910
+ "feeds.activity.reaction.updated": handleActivityReactionUpdated.bind(this),
5782
5911
  "feeds.activity.removed_from_feed": handleActivityRemovedFromFeed.bind(this),
5783
5912
  "feeds.activity.updated": handleActivityUpdated.bind(this),
5784
5913
  "feeds.bookmark.added": handleBookmarkAdded.bind(this),
@@ -5799,7 +5928,7 @@ const _Feed = class _Feed extends FeedApi {
5799
5928
  "feeds.follow.updated": handleFollowUpdated.bind(this),
5800
5929
  "feeds.comment.reaction.added": handleCommentReactionAdded.bind(this),
5801
5930
  "feeds.comment.reaction.deleted": handleCommentReactionDeleted.bind(this),
5802
- "feeds.comment.reaction.updated": _Feed.noop,
5931
+ "feeds.comment.reaction.updated": handleCommentReactionUpdated.bind(this),
5803
5932
  "feeds.feed_member.added": handleFeedMemberAdded.bind(this),
5804
5933
  "feeds.feed_member.removed": handleFeedMemberRemoved.bind(this),
5805
5934
  "feeds.feed_member.updated": handleFeedMemberUpdated.bind(this),
@@ -6493,9 +6622,14 @@ class FeedsClient extends FeedsApi {
6493
6622
  return response;
6494
6623
  };
6495
6624
  this.addActivityReaction = async (request) => {
6625
+ const shouldEnforceUnique = request.enforce_unique;
6496
6626
  const response = await super.addActivityReaction(request);
6497
6627
  for (const feed of Object.values(this.activeFeeds)) {
6498
- handleActivityReactionAdded.bind(feed)(response, false);
6628
+ if (shouldEnforceUnique) {
6629
+ handleActivityReactionUpdated.bind(feed)(response, false);
6630
+ } else {
6631
+ handleActivityReactionAdded.bind(feed)(response, false);
6632
+ }
6499
6633
  }
6500
6634
  return response;
6501
6635
  };
@@ -6510,9 +6644,14 @@ class FeedsClient extends FeedsApi {
6510
6644
  return response;
6511
6645
  };
6512
6646
  this.addCommentReaction = async (request) => {
6647
+ const shouldEnforceUnique = request.enforce_unique;
6513
6648
  const response = await super.addCommentReaction(request);
6514
6649
  for (const feed of Object.values(this.activeFeeds)) {
6515
- handleCommentReactionAdded.bind(feed)(response, false);
6650
+ if (shouldEnforceUnique) {
6651
+ handleCommentReactionUpdated.bind(feed)(response, false);
6652
+ } else {
6653
+ handleCommentReactionAdded.bind(feed)(response, false);
6654
+ }
6516
6655
  }
6517
6656
  return response;
6518
6657
  };
@@ -6880,4 +7019,4 @@ exports.replaceUniqueArrayMerge = replaceUniqueArrayMerge;
6880
7019
  exports.shouldUpdateState = shouldUpdateState;
6881
7020
  exports.uniqueArrayMerge = uniqueArrayMerge;
6882
7021
  exports.updateEntityInArray = updateEntityInArray;
6883
- //# sourceMappingURL=index-CaFrpjpl.js.map
7022
+ //# sourceMappingURL=index-nq6SDtbt.js.map