//! The call history, decided on the Mac (sealing plan L8): what the Worker's //! `/v1/import/calls` decided (apps/business/src/import/calls.ts), run over //! the importer's own copy with the same rules, so a sealed `call`, //! `person_handle` or `message_contact_handle` table costs the match nothing //! and the object never reads a number. //! //! One row per (source, external id): a re-read writes the same rows. A new //! call's id is made from (namespace, source, external id); one the copy //! already holds keeps its own, so sealing `call` (whose ids are then made //! under the keyed namespace) never lands a call twice. The handle is kept normalised where it can be and as dialled where //! it cannot; its person is the one person holding it (who; a handle two //! people claim is nobody's), else the person the contact was linked to by //! hand. A name the phone gave never names a person (PEOPLE BUILD 2). use cotton_client::ingest::DeviceCall; use cotton_rules::handles::{handle_key, normalize_contact_handle_in, HandleKind}; use cotton_rules::import::asked::Asked; use cotton_rules::messages::resolve_contact::{find_contact_by_handle, people_by_handle}; use serde_json::{json, Map, Value}; use crate::copy::{Change, CopyError, Landing, Result}; fn engine(error: impl std::fmt::Display) -> CopyError { CopyError::Engine { code: "calls".into(), message: error.to_string() } } /// A call log entry's id: `(namespace, source, external id)`, as `callLogId`. pub fn call_id(namespace: &str, source: &str, external_id: &str) -> String { cotton_rules::ids::derived_id("cotton_call_log", &[namespace, source, external_id]) } /// The rows a batch of calls owes the object, planned over `landing`'s copy. pub fn plan_calls(landing: &Landing<'_>, namespace: &str, calls: &[DeviceCall], now: &str) -> Result> { let asker = landing.asker(); let db = Asked(&asker); let people = people_by_handle(&db, "").map_err(|e| engine(e.0))?; let region = landing .owner_phone() .and_then(cotton_rules::phone::normalize_phone) .and_then(|phone| cotton_rules::phone::region_of(&phone)); let mut changes: Vec = Vec::new(); for call in calls { // A call the copy holds already keeps its id (one made before `call` was sealed is under the plain namespace). let id = match landing.held_call(&call.source, &call.external_id)? { Some(held) => held, None => call_id(namespace, &call.source, &call.external_id), }; // The same call twice in one batch: the last is kept, as last-write-wins would. changes.retain(|change| change.id != id); let kind = if call.handle_kind == "email" { HandleKind::Email } else { HandleKind::Phone }; let handle = call.handle.as_deref().and_then(|raw| normalize_contact_handle_in(kind, raw, region)); let contact = match &handle { Some(handle) => find_contact_by_handle(&db, "", handle).map_err(|e| engine(e.0))?, None => None, }; // who(handle) first: the person holding the number now, so a contact // linked before the number moved follows it; else a link made by hand // on the contact. Never a name (PEOPLE BUILD 2). let person = handle .as_ref() .and_then(|h| people.get(&handle_key(h)).map(|p| p.id.clone())) .or_else(|| contact.as_ref().and_then(|c| c.person_id.clone())); let patch = json!({ "id": id, "source": call.source, "external_id": call.external_id, "provider": call.provider, "direction": call.direction, "outcome": call.outcome, "started_at": call.started_at, "duration_s": call.duration_s, "handle": handle.as_ref().map(|h| h.value.clone()).or_else(|| call.handle.clone()), "contact_id": contact.as_ref().map(|c| c.id.clone()), "person_id": person, "device": call.device, "meta": Value::Object(call.meta.clone()), "created_at": now, "updated_at": now, }); let patch: Map = patch.as_object().cloned().unwrap_or_default(); changes.push(Change { table: "call".into(), id, patch: Some(patch) }); } Ok(changes) }