cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1 | use ruma::{event_id, user_id}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 2 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 3 | use super::state::{Data, SavedEvent, Window, id}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 4 | use crate::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 5 | MentionCaptureState, MentionEntry, MentionKey, MentionReason, Message, MessageKind, SendState, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 6 | Sender, TimelineContent, TimelineEvent, TimelineItem, TimelineItemId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 7 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 8 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 9 | fn key(room: &str, event: &str) -> MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 10 | MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 11 | room_id: format!("!{room}:mentions.example").parse().unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 12 | event_id: format!("${event}:mentions.example").parse().unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 13 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 14 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 15 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 16 | fn entry(key: MentionKey, timestamp_ms: u64, discovery_revision: u64, read: bool) -> MentionEntry { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 17 | MentionEntry { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 18 | key, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 19 | timestamp_ms, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 20 | discovered_at_ms: timestamp_ms, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 21 | discovery_revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 22 | revision: discovery_revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 23 | event: TimelineItem::DayDivider { timestamp_ms }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 24 | reasons: vec![MentionReason::Direct], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 25 | read, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 26 | capture: MentionCaptureState::Ready, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 27 | room_name: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 28 | notification_suppressed: false, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 29 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 30 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 31 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 32 | fn message_event(event_id: &str, timestamp_ms: u64, body: &str) -> TimelineItem { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 33 | let event_id: ruma::OwnedEventId = event_id.parse().unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 34 | TimelineItem::Event(Box::new(TimelineEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 35 | item_id: TimelineItemId::new(format!("item-{event_id}")), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 36 | event_id: Some(event_id), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 37 | transaction_id: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 38 | sender: Sender { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 39 | user_id: user_id!("@bob:mentions.example").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 40 | display_name: Some("Bob".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 41 | avatar: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 42 | name_ambiguous: false, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 43 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 44 | timestamp_ms, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 45 | own: false, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 46 | send_state: SendState::Sent, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 47 | content: TimelineContent::Message(Message { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 48 | kind: MessageKind::Text, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 49 | body: body.to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 50 | geo_uri: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 51 | formatted_body: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 52 | mentions: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 53 | edited: false, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 54 | media: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 55 | link_previews: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 56 | }), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 57 | reply_to: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 58 | thread_root: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 59 | thread_summary: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 60 | reactions: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 61 | read_by: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 62 | editable: false, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 63 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 64 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 65 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 66 | #[cfg_attr(not(target_family = "wasm"), tokio::test)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 67 | #[cfg_attr(target_family = "wasm", wasm_bindgen_test::wasm_bindgen_test)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 68 | async fn pagination_keeps_its_discovery_boundary_and_stable_ties() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 69 | let account = user_id!("@alice:mentions.example").to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 70 | let ordered = vec![ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 71 | key("room", "latest"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 72 | key("room", "tie-z"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 73 | key("room", "tie-b"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 74 | key("room", "tie-a"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 75 | key("room", "oldest"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 76 | ]; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 77 | let mut data = Data { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | revision: 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 79 | ..Data::default() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 80 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 81 | for (mention, timestamp) in [ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 82 | (ordered[0].clone(), 300), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | (ordered[1].clone(), 200), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 84 | (ordered[2].clone(), 200), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 85 | (ordered[3].clone(), 200), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 86 | (ordered[4].clone(), 100), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | ] { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | data.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 89 | .insert(id(&mention), entry(mention, timestamp, 1, false)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 90 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 91 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 92 | let first = data.page(&account, None, 2).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 93 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | first |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | .map(|entry| entry.key.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | .collect::<Vec<_>>(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | ordered[..2].to_vec() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 100 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 101 | let mut cursor = first.next_cursor.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 103 | data.revision = 2; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 104 | let later = key("room", "discovered-later"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 105 | data.entries.insert(id(&later), entry(later, 50, 2, false)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 106 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 107 | let mut paged = first |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 108 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 109 | .into_iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 110 | .map(|entry| entry.key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 111 | .collect::<Vec<_>>(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | loop { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | let page = data.page(&account, Some(&cursor), 2).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | paged.extend(page.entries.into_iter().map(|entry| entry.key)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | match page.next_cursor { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | Some(next) => cursor = next, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 117 | None => break, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 118 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 119 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 120 | assert_eq!(paged, ordered); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 121 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 122 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 123 | #[cfg_attr(not(target_family = "wasm"), tokio::test)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 124 | #[cfg_attr(target_family = "wasm", wasm_bindgen_test::wasm_bindgen_test)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 125 | async fn pagination_rejects_a_forged_account_and_a_future_revision() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 126 | let account = user_id!("@alice:mentions.example").to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 127 | let mut data = Data { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 128 | revision: 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 129 | ..Data::default() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 130 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 131 | for event in ["one", "two"] { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 132 | let mention = key("room", event); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 133 | data.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 134 | .insert(id(&mention), entry(mention, 10, 1, false)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 135 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 136 | let cursor = data.page(&account, None, 1).unwrap().next_cursor.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 137 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 138 | let mut forged_account: serde_json::Value = serde_json::from_str(&cursor).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 139 | forged_account["account"] = serde_json::Value::String("@mallory:mentions.example".to_owned()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 140 | assert!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 141 | data.page(&account, Some(&forged_account.to_string()), 1) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 142 | .is_err() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 143 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 144 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 145 | let mut future_revision: serde_json::Value = serde_json::from_str(&cursor).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 146 | future_revision["revision"] = serde_json::json!(data.revision + 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 147 | assert!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 148 | data.page(&account, Some(&future_revision.to_string()), 1) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 149 | .is_err() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 150 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 151 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 152 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 153 | #[cfg_attr(not(target_family = "wasm"), tokio::test)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 154 | #[cfg_attr(target_family = "wasm", wasm_bindgen_test::wasm_bindgen_test)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 155 | async fn retention_discards_read_context_first_and_keeps_shared_events_until_unused() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 156 | let read_key = key("room", "read-mention"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 157 | let unread_key = key("room", "unread-mention"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 158 | let shared_key = key("room", "shared-context"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 159 | let read_id = id(&read_key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 160 | let unread_id = id(&unread_key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 161 | let shared_id = id(&shared_key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 162 | let mut data = Data { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 163 | revision: 3, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 164 | ..Data::default() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 165 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 166 | data.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 167 | .insert(read_id.clone(), entry(read_key, 10, 1, true)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 168 | data.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 169 | .insert(unread_id.clone(), entry(unread_key, 20, 2, false)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 170 | data.events.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 171 | shared_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 172 | SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 173 | item: message_event("$shared-context:mentions.example", 15, "shared"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 174 | media: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 175 | raw: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 176 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 177 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 178 | data.windows.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 179 | read_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 180 | Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 181 | before: vec![shared_id.clone()], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 182 | target: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 183 | after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 184 | start: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 185 | end: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 186 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 187 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 188 | data.windows.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 189 | unread_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 190 | Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 191 | before: vec![shared_id.clone()], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 192 | target: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 193 | after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 194 | start: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 195 | end: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 196 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 197 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 198 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 199 | assert!(data.evict_one().is_some()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 200 | assert!(!data.windows.contains_key(&read_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 201 | assert!(data.windows.contains_key(&unread_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 202 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 203 | data.entries[&read_id].capture, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 204 | MentionCaptureState::Unavailable |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 205 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 206 | assert!(!data.entries[&unread_id].read); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 207 | assert!(data.events.contains_key(&shared_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 208 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 209 | assert!(data.evict_one().is_some()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 210 | assert!(data.windows.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 211 | assert!(data.entries.contains_key(&read_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 212 | assert!(data.entries.contains_key(&unread_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 213 | assert!(!data.events.contains_key(&shared_id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 214 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 215 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 216 | #[cfg_attr(not(target_family = "wasm"), tokio::test)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 217 | #[cfg_attr(target_family = "wasm", wasm_bindgen_test::wasm_bindgen_test)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 218 | async fn context_uses_the_redacted_saved_event_instead_of_the_entry_preview() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 219 | let mention = key("room", "redacted-target"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 220 | let mention_id = id(&mention); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 221 | let original = message_event( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 222 | "$redacted-target:mentions.example", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 223 | 42, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 224 | "private original body", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 225 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 226 | let mut redacted = original.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 227 | if let TimelineItem::Event(event) = &mut redacted { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 228 | event.content = TimelineContent::Redacted; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 229 | event.reply_to = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 230 | event.reactions.clear(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 231 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 232 | let mut data = Data::default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 233 | data.entries.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 234 | mention_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 235 | MentionEntry { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 236 | event: original, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 237 | ..entry(mention.clone(), 42, 1, false) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 238 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 239 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 240 | data.events.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 241 | mention_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 242 | SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 243 | item: redacted, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 244 | media: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 245 | raw: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 246 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 247 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 248 | data.windows.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 249 | mention_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 250 | Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 251 | before: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 252 | target: Some(id(&mention)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 253 | after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 254 | start: Some("start".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 255 | end: Some("end".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 256 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 257 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 258 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 259 | let (context, media) = data.context(&mention).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 260 | let saved = context.context.unwrap().event.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 261 | let TimelineItem::Event(saved) = saved else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 262 | panic!("the saved target should remain a timeline event"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 263 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 264 | assert_eq!(saved.content, TimelineContent::Redacted); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 265 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 266 | saved.event_id.as_deref(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 267 | Some(event_id!("$redacted-target:mentions.example")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 268 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 269 | assert!(media.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 270 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 271 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 272 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 273 | mod persistence { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 274 | use std::sync::atomic::{AtomicUsize, Ordering}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 275 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 276 | use ruma::{events::room::MediaSource, user_id}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 277 | use serde_json::json; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 278 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 279 | use super::*; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 280 | use crate::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 281 | MediaId, MediaKind, StoreConfig, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 282 | mentions::store::{Account, MentionsStore, Write}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 283 | normalise::{MediaRegistration, MediaSourceInfo}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 284 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 285 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 286 | static STORES: AtomicUsize = AtomicUsize::new(0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 287 | const MAX_BYTES: usize = 64 * 1024 * 1024; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 288 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 289 | fn account() -> Account { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 290 | Account { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 291 | homeserver: "https://mentions.example/".to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 292 | user_id: user_id!("@alice:mentions.example").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 293 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 294 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 295 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 296 | fn config() -> StoreConfig { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 297 | let number = STORES.fetch_add(1, Ordering::Relaxed); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 298 | #[cfg(feature = "sqlite")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 299 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 300 | StoreConfig::sqlite( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 301 | std::env::temp_dir().join(format!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 302 | "macltok-mentions-state-{}-{number}", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 303 | std::process::id() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 304 | )), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 305 | "state test passphrase", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 306 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 307 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 308 | #[cfg(feature = "indexeddb")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 309 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 310 | StoreConfig::indexeddb( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 311 | format!("macltok-mentions-state-{}-{number}", js_sys::Date::now()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 312 | "state test passphrase", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 313 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 314 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 315 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 316 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 317 | fn media_registration() -> MediaRegistration { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 318 | MediaRegistration { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 319 | id: MediaId::new("test-scope#image".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 320 | kind: MediaKind::Image, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 321 | full: MediaSourceInfo { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 322 | source: MediaSource::Plain( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 323 | ruma::mxc_uri!("mxc://mentions.example/image").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 324 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 325 | mimetype: Some("image/png".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 326 | declared_size: Some(1234), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 327 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 328 | thumbnail: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 329 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 330 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 331 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 332 | fn saved_data() -> Data { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 333 | let mention = key("persisted-room", "persisted-mention"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 334 | let mention_id = id(&mention); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 335 | let context_item = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 336 | message_event("$persisted-mention:mentions.example", 42, "saved message"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 337 | let mut data = Data { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 338 | revision: 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 339 | ..Data::default() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 340 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 341 | data.entries.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 342 | mention_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 343 | MentionEntry { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 344 | event: context_item.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 345 | ..entry(mention.clone(), 42, 1, true) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 346 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 347 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 348 | data.events.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 349 | mention_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 350 | SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 351 | item: context_item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 352 | media: vec![media_registration()], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 353 | raw: Some( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 354 | json!({"event_id": "$persisted-mention:mentions.example", "body": "saved raw"}), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 355 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 356 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 357 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 358 | data.windows.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 359 | mention_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 360 | Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 361 | before: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 362 | target: Some(mention_id), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 363 | after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 364 | start: Some("start-token".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 365 | end: Some("end-token".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 366 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 367 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 368 | data.history_cursor = Some("history-position".to_owned()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 369 | data |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 370 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 371 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 372 | async fn open(config: &StoreConfig) -> MentionsStore { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 373 | MentionsStore::open(config, account(), MAX_BYTES) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 374 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 375 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 376 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 377 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 378 | #[cfg_attr(not(target_family = "wasm"), tokio::test)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 379 | #[cfg_attr(target_family = "wasm", wasm_bindgen_test::wasm_bindgen_test)] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 380 | async fn encrypted_store_roundtrips_inbox_context_and_read_state() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 381 | let config = config(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 382 | let data = saved_data(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 383 | let mut store = open(&config).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 384 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 385 | store |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 386 | .commit( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 387 | 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 388 | vec![Write::Set( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 389 | "inbox".to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 390 | serde_json::to_value(&data).unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 391 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 392 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 393 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 394 | .unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 395 | 1 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 396 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 397 | drop(store); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 398 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 399 | let mut restored = open(&config).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 400 | let mut restored_data: Data = restored.read("inbox").unwrap().unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 401 | assert_eq!(restored.revision(), restored_data.revision); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 402 | assert!(restored_data.entries.values().next().unwrap().read); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 403 | let mention = restored_data.entries.values().next().unwrap().key.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 404 | let (context, media) = restored_data.context(&mention).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 405 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 406 | context.context.unwrap().start_token.as_deref(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 407 | Some("start-token") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 408 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 409 | assert_eq!(media.len(), 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 410 | assert_eq!(media[0].id.as_str(), "test-scope#image"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 411 | assert_eq!(media[0].full.mimetype.as_deref(), Some("image/png")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 412 | let event = restored_data.events.values().next().unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 413 | assert_eq!(event.raw.as_ref().unwrap()["body"], "saved raw"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 414 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 415 | restored_data.revision = 2; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 416 | let updated_entry = restored_data.entries.values_mut().next().unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 417 | updated_entry.read = false; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 418 | updated_entry.revision = 2; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 419 | restored |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 420 | .commit( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 421 | 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 422 | vec![Write::Set( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 423 | "inbox".to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 424 | serde_json::to_value(&restored_data).unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 425 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 426 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 427 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 428 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 429 | drop(restored); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 430 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 431 | let restored = open(&config).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 432 | let restored_data: Data = restored.read("inbox").unwrap().unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 433 | assert_eq!(restored.revision(), 2); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 434 | assert_eq!(restored_data.revision, 2); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 435 | assert!(!restored_data.entries.values().next().unwrap().read); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 436 | drop(restored); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 437 | config.delete().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 438 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 439 | } |