@fabriktor/fx 0.0.163 → 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,44 +369,82 @@ export class FeedFX {
369
369
  });
370
370
  }
371
371
  async addReaction(opts) {
372
- const post_id = feedPostID(opts.post);
373
- return await this.runAction(post_id, FeedAction.Reaction, async (signal) => {
374
- const fresh = await this.requireFeedPost(post_id, signal);
375
- if (resolvedID(fresh.self.reaction_id))
376
- throw errValidation({
377
- msg: "An action already exists for this post.",
378
- });
379
- if (![FeedLike, FeedDislike, FeedReport].includes(opts.reaction_type))
380
- throw errValidation({
381
- field: "reaction_type",
382
- msg: "Unsupported reaction.",
383
- });
384
- const out = await this.feed.insertOneFeedReaction({
385
- signal,
386
- in: {
387
- post_id,
388
- reaction_type: opts.reaction_type,
389
- },
390
- });
391
- signal.throwIfAborted();
392
- this.markAction(post_id, FeedAction.Reaction, operationID(out));
393
- return out;
394
- });
372
+ return this.setReaction(opts, opts.reaction_type);
395
373
  }
396
374
  async removeReaction(opts) {
375
+ return this.setReaction(opts, null);
376
+ }
377
+ async setReaction(opts, type) {
397
378
  const post_id = feedPostID(opts.post);
398
- return await this.runAction(post_id, FeedAction.Reaction, async (signal) => {
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." });
399
382
  const fresh = await this.requireFeedPost(post_id, signal);
400
383
  const id = resolvedID(fresh.self.reaction_id);
401
- if (!id)
402
- throw errValidation({ msg: "This action is no longer present." });
403
- const out = await this.feed.deleteOneFeedReaction({
404
- signal,
405
- id,
406
- });
407
- signal.throwIfAborted();
408
- this.markActionRemoved(post_id, FeedAction.Reaction);
409
- return out;
384
+ let existing = [];
385
+ if (id) {
386
+ const current = await this.readOwnReaction({ id, signal });
387
+ if (!current)
388
+ throw errValidation({ msg: msgFeedReactionMissing });
389
+ if (current.reaction_type === FeedReport &&
390
+ type !== null && type !== FeedReport)
391
+ throw errValidation({ msg: msgFeedReactionAlreadyExists });
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
+ });
401
+ signal.throwIfAborted();
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
+ }
447
+ }
410
448
  });
411
449
  }
412
450
  async addFavorite(opts) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.163",
3
+ "version": "0.0.165",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {