// A call's ring, or its stop, handed over the chat socket (`{t:"ring", kind: ring|stop, …}`). // // The Worker rings an open phone over its chat socket INSTEAD of a push (`apps/business/src/calling/calls.ts` // `ringIn`, `frameTo`: "Over its open socket when it has one, else by push"), so a phone with Comms open is rung only // here. The chat socket (`ChatCenter`) reads the frame and posts it; the calls engine (`CallingEngine`) takes it as it // takes a VoIP push, without CallKit's push report (the core's `system_call_incoming` reports the ring). Android does // the same (`ChatFrames.ring`). import Foundation public enum CallRingFrame: Equatable, Sendable { /// `at`: when the ring began, as the object kept it (to a phone that speaks the new way), so one rung late starts there. case ring(room: String, video: Bool, box: String?, at: Int64? = nil) /// `box`: a missed call's caller, sealed as the ring was, so a phone that never heard the ring writes its row; /// `times`: when it rang and ended, for that row. case stop(room: String, reason: String, box: String?, times: CallTimes? = nil) /// A missed call kept for this phone and told once as its socket opened: it writes the row and rings nothing. case missed(room: String, reason: String, box: String?, times: CallTimes?) /// `{t:"ring", kind: ring|stop|missed, room, video:"1"|"0" | stop, box?, at?, ended?}`; nil for any other frame. public init?(frame: JSONValue) { guard frame["t"]?.string == "ring", let room = frame["room"]?.string, !room.isEmpty else { return nil } let box = frame["box"]?.string.flatMap { $0.isEmpty ? nil : $0 } let times = CallTimes(at: frame["at"]?.string.flatMap { Int64($0) }, ended: frame["ended"]?.string.flatMap { Int64($0) }) switch frame["kind"]?.string { case "stop": self = .stop(room: room, reason: frame["stop"]?.string ?? "", box: box, times: times) case "missed": self = .missed(room: room, reason: frame["stop"]?.string ?? "missed", box: box, times: times) default: self = .ring(room: room, video: frame["video"]?.string == "1", box: box, at: frame["at"]?.string.flatMap { Int64($0) }.flatMap { $0 > 0 ? $0 : nil }) } } public var room: String { switch self { case .ring(let room, _, _, _), .stop(let room, _, _, _), .missed(let room, _, _, _): room } } /// What this build says it speaks as its chat socket opens (`{t:"hello", can}`): it acks each ring frame, takes a /// late ring, and a missed call kept for it. A build that says nothing is rung as before (CALLS FIX 2). public static let can: [String] = ["ring_ack"] /// Where the chat socket posts a ring for the calls engine (the frame as `object`). public static let arrived = Notification.Name("com.cottondb.calls.ring-frame") } /// When a ring began and ended (ms since 1970), as the person's object kept them: a missed call's row says when it rang. public struct CallTimes: Equatable, Sendable { public var at: Int64 public var ended: Int64 public init(at: Int64, ended: Int64) { self.at = at self.ended = ended } /// Both, or none. public init?(at: Int64?, ended: Int64?) { guard let at, let ended, at > 0, ended >= at else { return nil } self.init(at: at, ended: ended) } }