cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1 | mod support; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 2 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 3 | use std::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 4 | collections::{BTreeMap, HashSet}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 5 | sync::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 6 | Arc, Mutex, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 7 | atomic::{AtomicUsize, Ordering}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 8 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 9 | time::Duration, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 10 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 11 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 12 | #[cfg(feature = "sqlite")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 13 | use macltok::{EncryptedSessionStore, PasswordLogin, Restore, Session, SessionStore, StoreConfig}; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 14 | use macltok::{Engine, EngineConfig, Event, MentionReason, MentionsPage}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 15 | use matrix_sdk::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 16 | Client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 17 | ruma::{OwnedRoomId, OwnedUserId, event_id, events::Mentions, room_id, user_id}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 18 | test_utils::mocks::{MatrixMockServer, RoomContextResponseTemplate}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 19 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 20 | use matrix_sdk_test::{ALICE, JoinedRoomBuilder, event_factory::EventFactory}; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 21 | use ruma::push::{ |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 22 | Action, EventMatchConditionData, HighlightTweakValue, NewConditionalPushRule, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 23 | NewPatternedPushRule, PushCondition, Ruleset, SoundTweakValue, Tweak, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 24 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 25 | use serde_json::{Value, json}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 26 | #[cfg(feature = "sqlite")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 27 | use support::receive_until; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 28 | use support::shut_down; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 29 | use wiremock::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 30 | Mock, Request, Respond, ResponseTemplate, |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 31 | matchers::{method, path, path_regex, query_param}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 32 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 33 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 34 | #[cfg(feature = "serde")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 35 | use macltok::{MentionCoverage, MentionsBoundary, MentionsCommand}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 36 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 37 | const SLIDING_SYNC_PATH: &str = "/_matrix/client/unstable/org.matrix.simplified_msc3575/sync"; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 38 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 39 | struct SlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 40 | rooms: Arc<Mutex<BTreeMap<OwnedRoomId, Vec<Value>>>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 41 | required_state: Arc<Mutex<BTreeMap<OwnedRoomId, Vec<Value>>>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 42 | requests: Arc<AtomicUsize>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 43 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 44 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 45 | impl SlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 46 | fn new(room_id: OwnedRoomId, events: Vec<Value>) -> Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 47 | Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 48 | rooms: Arc::new(Mutex::new(BTreeMap::from([(room_id, events)]))), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 49 | required_state: Arc::new(Mutex::new(BTreeMap::new())), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 50 | requests: Arc::new(AtomicUsize::new(0)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 51 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 52 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 53 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 54 | fn replace(&self, room_id: &ruma::RoomId, events: Vec<Value>) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 55 | self.rooms |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 56 | .lock() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 57 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 58 | .insert(room_id.to_owned(), events); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 59 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 60 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 61 | fn add_room(&self, room_id: &ruma::RoomId, events: Vec<Value>) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 62 | self.rooms |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 63 | .lock() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 64 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 65 | .insert(room_id.to_owned(), events); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 66 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 67 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 68 | fn set_required_state(&self, room_id: &ruma::RoomId, events: Vec<Value>) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 69 | self.required_state |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 70 | .lock() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 71 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 72 | .insert(room_id.to_owned(), events); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 73 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 74 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 75 | async fn mount(&self, server: &MatrixMockServer) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 76 | Mock::given(method("POST")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 77 | .and(path(SLIDING_SYNC_PATH)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | .respond_with(self.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 79 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 80 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 81 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 82 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 84 | impl Clone for SlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 85 | fn clone(&self) -> Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 86 | Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | rooms: self.rooms.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | required_state: self.required_state.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 89 | requests: self.requests.clone(), |
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 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 93 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | impl Respond for SlidingFeed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | fn respond(&self, _request: &Request) -> ResponseTemplate { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | let request = self.requests.fetch_add(1, Ordering::AcqRel); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | let required_state = self.required_state.lock().unwrap().clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | let rooms: serde_json::Map<String, Value> = self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | .rooms |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 100 | .lock() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 101 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 103 | .map(|(room_id, events)| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 104 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 105 | room_id.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 106 | json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 107 | "initial": request == 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 108 | "required_state": required_state |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 109 | .get(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 110 | .cloned() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 111 | .unwrap_or_default(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | "timeline": events, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | }), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 117 | ResponseTemplate::new(200) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 118 | .set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 119 | "txn_id": null, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 120 | "pos": format!("mentions-{request}"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 121 | "lists": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 122 | "rooms": rooms, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 123 | "extensions": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 124 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 125 | .set_delay(Duration::from_millis(20)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 126 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 127 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 128 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 129 | async fn joined_client(server: &MatrixMockServer, room_id: &ruma::RoomId) -> (Client, OwnedUserId) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 130 | let client = server.client_builder().build().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 131 | let own_user = client.user_id().unwrap().to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 132 | let room_id = room_id.to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 133 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 134 | .mock_sync() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 135 | .ok_and_run(&client, { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 136 | let own_user = own_user.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 137 | move |builder| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 138 | let factory = EventFactory::new().room(&room_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 139 | builder.add_global_account_data( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 140 | factory.push_rules(Ruleset::server_default(&own_user)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 141 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 142 | builder.add_joined_room( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 143 | JoinedRoomBuilder::new(&room_id).add_state_event(factory.member(&own_user)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 144 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 145 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 146 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 147 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 148 | (client, own_user) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 149 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 150 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 151 | fn message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 152 | event_id: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 153 | sender: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 154 | timestamp: u64, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 155 | body: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 156 | mentioned: Option<&str>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 157 | ) -> Value { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 158 | let mut content = json!({ "msgtype": "m.text", "body": body }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 159 | if let Some(mentioned) = mentioned { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 160 | content["m.mentions"] = json!({ "user_ids": [mentioned] }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 161 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 162 | json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 163 | "type": "m.room.message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 164 | "event_id": event_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 165 | "sender": sender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 166 | "origin_server_ts": timestamp, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 167 | "content": content, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 168 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 169 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 170 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 171 | fn room_mention(event_id: &str, sender: &str, timestamp: u64) -> Value { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 172 | let mut event = message(event_id, sender, timestamp, "room mention", None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 173 | event["content"]["m.mentions"] = json!({ "room": true }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 174 | event |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 175 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 176 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 177 | async fn mentions_page( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 178 | events: &mut macltok::EventStream, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 179 | room_id: &ruma::RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 180 | predicate: impl Fn(&MentionsPage) -> bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 181 | ) -> MentionsPage { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 182 | mentions_page_with_diagnostics(events, room_id, predicate, None).await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 183 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 184 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 185 | async fn mentions_page_with_diagnostics( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 186 | events: &mut macltok::EventStream, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 187 | room_id: &ruma::RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 188 | predicate: impl Fn(&MentionsPage) -> bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 189 | diagnostic_server: Option<&MatrixMockServer>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 190 | ) -> MentionsPage { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 191 | let mut last_page = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 192 | let result = tokio::time::timeout(Duration::from_secs(5), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 193 | while let Some(event) = events.recv().await { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 194 | match event { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 195 | Event::TimelineReset { timeline_id, .. } if timeline_id.room_id() == room_id => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 196 | panic!("the unopened room produced a timeline reset"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 197 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 198 | Event::MentionsChanged(update) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 199 | let snapshot = update.snapshot; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 200 | let matched = predicate(&snapshot); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 201 | last_page = Some(snapshot.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 202 | if matched { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 203 | return snapshot; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 204 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 205 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 206 | _ => {} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 207 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 208 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 209 | panic!("event stream closed before the expected mentions page"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 210 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 211 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 212 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 213 | match result { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 214 | Ok(page) => page, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 215 | Err(_) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 216 | let last_state = last_page.map(|page: MentionsPage| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 217 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 218 | page.revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 219 | page.unread_count, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 220 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 221 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 222 | .map(|entry| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 223 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 224 | entry.key.room_id.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 225 | entry.key.event_id.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 226 | entry.capture.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 227 | entry.revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 228 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 229 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 230 | .collect::<Vec<_>>(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 231 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 232 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 233 | let requests = if let Some(server) = diagnostic_server { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 234 | server.server().received_requests().await.map(|requests| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 235 | requests |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 236 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 237 | .filter(|request| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 238 | let path = request.url.path(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 239 | path.contains(room_id.as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 240 | && (path.contains("/context/") || path.ends_with("/messages")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 241 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 242 | .map(|request| format!("{} {}", request.method, request.url)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 243 | .collect::<Vec<_>>() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 244 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 245 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 246 | None |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 247 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 248 | panic!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 249 | "expected mentions update did not arrive, last state {last_state:?}, requests {requests:?}" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 250 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 251 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 252 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 253 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 254 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 255 | fn entry_ids(page: &MentionsPage) -> HashSet<String> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 256 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 257 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 258 | .map(|entry| entry.key.event_id.to_string()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 259 | .collect() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 260 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 261 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 262 | async fn wait_for_requests(feed: &SlidingFeed, minimum: usize) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 263 | tokio::time::timeout(Duration::from_secs(5), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 264 | while feed.requests.load(Ordering::Acquire) < minimum { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 265 | tokio::task::yield_now().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 266 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 267 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 268 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 269 | .expect("the sliding sync feed did not receive another request"); |
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 | async fn mount_messages_page( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 273 | server: &MatrixMockServer, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 274 | room_id: &ruma::RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 275 | direction: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 276 | token: &str, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 277 | limit: u16, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 278 | ) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 279 | Mock::given(method("GET")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 280 | .and(path_regex(format!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 281 | r"^/_matrix/client/v3/rooms/{room_id}/messages$" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 282 | ))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 283 | .and(query_param("dir", direction)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 284 | .and(query_param("from", token)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 285 | .and(move |request: &Request| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 286 | request |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 287 | .url |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 288 | .query_pairs() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 289 | .find(|(key, _)| key == "limit") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 290 | .map_or(limit == 10, |(_, value)| value == limit.to_string()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 291 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 292 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 293 | "start": token, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 294 | "end": format!("{token}-next"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 295 | "chunk": [], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 296 | "state": [], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 297 | }))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 298 | .expect(1) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 299 | .mount(server.server()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 300 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 301 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 302 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 303 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 304 | async fn unopened_room_mentions_are_deduplicated_and_read_through_uses_a_revision_boundary() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 305 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 306 | let room_id = room_id!("!mentions-inbox:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 307 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 308 | let sender = user_id!("@alice:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 309 | let initial = vec![ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 310 | message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 311 | "$mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 312 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 313 | 2_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 314 | "direct mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 315 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 316 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 317 | message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 318 | "$own-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 319 | own_user.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 320 | 2_100, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 321 | "own event", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 322 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 323 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 324 | message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 325 | "$unrelated", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 326 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 327 | 2_200, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 328 | "ordinary message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 329 | None, |
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 | let feed = SlidingFeed::new(room_id.to_owned(), initial.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 333 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 334 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 335 | let (engine, handle, mut events) = Engine::from_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 336 | client, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 337 | EngineConfig::default() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 338 | .with_initial_history_limit(0) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 339 | .with_search_backfill_pages_per_room(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 340 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 341 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 342 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 343 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 344 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 345 | let first = mentions_page(&mut events, room_id, |page| !page.entries.is_empty()).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 346 | assert_eq!(entry_ids(&first), HashSet::from(["$mention".to_owned()])); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 347 | assert_eq!(first.unread_count, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 348 | let first_boundary = first.boundary.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 349 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 350 | wait_for_requests(&feed, 3).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 351 | let replayed = handle.mentions_page(None, 50).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 352 | assert_eq!(entry_ids(&replayed), HashSet::from(["$mention".to_owned()])); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 353 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 354 | let key = replayed.entries[0].key.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 355 | handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 356 | .set_mentions_read(vec![key.clone()], true) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 357 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 358 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 359 | let read = handle.mentions_page(None, 50).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 360 | assert_eq!(read.unread_count, 0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 361 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 362 | feed.replace( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 363 | room_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 364 | [ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 365 | initial, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 366 | vec![message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 367 | "$older-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 368 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 369 | 1_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 370 | "older direct mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 371 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 372 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 373 | ] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 374 | .concat(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 375 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 376 | let updated = mentions_page(&mut events, room_id, |page| page.entries.len() == 2).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 377 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 378 | entry_ids(&updated), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 379 | HashSet::from(["$mention".to_owned(), "$older-mention".to_owned(),]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 380 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 381 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 382 | handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 383 | .mark_mentions_read_through(first_boundary) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 384 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 385 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 386 | let after_boundary = handle.mentions_page(None, 50).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 387 | let read_states: Vec<_> = after_boundary |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 388 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 389 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 390 | .map(|entry| (entry.key.event_id.to_string(), entry.read)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 391 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 392 | assert!(read_states.contains(&("$mention".to_owned(), true))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 393 | assert!(read_states.contains(&("$older-mention".to_owned(), false))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 394 | assert_eq!(after_boundary.unread_count, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 395 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 396 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 397 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 398 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 399 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 400 | async fn edits_can_remove_and_restore_a_mention_until_redaction_removes_its_target() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 401 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 402 | let room_id = room_id!("!mentions-edits:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 403 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 404 | let sender = user_id!("@alice:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 405 | let original = message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 406 | "$edited-target", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 407 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 408 | 1_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 409 | "original mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 410 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 411 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 412 | let removal = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 413 | "type": "m.room.message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 414 | "event_id": "$remove-edit", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 415 | "sender": sender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 416 | "origin_server_ts": 2_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 417 | "content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 418 | "msgtype": "m.text", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 419 | "body": "* edited without a mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 420 | "m.relates_to": { "rel_type": "m.replace", "event_id": "$edited-target" }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 421 | "m.new_content": { "msgtype": "m.text", "body": "edited without a mention" }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 422 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 423 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 424 | let addition = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 425 | "type": "m.room.message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 426 | "event_id": "$add-edit", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 427 | "sender": sender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 428 | "origin_server_ts": 3_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 429 | "content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 430 | "msgtype": "m.text", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 431 | "body": "* edited to mention you", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 432 | "m.relates_to": { "rel_type": "m.replace", "event_id": "$edited-target" }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 433 | "m.new_content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 434 | "msgtype": "m.text", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 435 | "body": "edited to mention you", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 436 | "m.mentions": { "user_ids": [own_user.as_str()] }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 437 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 438 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 439 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 440 | let redaction = json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 441 | "type": "m.room.redaction", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 442 | "event_id": "$redaction", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 443 | "sender": sender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 444 | "origin_server_ts": 4_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 445 | "redacts": "$edited-target", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 446 | "content": {}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 447 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 448 | let feed = SlidingFeed::new(room_id.to_owned(), vec![original.clone()]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 449 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 450 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 451 | let (engine, handle, mut events) = Engine::from_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 452 | client, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 453 | EngineConfig::default() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 454 | .with_initial_history_limit(0) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 455 | .with_search_backfill_pages_per_room(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 456 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 457 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 458 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 459 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 460 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 461 | let discovered = mentions_page(&mut events, room_id, |page| !page.entries.is_empty()).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 462 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 463 | entry_ids(&discovered), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 464 | HashSet::from(["$edited-target".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 465 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 466 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 467 | feed.replace(room_id, vec![original.clone(), removal.clone()]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 468 | let removed = mentions_page(&mut events, room_id, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 469 | page.revision > discovered.revision && page.entries.is_empty() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 470 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 471 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 472 | assert!(removed.entries.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 473 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 474 | handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 475 | .mention_context(room_id, ruma::event_id!("$edited-target")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 476 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 477 | .unwrap_err() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 478 | .kind, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 479 | macltok::ErrorKind::EventNotFound |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 480 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 481 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 482 | feed.replace(room_id, vec![original.clone(), removal, addition]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 483 | let restored = mentions_page(&mut events, room_id, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 484 | page.revision > removed.revision && entry_ids(page).contains("$edited-target") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 485 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 486 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 487 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 488 | entry_ids(&restored), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 489 | HashSet::from(["$edited-target".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 490 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 491 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 492 | feed.replace(room_id, vec![original, redaction]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 493 | let redacted = mentions_page(&mut events, room_id, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 494 | page.revision > restored.revision && page.entries.is_empty() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 495 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 496 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 497 | assert!(redacted.entries.is_empty()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 498 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 499 | handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 500 | .mention_context(room_id, ruma::event_id!("$edited-target")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 501 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 502 | .unwrap_err() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 503 | .kind, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 504 | macltok::ErrorKind::EventNotFound |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 505 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 506 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 507 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 508 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 509 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 510 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 511 | async fn room_mentions_require_power_while_muted_direct_mentions_remain_and_ignored_senders_drop() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 512 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 513 | let client = server.client_builder().build().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 514 | let own_user = client.user_id().unwrap().to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 515 | let room_mention_room = room_id!("!room-mention-policy:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 516 | let muted_room = room_id!("!muted-mention-policy:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 517 | let ignored_sender = user_id!("@ignored:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 518 | let sender = user_id!("@alice:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 519 | let mut rules = Ruleset::server_default(&own_user); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 520 | rules.override_.shift_insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 521 | 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 522 | NewConditionalPushRule::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 523 | "mute-inbox-room".to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 524 | vec![PushCondition::EventMatch(EventMatchConditionData::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 525 | "room_id".to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 526 | muted_room.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 527 | ))], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 528 | Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 529 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 530 | .into(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 531 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 532 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 533 | .mock_sync() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 534 | .ok_and_run(&client, { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 535 | let own_user = own_user.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 536 | let ignored_sender = ignored_sender.to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 537 | move |builder| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 538 | let factory = EventFactory::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 539 | builder.add_global_account_data(factory.push_rules(rules)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 540 | builder.add_global_account_data(factory.ignored_user_list([ignored_sender])); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 541 | builder.add_joined_room( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 542 | JoinedRoomBuilder::new(room_mention_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 543 | .add_state_event( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 544 | EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 545 | .room(room_mention_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 546 | .member(&own_user), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 547 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 548 | .add_state_event( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 549 | EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 550 | .room(room_mention_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 551 | .default_power_levels() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 552 | .sender(&own_user), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 553 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 554 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 555 | builder.add_joined_room( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 556 | JoinedRoomBuilder::new(muted_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 557 | .add_state_event(EventFactory::new().room(muted_room).member(&own_user)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 558 | .add_state_event( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 559 | EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 560 | .room(muted_room) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 561 | .default_power_levels() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 562 | .sender(&own_user), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 563 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 564 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 565 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 566 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 567 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 568 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 569 | let feed = SlidingFeed::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 570 | room_mention_room.to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 571 | vec![room_mention( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 572 | "$unauthorized-room-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 573 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 574 | 3_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 575 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 576 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 577 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 578 | let muted_events = vec![message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 579 | "$muted-direct-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 580 | sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 581 | 2_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 582 | "muted direct mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 583 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 584 | )]; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 585 | let ignored = message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 586 | "$ignored-direct-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 587 | ignored_sender.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 588 | 1_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 589 | "ignored direct mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 590 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 591 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 592 | let mut muted_events = muted_events; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 593 | muted_events.push(ignored); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 594 | feed.add_room(muted_room, muted_events); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 595 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 596 | let (engine, handle, mut events) = Engine::from_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 597 | client, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 598 | EngineConfig::default() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 599 | .with_initial_history_limit(0) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 600 | .with_search_backfill_pages_per_room(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 601 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 602 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 603 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 604 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 605 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 606 | let page = mentions_page(&mut events, room_mention_room, |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 607 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 608 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 609 | .any(|entry| entry.key.event_id.as_str() == "$muted-direct-mention") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 610 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 611 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 612 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 613 | entry_ids(&page), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 614 | HashSet::from(["$muted-direct-mention".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 615 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 616 | assert!(page.entries[0].notification_suppressed); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 617 | assert!(!entry_ids(&page).contains("$unauthorized-room-mention")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 618 | assert!(!entry_ids(&page).contains("$ignored-direct-mention")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 619 | let room_ids: HashSet<_> = page |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 620 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 621 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 622 | .map(|entry| entry.key.room_id.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 623 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 624 | assert!(room_ids.contains(&muted_room.to_owned())); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 625 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 626 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 627 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 628 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 629 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 630 | async fn event_context_uses_independent_direction_limits_and_saved_mention_context_is_offline() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 631 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 632 | let room_id = room_id!("!mentions-context:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 633 | let (client, own_user) = joined_client(&server, room_id).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 634 | let mention_id = event_id!("$mention-context"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 635 | let context_id = event_id!("$manual-context"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 636 | let mention_event = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 637 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 638 | .text_msg("mention with saved context") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 639 | .mentions(Mentions::with_user_ids([own_user.clone()])) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 640 | .event_id(mention_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 641 | .sender(&ALICE) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 642 | .into_event(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 643 | let context_event = EventFactory::new() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 644 | .room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 645 | .text_msg("manually fetched context") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 646 | .event_id(context_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 647 | .sender(&ALICE) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 648 | .into_event(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 649 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 650 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 651 | .mock_room_event_context() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 652 | .room(room_id.to_owned()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 653 | .match_event_id() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 654 | .ok(RoomContextResponseTemplate::new(mention_event.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 655 | .start("mention-before") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 656 | .end("mention-after")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 657 | .expect(1) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 658 | .mount() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 659 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 660 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 661 | .mock_room_event_context() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 662 | .room(room_id.to_owned()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 663 | .match_event_id() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 664 | .ok(RoomContextResponseTemplate::new(context_event) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 665 | .start("manual-before") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 666 | .end("manual-after")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 667 | .expect(1) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 668 | .mount() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 669 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 670 | mount_messages_page(&server, room_id, "b", "mention-before", 10).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 671 | mount_messages_page(&server, room_id, "f", "mention-after", 10).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 672 | mount_messages_page(&server, room_id, "b", "manual-before", 2).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 673 | mount_messages_page(&server, room_id, "f", "manual-after", 3).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 674 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 675 | let feed = SlidingFeed::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 676 | room_id.to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 677 | vec![message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 678 | mention_id.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 679 | ALICE.as_str(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 680 | 5_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 681 | "mention with saved context", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 682 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 683 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 684 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 685 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 686 | let (engine, handle, mut events) = Engine::from_client( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 687 | client, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 688 | EngineConfig::default() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 689 | .with_initial_history_limit(0) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 690 | .with_search_backfill_pages_per_room(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 691 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 692 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 693 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 694 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 695 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 696 | let captured = mentions_page_with_diagnostics( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 697 | &mut events, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 698 | room_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 699 | |page| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 700 | page.entries.iter().any(|entry| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 701 | entry.key.event_id == mention_id |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 702 | && matches!(&entry.capture, macltok::MentionCaptureState::Partial) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 703 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 704 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 705 | Some(&server), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 706 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 707 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 708 | let saved = handle.mention_context(room_id, mention_id).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 709 | assert!(saved.context.is_some()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 710 | assert!(matches!(saved.state, macltok::MentionCaptureState::Partial)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 711 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 712 | let before_saved_request_count = server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 713 | .server() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 714 | .received_requests() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 715 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 716 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 717 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 718 | .filter(|request| request.url.path().contains("/context/$mention-context")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 719 | .count(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 720 | let _ = handle.mention_context(room_id, mention_id).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 721 | let after_saved_request_count = server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 722 | .server() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 723 | .received_requests() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 724 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 725 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 726 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 727 | .filter(|request| request.url.path().contains("/context/$mention-context")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 728 | .count(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 729 | assert_eq!(after_saved_request_count, before_saved_request_count); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 730 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 731 | let session = handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 732 | .event_context(room_id, context_id, 2, 3) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 733 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 734 | .unwrap(); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 735 | let context = &session.context; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 736 | assert_eq!(context.start_token.as_deref(), Some("manual-before-next")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 737 | assert_eq!(context.end_token.as_deref(), Some("manual-after-next")); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 738 | handle |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 739 | .close_event_context(&session.context_id) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 740 | .await |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 741 | .unwrap(); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 742 | let closed = handle |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 743 | .event_context_before(&session.context_id, "manual-before-next", 5) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 744 | .await |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 745 | .unwrap_err(); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 746 | assert_eq!(closed.kind, macltok::ErrorKind::EventContextNotOpen); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 747 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 748 | let requests = server.server().received_requests().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 749 | let context_limits: Vec<_> = requests |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 750 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 751 | .filter(|request| request.url.path().contains("/context/")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 752 | .map(|request| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 753 | request |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 754 | .url |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 755 | .query_pairs() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 756 | .find(|(key, _)| key == "limit") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 757 | .map(|(_, value)| value.into_owned()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 758 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 759 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 760 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 761 | assert_eq!(context_limits, ["0", "0"]); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 762 | let queries: Vec<_> = requests |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 763 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 764 | .filter(|request| request.url.path().ends_with("/messages")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 765 | .map(|request| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 766 | let query: std::collections::HashMap<_, _> = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 767 | request.url.query_pairs().into_owned().collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 768 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 769 | query.get("dir").cloned().unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 770 | query.get("from").cloned().unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 771 | query |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 772 | .get("limit") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 773 | .cloned() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 774 | .unwrap_or_else(|| "10".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 775 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 776 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 777 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 778 | assert!(queries.contains(&("b".to_owned(), "mention-before".to_owned(), "10".to_owned()))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 779 | assert!(queries.contains(&("f".to_owned(), "mention-after".to_owned(), "10".to_owned()))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 780 | assert!(queries.contains(&("b".to_owned(), "manual-before".to_owned(), "2".to_owned()))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 781 | assert!(queries.contains(&("f".to_owned(), "manual-after".to_owned(), "3".to_owned()))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 782 | assert_eq!(captured.unread_count, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 783 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 784 | shut_down(&handle, &mut events, running).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 785 | server.server().verify().await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 786 | } |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 787 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 788 | #[tokio::test] |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 789 | async fn highlighting_keywords_are_mentions_and_quiet_ones_are_not() { |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 790 | let server = MatrixMockServer::new().await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 791 | let client = server.client_builder().build().await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 792 | let own_user = client.user_id().unwrap().to_owned(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 793 | let room_id = room_id!("!keyword-policy:localhost"); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 794 | let sender = user_id!("@alice:localhost"); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 795 | let mut rules = Ruleset::server_default(&own_user); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 796 | let notify = [ |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 797 | Action::Notify, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 798 | Action::SetTweak(Tweak::Sound(SoundTweakValue::Default)), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 799 | ]; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 800 | let mut loud = notify.to_vec(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 801 | loud.push(Action::SetTweak(Tweak::Highlight(HighlightTweakValue::Yes))); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 802 | rules |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 803 | .content |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 804 | .insert(NewPatternedPushRule::new("deploy".to_owned(), "deploy".to_owned(), loud).into()); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 805 | rules.content.insert( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 806 | NewPatternedPushRule::new("lunch".to_owned(), "lunch".to_owned(), notify.to_vec()).into(), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 807 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 808 | server |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 809 | .mock_sync() |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 810 | .ok_and_run(&client, { |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 811 | let own_user = own_user.clone(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 812 | move |builder| { |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 813 | let factory = EventFactory::new().room(room_id); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 814 | builder.add_global_account_data(factory.push_rules(rules)); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 815 | builder.add_joined_room( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 816 | JoinedRoomBuilder::new(room_id).add_state_event(factory.member(&own_user)), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 817 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 818 | } |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 819 | }) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 820 | .await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 821 | let feed = SlidingFeed::new( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 822 | room_id.to_owned(), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 823 | vec![ |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 824 | message( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 825 | "$keyword", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 826 | sender.as_str(), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 827 | 1_000, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 828 | "the deploy is done", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 829 | None, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 830 | ), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 831 | message("$quiet", sender.as_str(), 2_000, "lunch anyone", None), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 832 | message("$plain", sender.as_str(), 3_000, "nothing to see", None), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 833 | message( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 834 | "$keyword-direct", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 835 | sender.as_str(), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 836 | 4_000, |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 837 | "deploy now", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 838 | Some(own_user.as_str()), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 839 | ), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 840 | message("$own-keyword", own_user.as_str(), 5_000, "my deploy", None), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 841 | ], |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 842 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 843 | feed.mount(&server).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 844 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 845 | let (engine, handle, mut events) = Engine::from_client( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 846 | client, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 847 | EngineConfig::default() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 848 | .with_initial_history_limit(0) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 849 | .with_search_backfill_pages_per_room(0), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 850 | ) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 851 | .await |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 852 | .unwrap(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 853 | let running = tokio::spawn(engine.run()); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 854 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 855 | let page = mentions_page(&mut events, room_id, |page| page.entries.len() >= 2).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 856 | assert_eq!( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 857 | entry_ids(&page), |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 858 | HashSet::from(["$keyword".to_owned(), "$keyword-direct".to_owned()]) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 859 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 860 | let reasons: BTreeMap<_, _> = page |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 861 | .entries |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 862 | .iter() |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 863 | .map(|entry| (entry.key.event_id.to_string(), entry.reasons.clone())) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 864 | .collect(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 865 | assert_eq!(reasons["$keyword"], vec![MentionReason::Keyword]); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 866 | assert_eq!(reasons["$keyword-direct"], vec![MentionReason::Direct]); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 867 | assert_eq!(page.unread_count, 2); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 868 | assert!( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 869 | page.entries |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 870 | .iter() |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 871 | .all(|entry| !entry.notification_suppressed) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 872 | ); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 873 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 874 | shut_down(&handle, &mut events, running).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 875 | } |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 876 | |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 877 | fn actions_are(expected: Value) -> impl Fn(&Request) -> bool + Send + Sync + 'static { |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 878 | move |request: &Request| { |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 879 | serde_json::from_slice::<Value>(&request.body).is_ok_and(|body| body["actions"] == expected) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 880 | } |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 881 | } |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 882 | |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 883 | async fn mount_keyword_rule(server: &MatrixMockServer, actions: Value) { |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 884 | Mock::given(method("GET")) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 885 | .and(path("/_matrix/client/v3/pushrules/global/content/deploy")) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 886 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({ |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 887 | "rule_id": "deploy", |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 888 | "pattern": "deploy", |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 889 | "default": false, |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 890 | "enabled": true, |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 891 | "actions": actions, |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 892 | }))) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 893 | .mount(server.server()) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 894 | .await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 895 | } |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 896 | |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 897 | #[tokio::test] |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 898 | async fn adding_a_keyword_gives_its_rule_a_highlight() { |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 899 | let server = MatrixMockServer::new().await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 900 | let room_id = room_id!("!keyword-rule:localhost"); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 901 | let (client, _) = joined_client(&server, room_id).await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 902 | Mock::given(method("PUT")) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 903 | .and(path("/_matrix/client/v3/pushrules/global/content/deploy")) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 904 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({}))) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 905 | .expect(1) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 906 | .mount(server.server()) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 907 | .await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 908 | mount_keyword_rule( |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 909 | &server, |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 910 | json!(["notify", {"set_tweak": "sound", "value": "default"}]), |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 911 | ) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 912 | .await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 913 | Mock::given(method("PUT")) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 914 | .and(path( |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 915 | "/_matrix/client/v3/pushrules/global/content/deploy/actions", |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 916 | )) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 917 | .and(actions_are(json!([ |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 918 | "notify", |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 919 | {"set_tweak": "sound", "value": "default"}, |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 920 | {"set_tweak": "highlight"}, |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 921 | ]))) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 922 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({}))) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 923 | .expect(1) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 924 | .mount(server.server()) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 925 | .await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 926 | SlidingFeed::new(room_id.to_owned(), Vec::new()) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 927 | .mount(&server) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 928 | .await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 929 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 930 | let (engine, handle, mut events) = Engine::from_client( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 931 | client, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 932 | EngineConfig::default().with_search_backfill_pages_per_room(0), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 933 | ) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 934 | .await |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 935 | .unwrap(); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 936 | let running = tokio::spawn(engine.run()); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 937 | handle.add_notification_keyword("deploy").await.unwrap(); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 938 | shut_down(&handle, &mut events, running).await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 939 | server.server().verify().await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 940 | } |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 941 | |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 942 | #[tokio::test] |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 943 | async fn a_quiet_keyword_rule_only_gains_the_highlight() { |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 944 | let server = MatrixMockServer::new().await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 945 | let client = server.client_builder().build().await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 946 | let own_user = client.user_id().unwrap().to_owned(); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 947 | let room_id = room_id!("!quiet-keyword-rule:localhost"); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 948 | let mut rules = Ruleset::server_default(&own_user); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 949 | rules.content.insert( |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 950 | NewPatternedPushRule::new("deploy".to_owned(), "deploy".to_owned(), Vec::new()).into(), |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 951 | ); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 952 | server |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 953 | .mock_sync() |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 954 | .ok_and_run(&client, { |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 955 | let own_user = own_user.clone(); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 956 | move |builder| { |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 957 | let factory = EventFactory::new().room(room_id); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 958 | builder.add_global_account_data(factory.push_rules(rules)); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 959 | builder.add_joined_room( |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 960 | JoinedRoomBuilder::new(room_id).add_state_event(factory.member(&own_user)), |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 961 | ); |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 962 | } |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 963 | }) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 964 | .await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 965 | mount_keyword_rule(&server, json!([])).await; |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 966 | Mock::given(method("PUT")) |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 967 | .and(path( |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 968 | "/_matrix/client/v3/pushrules/global/content/deploy/actions", |
e9af9e0e0f31 | atagen | 2026-10-25 10:55 UTC | 969 | )) |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 970 | .and(actions_are(json!([{"set_tweak": "highlight"}]))) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 971 | .respond_with(ResponseTemplate::new(200).set_body_json(json!({}))) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 972 | .expect(1) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 973 | .mount(server.server()) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 974 | .await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 975 | SlidingFeed::new(room_id.to_owned(), Vec::new()) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 976 | .mount(&server) |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 977 | .await; |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 978 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 979 | let (engine, handle, mut events) = Engine::from_client( |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 980 | client, |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 981 | EngineConfig::default().with_search_backfill_pages_per_room(0), |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 982 | ) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 983 | .await |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 984 | .unwrap(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 985 | let running = tokio::spawn(engine.run()); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 986 | handle.add_notification_keyword("deploy").await.unwrap(); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 987 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 988 | server.server().verify().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 989 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 990 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 991 | #[cfg(feature = "sqlite")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 992 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 993 | async fn sqlite_mentions_survive_engine_shutdown_and_session_resume() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 994 | static NEXT_TEMPORARY_PATH: AtomicUsize = AtomicUsize::new(0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 995 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 996 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 997 | server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 998 | .mock_versions() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 999 | .with_simplified_sliding_sync() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1000 | .ok() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1001 | .mount() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1002 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1003 | server.mock_login().ok().mount().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1004 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1005 | let root = std::env::temp_dir().join(format!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1006 | "macltok-mentions-inbox-{}-{}", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1007 | std::process::id(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1008 | NEXT_TEMPORARY_PATH.fetch_add(1, Ordering::Relaxed) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1009 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1010 | let store_path = root.join("store"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1011 | let sessions_path = root.join("records").join("session"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1012 | let sessions = Arc::new(EncryptedSessionStore::file( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1013 | &sessions_path, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1014 | StoreConfig::sqlite(&store_path, "mentions inbox test passphrase"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1015 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1016 | let room_id = room_id!("!mentions-resume:matrix.org"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1017 | let own_user = user_id!("@cheeky_monkey:matrix.org"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1018 | let feed = SlidingFeed::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1019 | room_id.to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1020 | vec![message( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1021 | "$durable-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1022 | "@alice:matrix.org", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1023 | 7_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1024 | "persist this mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1025 | Some(own_user.as_str()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1026 | )], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1027 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1028 | feed.set_required_state( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1029 | room_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1030 | vec![json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1031 | "type": "m.room.member", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1032 | "event_id": "$joined-member", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1033 | "sender": own_user, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1034 | "state_key": own_user, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1035 | "origin_server_ts": 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1036 | "content": { "membership": "join" }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1037 | })], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1038 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1039 | feed.mount(&server).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1040 | |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1041 | let config = EngineConfig::default() |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1042 | .with_initial_history_limit(0) |
74befbc72baf | atagen | 2026-10-25 11:18 UTC | 1043 | .with_search_backfill_pages_per_room(0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1044 | let session = Session::login_password( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1045 | PasswordLogin::new(server.uri(), "example", "wordpass").allow_insecure_loopback(true), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1046 | sessions.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1047 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1048 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1049 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1050 | let (engine, handle, mut events) = session.build(config.clone()).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1051 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1052 | let first = mentions_page(&mut events, room_id, |page| !page.entries.is_empty()).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1053 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1054 | entry_ids(&first), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1055 | HashSet::from(["$durable-mention".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1056 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1057 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1058 | drop(handle); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1059 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1060 | let Some(Restore::Resumed(session)) = Session::restore(sessions.clone()).await.unwrap() else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1061 | panic!("the saved session did not resume"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1062 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1063 | let (engine, handle, mut events) = session.build(config).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1064 | let running = tokio::spawn(engine.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1065 | receive_until(&mut events, |event| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1066 | matches!(event, Event::Authenticated { .. }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1067 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1068 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1069 | let resumed = handle.mentions_page(None, 50).await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1070 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1071 | entry_ids(&resumed), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1072 | HashSet::from(["$durable-mention".to_owned()]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1073 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1074 | assert_eq!(resumed.unread_count, 1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1075 | shut_down(&handle, &mut events, running).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1076 | drop(handle); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1077 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1078 | sessions.discard().await.unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1079 | std::fs::remove_dir_all(root).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1080 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1081 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1082 | #[cfg(feature = "serde")] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1083 | #[test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1084 | fn mentions_command_and_page_round_trip_through_serde() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1085 | let command = MentionsCommand::ReadThrough(MentionsBoundary { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1086 | account: user_id!("@alice:example.org").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1087 | revision: 7, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1088 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1089 | let encoded_command = serde_json::to_value(&command).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1090 | let decoded_command: MentionsCommand = serde_json::from_value(encoded_command).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1091 | assert_eq!(decoded_command, command); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1092 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1093 | let page = MentionsPage { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1094 | entries: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1095 | next_cursor: Some("opaque cursor".to_owned()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1096 | end_reached: false, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1097 | coverage: MentionCoverage::default(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1098 | unread_count: 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1099 | revision: 7, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1100 | boundary: MentionsBoundary { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1101 | account: user_id!("@alice:example.org").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1102 | revision: 7, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1103 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1104 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1105 | let encoded_page = serde_json::to_value(&page).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1106 | let decoded_page: MentionsPage = serde_json::from_value(encoded_page).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1107 | assert_eq!(decoded_page, page); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1108 | } |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1109 | |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1110 | #[tokio::test] |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1111 | async fn inbox_rows_carry_sender_profiles_and_never_leave_replies_pending() { |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1112 | let server = MatrixMockServer::new().await; |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1113 | let room_id = room_id!("!inbox-profiles:localhost"); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1114 | let (client, own_user) = joined_client(&server, room_id).await; |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1115 | let sender = user_id!("@alice:localhost"); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1116 | let feed = SlidingFeed::new( |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1117 | room_id.to_owned(), |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1118 | vec![message("$parent", sender.as_str(), 1_000, "parent", None)], |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1119 | ); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1120 | feed.set_required_state( |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1121 | room_id, |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1122 | vec![json!({ |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1123 | "type": "m.room.member", |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1124 | "state_key": sender, |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1125 | "sender": sender, |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1126 | "event_id": "$alice-member", |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1127 | "origin_server_ts": 1, |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1128 | "content": { "membership": "join", "displayname": "Alice" }, |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1129 | })], |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1130 | ); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1131 | feed.mount(&server).await; |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1132 | let (engine, handle, mut events) = Engine::from_client( |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1133 | client, |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1134 | EngineConfig::default() |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1135 | .with_initial_history_limit(0) |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1136 | .with_search_backfill_pages_per_room(0), |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1137 | ) |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1138 | .await |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1139 | .unwrap(); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1140 | let running = tokio::spawn(engine.run()); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1141 | wait_for_requests(&feed, 2).await; |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1142 | |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1143 | let reply = |event_id: &str, target: &str, timestamp| { |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1144 | let mut event = message( |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1145 | event_id, |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1146 | sender.as_str(), |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1147 | timestamp, |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1148 | "reply", |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1149 | Some(own_user.as_str()), |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1150 | ); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1151 | event["content"]["m.relates_to"] = json!({ "m.in_reply_to": { "event_id": target } }); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1152 | event |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1153 | }; |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1154 | feed.replace( |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1155 | room_id, |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1156 | vec![ |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1157 | reply("$answer", "$parent", 2_000), |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1158 | reply("$orphan", "$missing", 2_100), |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1159 | ], |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1160 | ); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1161 | let page = mentions_page(&mut events, room_id, |page| page.entries.len() == 2).await; |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1162 | for entry in &page.entries { |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1163 | let macltok::TimelineItem::Event(event) = &entry.event else { |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1164 | panic!("a mention should be an event"); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1165 | }; |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1166 | assert_eq!(event.sender.display_name.as_deref(), Some("Alice")); |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1167 | let details = &event |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1168 | .reply_to |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1169 | .as_ref() |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1170 | .expect("both mentions reply") |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1171 | .details; |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1172 | match entry.key.event_id.as_str() { |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1173 | "$answer" => assert!(matches!(details, macltok::ReplyDetails::Ready { .. })), |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1174 | _ => assert_eq!(details, &macltok::ReplyDetails::Unavailable), |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1175 | } |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1176 | } |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1177 | shut_down(&handle, &mut events, running).await; |
b34c2df6d896 | atagen | 2026-10-25 13:52 UTC | 1178 | } |