Cotton code
121 lines · 4.6 KB · RustRaw
//! The composer: what the one button is, the reply or edit strip, the//! placeholder. Pure: the draft and the quoted message arrive read.use crate::rules::shell_lines::LineSet;use super::can::Can;use super::list::say;use super::shapes::{Button, ComposeAnswer, PlaceMark, Strip, StripKind};/// The message a strip quotes.#[derive(Debug, Clone, PartialEq, Eq)]pub struct Quoted {    pub message: String,    pub sender: String,    pub line: String,}/// Is there anything to send? ECMAScript's trim, as the send verb trims.pub fn has_text(text: &str) -> bool {    !crate::messages::js_trim(text).is_empty()}pub fn button(can: &Can, text: &str, editing: bool) -> Button {    if has_text(text) {        Button::Send    } else if can.voice && !editing {        Button::Mic    } else {        Button::Idle    }}/// The composer's answer. `editing` wins over `reply` (an edit is never a reply). `attached` counts the pictures/// waiting in the writing box to go with what is written as their caption: with any, Send is live with nothing written.pub fn answer(lines: &LineSet, thread: &str, can: &Can, text: &str, reply: Option<&Quoted>, editing: Option<&Quoted>, attached: usize) -> ComposeAnswer {    let button = if attached > 0 && editing.is_none() && can.attach { Button::Send } else { button(can, text, editing.is_some()) };    let strip = match (editing, reply) {        (Some(edit), _) => Some(Strip {            kind: StripKind::Edit,            message: edit.message.clone(),            title: say(lines, "app.compose.editing", &[]),            line: edit.line.clone(),            cancel: say(lines, "app.compose.cancel", &[]),        }),        (None, Some(quoted)) => Some(Strip {            kind: StripKind::Reply,            message: quoted.message.clone(),            title: say(lines, "app.compose.replying", &[("name", &quoted.sender)]),            line: quoted.line.clone(),            cancel: say(lines, "app.compose.cancel", &[]),        }),        _ => None,    };    let button_label = match button {        Button::Mic => say(lines, "app.compose.voice", &[]),        Button::Send | Button::Idle if editing.is_some() => say(lines, "app.compose.save", &[]),        _ => say(lines, "app.compose.send", &[]),    };    ComposeAnswer {        thread: thread.to_string(),        text: text.to_string(),        placeholder: say(lines, "app.compose.placeholder", &[]),        button,        button_label,        attach: can.attach && editing.is_none(),        attach_label: say(lines, "app.compose.attach", &[]),        strip,        send_enabled: button == Button::Send,        dictate: can.send,        dictate_label: say(lines, "app.compose.dictate", &[]),        return_sends: false,        hardware_return_sends: true,        place: None,        poll: None,        preview: None,    }}/// At most this many options in a poll he makes: WhatsApp's own limit.pub const POLL_MOST: usize = 12;/// The words of the sheet a poll is made on (chat CONTRACT.md "Making a poll").pub fn poll_maker(lines: &LineSet) -> crate::messages::app::shapes::PollMaker {    crate::messages::app::shapes::PollMaker {        label: say(lines, "app.poll.make", &[]),        question: say(lines, "app.poll.question", &[]),        option: say(lines, "app.poll.option", &[]),        add: say(lines, "app.poll.add", &[]),        many: say(lines, "app.poll.many", &[]),        send: say(lines, "app.compose.send", &[]),        most: POLL_MOST as u32,    }}/// A poll as he wrote it, made fit to send: the question and each option trimmed, empty options left out, the same/// option once. `Err` names the line that says what is missing.pub fn poll_of(question: &str, options: &[String]) -> Result<(String, Vec<String>), &'static str> {    let question = crate::messages::js_trim(question).to_string();    if question.is_empty() {        return Err("app.refused.poll_question");    }    let mut kept: Vec<String> = Vec::new();    for option in options {        let option = crate::messages::js_trim(option).to_string();        if !option.is_empty() && !kept.iter().any(|k| k.to_lowercase() == option.to_lowercase()) {            kept.push(option);        }    }    if kept.len() < 2 {        return Err("app.refused.poll_options");    }    if kept.len() > POLL_MOST {        return Err("app.refused.poll_most");    }    Ok((question, kept))}/// The mark that says where a message goes (`app.goes.*`): `place` is a [`crate::messages::send::Route::place`].pub fn place_mark(lines: &LineSet, place: &str) -> PlaceMark {    PlaceMark { id: place.to_string(), mark: say(lines, &format!("app.goes.{place}"), &[]) }}