@fabriktor/fx 0.0.183 → 0.0.185

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.
@@ -55,6 +55,7 @@ export * from "./routes/tracking.js";
55
55
  export * from "./routes/validation.js";
56
56
  export * from "./routes/workspace.js";
57
57
  export * from "./session/session.js";
58
+ export * from "./storage/capture.js";
58
59
  export * from "./storage/storage.js";
59
60
  export * from "./timesheets/access.js";
60
61
  export * from "./timesheets/timesheets.js";
package/dist/src/index.js CHANGED
@@ -55,6 +55,7 @@ export * from "./routes/tracking.js";
55
55
  export * from "./routes/validation.js";
56
56
  export * from "./routes/workspace.js";
57
57
  export * from "./session/session.js";
58
+ export * from "./storage/capture.js";
58
59
  export * from "./storage/storage.js";
59
60
  export * from "./timesheets/access.js";
60
61
  export * from "./timesheets/timesheets.js";
@@ -5,6 +5,7 @@ import { type NotificationsFXOperator } from "./notifications.js";
5
5
  export type NotificationInboxState = SearchState<Notification> & {
6
6
  readonly identity: string;
7
7
  readonly reading: readonly string[];
8
+ readonly deleting: readonly string[];
8
9
  readonly has_unread: boolean;
9
10
  };
