//! Take notes on a live Cotton call, the note-taker's half (chat DECISIONS "Recordings", CONTRACT "Take notes"). //! //! The room says who takes notes (`Member.notes`); this phone follows it. Once the room names this //! phone's member among them, the recording begins: a Meetings recording of the call (`source: call`, `call`: the //! room), one file per person, and the shell is told `notes_start { meeting, tracks }`. Each person who comes into //! the call later is `notes_track`; when the room no longer names this phone, or the call ends, `notes_stop { meeting, //! tracks }`, and the shell hands the files to `meetings.record.stop { id, tracks }`. Nothing starts on the tap alone: //! the recording begins only when everyone has been told. //! //! The phone says it can draw the mark (`can: ["notes"]`) when the shell says so on `calls.start`, `calls.enter`, //! `calls.incoming` or `calls.join` (`can`), so an app that cannot show it is never counted as one that can. use serde_json::{json, Value}; use cotton_core::calling::wire::{Member, MemberState}; use cotton_core::calling::Call; use crate::shell::client::ClientController; /// What this phone holds of the notes it takes. #[derive(Debug, Default, Clone)] pub struct Notes { /// The shell draws the mark: `can: ["notes"]` goes on the call's start, join and enter. pub can: bool, /// The recording of the notes being taken now. pub meeting: Option, /// The room they are taken in. pub room: Option, /// One per person: `{ track, member, person?, name?, path }`. pub tracks: Vec, } /// `can` as the shell said it on a call's start, enter, ring or join; left as it was when not said. pub(crate) fn said_can(client: &ClientController, request: &Value) { if let Some(can) = request.get("can").and_then(Value::as_array) { client.notes_slot().lock().unwrap_or_else(|p| p.into_inner()).can = can.iter().any(|c| c == "notes"); } } /// What a start, join or enter asks the room to keep: `can: ["notes"]` from a shell that draws the mark. pub(crate) fn can(client: &ClientController, body: &mut Value) { if client.notes_slot().lock().unwrap_or_else(|p| p.into_inner()).can { body["can"] = json!(["notes"]); } } /// After every step of the call: the notes this phone takes, begun, grown or ended as the room says. pub(crate) fn follow(client: &ClientController, call: &Call) -> Vec { let mut held = client.notes_slot().lock().unwrap_or_else(|p| p.into_inner()); let me = call.me.clone(); let mine = call.is_active() && me.as_ref().is_some_and(|me| call.notes.contains(me)); let mut said = Vec::new(); match (mine, held.meeting.clone()) { (true, None) => match begin(client, call) { Ok((meeting, tracks)) => { held.meeting = Some(meeting.clone()); held.room = call.room.clone(); held.tracks = tracks.clone(); said.push(json!({ "do": "notes_start", "meeting": meeting, "tracks": tracks })); } Err(why) => said.push(json!({ "do": "notes_failed", "why": why })), }, (true, Some(meeting)) => { let root = client.layout().root().to_path_buf(); let names = names(client, call); for member in others(call) { let track = member.id.clone(); if held.tracks.iter().any(|t| t["track"] == json!(track)) { continue; } let mut new = track_of(&root, &meeting, &track, Some(member), &names); if let Some(group) = group_name(call) { new["group"] = json!(group); } held.tracks.push(new.clone()); let _ = keep(client, &meeting, &held.tracks); let mut effect = new; effect["do"] = json!("notes_track"); effect["meeting"] = json!(meeting); said.push(effect); } } (false, Some(meeting)) => { said.push(json!({ "do": "notes_stop", "meeting": meeting, "tracks": held.tracks })); *held = Notes { can: held.can, ..Notes::default() }; } (false, None) => {} } said } /// The others in the call now. fn others(call: &Call) -> impl Iterator { call.members.iter().filter(move |m| Some(&m.id) != call.me.as_ref() && m.state == MemberState::Joined) } /// The group's name, on a group's call. fn group_name(call: &Call) -> Option { call.group.as_ref().and(call.name.clone()).filter(|n| !n.trim().is_empty()) } /// The names Chat knows the call's people by (a group's members come with a number and no name), by number. fn names(client: &ClientController, call: &Call) -> std::collections::HashMap { let numbers: Vec = others(call).filter(|m| m.name.is_none()).filter_map(|m| m.number.clone()).collect(); if numbers.is_empty() { return Default::default(); } crate::phone::chat_calls::names_of_numbers(client, &numbers) } /// One person's track: its file beside the recording's, named as the call names them (the member's own name, else /// the name Chat knows their number by). fn track_of(root: &std::path::Path, meeting: &str, track: &str, member: Option<&Member>, names: &std::collections::HashMap) -> Value { let safe: String = track.chars().map(|c| if c.is_ascii_alphanumeric() || c == '-' || c == '_' { c } else { '_' }).collect(); let path = crate::phone::meetings_app::recording_path(root, &format!("{meeting}.{safe}"), "aac"); let mut said = json!({ "track": track, "member": member.map_or_else(|| "me".to_string(), |m| m.id.clone()), "path": path.to_string_lossy() }); if let Some(member) = member { if let Some(person) = &member.person { said["person"] = json!(person); } if let Some(name) = member.name.as_ref().or_else(|| member.number.as_ref().and_then(|n| names.get(n))) { said["name"] = json!(name); } if let Some(number) = &member.number { said["number"] = json!(number); } } said } /// The recording of the call's notes made, with a track for this phone's own voice and each other person's. fn begin(client: &ClientController, call: &Call) -> Result<(String, Vec), String> { let root = client.layout().root().to_path_buf(); let room = call.room.clone().unwrap_or_default(); let made = crate::phone::cotton_chat::write(client, "calls.notes.start", |ctx| { crate::phone::meetings_app::start(ctx, &root, &json!({ "kind": "meeting", "source": "call", "call": room })).map_err(|e| e.message) }) .map_err(|e| e.message)?; let meeting = made["meeting"].as_str().ok_or("no recording was made")?.to_string(); let names = names(client, call); let mut tracks = vec![track_of(&root, &meeting, "me", None, &names)]; tracks.extend(others(call).map(|m| track_of(&root, &meeting, &m.id, Some(m), &names))); // A group's call: its notes are named by the group, which every track carries (kept with them on the recording). if let Some(group) = group_name(call) { for track in &mut tracks { track["group"] = json!(group); } } keep(client, &meeting, &tracks)?; Ok((meeting, tracks)) } /// The tracks named on the recording, so a stop (even after the app was closed) knows whose each file is. fn keep(client: &ClientController, meeting: &str, tracks: &[Value]) -> Result<(), String> { let named: Vec = tracks .iter() .map(|t| { let mut t = t.clone(); t.as_object_mut().map(|o| o.remove("path")); t }) .collect(); crate::phone::cotton_chat::write(client, "calls.notes.tracks", |ctx| { crate::phone::meetings_app::update(ctx, crate::phone::meetings_app::MEETING, meeting, crate::phone::meetings_app::object(json!({ "tracks": json!(named).to_string() }))) .map(|_| Value::Null) .map_err(|e| e.message) }) .map(|_| ()) .map_err(|e| e.message) }