2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 1 | //! the homeserver side of `POST /_matrix/push/v1/notify`, for a gateway that holds no session |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 2 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 3 | use ruma_common::{OwnedEventId, OwnedRoomAliasId, OwnedRoomId, OwnedUserId}; |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 4 | use serde::{Deserialize, Serialize}; |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 5 | use serde_json::{Map, Value, value::RawValue}; |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 6 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 7 | #[derive(Clone, Debug, Deserialize)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 8 | #[non_exhaustive] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 9 | pub struct GatewayNotification { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 10 | pub event_id: Option<OwnedEventId>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 11 | pub room_id: Option<OwnedRoomId>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 12 | #[serde(rename = "type")] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 13 | pub event_type: Option<String>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 14 | pub sender: Option<OwnedUserId>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 15 | pub sender_display_name: Option<String>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 16 | pub room_name: Option<String>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 17 | pub room_alias: Option<OwnedRoomAliasId>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 18 | #[serde(default)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 19 | pub user_is_target: bool, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 20 | #[serde(default)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 21 | pub prio: NotifyPrio, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 22 | /// only in the full format, and still encrypted for an encrypted room |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 23 | pub content: Option<Box<RawValue>>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 24 | #[serde(default)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 25 | pub counts: NotifyCounts, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 26 | #[serde(default)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 27 | pub devices: Vec<GatewayDevice>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 28 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 29 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 30 | impl GatewayNotification { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 31 | /// a count-only push carries no event, and iOS punishes a web push that shows nothing |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 32 | pub fn is_event(&self) -> bool { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 33 | self.event_id.is_some() |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 34 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 35 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 36 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 37 | #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 38 | #[serde(rename_all = "lowercase")] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 39 | pub enum NotifyPrio { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 40 | #[default] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 41 | High, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 42 | Low, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 43 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 44 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 45 | #[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 46 | #[non_exhaustive] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 47 | pub struct NotifyCounts { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 48 | #[serde(default)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 49 | pub unread: u64, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 50 | #[serde(default)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 51 | pub missed_calls: u64, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 52 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 53 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 54 | #[derive(Clone, Debug, Deserialize)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 55 | #[non_exhaustive] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 56 | pub struct GatewayDevice { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 57 | pub app_id: String, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 58 | pub pushkey: String, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 59 | pub pushkey_ts: Option<u64>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 60 | /// the pusher's data as it was set, `format` included |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 61 | #[serde(default)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 62 | pub data: Map<String, Value>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 63 | #[serde(default)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 64 | pub tweaks: Map<String, Value>, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 65 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 66 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 67 | #[derive(Deserialize)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 68 | struct NotifyBody { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 69 | notification: GatewayNotification, |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 70 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 71 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 72 | pub fn parse_notify(body: &[u8]) -> Result<GatewayNotification, serde_json::Error> { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 73 | serde_json::from_slice::<NotifyBody>(body).map(|body| body.notification) |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 74 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 75 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 76 | /// the pushkeys the homeserver should drop, such as one whose web push endpoint answered 404 or 410 |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 77 | pub fn notify_response(rejected: &[String]) -> Vec<u8> { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 78 | #[derive(Serialize)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 79 | struct Response<'a> { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 80 | rejected: &'a [String], |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 81 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 82 | serde_json::to_vec(&Response { rejected }).expect("a list of strings serialises") |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 83 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 84 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 85 | #[cfg(test)] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 86 | mod tests { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 87 | use super::*; |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 88 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 89 | #[test] |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 90 | fn a_count_only_push_is_no_event_and_devices_keep_their_data() { |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 91 | let body = br#"{"notification":{"counts":{"unread":2},"devices":[{"app_id":"me.malice","pushkey":"https://push.example/abc","data":{"format":"event_id_only","endpoint":"https://push.example/abc","auth":"secret"},"tweaks":{"sound":"default"}}]}}"#; |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 92 | let notification = parse_notify(body).unwrap(); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 93 | assert!(!notification.is_event()); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 94 | assert_eq!(notification.counts.unread, 2); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 95 | assert_eq!(notification.prio, NotifyPrio::High); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 96 | assert_eq!(notification.devices[0].data["auth"], "secret"); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 97 | assert_eq!(notification.devices[0].tweaks["sound"], "default"); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 98 | |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 99 | let body = br#"{"notification":{"event_id":"$e","room_id":"!r:example.org","type":"m.room.message","sender":"@a:example.org","prio":"low","content":{"body":"hi"},"devices":[]}}"#; |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 100 | let notification = parse_notify(body).unwrap(); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 101 | assert!(notification.is_event()); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 102 | assert_eq!(notification.prio, NotifyPrio::Low); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 103 | assert_eq!(notification.content.unwrap().get(), r#"{"body":"hi"}"#); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 104 | assert_eq!( |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 105 | notify_response(&["gone".to_owned()]), |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 106 | br#"{"rejected":["gone"]}"# |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 107 | ); |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 108 | } |
2006a11ce142 | atagen | 2026-10-26 04:02 UTC | 109 | } |