10
11
  export type NotificationInboxOptions = {
@@ -17,6 +18,7 @@ export declare class NotificationInboxController {
17
18
  private readonly options;
18
19
  private readonly search;
19
20
  private readonly stopRead;
21
+ private readonly stopDelete;
20
22
  private ownerID;
21
23
  private identity;
22
24
  private session;
@@ -25,6 +27,8 @@ export declare class NotificationInboxController {
25
27
  private unreadRequest?;
26
28
  private hasUnread;
27
29
  private readonly reads;
30
+ private readonly deletes;
31
+ private readonly deleted;
28
32
  constructor(options: NotificationInboxOptions);
29
33
  get state(): NotificationInboxState;
30
34
  setOwner(owner_id?: string, session_id?: string): void;
@@ -32,6 +36,7 @@ export declare class NotificationInboxController {
32
36
  loadMore(): Promise<void>;
33
37
  retry(): Promise<void>;
34
38
  markRead(id: string): Promise<void>;
39
+ delete(id: string): Promise<boolean>;
35
40
  dispose(): void;
36
41
  private refreshUnread;
37
42
  private clear;
@@ -7,6 +7,7 @@ export class NotificationInboxController {
7
7
  options;
8
8
  search;
9
9
  stopRead;
10
+ stopDelete;
10
11
  ownerID = "";
11
12
  identity = "";
12
13
  session = new AbortController();
@@ -15,6 +16,8 @@ export class NotificationInboxController {
15
16
  unreadRequest;
16
17
  hasUnread = false;
17
18
  reads = new Map();
19
+ deletes = new Map();
20
+ deleted = new Set();
18
21
  constructor(options) {
19
22
  this.options = options;
20
23
  this.search = new SearchController({
@@ -27,12 +30,29 @@ export class NotificationInboxController {
27
30
  this.search.patchResult(id, { is_read: true });
28
31
  void this.refreshUnread();
29
32
  });
33
+ this.stopDelete = options.events.subscribe(NotificationsEvents.Deleted, ({ owner_id, id }) => {
34
+ if (owner_id !== this.ownerID)
35
+ return;
36
+ this.deleted.add(id);
37
+ this.unreadRequest?.abort();
38
+ this.publish();
39
+ // Refill the displayed pages: deleting a row shifts later page offsets.
40
+ void this.refresh();
41
+ });
30
42
  }
31
43
  get state() {
44
+ const state = this.search.state;
45
+ // An older in-flight response must never restore a confirmed deletion.
46
+ const results = state.results.filter((item) => !this.deleted.has(item.id));
32
47
  return {
33
- ...this.search.state,
48
+ ...state,
49
+ results,
50
+ total_count: state.total_count === undefined
51
+ ? undefined
52
+ : Math.max(0, state.total_count - (state.results.length - results.length)),
34
53
  identity: this.identity,
35
54
  reading: [...this.reads.keys()],
55
+ deleting: [...this.deletes.keys()],
36
56
  has_unread: this.hasUnread,
37
57
  };
38
58
  }
@@ -88,7 +108,7 @@ export class NotificationInboxController {
88
108
  await Promise.all([this.search.retry(), this.refreshUnread()]);
89
109
  }
90
110
  markRead(id) {
91
- if (!this.ownerID)
111
+ if (!this.ownerID || this.deleted.has(id) || this.deletes.has(id))
92
112
  return Promise.resolve();
93
113
  const pending = this.reads.get(id);
94
114
  if (pending)
@@ -115,8 +135,47 @@ export class NotificationInboxController {
115
135
  this.publish();
116
136
  return run;
117
137
  }
138
+ delete(id) {
139
+ if (!this.ownerID)
140
+ return Promise.resolve(false);
141
+ if (this.deleted.has(id))
142
+ return Promise.resolve(true);
143
+ const pending = this.deletes.get(id);
144
+ if (pending)
145
+ return pending;
146
+ const session = this.session;
147
+ const owner_id = this.ownerID;
148
+ const run = Promise.resolve()
149
+ .then(async () => {
150
+ // Finish an already-started read before deleting the same document.
151
+ await this.reads.get(id)?.catch(() => undefined);
152
+ if (session.signal.aborted)
153
+ return false;
154
+ await this.options.notifications.deleteNotification({
155
+ id,
156
+ owner_id,
157
+ signal: session.signal,
158
+ });
159
+ return !session.signal.aborted;
160
+ })
161
+ .catch((error) => {
162
+ if (session.signal.aborted)
163
+ return false;
164
+ throw error;
165
+ })
166
+ .finally(() => {
167
+ if (this.deletes.get(id) !== run)
168
+ return;
169
+ this.deletes.delete(id);
170
+ this.publish();
171
+ });
172
+ this.deletes.set(id, run);
173
+ this.publish();
174
+ return run;
175
+ }
118
176
  dispose() {
119
177
  this.stopRead();
178
+ this.stopDelete();
120
179
  this.clear();
121
180
  }
122
181
  async refreshUnread() {
@@ -155,6 +214,8 @@ export class NotificationInboxController {
155
214
  this.refreshing = undefined;
156
215
  this.refreshPending = false;
157
216
  this.reads.clear();
217
+ this.deletes.clear();
218
+ this.deleted.clear();
158
219
  this.search.clear();
159
220
  }
160
221
  publish() {
@@ -4,6 +4,7 @@ import { EventBus } from "../core/events.js";
4
4
  import { type OwnerSearchOptions, type SearchPage } from "../core/search.js";
5
5
  export declare const NotificationsEvents: {
6
6
  readonly Read: import("../index.js").EventKey<Pick<Notification, "id" | "owner_id">>;
7
+ readonly Deleted: import("../index.js").EventKey<Pick<Notification, "id" | "owner_id">>;
7
8
  };
8
9
  export type DeviceTokenProvider = () => Promise<string>;
9
10
  export type NotificationsFXOptions = {
@@ -14,6 +15,7 @@ export type NotificationsFXOptions = {
14
15
  export type SearchNotificationsOptions = OwnerSearchOptions & Partial<Pick<Notification, "is_read" | "notif_k" | "key">>;
15
16
  export type SearchNotificationsPage = SearchPage<Notification>;
16
17
  export type MarkNotificationReadOptions = RequestOptions & Pick<Notification, "id" | "owner_id">;
18
+ export type DeleteNotificationOptions = Pick<RequestOptions, "signal"> & Pick<Notification, "id" | "owner_id">;
17
19
  export type SyncDeviceTokenOptions = {
18
20
  owner_id: string;
19
21
  previous_id?: string;
@@ -28,6 +30,7 @@ export type SyncDeviceTokenResult = Awaited<ReturnType<NotificationsOperator["up
28
30
  export interface NotificationsFXOperator {
29
31
  searchNotifications(opts: SearchNotificationsOptions): Promise<SearchNotificationsPage>;
30
32
  markNotificationRead(opts: MarkNotificationReadOptions): Promise<void>;
33
+ deleteNotification(opts: DeleteNotificationOptions): Promise<void>;
31
34
  syncDeviceToken(opts: SyncDeviceTokenOptions): Promise<SyncDeviceTokenResult>;
32
35
  unregisterDeviceToken(opts: UnregisterDeviceTokenOptions): Promise<void>;
33
36
  }
@@ -38,6 +41,7 @@ export declare class NotificationsFX implements NotificationsFXOperator {
38
41
  constructor(opts: NotificationsFXOptions);
39
42
  searchNotifications(opts: SearchNotificationsOptions): Promise<SearchNotificationsPage>;
40
43
  markNotificationRead(opts: MarkNotificationReadOptions): Promise<void>;
44
+ deleteNotification(opts: DeleteNotificationOptions): Promise<void>;
41
45
  syncDeviceToken(opts: SyncDeviceTokenOptions): Promise<SyncDeviceTokenResult>;
42
46
  unregisterDeviceToken(opts: UnregisterDeviceTokenOptions): Promise<void>;
43
47
  }
@@ -1,11 +1,12 @@
1
1
  import { IDKey, OwnerIDKey, ZeroID, PushTransportFCM, } from "@fabriktor/schema";
2
- import { queryParams, } from "@fabriktor/client";
2
+ import { isClientErr, queryParams, } from "@fabriktor/client";
3
3
  import { errRequired } from "../core/err.js";
4
4
  import { createEvent, EventBus } from "../core/events.js";
5
5
  import { requiredOperationID, resolvedID } from "../core/id.js";
6
6
  import { runOwnerSearch, } from "../core/search.js";
7
7
  export const NotificationsEvents = {
8
8
  Read: createEvent(),
9
+ Deleted: createEvent(),
9
10
  };
10
11
  const msgOwnerIDRequired = "Owner ID is required.";
11
12
  const msgNotificationIDRequired = "Notification ID is required.";
@@ -60,6 +61,35 @@ export class NotificationsFX {
60
61
  });
61
62
  this.events.publish(NotificationsEvents.Read, { owner_id, id });
62
63
  }
64
+ async deleteNotification(opts) {
65
+ const owner_id = resolvedID(opts.owner_id);
66
+ if (owner_id === undefined) {
67
+ throw errRequired({
68
+ field: OwnerIDKey,
69
+ msg: msgOwnerIDRequired,
70
+ });
71
+ }
72
+ const id = resolvedID(opts.id);
73
+ if (id === undefined) {
74
+ throw errRequired({
75
+ field: IDKey,
76
+ msg: msgNotificationIDRequired,
77
+ });
78
+ }
79
+ opts.signal?.throwIfAborted();
80
+ try {
81
+ await this.notifications.deleteOneNotification({
82
+ id,
83
+ signal: opts.signal,
84
+ });
85
+ }
86
+ catch (error) {
87
+ if (!isClientErr(error) || error.status !== 404)
88
+ throw error;
89
+ }
90
+ opts.signal?.throwIfAborted();
91
+ this.events.publish(NotificationsEvents.Deleted, { owner_id, id });
92
+ }
63
93
  async syncDeviceToken(opts) {
64
94
  const owner_id = opts.owner_id.trim();
65
95
  if (owner_id === "" || owner_id === ZeroID) {
@@ -0,0 +1,42 @@
1
+ import type { InMemo } from "@fabriktor/schema";
2
+ import type { ChatFXOperator } from "../chat/chat.js";
3
+ import type { ChatSendQueue } from "../chat/send-queue.js";
4
+ import { type StorageFXOperator, type StorageUploadResult } from "./storage.js";
5
+ export declare const CaptureKind: {
6
+ readonly Photo: "photo";
7
+ readonly Video: "video";
8
+ };
9
+ export type CaptureKind = (typeof CaptureKind)[keyof typeof CaptureKind];
10
+ export declare function captureKinds(accept?: string): CaptureKind[];
11
+ export declare const RecordingDestination: {
12
+ readonly Chat: "chat";
13
+ readonly Memo: "memo";
14
+ readonly Storage: "storage";
15
+ };
16
+ export type RecordingDestination = (typeof RecordingDestination)[keyof typeof RecordingDestination];
17
+ export type RecordedMemoDraft = {
18
+ input: InMemo;
19
+ files: File[];
20
+ };
21
+ export type RecordedFileWorkflowOptions = {
22
+ file: File;
23
+ owner_id: string;
24
+ current: () => boolean;
25
+ chat: Pick<ChatFXOperator, "ensureChatRoom">;
26
+ queue: Pick<ChatSendQueue, "enqueue">;
27
+ storage: Pick<StorageFXOperator, "uploadUserObjects">;
28
+ };
29
+ export declare class RecordedFileWorkflow {
30
+ private readonly opts;
31
+ private readonly abort;
32
+ private readonly key;
33
+ private busy;
34
+ private completed;
35
+ constructor(opts: RecordedFileWorkflowOptions);
36
+ sendToChat(members: readonly string[]): Promise<string | undefined>;
37
+ saveToStorage(): Promise<StorageUploadResult | undefined>;
38
+ memoDraft(label: string): RecordedMemoDraft | undefined;
39
+ dispose(): void;
40
+ private current;
41
+ private ready;
42
+ }
@@ -0,0 +1,102 @@
1
+ import { newChatInput } from "../chat/optimistic.js";
2
+ import { validateStorageUploadFiles, } from "./storage.js";
3
+ export const CaptureKind = { Photo: "photo", Video: "video" };
4
+ export function captureKinds(accept = "") {
5
+ const types = accept
6
+ .toLowerCase()
7
+ .split(",")
8
+ .map((value) => value.trim());
9
+ const all = !accept.trim() || types.includes("*/*");
10
+ const out = [];
11
+ if (all ||
12
+ types.some((value) => ["image/*", "image/jpeg", ".jpg", ".jpeg"].includes(value)))
13
+ out.push(CaptureKind.Photo);
14
+ if (all ||
15
+ types.some((value) => [
16
+ "video/*",
17
+ "video/mp4",
18
+ "video/webm",
19
+ "video/quicktime",
20
+ ".mp4",
21
+ ".webm",
22
+ ".mov",
23
+ ].includes(value)))
24
+ out.push(CaptureKind.Video);
25
+ return out;
26
+ }
27
+ export const RecordingDestination = {
28
+ Chat: "chat",
29
+ Memo: "memo",
30
+ Storage: "storage",
31
+ };
32
+ // One recording, one destination. Retrying storage keeps its idempotency key.
33
+ export class RecordedFileWorkflow {
34
+ opts;
35
+ abort = new AbortController();
36
+ key = crypto.randomUUID();
37
+ busy = false;
38
+ completed = false;
39
+ constructor(opts) {
40
+ this.opts = opts;
41
+ validateStorageUploadFiles([opts.file]);
42
+ }
43
+ async sendToChat(members) {
44
+ if (!this.ready() || !members.length)
45
+ return;
46
+ this.busy = true;
47
+ try {
48
+ const room = await this.opts.chat.ensureChatRoom({
49
+ members: [...new Set(members)],
50
+ });
51
+ if (!this.current())
52
+ return;
53
+ const out = this.opts.queue.enqueue(newChatInput({ chat_room_id: room, text: "", has_objects: true }), {
54
+ files: [this.opts.file],
55
+ save_to_personal_box: false,
56
+ full_quality: false,
57
+ });
58
+ this.completed = true;
59
+ return out;
60
+ }
61
+ finally {
62
+ this.busy = false;
63
+ }
64
+ }
65
+ async saveToStorage() {
66
+ if (!this.ready())
67
+ return;
68
+ this.busy = true;
69
+ try {
70
+ const out = await this.opts.storage.uploadUserObjects({
71
+ user_id: this.opts.owner_id,
72
+ uploader_id: this.opts.owner_id,
73
+ files: [this.opts.file],
74
+ full_quality: false,
75
+ idempotency_key: this.key,
76
+ signal: this.abort.signal,
77
+ });
78
+ this.completed = true;
79
+ return this.current() ? out : undefined;
80
+ }
81
+ finally {
82
+ this.busy = false;
83
+ }
84
+ }
85
+ memoDraft(label) {
86
+ if (!this.ready())
87
+ return;
88
+ return {
89
+ input: { is_archived: false, folder_id: "", title: "", text: label },
90
+ files: [this.opts.file],
91
+ };
92
+ }
93
+ dispose() {
94
+ this.abort.abort();
95
+ }
96
+ current() {
97
+ return !this.abort.signal.aborted && this.opts.current();
98
+ }
99
+ ready() {
100
+ return !this.busy && !this.completed && this.current();
101
+ }
102
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.183",
3
+ "version": "0.0.185",
4
4
  "description": "![Fabriktor Logo](./img/fabriktor-character.gif)",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  "typescript": "^6.0.3"
18
18
  },
19
19
  "dependencies": {
20
- "@fabriktor/client": "0.0.112",
20
+ "@fabriktor/client": "0.0.113",
21
21
  "@fabriktor/fx": "0.0.94",
22
22
  "@fabriktor/schema": "0.0.84",
23
23
  "convert": "8.0.2"