@formant/data-sdk 0.0.58 → 0.0.62

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.
@@ -17982,6 +17982,25 @@ Found: ${e2.toString().split("\n")[0]}
17982
17982
  return r2.sort((e3, t3) => n2(t3) - n2(e3)).map((e3) => Z(e3));
17983
17983
  });
17984
17984
  }
17985
+ getSessions() {
17986
+ return K(this, void 0, void 0, function* () {
17987
+ if (this.config.lanOnlyMode)
17988
+ throw new Error("getPeers method cannot be used in local-only mode.");
17989
+ const { signalingClient: e2 } = this.config, t2 = yield (() => K(this, void 0, void 0, function* () {
17990
+ try {
17991
+ return yield e2.getPeers(new C.GetPeersRequest(), yield this.getMetadata({}));
17992
+ } catch (e3) {
17993
+ _.a.warn("Error when getting peers", { error: e3 });
17994
+ }
17995
+ }))();
17996
+ if (!t2)
17997
+ return {};
17998
+ const r2 = t2.getPeersList(), n2 = {};
17999
+ for (const e3 of r2)
18000
+ n2[e3.getPeerId()] = e3.getSessionIdsList();
18001
+ return n2;
18002
+ });
18003
+ }
17985
18004
  getPing(e2) {
17986
18005
  const t2 = this.getActiveConnection(e2);
17987
18006
  if (t2)
@@ -18377,22 +18396,34 @@ const _Authentication = class {
18377
18396
  static async loginWithToken(token, refreshToken) {
18378
18397
  const tokenData = JSON.parse(atob(token.split(".")[1]));
18379
18398
  try {
18380
- let userId = tokenData.sub;
18399
+ let userId;
18400
+ _Authentication.isShareToken = tokenData["formant:claims"] && tokenData["formant:claims"].type == "share";
18401
+ if (tokenData["formant:claims"]) {
18402
+ _Authentication.currentOrganization = tokenData["formant:claims"].organizationId;
18403
+ }
18404
+ if (tokenData["custom:organization_id"]) {
18405
+ _Authentication.currentOrganization = tokenData["custom:organization_id"];
18406
+ }
18407
+ if (!_Authentication.isShareToken) {
18408
+ userId = tokenData.sub;
18409
+ }
18381
18410
  if (tokenData["formant:claims"] && tokenData["formant:claims"].userId) {
18382
18411
  userId = tokenData["formant:claims"].userId;
18383
18412
  }
18384
- const result = await fetch(`${FORMANT_API_URL}/v1/admin/users/${userId}`, {
18385
- method: "GET",
18386
- headers: {
18387
- "Content-Type": "application/json",
18388
- Authorization: "Bearer " + token
18413
+ if (userId) {
18414
+ const result = await fetch(`${FORMANT_API_URL}/v1/admin/users/${userId}`, {
18415
+ method: "GET",
18416
+ headers: {
18417
+ "Content-Type": "application/json",
18418
+ Authorization: "Bearer " + token
18419
+ }
18420
+ });
18421
+ const data = await result.json();
18422
+ if (result.status !== 200) {
18423
+ throw new Error(data.message);
18389
18424
  }
18390
- });
18391
- const data = await result.json();
18392
- if (result.status !== 200) {
18393
- throw new Error(data.message);
18425
+ _Authentication.currentUser = data;
18394
18426
  }
18395
- _Authentication.currentUser = data;
18396
18427
  _Authentication.token = token;
18397
18428
  _Authentication.waitingForAuth.forEach((_) => _(true));
18398
18429
  } catch (e) {
@@ -18449,6 +18480,8 @@ let Authentication = _Authentication;
18449
18480
  __publicField(Authentication, "token");
18450
18481
  __publicField(Authentication, "refreshToken");
18451
18482
  __publicField(Authentication, "currentUser");
18483
+ __publicField(Authentication, "currentOrganization");
18484
+ __publicField(Authentication, "isShareToken", false);
18452
18485
  __publicField(Authentication, "defaultDeviceId");
18453
18486
  __publicField(Authentication, "waitingForAuth", []);
18454
18487
  class DataChannel {
@@ -18940,6 +18973,15 @@ class Device {
18940
18973
  throw new Error(`Realtime connection hasn't been started for ${this.id}`);
18941
18974
  }
18942
18975
  }
18976
+ async isInRealtimeSession() {
18977
+ let peers = await Fleet.getPeers();
18978
+ let sessions = await Fleet.getRealtimeSessions();
18979
+ let peer = peers.find((_) => _.deviceId === this.id);
18980
+ if (peer) {
18981
+ return sessions[peer.id].length > 0;
18982
+ }
18983
+ return false;
18984
+ }
18943
18985
  async getAvailableCommands() {
18944
18986
  const result = await fetch(`${FORMANT_API_URL}/v1/admin/command-templates/`, {
18945
18987
  method: "GET",
@@ -19077,16 +19119,17 @@ const _Fleet = class {
19077
19119
  if (!_Fleet.defaultDeviceId) {
19078
19120
  throw new Error("No known default device");
19079
19121
  }
19080
- const data = await fetch(`${FORMANT_API_URL}/v1/admin/devices/${_Fleet.defaultDeviceId}`, {
19081
- method: "GET",
19122
+ const data = await fetch(`${FORMANT_API_URL}/v1/admin/device-details/query`, {
19123
+ method: "POST",
19082
19124
  headers: {
19083
19125
  "Content-Type": "application/json",
19084
19126
  Authorization: "Bearer " + Authentication.token
19085
19127
  }
19086
19128
  });
19087
- const device = await data.json();
19129
+ const devices = await data.json();
19130
+ const device = devices.items.find((_) => _.id === _Fleet.defaultDeviceId);
19088
19131
  const name = device.name;
19089
- const context = new Device(_Fleet.defaultDeviceId, name, device.organizationId);
19132
+ const context = new Device(_Fleet.defaultDeviceId, name, defined(Authentication.currentOrganization));
19090
19133
  _Fleet.knownContext.push(new WeakRef(context));
19091
19134
  return context;
19092
19135
  }
@@ -19139,6 +19182,34 @@ const _Fleet = class {
19139
19182
  const allDevices = await _Fleet.getDevices();
19140
19183
  return allDevices.filter((_) => onlineIds.includes(_.id));
19141
19184
  }
19185
+ static async getPeers() {
19186
+ if (!Authentication.token) {
19187
+ throw new Error("Not authenticated");
19188
+ }
19189
+ const rtcClient = new dist.exports.RtcClient({
19190
+ signalingClient: new dist.exports.SignalingPromiseClient(FORMANT_API_URL, null, null),
19191
+ getToken: async () => {
19192
+ return defined(Authentication.token, "Realtime when user isn't authorized");
19193
+ },
19194
+ receive: () => {
19195
+ }
19196
+ });
19197
+ return await rtcClient.getPeers();
19198
+ }
19199
+ static async getRealtimeSessions() {
19200
+ if (!Authentication.token) {
19201
+ throw new Error("Not authenticated");
19202
+ }
19203
+ const rtcClient = new dist.exports.RtcClient({
19204
+ signalingClient: new dist.exports.SignalingPromiseClient(FORMANT_API_URL, null, null),
19205
+ getToken: async () => {
19206
+ return defined(Authentication.token, "Realtime when user isn't authorized");
19207
+ },
19208
+ receive: () => {
19209
+ }
19210
+ });
19211
+ return await rtcClient.getSessions();
19212
+ }
19142
19213
  static async getRealtimeDevices() {
19143
19214
  if (!Authentication.token) {
19144
19215
  throw new Error("Not authenticated");