@poolse/sdk 2.21.0 → 2.23.0

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.
package/dist/index.cjs CHANGED
@@ -962,6 +962,18 @@ var ConversationChannel = class {
962
962
  onMemberRead(fn) {
963
963
  return this.subscribe("member:read", fn);
964
964
  }
965
+ /** A call went live on this conversation (`call:started`). */
966
+ onCallStarted(fn) {
967
+ return this.subscribe("call:started", fn);
968
+ }
969
+ /** Someone joined or left the live call (`call:participants`): user ids in the room now. */
970
+ onCallParticipants(fn) {
971
+ return this.subscribe("call:participants", fn);
972
+ }
973
+ /** The call is over for everyone (`call:finished`). */
974
+ onCallFinished(fn) {
975
+ return this.subscribe("call:finished", fn);
976
+ }
965
977
  onPresenceState(fn) {
966
978
  let set = this.listeners.get("presence_state");
967
979
  if (!set) {
@@ -1527,13 +1539,43 @@ var MeResource = class {
1527
1539
  this.client = client;
1528
1540
  }
1529
1541
  client;
1542
+ current = null;
1543
+ listeners = /* @__PURE__ */ new Set();
1530
1544
  /** GET /v1/me */
1531
- show(signal) {
1532
- return this.client.request({
1545
+ async show(signal) {
1546
+ const me = await this.client.request({
1533
1547
  method: "GET",
1534
1548
  path: "/v1/me",
1535
1549
  ...signal ? { signal } : {}
1536
1550
  });
1551
+ this.set(me);
1552
+ return me;
1553
+ }
1554
+ /** PATCH /v1/me — the user's own preferences. */
1555
+ async update(body, signal) {
1556
+ const me = await this.client.request({
1557
+ method: "PATCH",
1558
+ path: "/v1/me",
1559
+ body,
1560
+ ...signal ? { signal } : {}
1561
+ });
1562
+ this.set(me);
1563
+ return me;
1564
+ }
1565
+ /** The last user `show` / `update` returned, or null before the first. */
1566
+ peek() {
1567
+ return this.current;
1568
+ }
1569
+ /** Called with the user whenever `show` or `update` returns a new one. */
1570
+ onChange(fn) {
1571
+ this.listeners.add(fn);
1572
+ return () => {
1573
+ this.listeners.delete(fn);
1574
+ };
1575
+ }
1576
+ set(me) {
1577
+ this.current = me;
1578
+ for (const fn of this.listeners) fn(me);
1537
1579
  }
1538
1580
  };
1539
1581
 
@@ -1659,6 +1701,28 @@ var CallTokensResource = class {
1659
1701
  }
1660
1702
  };
1661
1703
 
1704
+ // src/resources/live-calls.ts
1705
+ var LiveCallsResource = class {
1706
+ constructor(client) {
1707
+ this.client = client;
1708
+ }
1709
+ client;
1710
+ /** GET /v1/conversations/:conversationId/call — `null` when no call is live. */
1711
+ async get(conversationId, signal) {
1712
+ try {
1713
+ const res = await this.client.request({
1714
+ method: "GET",
1715
+ path: `/v1/conversations/${encodeURIComponent(conversationId)}/call`,
1716
+ ...signal ? { signal } : {}
1717
+ });
1718
+ return res.call.state === "live" ? res.call : null;
1719
+ } catch (err) {
1720
+ if (err instanceof ApiError && err.status === 404 && err.code === "no_live_call") return null;
1721
+ throw err;
1722
+ }
1723
+ }
1724
+ };
1725
+
1662
1726
  // src/voice/call-room.ts
1663
1727
  function toDevices(raw, kind) {
1664
1728
  const out = [];
@@ -2395,6 +2459,8 @@ var Poolse = class {
2395
2459
  iceServers;
2396
2460
  /** SFU credentials for a call's media — see `CallTokensResource`. */
2397
2461
  callTokens;
2462
+ /** Whether a call is running on a conversation — for a Join banner. */
2463
+ liveCalls;
2398
2464
  resolved;
2399
2465
  tokenCache;
2400
2466
  constructor(config) {
@@ -2413,6 +2479,7 @@ var Poolse = class {
2413
2479
  this.users = new UsersResource(cachedConfig);
2414
2480
  this.iceServers = new IceServersResource(this.rest);
2415
2481
  this.callTokens = new CallTokensResource(this.rest);
2482
+ this.liveCalls = new LiveCallsResource(this.rest);
2416
2483
  this.realtime = new PoolseRealtime(cachedConfig, this.tokenCache, {
2417
2484
  ...this.resolved.wsUrl !== void 0 ? { wsUrl: this.resolved.wsUrl } : {},
2418
2485
  socketPath: this.resolved.socketPath,
@@ -2455,7 +2522,7 @@ var Poolse = class {
2455
2522
  };
2456
2523
 
2457
2524
  // src/version.ts
2458
- var version = "2.20.0";
2525
+ var version = "2.23.0";
2459
2526
 
2460
2527
  // src/call-log.ts
2461
2528
  function parseCallLogBody(body) {
@@ -2477,12 +2544,25 @@ function parseCallLogBody(body) {
2477
2544
  const d = obj.duration_ms;
2478
2545
  const durationMs = typeof d === "number" && Number.isFinite(d) && d >= 0 ? d : null;
2479
2546
  const joined = Array.isArray(obj.joined_user_ids) ? obj.joined_user_ids.filter((u) => typeof u === "string") : [];
2547
+ const participants = Array.isArray(obj.participants) ? obj.participants.flatMap((p) => {
2548
+ if (!p || typeof p !== "object") return [];
2549
+ const { user_id: userId, status } = p;
2550
+ if (typeof userId !== "string" || !isParticipantStatus(status)) return [];
2551
+ return [{ userId, status }];
2552
+ }) : [];
2553
+ const num = (v) => typeof v === "number" && Number.isFinite(v) ? v : null;
2480
2554
  return {
2481
2555
  kind: "call",
2482
2556
  callId,
2483
2557
  media: media ?? "audio",
2484
2558
  durationMs,
2485
- joinedUserIds: joined
2559
+ joinedUserIds: joined,
2560
+ version: obj.v === 2 ? 2 : 1,
2561
+ callerUserId: typeof obj.caller_user_id === "string" ? obj.caller_user_id : null,
2562
+ participants,
2563
+ endReason: isEndReason(obj.end_reason) ? obj.end_reason : null,
2564
+ startedAt: num(obj.started_at),
2565
+ endedAt: num(obj.ended_at)
2486
2566
  };
2487
2567
  }
2488
2568
  if (obj.kind === "call_missed") {
@@ -2491,6 +2571,12 @@ function parseCallLogBody(body) {
2491
2571
  }
2492
2572
  return null;
2493
2573
  }
2574
+ function isParticipantStatus(v) {
2575
+ return v === "joined" || v === "declined" || v === "busy" || v === "missed";
2576
+ }
2577
+ function isEndReason(v) {
2578
+ return v === "finished" || v === "declined_all" || v === "no_answer" || v === "cancelled";
2579
+ }
2494
2580
  function parseCallLog(message) {
2495
2581
  if (message.type !== "system" && message.type !== "custom") return null;
2496
2582
  if (message.deleted_at) return null;
@@ -2512,16 +2598,64 @@ function describeCallLog(log, currentUserId) {
2512
2598
  if (log.kind === "call_missed") {
2513
2599
  if (currentUserId === null || log.userId !== currentUserId) return null;
2514
2600
  const media = log.media ?? "audio";
2515
- return { media, title: "Missed call", detail: null, missed: true, summary: "Missed call" };
2601
+ return display(media, "Missed call", null, {
2602
+ missed: true,
2603
+ direction: "incoming",
2604
+ callBack: true
2605
+ });
2516
2606
  }
2517
- const title = log.media === "video" ? "Video call" : "Voice call";
2518
- const detail = log.durationMs !== null ? formatCallDuration(log.durationMs) : "No answer";
2607
+ const kind = log.media === "video" ? "video" : "voice";
2608
+ const duration = log.durationMs !== null ? formatCallDuration(log.durationMs) : null;
2609
+ const group = log.participants.length > 1;
2610
+ const me = currentUserId;
2611
+ const mine = me !== null ? log.participants.find((p) => p.userId === me) : void 0;
2612
+ const isCaller = me !== null && log.callerUserId === me;
2613
+ if (log.version === 1 || !isCaller && !mine) {
2614
+ const title = group ? `Group ${kind} call` : log.media === "video" ? "Video call" : "Voice call";
2615
+ return display(log.media, title, duration ?? "No answer", { direction: null, callBack: false });
2616
+ }
2617
+ if (isCaller) {
2618
+ const title = `Outgoing ${kind} call`;
2619
+ if (duration)
2620
+ return display(log.media, title, duration, { direction: "outgoing", callBack: false });
2621
+ const allBusy = log.participants.length > 0 && log.participants.every((p) => p.status === "busy");
2622
+ const detail = log.endReason === "cancelled" ? "Cancelled" : log.endReason === "declined_all" ? allBusy ? "Busy" : "Declined" : "No answer";
2623
+ return display(log.media, title, detail, { direction: "outgoing", callBack: true });
2624
+ }
2625
+ switch (mine.status) {
2626
+ case "joined":
2627
+ return display(log.media, group ? `Group ${kind} call` : `Incoming ${kind} call`, duration, {
2628
+ direction: "incoming",
2629
+ callBack: false
2630
+ });
2631
+ case "declined":
2632
+ return display(log.media, `Incoming ${kind} call`, "You declined", {
2633
+ direction: "incoming",
2634
+ callBack: true
2635
+ });
2636
+ case "busy":
2637
+ return display(log.media, `Missed ${kind} call`, "You were on another call", {
2638
+ missed: true,
2639
+ direction: "incoming",
2640
+ callBack: true
2641
+ });
2642
+ case "missed":
2643
+ return display(log.media, group ? `Missed group ${kind} call` : `Missed ${kind} call`, null, {
2644
+ missed: true,
2645
+ direction: "incoming",
2646
+ callBack: true
2647
+ });
2648
+ }
2649
+ }
2650
+ function display(media, title, detail, extra) {
2519
2651
  return {
2520
- media: log.media,
2652
+ media,
2521
2653
  title,
2522
2654
  detail,
2523
- missed: false,
2524
- summary: `${title} \xB7 ${detail}`
2655
+ missed: extra.missed ?? false,
2656
+ summary: detail ? `${title} \xB7 ${detail}` : title,
2657
+ direction: extra.direction,
2658
+ callBack: extra.callBack
2525
2659
  };
2526
2660
  }
2527
2661
  function callLogDisplayFor(message, currentUserId) {
@@ -2535,8 +2669,8 @@ function isMessageVisibleTo(message, currentUserId) {
2535
2669
  function formatCallLogPreview(preview, currentUserId) {
2536
2670
  const log = parseCallLogBody(preview);
2537
2671
  if (!log) return null;
2538
- const display = describeCallLog(log, currentUserId);
2539
- if (display) return display.summary;
2672
+ const display2 = describeCallLog(log, currentUserId);
2673
+ if (display2) return display2.summary;
2540
2674
  if (log.kind === "call_missed" && log.media) {
2541
2675
  return log.media === "video" ? "Video call" : "Voice call";
2542
2676
  }
@@ -2556,6 +2690,7 @@ exports.ConversationMessages = ConversationMessages;
2556
2690
  exports.ConversationsResource = ConversationsResource;
2557
2691
  exports.DevicesResource = DevicesResource;
2558
2692
  exports.IceServersResource = IceServersResource;
2693
+ exports.LiveCallsResource = LiveCallsResource;
2559
2694
  exports.MeResource = MeResource;
2560
2695
  exports.MessageHandle = MessageHandle;
2561
2696
  exports.MessagesResource = MessagesResource;