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