@poolse/sdk 2.20.0 → 2.22.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) {
@@ -1659,6 +1671,28 @@ var CallTokensResource = class {
1659
1671
  }
1660
1672
  };
1661
1673
 
1674
+ // src/resources/live-calls.ts
1675
+ var LiveCallsResource = class {
1676
+ constructor(client) {
1677
+ this.client = client;
1678
+ }
1679
+ client;
1680
+ /** GET /v1/conversations/:conversationId/call — `null` when no call is live. */
1681
+ async get(conversationId, signal) {
1682
+ try {
1683
+ const res = await this.client.request({
1684
+ method: "GET",
1685
+ path: `/v1/conversations/${encodeURIComponent(conversationId)}/call`,
1686
+ ...signal ? { signal } : {}
1687
+ });
1688
+ return res.call.state === "live" ? res.call : null;
1689
+ } catch (err) {
1690
+ if (err instanceof ApiError && err.status === 404 && err.code === "no_live_call") return null;
1691
+ throw err;
1692
+ }
1693
+ }
1694
+ };
1695
+
1662
1696
  // src/voice/call-room.ts
1663
1697
  function toDevices(raw, kind) {
1664
1698
  const out = [];
@@ -2395,6 +2429,8 @@ var Poolse = class {
2395
2429
  iceServers;
2396
2430
  /** SFU credentials for a call's media — see `CallTokensResource`. */
2397
2431
  callTokens;
2432
+ /** Whether a call is running on a conversation — for a Join banner. */
2433
+ liveCalls;
2398
2434
  resolved;
2399
2435
  tokenCache;
2400
2436
  constructor(config) {
@@ -2413,6 +2449,7 @@ var Poolse = class {
2413
2449
  this.users = new UsersResource(cachedConfig);
2414
2450
  this.iceServers = new IceServersResource(this.rest);
2415
2451
  this.callTokens = new CallTokensResource(this.rest);
2452
+ this.liveCalls = new LiveCallsResource(this.rest);
2416
2453
  this.realtime = new PoolseRealtime(cachedConfig, this.tokenCache, {
2417
2454
  ...this.resolved.wsUrl !== void 0 ? { wsUrl: this.resolved.wsUrl } : {},
2418
2455
  socketPath: this.resolved.socketPath,
@@ -2455,7 +2492,160 @@ var Poolse = class {
2455
2492
  };
2456
2493
 
2457
2494
  // src/version.ts
2458
- var version = "2.20.0";
2495
+ var version = "2.22.0";
2496
+
2497
+ // src/call-log.ts
2498
+ function parseCallLogBody(body) {
2499
+ if (typeof body !== "string") return null;
2500
+ const trimmed = body.trim();
2501
+ if (!trimmed.startsWith("{") || !trimmed.includes('"kind"')) return null;
2502
+ let raw;
2503
+ try {
2504
+ raw = JSON.parse(trimmed);
2505
+ } catch {
2506
+ return null;
2507
+ }
2508
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
2509
+ const obj = raw;
2510
+ const callId = typeof obj.call_id === "string" ? obj.call_id : null;
2511
+ if (!callId) return null;
2512
+ const media = obj.media === "audio" || obj.media === "video" ? obj.media : null;
2513
+ if (obj.kind === "call") {
2514
+ const d = obj.duration_ms;
2515
+ const durationMs = typeof d === "number" && Number.isFinite(d) && d >= 0 ? d : null;
2516
+ const joined = Array.isArray(obj.joined_user_ids) ? obj.joined_user_ids.filter((u) => typeof u === "string") : [];
2517
+ const participants = Array.isArray(obj.participants) ? obj.participants.flatMap((p) => {
2518
+ if (!p || typeof p !== "object") return [];
2519
+ const { user_id: userId, status } = p;
2520
+ if (typeof userId !== "string" || !isParticipantStatus(status)) return [];
2521
+ return [{ userId, status }];
2522
+ }) : [];
2523
+ const num = (v) => typeof v === "number" && Number.isFinite(v) ? v : null;
2524
+ return {
2525
+ kind: "call",
2526
+ callId,
2527
+ media: media ?? "audio",
2528
+ durationMs,
2529
+ joinedUserIds: joined,
2530
+ version: obj.v === 2 ? 2 : 1,
2531
+ callerUserId: typeof obj.caller_user_id === "string" ? obj.caller_user_id : null,
2532
+ participants,
2533
+ endReason: isEndReason(obj.end_reason) ? obj.end_reason : null,
2534
+ startedAt: num(obj.started_at),
2535
+ endedAt: num(obj.ended_at)
2536
+ };
2537
+ }
2538
+ if (obj.kind === "call_missed") {
2539
+ if (typeof obj.user_id !== "string" || obj.user_id === "") return null;
2540
+ return { kind: "call_missed", callId, userId: obj.user_id, media };
2541
+ }
2542
+ return null;
2543
+ }
2544
+ function isParticipantStatus(v) {
2545
+ return v === "joined" || v === "declined" || v === "busy" || v === "missed";
2546
+ }
2547
+ function isEndReason(v) {
2548
+ return v === "finished" || v === "declined_all" || v === "no_answer" || v === "cancelled";
2549
+ }
2550
+ function parseCallLog(message) {
2551
+ if (message.type !== "system" && message.type !== "custom") return null;
2552
+ if (message.deleted_at) return null;
2553
+ return parseCallLogBody(message.body);
2554
+ }
2555
+ function isCallLogMessage(message) {
2556
+ return parseCallLog(message) !== null;
2557
+ }
2558
+ function formatCallDuration(ms) {
2559
+ const total = Math.max(0, Math.round(ms / 1e3));
2560
+ const h = Math.floor(total / 3600);
2561
+ const m = Math.floor(total % 3600 / 60);
2562
+ const s = total % 60;
2563
+ const ss = String(s).padStart(2, "0");
2564
+ if (h > 0) return `${h}:${String(m).padStart(2, "0")}:${ss}`;
2565
+ return `${m}:${ss}`;
2566
+ }
2567
+ function describeCallLog(log, currentUserId) {
2568
+ if (log.kind === "call_missed") {
2569
+ if (currentUserId === null || log.userId !== currentUserId) return null;
2570
+ const media = log.media ?? "audio";
2571
+ return display(media, "Missed call", null, {
2572
+ missed: true,
2573
+ direction: "incoming",
2574
+ callBack: true
2575
+ });
2576
+ }
2577
+ const kind = log.media === "video" ? "video" : "voice";
2578
+ const duration = log.durationMs !== null ? formatCallDuration(log.durationMs) : null;
2579
+ const group = log.participants.length > 1;
2580
+ const me = currentUserId;
2581
+ const mine = me !== null ? log.participants.find((p) => p.userId === me) : void 0;
2582
+ const isCaller = me !== null && log.callerUserId === me;
2583
+ if (log.version === 1 || !isCaller && !mine) {
2584
+ const title = group ? `Group ${kind} call` : log.media === "video" ? "Video call" : "Voice call";
2585
+ return display(log.media, title, duration ?? "No answer", { direction: null, callBack: false });
2586
+ }
2587
+ if (isCaller) {
2588
+ const title = `Outgoing ${kind} call`;
2589
+ if (duration)
2590
+ return display(log.media, title, duration, { direction: "outgoing", callBack: false });
2591
+ const allBusy = log.participants.length > 0 && log.participants.every((p) => p.status === "busy");
2592
+ const detail = log.endReason === "cancelled" ? "Cancelled" : log.endReason === "declined_all" ? allBusy ? "Busy" : "Declined" : "No answer";
2593
+ return display(log.media, title, detail, { direction: "outgoing", callBack: true });
2594
+ }
2595
+ switch (mine.status) {
2596
+ case "joined":
2597
+ return display(log.media, group ? `Group ${kind} call` : `Incoming ${kind} call`, duration, {
2598
+ direction: "incoming",
2599
+ callBack: false
2600
+ });
2601
+ case "declined":
2602
+ return display(log.media, `Incoming ${kind} call`, "You declined", {
2603
+ direction: "incoming",
2604
+ callBack: true
2605
+ });
2606
+ case "busy":
2607
+ return display(log.media, `Missed ${kind} call`, "You were on another call", {
2608
+ missed: true,
2609
+ direction: "incoming",
2610
+ callBack: true
2611
+ });
2612
+ case "missed":
2613
+ return display(log.media, group ? `Missed group ${kind} call` : `Missed ${kind} call`, null, {
2614
+ missed: true,
2615
+ direction: "incoming",
2616
+ callBack: true
2617
+ });
2618
+ }
2619
+ }
2620
+ function display(media, title, detail, extra) {
2621
+ return {
2622
+ media,
2623
+ title,
2624
+ detail,
2625
+ missed: extra.missed ?? false,
2626
+ summary: detail ? `${title} \xB7 ${detail}` : title,
2627
+ direction: extra.direction,
2628
+ callBack: extra.callBack
2629
+ };
2630
+ }
2631
+ function callLogDisplayFor(message, currentUserId) {
2632
+ const log = parseCallLog(message);
2633
+ if (!log) return void 0;
2634
+ return describeCallLog(log, currentUserId);
2635
+ }
2636
+ function isMessageVisibleTo(message, currentUserId) {
2637
+ return callLogDisplayFor(message, currentUserId) !== null;
2638
+ }
2639
+ function formatCallLogPreview(preview, currentUserId) {
2640
+ const log = parseCallLogBody(preview);
2641
+ if (!log) return null;
2642
+ const display2 = describeCallLog(log, currentUserId);
2643
+ if (display2) return display2.summary;
2644
+ if (log.kind === "call_missed" && log.media) {
2645
+ return log.media === "video" ? "Video call" : "Voice call";
2646
+ }
2647
+ return "Call";
2648
+ }
2459
2649
 
2460
2650
  exports.ApiError = ApiError;
2461
2651
  exports.AttachmentHandle = AttachmentHandle;
@@ -2470,6 +2660,7 @@ exports.ConversationMessages = ConversationMessages;
2470
2660
  exports.ConversationsResource = ConversationsResource;
2471
2661
  exports.DevicesResource = DevicesResource;
2472
2662
  exports.IceServersResource = IceServersResource;
2663
+ exports.LiveCallsResource = LiveCallsResource;
2473
2664
  exports.MeResource = MeResource;
2474
2665
  exports.MessageHandle = MessageHandle;
2475
2666
  exports.MessagesResource = MessagesResource;
@@ -2482,8 +2673,16 @@ exports.RateLimitedError = RateLimitedError;
2482
2673
  exports.UserChannel = UserChannel;
2483
2674
  exports.UsersResource = UsersResource;
2484
2675
  exports.VoiceRoom = VoiceRoom;
2676
+ exports.callLogDisplayFor = callLogDisplayFor;
2485
2677
  exports.createBrowserWebRtcAdapter = createBrowserWebRtcAdapter;
2678
+ exports.describeCallLog = describeCallLog;
2679
+ exports.formatCallDuration = formatCallDuration;
2680
+ exports.formatCallLogPreview = formatCallLogPreview;
2681
+ exports.isCallLogMessage = isCallLogMessage;
2682
+ exports.isMessageVisibleTo = isMessageVisibleTo;
2486
2683
  exports.isWebRtcAvailable = isWebRtcAvailable;
2684
+ exports.parseCallLog = parseCallLog;
2685
+ exports.parseCallLogBody = parseCallLogBody;
2487
2686
  exports.safeUuid = safeUuid;
2488
2687
  exports.version = version;
2489
2688
  //# sourceMappingURL=index.cjs.map