@fabriktor/fx 0.0.164 → 0.0.165

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.
@@ -81,12 +81,16 @@ export type StartFeedSessionOptions = FeedPullContext & {
81
81
  signal?: AbortSignal;
82
82
  on_error?: FeedSessionErrorHandler;
83
83
  };
84
- export type AddFeedReactionOptions = {
85
- post: FeedPost;
84
+ export type FeedReactionChange = {
85
+ reaction_type: FeedReactionType | null;
86
+ count_delta: FeedPost["stats"]["count_per_reaction"];
87
+ };
88
+ export type AddFeedReactionOptions = RemoveFeedReactionOptions & {
86
89
  reaction_type: FeedReactionType;
87
90
  };
88
91
  export type RemoveFeedReactionOptions = {
89
92
  post: FeedPost;
93
+ on_change?: (change: FeedReactionChange) => void;
90
94
  };
91
95
  export type AddFeedFavoriteOptions = {
92
96
  post: FeedPost;
@@ -234,6 +238,7 @@ export declare class FeedFX implements FeedFXOperator {
234
238
  startSession(opts: StartFeedSessionOptions): Promise<FeedSession>;
235
239
  addReaction(opts: AddFeedReactionOptions): Promise<OperationResult>;
236
240
  removeReaction(opts: RemoveFeedReactionOptions): Promise<OperationResult>;
241
+ private setReaction;
237
242
  addFavorite(opts: AddFeedFavoriteOptions): Promise<OperationResult>;
238
243
  removeFavorite(opts: RemoveFeedFavoriteOptions): Promise<OperationResult>;
239
244
  createReply(opts: CreateFeedReplyOptions): Promise<OperationResult>;
@@ -11,7 +11,7 @@ import { MutationQueue } from "../core/queue.js";
11
11
  import { startPullSession, PullSessionScope, } from "../core/pull.js";
12
12
  import { runOwnerSearch, } from "../core/search.js";
13
13
  import { validateStorageUploadFiles, } from "../storage/storage.js";
14
- import { createEvent, EventBus } from "../core/events.js";
14
+ import { createEvent, EventBus, notifyObserver } from "../core/events.js";
15
15
  import { prepareFeedTextPost, validateFeedVisibility, feedPostInput, checkFeedPostVersion, } from "./validation.js";
16
16
  export * from "./validation.js";
17
17
  export * from "./media.js";
@@ -369,52 +369,82 @@ export class FeedFX {
369
369
  });
370
370
  }
371
371
  async addReaction(opts) {
372
+ return this.setReaction(opts, opts.reaction_type);
373
+ }
374
+ async removeReaction(opts) {
375
+ return this.setReaction(opts, null);
376
+ }
377
+ async setReaction(opts, type) {
372
378
  const post_id = feedPostID(opts.post);
373
- return await this.runAction(post_id, FeedAction.Reaction, async (signal) => {
374
- if (![FeedLike, FeedDislike, FeedReport].includes(opts.reaction_type))
375
- throw errValidation({
376
- field: "reaction_type",
377
- msg: "Unsupported reaction.",
378
- });
379
+ return this.runAction(post_id, FeedAction.Reaction, async (signal) => {
380
+ if (type !== null && ![FeedLike, FeedDislike, FeedReport].includes(type))
381
+ throw errValidation({ field: "reaction_type", msg: "Unsupported reaction." });
379
382
  const fresh = await this.requireFeedPost(post_id, signal);
380
383
  const id = resolvedID(fresh.self.reaction_id);
384
+ let existing = [];
381
385
  if (id) {
382
386
  const current = await this.readOwnReaction({ id, signal });
383
387
  if (!current)
384
388
  throw errValidation({ msg: msgFeedReactionMissing });
385
- if (current.reaction_type === opts.reaction_type ||
386
- current.reaction_type === FeedReport)
389
+ if (current.reaction_type === FeedReport &&
390
+ type !== null && type !== FeedReport)
387
391
  throw errValidation({ msg: msgFeedReactionAlreadyExists });
388
- await this.feed.deleteOneFeedReaction({ id, signal });
392
+ // The query route is unpaged. Scope it to the authenticated user's
393
+ // resolved reaction owner, never to the author of the post.
394
+ const out = await this.feed.queryFeedReactions({
395
+ query: queryParams({
396
+ owner_id: requireFeedID(current.owner_id),
397
+ post_id,
398
+ }),
399
+ signal,
400
+ });
389
401
  signal.throwIfAborted();
390
- this.markActionRemoved(post_id, FeedAction.Reaction);
402
+ existing = out.value ?? [];
403
+ }
404
+ const keep = existing.find((reaction) => reaction.reaction_type === type);
405
+ const remaining = new Map(existing.map((reaction) => [reaction.id, reaction]));
406
+ const count_delta = {};
407
+ let inserted_id;
408
+ let out = {};
409
+ try {
410
+ for (const reaction of existing) {
411
+ if (reaction.id === keep?.id)
412
+ continue;
413
+ signal.throwIfAborted();
414
+ out = await this.feed.deleteOneFeedReaction({ id: reaction.id, signal });
415
+ signal.throwIfAborted();
416
+ remaining.delete(reaction.id);
417
+ count_delta[reaction.reaction_type] =
418
+ (count_delta[reaction.reaction_type] ?? 0) - 1;
419
+ }
420
+ if (keep)
421
+ return { id: keep.id };
422
+ if (type === null)
423
+ return out;
424
+ out = await this.feed.insertOneFeedReaction({
425
+ signal,
426
+ in: { post_id, reaction_type: type },
427
+ });
428
+ signal.throwIfAborted();
429
+ inserted_id = requiredOperationID(out, msgCreatedFeedReactionIDMissing);
430
+ count_delta[type] = (count_delta[type] ?? 0) + 1;
431
+ return out;
432
+ }
433
+ finally {
434
+ if (!signal.aborted) {
435
+ const current = remaining.values().next().value;
436
+ const current_id = inserted_id ?? current?.id;
437
+ if (current_id)
438
+ this.markAction(post_id, FeedAction.Reaction, current_id);
439
+ else
440
+ this.markActionRemoved(post_id, FeedAction.Reaction);
441
+ // Report acknowledged changes even if a later deletion/insertion fails.
442
+ notifyObserver(() => opts.on_change?.({
443
+ reaction_type: inserted_id ? type : (current?.reaction_type ?? null),
444
+ count_delta,
445
+ }));
446
+ }
391
447
  }
392
- const out = await this.feed.insertOneFeedReaction({
393
- signal,
394
- in: {
395
- post_id,
396
- reaction_type: opts.reaction_type,
397
- },
398
- });
399
- signal.throwIfAborted();
400
- this.markAction(post_id, FeedAction.Reaction, operationID(out));
401
- return out;
402
- });
403
- }
404
- async removeReaction(opts) {
405
- const post_id = feedPostID(opts.post);
406
- return await this.runAction(post_id, FeedAction.Reaction, async (signal) => {
407
- const fresh = await this.requireFeedPost(post_id, signal);
408
- const id = resolvedID(fresh.self.reaction_id);
409
- if (!id)
410
- throw errValidation({ msg: "This action is no longer present." });
411
- const out = await this.feed.deleteOneFeedReaction({
412
- signal,
413
- id,
414
- });
415
- signal.throwIfAborted();
416
- this.markActionRemoved(post_id, FeedAction.Reaction);
417
- return out;
418
448
  });
419
449
  }
420
450
  async addFavorite(opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.164",
3
+ "version": "0.0.165",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {