aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 1 | use serde_json::{Value, json}; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 2 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 3 | const TITLE_CHARS: usize = 80; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 4 | const BODY_CHARS: usize = 400; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 5 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 6 | /// What a push says about its event, before it becomes notification text. |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 7 | #[derive(Debug, Clone, PartialEq, Eq)] |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 8 | pub enum Content { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 9 | Text(String), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 10 | Emote(String), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 11 | /// e.g. "sent an image", shown after the sender's name |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 12 | Action(&'static str), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 13 | Encrypted, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 14 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 15 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 16 | impl Content { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 17 | pub fn of(event_type: Option<&str>, content: Option<&Value>, user_is_target: bool) -> Self { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 18 | let field = |name: &str| content.and_then(|content| content.get(name)?.as_str()); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 19 | match event_type.unwrap_or_default() { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 20 | "m.room.encrypted" => Self::Encrypted, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 21 | "m.room.message" => match (field("msgtype"), field("body")) { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 22 | (Some("m.text" | "m.notice"), Some(body)) => Self::Text(strip_reply_fallback(body)), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 23 | (Some("m.emote"), Some(body)) => Self::Emote(strip_reply_fallback(body)), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 24 | (Some("m.image"), _) => Self::Action("sent an image"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 25 | (Some("m.video"), _) => Self::Action("sent a video"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 26 | (Some("m.audio"), _) => Self::Action("sent an audio clip"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 27 | (Some("m.file"), _) => Self::Action("sent a file"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 28 | (Some("m.location"), _) => Self::Action("shared a location"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 29 | (_, Some(body)) => Self::Text(strip_reply_fallback(body)), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 30 | (_, None) => Self::Action("sent a message"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 31 | }, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 32 | "m.sticker" => Self::Action("sent a sticker"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 33 | "m.room.member" if user_is_target && field("membership") == Some("invite") => { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 34 | Self::Action("invited you") |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 35 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 36 | "m.call.invite" => Self::Action("is calling"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 37 | "m.poll.start" | "org.matrix.msc3381.poll.start" => Self::Action("started a poll"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 38 | _ => Self::Action("sent a message"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 39 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 40 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 41 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 42 | pub fn body(&self, sender: &str, direct: bool) -> String { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 43 | match (self, direct) { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 44 | (Self::Text(text), true) => text.clone(), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 45 | (Self::Text(text), false) => format!("{sender}: {text}"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 46 | (Self::Emote(text), _) => format!("* {sender} {text}"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 47 | (Self::Action(action), _) => format!("{sender} {action}"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 48 | (Self::Encrypted, true) => "Encrypted message".to_owned(), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 49 | (Self::Encrypted, false) => format!("{sender}: Encrypted message"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 50 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 51 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 52 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 53 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 54 | /// Older clients quote the replied-to message as leading "> " lines and a |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 55 | /// blank line, which would crowd out the reply itself. |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 56 | fn strip_reply_fallback(body: &str) -> String { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 57 | if !body.starts_with("> ") { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 58 | return body.to_owned(); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 59 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 60 | let mut lines = body.lines().skip_while(|line| line.starts_with('>')); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 61 | let rest: Vec<&str> = match lines.next() { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 62 | Some("") | None => lines.collect(), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 63 | Some(first) => std::iter::once(first).chain(lines).collect(), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 64 | }; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 65 | if rest.is_empty() { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 66 | body.to_owned() |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 67 | } else { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 68 | rest.join("\n") |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 69 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 70 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 71 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 72 | /// The notification one push shows, in the Declarative Web Push shape so |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 73 | /// Safari can display it without running the service worker. |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 74 | #[derive(Debug, Clone, Default, PartialEq, Eq)] |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 75 | pub struct Notice { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 76 | pub title: String, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 77 | pub body: String, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 78 | pub room_id: String, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 79 | pub event_id: String, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 80 | /// the body is generic because the event could not be read |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 81 | pub encrypted: bool, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 82 | pub mention: bool, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 83 | pub silent: bool, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 84 | pub unread: Option<u64>, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 85 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 86 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 87 | impl Notice { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 88 | /// `origin` without a trailing slash, or empty for a relative `navigate`. |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 89 | pub fn to_value(&self, origin: &str) -> Value { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 90 | let room = &self.room_id; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 91 | let mut notification = json!({ |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 92 | "title": truncate(&self.title, TITLE_CHARS), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 93 | "body": truncate(&self.body, BODY_CHARS), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 94 | "navigate": format!("{origin}/?room={}", query_escape(room)), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 95 | "tag": format!("room:{room}"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 96 | "silent": self.silent, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 97 | }); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 98 | if let Some(unread) = self.unread { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 99 | notification["app_badge"] = Value::String(unread.to_string()); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 100 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 101 | json!({ |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 102 | "web_push": 8030, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 103 | "notification": notification, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 104 | "malice": { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 105 | "room_id": room, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 106 | "event_id": self.event_id, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 107 | "encrypted": self.encrypted, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 108 | "mention": self.mention, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 109 | "unread": self.unread, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 110 | }, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 111 | }) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 112 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 113 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 114 | pub fn to_bytes(&self, origin: &str) -> Vec<u8> { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 115 | serde_json::to_vec(&self.to_value(origin)).expect("a json value serialises") |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 116 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 117 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 118 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 119 | fn truncate(text: &str, max: usize) -> String { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 120 | match text.char_indices().nth(max) { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 121 | Some((end, _)) => format!("{}…", text[..end].trim_end()), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 122 | None => text.to_owned(), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 123 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 124 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 125 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 126 | fn query_escape(value: &str) -> String { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 127 | let mut out = String::with_capacity(value.len()); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 128 | for byte in value.bytes() { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 129 | match byte { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 130 | b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' | b'!' | b':' => { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 131 | out.push(byte as char); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 132 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 133 | _ => out.push_str(&format!("%{byte:02X}")), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 134 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 135 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 136 | out |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 137 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 138 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 139 | #[cfg(test)] |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 140 | mod tests { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 141 | use super::*; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 142 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 143 | #[test] |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 144 | fn content_reads_messages_and_hides_encrypted_ones() { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 145 | let text = json!({"msgtype": "m.text", "body": "> <@a:x> old\n\nnew"}); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 146 | assert_eq!( |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 147 | Content::of(Some("m.room.message"), Some(&text), false), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 148 | Content::Text("new".to_owned()) |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 149 | ); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 150 | assert_eq!( |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 151 | Content::of( |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 152 | Some("m.room.encrypted"), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 153 | Some(&json!({"ciphertext": "x"})), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 154 | false |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 155 | ), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 156 | Content::Encrypted |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 157 | ); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 158 | let image = json!({"msgtype": "m.image", "body": "cat.png"}); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 159 | assert_eq!( |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 160 | Content::of(Some("m.room.message"), Some(&image), false).body("Ann", false), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 161 | "Ann sent an image" |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 162 | ); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 163 | assert_eq!(Content::Text("hi".to_owned()).body("Ann", false), "Ann: hi"); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 164 | assert_eq!(Content::Text("hi".to_owned()).body("Ann", true), "hi"); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 165 | let invite = json!({"membership": "invite"}); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 166 | assert_eq!( |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 167 | Content::of(Some("m.room.member"), Some(&invite), true), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 168 | Content::Action("invited you") |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 169 | ); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 170 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 171 | |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 172 | #[test] |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 173 | fn notice_matches_the_wire_contract_and_stays_small() { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 174 | let notice = Notice { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 175 | title: "Room".to_owned(), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 176 | body: "x".repeat(5000), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 177 | room_id: "!r:example.org".to_owned(), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 178 | event_id: "$e".to_owned(), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 179 | encrypted: false, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 180 | mention: true, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 181 | silent: false, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 182 | unread: Some(3), |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 183 | }; |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 184 | let value = notice.to_value("https://chat.example"); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 185 | assert_eq!(value["web_push"], 8030); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 186 | assert_eq!( |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 187 | value["notification"]["navigate"], |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 188 | "https://chat.example/?room=!r:example.org" |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 189 | ); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 190 | assert_eq!(value["notification"]["tag"], "room:!r:example.org"); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 191 | assert_eq!(value["notification"]["app_badge"], "3"); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 192 | assert_eq!(value["malice"]["unread"], 3); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 193 | assert_eq!(value["malice"]["mention"], true); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 194 | assert!(notice.to_bytes("https://chat.example").len() < 3 * 1024); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 195 | let unknown = Notice { |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 196 | unread: None, |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 197 | ..notice |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 198 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 199 | .to_value(""); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 200 | assert!(unknown["notification"].get("app_badge").is_none()); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 201 | assert_eq!(unknown["malice"]["unread"], Value::Null); |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 202 | } |
aa3dce5114d0 | atagen | 2026-10-26 06:09 UTC | 203 | } |