@fabriktor/fx 0.0.164 → 0.0.166

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.
@@ -1,9 +1,4 @@
1
1
  import type { CallsOperator, NotificationsOperator } from "@fabriktor/client";
2
- import type { Event } from "@fabriktor/schema";
3
- type EventInsertOptions = Parameters<CallsOperator["insertOneEvent"]>[0];
4
- type EventReplaceOptions = Parameters<CallsOperator["replaceOneEvent"]>[0];
5
- export type CreateEventOptions = EventInsertOptions;
6
- export type ReplaceEventOptions = EventReplaceOptions;
7
2
  export type MintVideoTokenOptions = Parameters<CallsOperator["mintVideoToken"]>[0];
8
3
  export type RelayNotificationOptions = Parameters<NotificationsOperator["relayNotification"]>[0];
9
4
  export type StartCallOptions = {
@@ -22,8 +17,6 @@ export type CallsFXOptions = {
22
17
  notifications: NotificationsOperator;
23
18
  };
24
19
  export interface CallsFXOperator {
25
- createEvent(opts: CreateEventOptions): Promise<Event>;
26
- replaceEvent(opts: ReplaceEventOptions): Promise<Event>;
27
20
  startCall(opts: StartCallOptions): Promise<string>;
28
21
  answerCall(opts: AnswerCallOptions): Promise<string>;
29
22
  cancelCall(opts: CancelCallOptions): Promise<void>;
@@ -32,13 +25,9 @@ export declare class CallsFX implements CallsFXOperator {
32
25
  private calls;
33
26
  private notifications;
34
27
  constructor(opts: CallsFXOptions);
35
- createEvent(opts: CreateEventOptions): Promise<Event>;
36
- replaceEvent(opts: ReplaceEventOptions): Promise<Event>;
37
28
  startCall(opts: StartCallOptions): Promise<string>;
38
29
  answerCall(opts: AnswerCallOptions): Promise<string>;
39
30
  cancelCall(opts: CancelCallOptions): Promise<void>;
40
- private findEvent;
41
31
  private requireVideoToken;
42
32
  }
43
33
  export declare function newCallsFX(opts: CallsFXOptions): CallsFX;
44
- export {};
@@ -4,11 +4,7 @@
4
4
  // not use this file except in compliance with the License. You may obtain
5
5
  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
6
  import { errResolveFailed } from "../core/err.js";
7
- import { requiredOperationID } from "../core/id.js";
8
- const resourceEvent = "event";
9
7
  const resourceVideoToken = "video_token";
10
- const msgCreatedEventIDMissing = "Created event response did not contain an ID.";
11
- const msgEventMissing = "Event could not be resolved after the mutation.";
12
8
  const msgUnableToMintVideoToken = "Unable to mint video token.";
13
9
  export class CallsFX {
14
10
  calls;
@@ -17,15 +13,6 @@ export class CallsFX {
17
13
  this.calls = opts.calls;
18
14
  this.notifications = opts.notifications;
19
15
  }
20
- async createEvent(opts) {
21
- const out = await this.calls.insertOneEvent(opts);
22
- const id = requiredOperationID(out, msgCreatedEventIDMissing);
23
- return await this.findEvent(id);
24
- }
25
- async replaceEvent(opts) {
26
- await this.calls.replaceOneEvent(opts);
27
- return await this.findEvent(opts.id);
28
- }
29
16
  async startCall(opts) {
30
17
  const token = await this.requireVideoToken(opts.mint);
31
18
  await this.notifications.relayNotification(opts.notification);
@@ -39,19 +26,6 @@ export class CallsFX {
39
26
  async cancelCall(opts) {
40
27
  await this.notifications.relayNotification(opts.notification);
41
28
  }
42
- async findEvent(id) {
43
- const out = await this.calls.findOneEvent({
44
- id,
45
- });
46
- const event = out.value;
47
- if (event === undefined) {
48
- throw errResolveFailed({
49
- resource: resourceEvent,
50
- msg: msgEventMissing,
51
- });
52
- }
53
- return event;
54
- }
55
29
  async requireVideoToken(opts) {
56
30
  const out = await this.calls.mintVideoToken(opts);
57
31
  const token = out.value?.trim();
@@ -11,6 +11,7 @@ import { type SaveFeedPostOptions, type FeedPostSaveResult, type FeedPostSaveSta
11
11
  export declare const FeedEvents: {
12
12
  Changed: import("../index.js").EventKey<{
13
13
  post_id: string;
14
+ deleted?: boolean;
14
15
  }>;
15
16
  };
16
17
  export type FeedReadOptions = {
@@ -81,12 +82,16 @@ export type StartFeedSessionOptions = FeedPullContext & {
81
82
  signal?: AbortSignal;
82
83
  on_error?: FeedSessionErrorHandler;
83
84
  };
84
- export type AddFeedReactionOptions = {
85
- post: FeedPost;
85
+ export type FeedReactionChange = {
86
+ reaction_type: FeedReactionType | null;
87
+ count_delta: FeedPost["stats"]["count_per_reaction"];
88
+ };
89
+ export type AddFeedReactionOptions = RemoveFeedReactionOptions & {
86
90
  reaction_type: FeedReactionType;
87
91
  };
88
92
  export type RemoveFeedReactionOptions = {
89
93
  post: FeedPost;
94
+ on_change?: (change: FeedReactionChange) => void;
90
95
  };
91
96
  export type AddFeedFavoriteOptions = {
92
97
  post: FeedPost;
@@ -234,6 +239,7 @@ export declare class FeedFX implements FeedFXOperator {
234
239
  startSession(opts: StartFeedSessionOptions): Promise<FeedSession>;
235
240
  addReaction(opts: AddFeedReactionOptions): Promise<OperationResult>;
236
241
  removeReaction(opts: RemoveFeedReactionOptions): Promise<OperationResult>;
242
+ private setReaction;
237
243
  addFavorite(opts: AddFeedFavoriteOptions): Promise<OperationResult>;
238
244
  removeFavorite(opts: RemoveFeedFavoriteOptions): Promise<OperationResult>;
239
245
  createReply(opts: CreateFeedReplyOptions): Promise<OperationResult>;
@@ -11,12 +11,14 @@ 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";
18
18
  import { saveFeedPost, newFeedPostSaveState, } from "./media.js";
19
- export const FeedEvents = { Changed: createEvent() };
19
+ export const FeedEvents = {
20
+ Changed: createEvent(),
21
+ };
20
22
  const newFeedSessionToken = "";
21
23
  const resourceFeedSessionToken = "feed_session_token";
22
24
  const resourceFeedPost = "feed_post";
@@ -252,7 +254,7 @@ export class FeedFX {
252
254
  });
253
255
  await this.feed.deleteOneFeedPost({ id: current.id, signal });
254
256
  signal.throwIfAborted();
255
- this.changed(current.id);
257
+ this.changed(current.id, true);
256
258
  if (resolvedID(current.reply_to_post_id))
257
259
  this.changed(current.reply_to_post_id);
258
260
  if (resolvedID(current.referenced_post_id))
@@ -276,8 +278,8 @@ export class FeedFX {
276
278
  });
277
279
  return post;
278
280
  }
279
- changed(post_id) {
280
- this.events.publish(FeedEvents.Changed, { post_id });
281
+ changed(post_id, deleted = false) {
282
+ this.events.publish(FeedEvents.Changed, { post_id, deleted });
281
283
  }
282
284
  async mutate(fn, parent) {
283
285
  const signal = this.sessions.signal(parent);
@@ -369,52 +371,82 @@ export class FeedFX {
369
371
  });
370
372
  }
371
373
  async addReaction(opts) {
374
+ return this.setReaction(opts, opts.reaction_type);
375
+ }
376
+ async removeReaction(opts) {
377
+ return this.setReaction(opts, null);
378
+ }
379
+ async setReaction(opts, type) {
372
380
  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
- });
381
+ return this.runAction(post_id, FeedAction.Reaction, async (signal) => {
382
+ if (type !== null && ![FeedLike, FeedDislike, FeedReport].includes(type))
383
+ throw errValidation({ field: "reaction_type", msg: "Unsupported reaction." });
379
384
  const fresh = await this.requireFeedPost(post_id, signal);
380
385
  const id = resolvedID(fresh.self.reaction_id);
386
+ let existing = [];
381
387
  if (id) {
382
388
  const current = await this.readOwnReaction({ id, signal });
383
389
  if (!current)
384
390
  throw errValidation({ msg: msgFeedReactionMissing });
385
- if (current.reaction_type === opts.reaction_type ||
386
- current.reaction_type === FeedReport)
391
+ if (current.reaction_type === FeedReport &&
392
+ type !== null && type !== FeedReport)
387
393
  throw errValidation({ msg: msgFeedReactionAlreadyExists });
388
- await this.feed.deleteOneFeedReaction({ id, signal });
394
+ // The query route is unpaged. Scope it to the authenticated user's
395
+ // resolved reaction owner, never to the author of the post.
396
+ const out = await this.feed.queryFeedReactions({
397
+ query: queryParams({
398
+ owner_id: requireFeedID(current.owner_id),
399
+ post_id,
400
+ }),
401
+ signal,
402
+ });
389
403
  signal.throwIfAborted();
390
- this.markActionRemoved(post_id, FeedAction.Reaction);
404
+ existing = out.value ?? [];
405
+ }
406
+ const keep = existing.find((reaction) => reaction.reaction_type === type);
407
+ const remaining = new Map(existing.map((reaction) => [reaction.id, reaction]));
408
+ const count_delta = {};
409
+ let inserted_id;
410
+ let out = {};
411
+ try {
412
+ for (const reaction of existing) {
413
+ if (reaction.id === keep?.id)
414
+ continue;
415
+ signal.throwIfAborted();
416
+ out = await this.feed.deleteOneFeedReaction({ id: reaction.id, signal });
417
+ signal.throwIfAborted();
418
+ remaining.delete(reaction.id);
419
+ count_delta[reaction.reaction_type] =
420
+ (count_delta[reaction.reaction_type] ?? 0) - 1;
421
+ }
422
+ if (keep)
423
+ return { id: keep.id };
424
+ if (type === null)
425
+ return out;
426
+ out = await this.feed.insertOneFeedReaction({
427
+ signal,
428
+ in: { post_id, reaction_type: type },
429
+ });
430
+ signal.throwIfAborted();
431
+ inserted_id = requiredOperationID(out, msgCreatedFeedReactionIDMissing);
432
+ count_delta[type] = (count_delta[type] ?? 0) + 1;
433
+ return out;
434
+ }
435
+ finally {
436
+ if (!signal.aborted) {
437
+ const current = remaining.values().next().value;
438
+ const current_id = inserted_id ?? current?.id;
439
+ if (current_id)
440
+ this.markAction(post_id, FeedAction.Reaction, current_id);
441
+ else
442
+ this.markActionRemoved(post_id, FeedAction.Reaction);
443
+ // Report acknowledged changes even if a later deletion/insertion fails.
444
+ notifyObserver(() => opts.on_change?.({
445
+ reaction_type: inserted_id ? type : (current?.reaction_type ?? null),
446
+ count_delta,
447
+ }));
448
+ }
391
449
  }
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
450
  });
419
451
  }
420
452
  async addFavorite(opts) {
@@ -489,6 +521,7 @@ export class FeedFX {
489
521
  id,
490
522
  });
491
523
  signal.throwIfAborted();
524
+ this.changed(id, true);
492
525
  this.markActionRemoved(post_id, FeedAction.Reply);
493
526
  return out;
494
527
  });
@@ -530,6 +563,7 @@ export class FeedFX {
530
563
  id,
531
564
  });
532
565
  signal.throwIfAborted();
566
+ this.changed(id, true);
533
567
  this.markActionRemoved(post_id, FeedAction.Reference);
534
568
  return out;
535
569
  });
@@ -6,15 +6,21 @@ export type NotificationsFXOptions = {
6
6
  };
7
7
  export type SyncDeviceTokenOptions = {
8
8
  owner_id: string;
9
+ previous_id?: string;
10
+ };
11
+ export type UnregisterDeviceTokenOptions = {
12
+ id: string;
9
13
  };
10
14
  export type SyncDeviceTokenResult = Awaited<ReturnType<NotificationsOperator["upsertOneDTok"]>>;
11
15
  export interface NotificationsFXOperator {
12
16
  syncDeviceToken(opts: SyncDeviceTokenOptions): Promise<SyncDeviceTokenResult>;
17
+ unregisterDeviceToken(opts: UnregisterDeviceTokenOptions): Promise<void>;
13
18
  }
14
19
  export declare class NotificationsFX implements NotificationsFXOperator {
15
20
  private notifications;
16
21
  private getDeviceToken;
17
22
  constructor(opts: NotificationsFXOptions);
18
23
  syncDeviceToken(opts: SyncDeviceTokenOptions): Promise<SyncDeviceTokenResult>;
24
+ unregisterDeviceToken(opts: UnregisterDeviceTokenOptions): Promise<void>;
19
25
  }
20
26
  export declare function newNotificationsFX(opts: NotificationsFXOptions): NotificationsFX;
@@ -5,8 +5,11 @@
5
5
  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
6
  import { ZeroID } from "@fabriktor/schema";
7
7
  import { errRequired } from "../core/err.js";
8
+ import { requiredOperationID, resolvedID } from "../core/id.js";
8
9
  const msgOwnerIDRequired = "Owner ID is required.";
9
10
  const msgDeviceTokenRequired = "Device token is required.";
11
+ const msgDeviceRegistrationRequired = "Device registration is required.";
12
+ const msgDeviceRegistrationMissing = "Device registration response did not contain an ID.";
10
13
  export class NotificationsFX {
11
14
  notifications;
12
15
  getDeviceToken;
@@ -37,7 +40,23 @@ export class NotificationsFX {
37
40
  owner_id,
38
41
  },
39
42
  };
40
- return await this.notifications.upsertOneDTok(input);
43
+ const out = await this.notifications.upsertOneDTok(input);
44
+ const id = requiredOperationID(out, msgDeviceRegistrationMissing);
45
+ const previousID = resolvedID(opts.previous_id);
46
+ if (previousID !== undefined && previousID !== id) {
47
+ await this.notifications.deleteOneDTok({ id: previousID });
48
+ }
49
+ return out;
50
+ }
51
+ async unregisterDeviceToken(opts) {
52
+ const id = resolvedID(opts.id);
53
+ if (id === undefined) {
54
+ throw errRequired({
55
+ field: "id",
56
+ msg: msgDeviceRegistrationRequired,
57
+ });
58
+ }
59
+ await this.notifications.deleteOneDTok({ id });
41
60
  }
42
61
  }
43
62
  export function newNotificationsFX(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.166",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,9 +17,9 @@
17
17
  "typescript": "^6.0.3"
18
18
  },
19
19
  "dependencies": {
20
- "@fabriktor/client": "0.0.101",
20
+ "@fabriktor/client": "0.0.102",
21
21
  "@fabriktor/fx": "0.0.94",
22
- "@fabriktor/schema": "0.0.73",
22
+ "@fabriktor/schema": "0.0.74",
23
23
  "convert": "8.0.2"
24
24
  }
25
25
  }