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