package main// WhatsApp calls as lines in their chat (chat CONTRACT "Documents, links, a recording's date, WhatsApp calls": "a call// the WhatsApp link reports: write it as a message row in that chat with channel whatsapp and meta.call = the call's// facts, in CallFacts' shape"). The owner, 24 Nov 2026: WhatsApp shows "Voice call" as the chat's last line, Comms// showed the message before it.//// Two sources, both read only (nothing here answers, rejects or places a call):// - live: WhatsApp tells a linked device of a call as call stanzas (CallOffer or CallOfferNotice, CallAccept,// CallReject, CallTerminate). They are followed by call id until the call ends, then one message goes out;// - history: a missed call WhatsApp kept as a stub (CALL_MISSED_*), and a call log message (CallLogMessage).//// The message's id is the call's own (live: "call:<call id>"; history: the entry's message id), so a call reported// twice is one row.import ("strings""sync""time" waBinary "go.mau.fi/whatsmeow/binary""go.mau.fi/whatsmeow/proto/waE2E""go.mau.fi/whatsmeow/proto/waWeb""go.mau.fi/whatsmeow/types""go.mau.fi/whatsmeow/types/events")// Call is a call's facts in Cotton's shape (cotton-core messages::app::calls::CallFacts, camelCase).typeCallstruct {Room string `json:"room"`Direction string `json:"direction"`// in or outVideo bool `json:"video"`Outcome string `json:"outcome"`// answered, missed, cancelled or failedReason string `json:"reason"`// missed, declined, no_answer, cancelled, failed, answeredDurationS int64 `json:"durationS"`Missed bool `json:"missed"`// rang here and nobody here answered or declined itGroup bool `json:"group"`}// How long a call may go without its end before it is let go (said as missed when it never connected).const callForgotten = 2 * time.Hourtype ringing struct { account string chat types.JID group bool video bool incoming bool started time.Time accepted time.Time rejected bool}// Calls follows live calls by id until they end.typeCallsstruct { mu sync.Mutex open map[string]*ringing clock func() time.Time}funcNewCalls() *Calls {return &Calls{open: map[string]*ringing{}, clock: time.Now}}// videoOf reads a call stanza's media: a <video> child says video.func videoOf(node *waBinary.Node) bool {if node == nil {returnfalse }for _, child := range node.GetChildren() {if child.Tag == "video" {returntrue } }returnfalse}// Event takes one call event; it answers the message for a call that ended, else nil. own is the account's own// user (a stanza from it was sent by one of his own devices: his phone answering or declining).func (c *Calls) Event(account string, own string, evt any, resolve PNResolver) *Event { c.mu.Lock()defer c.mu.Unlock() c.forget() start := func(meta types.BasicCallMeta, video, group bool) {if _, seen := c.open[meta.CallID]; seen || meta.CallID == "" {return } chat := meta.Fromif !meta.GroupJID.IsEmpty() { chat, group = meta.GroupJID, true } elseif !meta.CallCreator.IsEmpty() { chat = meta.CallCreator } norm, _ := normalize(chat, resolve) incoming := meta.CallCreator.IsEmpty() || meta.CallCreator.User != own at := meta.Timestampif at.IsZero() { at = c.clock() } c.open[meta.CallID] = &ringing{account: account, chat: norm, group: group, video: video, incoming: incoming, started: at} }switch e := evt.(type) {case *events.CallOffer: start(e.BasicCallMeta, videoOf(e.Data), false)case *events.CallOfferNotice: start(e.BasicCallMeta, e.Media == "video" || videoOf(e.Data), e.Type == "group")case *events.CallAccept:if r := c.open[e.CallID]; r != nil && r.accepted.IsZero() { r.accepted = e.Timestampif r.accepted.IsZero() { r.accepted = c.clock() } }case *events.CallReject:if r := c.open[e.CallID]; r != nil { r.rejected = true }case *events.CallTerminate: r := c.open[e.CallID]if r == nil {returnnil } delete(c.open, e.CallID) end := e.Timestampif end.IsZero() { end = c.clock() }return r.message("call:"+e.CallID, r.facts(e.CallID, end, e.Reason)) }returnnil}// forget lets go of calls whose end never came.func (c *Calls) forget() {for id, r := range c.open {if c.clock().Sub(r.started) > callForgotten { delete(c.open, id) } }}func (r *ringing) facts(room string, end time.Time, why string) *Call { f := &Call{Room: room, Video: r.video, Group: r.group, Direction: "out"}if r.incoming { f.Direction = "in" }switch {case !r.accepted.IsZero(): f.Outcome, f.Reason = "answered", "answered"if d := end.Sub(r.accepted); d > 0 { f.DurationS = int64(d.Seconds()) }case r.incoming && r.rejected: f.Outcome, f.Reason = "missed", "declined"case r.incoming: f.Outcome, f.Reason, f.Missed = "missed", "missed", truecase r.rejected: f.Outcome, f.Reason = "missed", "declined"case strings.Contains(strings.ToLower(why), "timeout"): f.Outcome, f.Reason = "missed", "no_answer"default: f.Outcome, f.Reason = "cancelled", "cancelled" }return f}func (r *ringing) message(id string, call *Call) *Event { m := &Message{ID: id, Chat: r.chat.String(), FromMe: !r.incoming, IsGroup: r.group, Timestamp: r.started.Unix(), Kind: "call", Call: call, Origin: "live"}return &Event{Event: "message", Account: r.account, Message: m}}// callLog is a history entry's CallLogMessage as facts; nil for a call still going on.func callLog(x *waE2E.CallLogMessage, room string, fromMe, group bool) *Call { f := &Call{Room: room, Video: x.GetIsVideo(), Group: group, Direction: "in"}if fromMe { f.Direction = "out" }switch x.GetCallOutcome() {case waE2E.CallLogMessage_CONNECTED, waE2E.CallLogMessage_ACCEPTED_ELSEWHERE: f.Outcome, f.Reason, f.DurationS = "answered", "answered", x.GetDurationSecs()case waE2E.CallLogMessage_REJECTED: f.Outcome, f.Reason = "missed", "declined"case waE2E.CallLogMessage_FAILED: f.Outcome, f.Reason = "failed", "failed"case waE2E.CallLogMessage_ONGOING:returnnildefault: // MISSED, SILENCED_BY_DND, SILENCED_UNKNOWN_CALLERif fromMe { f.Outcome, f.Reason = "missed", "no_answer" } else { f.Outcome, f.Reason, f.Missed = "missed", "missed", true } }return f}// historyMissedCall is a missed call WhatsApp's history kept as a stub (CALL_MISSED_VOICE and the like); nil for any// other entry.func historyMissedCall(web *waWeb.WebMessageInfo, chat types.JID, resolve PNResolver) *Event {if web.GetMessage() != nil || web.GetKey().GetID() == "" || IsStatus(chat) {returnnil }var video, group boolswitch web.GetMessageStubType() {case waWeb.WebMessageInfo_CALL_MISSED_VOICE:case waWeb.WebMessageInfo_CALL_MISSED_VIDEO: video = truecase waWeb.WebMessageInfo_CALL_MISSED_GROUP_VOICE: group = truecase waWeb.WebMessageInfo_CALL_MISSED_GROUP_VIDEO: video, group = true, truedefault:returnnil } norm, _ := normalize(chat, resolve) id := web.GetKey().GetID() call := &Call{Room: id, Direction: "in", Video: video, Group: group, Outcome: "missed", Reason: "missed", Missed: true} m := &Message{ID: id, Chat: norm.String(), IsGroup: chat.Server == types.GroupServer, Timestamp: int64(web.GetMessageTimestamp()), Kind: "call", Call: call, Origin: "history"}return &Event{Event: "message", Chat: m.Chat, Message: m}}