@fabriktor/fx 0.0.167 → 0.0.168

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.
@@ -17,8 +17,7 @@ export class CallsFX {
17
17
  createSession(opts) {
18
18
  return new CallSession({
19
19
  ...opts,
20
- mint: (mint) => this.requireVideoToken(mint),
21
- notifications: this.notifications,
20
+ calls: this.calls,
22
21
  });
23
22
  }
24
23
  async startCall(opts) {
@@ -1,4 +1,4 @@
1
- import type { CallsOperator, NotificationsOperator } from "@fabriktor/client";
1
+ import type { CallsOperator } from "@fabriktor/client";
2
2
  import { CallModeAudio, CallModeVideo } from "@fabriktor/schema";
3
3
  export declare const callsNotificationKey = "calls.events.received";
4
4
  export declare const callRingTimeoutMS = 60000;
@@ -23,6 +23,7 @@ export interface CallMediaConnection {
23
23
  export interface CallMediaAdapter {
24
24
  connect(opts: {
25
25
  token: string;
26
+ call: CallInvitation;
26
27
  room_id: string;
27
28
  video: boolean;
28
29
  signal: AbortSignal;
@@ -35,6 +36,8 @@ export type CallInvitation = {
35
36
  room_id: string;
36
37
  caller_id: string;
37
38
  mode: CallMode;
39
+ ticket: string;
40
+ expires_at: number;
38
41
  };
39
42
  export type CallSessionState = {
40
43
  phase: CallPhase;
@@ -45,18 +48,19 @@ export type CallSessionState = {
45
48
  };
46
49
  export type CallSessionOptions = {
47
50
  owner_id: string;
51
+ device_id: string;
48
52
  media: CallMediaAdapter;
49
53
  on_change: (state: CallSessionState) => void;
50
54
  on_error: (error: unknown) => void;
51
55
  };
52
56
  export type CallSessionDependencies = CallSessionOptions & {
53
- mint: (opts: Parameters<CallsOperator["mintVideoToken"]>[0]) => Promise<string>;
54
- notifications: NotificationsOperator;
57
+ calls: CallsOperator;
55
58
  };
56
59
  export declare class CallSession {
57
60
  private opts;
58
61
  state: CallSessionState;
59
62
  private connection?;
63
+ private connecting;
60
64
  private controller?;
61
65
  private timer?;
62
66
  private recipients;
@@ -67,6 +71,7 @@ export declare class CallSession {
67
71
  constructor(opts: CallSessionDependencies);
68
72
  start(room_id: string, members: readonly string[], mode: CallMode): Promise<void>;
69
73
  receive(data: Readonly<Record<string, string>>): void;
74
+ restore(call: CallInvitation, phase: CallPhase): Promise<void>;
70
75
  answer(): Promise<void>;
71
76
  end(): Promise<void>;
72
77
  microphone(enabled: boolean): Promise<void>;
@@ -74,7 +79,8 @@ export declare class CallSession {
74
79
  dispose(): void;
75
80
  private begin;
76
81
  private connect;
77
- private relay;
82
+ private access;
83
+ private respond;
78
84
  private current;
79
85
  private publish;
80
86
  private reset;
@@ -3,14 +3,12 @@
3
3
  // Licensed under the Apache License, Version 2.0 (the "License"); you may
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
- import { CallIDKey, CallRoomIDKey, CallSenderIDKey, CallModeKey, CallSignalKey, CallModeAudio, CallModeVideo, CallSignalInvite, CallSignalAnswered, CallSignalCancelled, ColCallsEvents, SvcCalls, } from "@fabriktor/schema";
7
- import { errValidation } from "../core/err.js";
6
+ import { CallIDKey, CallTicketKey, CallOwnerIDKey, CallExpiresAtKey, CallActionRing, CallActionAnswer, CallActionEnd, CallStateEnded, CallRingTimeoutMS, CallRoomIDKey, CallSenderIDKey, CallModeKey, CallSignalKey, CallModeAudio, CallModeVideo, CallSignalInvite, CallSignalAnswered, CallSignalCancelled, ColCallsEvents, SvcCalls, } from "@fabriktor/schema";
7
+ import { errValidation, errResolveFailed } from "../core/err.js";
8
8
  // Notification keys are preference-map keys, not generated Go constants.
9
9
  export const callsNotificationKey = `${SvcCalls}.${ColCallsEvents}.received`;
10
- export const callRingTimeoutMS = 60_000;
10
+ export const callRingTimeoutMS = CallRingTimeoutMS;
11
11
  const closedCallLimit = 100;
12
- const callNotificationTitle = "Fabriktor call";
13
- const callNotificationBody = "Open Fabriktor to join the call.";
14
12
  function idleCallState() {
15
13
  return {
16
14
  phase: "idle",
@@ -25,6 +23,7 @@ export class CallSession {
25
23
  opts;
26
24
  state = idleCallState();
27
25
  connection;
26
+ connecting = "";
28
27
  controller;
29
28
  timer;
30
29
  recipients = [];
@@ -48,15 +47,31 @@ export class CallSession {
48
47
  room_id,
49
48
  caller_id: this.opts.owner_id,
50
49
  mode,
50
+ ticket: "",
51
+ expires_at: Date.now() + callRingTimeoutMS,
51
52
  };
52
53
  this.recipients = recipients;
53
54
  this.begin(call, "connecting");
54
55
  try {
55
- await this.connect(call);
56
+ const out = await this.opts.calls.startCallSession({
57
+ call_id: call.id,
58
+ room_id,
59
+ device_id: this.opts.device_id,
60
+ user_ids: recipients,
61
+ mode,
62
+ });
63
+ const access = this.access(out.value);
64
+ call.ticket = access.ticket;
65
+ call.expires_at = access.expires_at;
66
+ if (!this.current(call)) {
67
+ await this.respond(call, CallActionEnd);
68
+ return;
69
+ }
70
+ await this.connect(call, access.token);
56
71
  if (!this.current(call))
57
72
  return;
58
73
  // Do not ring recipients until the caller has joined successfully.
59
- const inviting = this.relay(call, CallSignalInvite, recipients);
74
+ const inviting = this.respond(call, CallActionRing).then(() => { });
60
75
  this.inviting = { id: call.id, promise: inviting };
61
76
  await inviting;
62
77
  if (!this.current(call))
@@ -68,6 +83,8 @@ export class CallSession {
68
83
  catch (error) {
69
84
  if (this.current(call)) {
70
85
  this.reset(call);
86
+ if (call.ticket)
87
+ void this.respond(call, CallActionEnd).catch(this.opts.on_error);
71
88
  throw error;
72
89
  }
73
90
  }
@@ -79,6 +96,8 @@ export class CallSession {
79
96
  receive(data) {
80
97
  if (this.disposed)
81
98
  return;
99
+ if (data[CallOwnerIDKey] !== this.opts.owner_id)
100
+ return;
82
101
  const id = data[CallIDKey], room_id = data[CallRoomIDKey], sender = data[CallSenderIDKey];
83
102
  const mode = data[CallModeKey], signal = data[CallSignalKey];
84
103
  if (!id ||
@@ -92,14 +111,21 @@ export class CallSession {
92
111
  this.closed.has(id) ||
93
112
  call?.id === id)
94
113
  return;
95
- const incoming = { id, room_id, caller_id: sender, mode };
114
+ const ticket = data[CallTicketKey], expires_at = Number(data[CallExpiresAtKey]);
115
+ if (!ticket || !Number.isFinite(expires_at) || expires_at <= Date.now())
116
+ return;
117
+ const incoming = {
118
+ id,
119
+ room_id,
120
+ caller_id: sender,
121
+ mode,
122
+ ticket,
123
+ expires_at,
124
+ };
96
125
  if (call) {
97
126
  // Busy/decline affects this recipient only, never an entire group.
98
127
  this.remember(id);
99
- void this.relay(incoming, CallSignalCancelled, [
100
- sender,
101
- this.opts.owner_id,
102
- ]).catch(this.opts.on_error);
128
+ void this.respond(incoming, CallActionEnd).catch(this.opts.on_error);
103
129
  return;
104
130
  }
105
131
  this.begin(incoming, "incoming");
@@ -130,6 +156,22 @@ export class CallSession {
130
156
  this.reset(call);
131
157
  }
132
158
  }
159
+ // Native media can already be connected when the WebView resumes.
160
+ async restore(call, phase) {
161
+ if (this.disposed || this.closed.has(call.id))
162
+ return;
163
+ if (this.state.call && this.state.call.id !== call.id)
164
+ return;
165
+ if (!this.state.call)
166
+ this.begin(call, phase);
167
+ else
168
+ this.publish({ phase });
169
+ const current = this.state.call;
170
+ if ((phase === "connected" || phase === "ringing") &&
171
+ !this.connection &&
172
+ this.connecting !== call.id)
173
+ await this.connect(current, "");
174
+ }
133
175
  async answer() {
134
176
  const call = this.state.call;
135
177
  if (!call || this.state.phase !== "incoming")
@@ -137,10 +179,14 @@ export class CallSession {
137
179
  this.clearTimer();
138
180
  this.publish({ phase: "connecting" });
139
181
  try {
140
- await this.connect(call);
182
+ const access = await this.respond(call, CallActionAnswer);
183
+ if (!this.current(call)) {
184
+ await this.respond(call, CallActionEnd);
185
+ return;
186
+ }
187
+ await this.connect(call, access.token);
141
188
  if (!this.current(call))
142
189
  return;
143
- await this.relay(call, CallSignalAnswered, [this.opts.owner_id]);
144
190
  if (this.current(call)) {
145
191
  this.publish({ phase: this.had_remote ? "connected" : "connecting" });
146
192
  this.armTimeout(call);
@@ -149,6 +195,8 @@ export class CallSession {
149
195
  catch (error) {
150
196
  if (this.current(call)) {
151
197
  this.reset(call);
198
+ if (call.ticket)
199
+ void this.respond(call, CallActionEnd).catch(this.opts.on_error);
152
200
  throw error;
153
201
  }
154
202
  }
@@ -157,18 +205,13 @@ export class CallSession {
157
205
  const call = this.state.call;
158
206
  if (!call)
159
207
  return;
160
- const recipients = call.caller_id === this.opts.owner_id
161
- ? [...this.recipients]
162
- : this.state.phase === "incoming" || this.state.phase === "connecting"
163
- ? [call.caller_id, this.opts.owner_id]
164
- : [this.opts.owner_id];
165
208
  const inviting = this.inviting?.id === call.id ? this.inviting.promise : undefined;
166
209
  // Release devices immediately, including a pending permission/connect request.
167
210
  this.reset(call);
168
211
  if (inviting)
169
212
  await inviting.catch(() => { });
170
- if (!this.disposed && recipients.length)
171
- await this.relay(call, CallSignalCancelled, recipients);
213
+ if (call.ticket)
214
+ await this.respond(call, CallActionEnd);
172
215
  }
173
216
  async microphone(enabled) {
174
217
  const connection = this.connection;
@@ -187,9 +230,8 @@ export class CallSession {
187
230
  this.publish({ camera: enabled });
188
231
  }
189
232
  dispose() {
233
+ void this.end().catch(this.opts.on_error);
190
234
  this.disposed = true;
191
- if (this.state.call)
192
- this.reset(this.state.call);
193
235
  this.closed.clear();
194
236
  }
195
237
  begin(call, phase) {
@@ -203,68 +245,81 @@ export class CallSession {
203
245
  };
204
246
  this.opts.on_change(this.state);
205
247
  }
206
- async connect(call) {
248
+ async connect(call, token) {
207
249
  const signal = this.controller.signal;
208
- const token = await this.opts.mint({ room_id: call.room_id });
209
250
  if (!this.current(call))
210
251
  return;
211
- const connection = await this.opts.media.connect({
212
- token,
213
- room_id: call.room_id,
214
- video: call.mode === CallModeVideo,
215
- signal,
216
- on_change: (state) => {
217
- if (!this.current(call))
218
- return;
219
- const remote = state.participants.some((participant) => !participant.local);
220
- if (remote) {
221
- this.had_remote = true;
222
- this.clearTimer();
223
- }
224
- else if (this.had_remote && !state.reconnecting) {
252
+ this.connecting = call.id;
253
+ let connection;
254
+ try {
255
+ connection = await this.opts.media.connect({
256
+ token,
257
+ call,
258
+ room_id: call.room_id,
259
+ video: call.mode === CallModeVideo,
260
+ signal,
261
+ on_change: (state) => {
262
+ if (!this.current(call))
263
+ return;
264
+ const remote = state.participants.some((participant) => !participant.local);
265
+ if (remote) {
266
+ this.had_remote = true;
267
+ this.clearTimer();
268
+ }
269
+ else if (this.had_remote && !state.reconnecting) {
270
+ void this.end().catch(this.opts.on_error);
271
+ return;
272
+ }
273
+ const local = state.participants.find((participant) => participant.local);
274
+ this.publish({
275
+ participants: state.participants,
276
+ ...(local ? { microphone: local.audio, camera: local.video } : {}),
277
+ phase: state.reconnecting
278
+ ? "reconnecting"
279
+ : this.had_remote
280
+ ? "connected"
281
+ : this.state.phase,
282
+ });
283
+ },
284
+ on_disconnect: (error) => {
285
+ if (!this.current(call))
286
+ return;
225
287
  void this.end().catch(this.opts.on_error);
226
- return;
227
- }
228
- const local = state.participants.find((participant) => participant.local);
229
- this.publish({
230
- participants: state.participants,
231
- ...(local ? { microphone: local.audio, camera: local.video } : {}),
232
- phase: state.reconnecting
233
- ? "reconnecting"
234
- : this.had_remote
235
- ? "connected"
236
- : this.state.phase,
237
- });
238
- },
239
- on_disconnect: (error) => {
240
- if (!this.current(call))
241
- return;
242
- void this.end().catch(this.opts.on_error);
243
- if (error)
244
- this.opts.on_error(error);
245
- },
246
- });
288
+ if (error)
289
+ this.opts.on_error(error);
290
+ },
291
+ });
292
+ }
293
+ finally {
294
+ if (this.connecting === call.id)
295
+ this.connecting = "";
296
+ }
247
297
  if (!this.current(call))
248
298
  connection.disconnect();
249
299
  else
250
300
  this.connection = connection;
251
301
  }
252
- relay(call, signal, user_ids) {
253
- return this.opts.notifications.relayNotification({
254
- user_ids,
255
- notif_k: callsNotificationKey,
256
- // The relay owns and translates notification wording.
257
- english_title: callNotificationTitle,
258
- english_body: callNotificationBody,
259
- path_custom_image: "",
260
- url_custom_sound: "",
261
- custom_data: {
262
- [CallIDKey]: call.id,
263
- [CallRoomIDKey]: call.room_id,
264
- [CallModeKey]: call.mode,
265
- [CallSignalKey]: signal,
266
- },
302
+ access(value) {
303
+ if (!value?.ticket ||
304
+ !value.call_id ||
305
+ !value.room_id ||
306
+ value.owner_id !== this.opts.owner_id ||
307
+ value.state === CallStateEnded)
308
+ throw errResolveFailed({
309
+ resource: "call",
310
+ msg: "This call is no longer available.",
311
+ });
312
+ return value;
313
+ }
314
+ async respond(call, action) {
315
+ const out = await this.opts.calls.respondCallSession({
316
+ ticket: call.ticket,
317
+ device_id: this.opts.device_id,
318
+ action,
267
319
  });
320
+ if (action === CallActionEnd && out.value)
321
+ return out.value;
322
+ return this.access(out.value);
268
323
  }
269
324
  current(call) {
270
325
  return (!this.disposed &&
@@ -299,7 +354,7 @@ export class CallSession {
299
354
  this.timer = setTimeout(() => {
300
355
  if (this.current(call))
301
356
  void this.end().catch(this.opts.on_error);
302
- }, callRingTimeoutMS);
357
+ }, Math.max(0, call.expires_at - Date.now()));
303
358
  }
304
359
  clearTimer() {
305
360
  if (this.timer)
@@ -7,6 +7,9 @@ export type NotificationsFXOptions = {
7
7
  export type SyncDeviceTokenOptions = {
8
8
  owner_id: string;
9
9
  previous_id?: string;
10
+ token?: string;
11
+ transport?: string;
12
+ sandbox?: boolean;
10
13
  };
11
14
  export type UnregisterDeviceTokenOptions = {
12
15
  id: string;
@@ -3,7 +3,7 @@
3
3
  // Licensed under the Apache License, Version 2.0 (the "License"); you may
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
- import { ZeroID } from "@fabriktor/schema";
6
+ import { ZeroID, PushTransportFCM } from "@fabriktor/schema";
7
7
  import { errRequired } from "../core/err.js";
8
8
  import { requiredOperationID, resolvedID } from "../core/id.js";
9
9
  const msgOwnerIDRequired = "Owner ID is required.";
@@ -25,7 +25,7 @@ export class NotificationsFX {
25
25
  msg: msgOwnerIDRequired,
26
26
  });
27
27
  }
28
- const token = (await this.getDeviceToken()).trim();
28
+ const token = (opts.token ?? (await this.getDeviceToken())).trim();
29
29
  if (token === "") {
30
30
  throw errRequired({
31
31
  field: "token",
@@ -35,6 +35,8 @@ export class NotificationsFX {
35
35
  const input = {
36
36
  in: {
37
37
  token,
38
+ transport: opts.transport ?? PushTransportFCM,
39
+ sandbox: opts.sandbox ?? false,
38
40
  },
39
41
  query: {
40
42
  owner_id,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabriktor/fx",
3
- "version": "0.0.167",
3
+ "version": "0.0.168",
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.102",
20
+ "@fabriktor/client": "0.0.103",
21
21
  "@fabriktor/fx": "0.0.94",
22
- "@fabriktor/schema": "0.0.74",
22
+ "@fabriktor/schema": "0.0.75",
23
23
  "convert": "8.0.2"
24
24
  }
25
25
  }