cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1 | use std::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 2 | collections::{BTreeMap, BTreeSet}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 3 | sync::Arc, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 4 | time::Duration, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 5 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 6 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 7 | use futures_util::{StreamExt, stream::FuturesUnordered}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 8 | use matrix_sdk::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 9 | Client, RoomState, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 10 | sliding_sync::{ResponseCommitBatch, ResponseCommitHook}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 11 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 12 | use ruma::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 13 | OwnedRoomId, OwnedUserId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 14 | events::{AnySyncTimelineEvent, room::encrypted::OriginalSyncRoomEncryptedEvent}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 15 | serde::Raw, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 16 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 17 | use serde_json::Value; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 18 | use tokio::sync::{mpsc, oneshot, watch}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 19 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 20 | use super::state::{Candidate, Data, SavedEvent, Window, id, invalid, now}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 21 | use crate::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 22 | CommandOutput, EngineConfig, Error, ErrorKind, Event, MentionCaptureState, MentionEntry, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 23 | MentionGap, MentionKey, MentionReason, MentionsCommand, MentionsPage, MentionsUpdate, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 24 | TimelineContent, TimelineItem, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 25 | engine::{MediaService, MentionsStore, context, executor::EngineBoxFuture}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 26 | normalise::{MediaRegistration, normalise_observed}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 27 | transport::EventSender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 28 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 29 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 30 | #[cfg(all(test, feature = "sqlite"))] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 31 | #[path = "service_tests.rs"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 32 | mod tests; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 33 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 34 | #[derive(Clone)] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 35 | pub(crate) struct Handle(mpsc::Sender<Request>); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 36 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 37 | enum Request { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 38 | Batch(ResponseCommitBatch, oneshot::Sender<Result<(), Error>>), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 39 | Command( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 40 | MentionsCommand, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 41 | oneshot::Sender<Result<CommandOutput, Error>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 42 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 43 | Stop(oneshot::Sender<()>), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 44 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 45 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 46 | pub(crate) struct Service { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 47 | client: Client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 48 | account: OwnedUserId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 49 | media: Arc<MediaService>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 50 | store: Option<MentionsStore>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 51 | data: Data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 52 | config: EngineConfig, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 53 | requests: mpsc::Receiver<Request>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 54 | updates: watch::Sender<MentionsPage>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 55 | recovery_budget: usize, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 56 | latest_scan: bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 57 | recovery_retry: u64, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 58 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 59 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 60 | enum Work { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 61 | Capture( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 62 | String, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 63 | u64, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 64 | Result<(crate::EventContext, Vec<MediaRegistration>, Vec<Value>), Error>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 65 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 66 | Candidate(String, Result<Value, Error>), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 67 | History( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 68 | bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 69 | Option<String>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 70 | Result<ruma::api::client::push::get_notifications::v3::Response, Error>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 71 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 72 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 73 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 74 | impl Handle { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 75 | pub(crate) fn hook(&self) -> ResponseCommitHook { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 76 | let handle = self.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 77 | ResponseCommitHook::new(move |batch| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | let handle = handle.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 79 | async move { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 80 | let (reply, response) = oneshot::channel(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 81 | handle |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 82 | .0 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | .send(Request::Batch(batch, reply)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 84 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 85 | .map_err(|_| "mentions service stopped".to_owned())?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 86 | response |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | .map_err(|_| "mentions commit acknowledgement dropped".to_owned())? |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 89 | .map_err(|error| error.to_string()) |
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 | pub(crate) async fn execute(&self, command: MentionsCommand) -> Result<CommandOutput, Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | let (reply, response) = oneshot::channel(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | self.0 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | .send(Request::Command(command, reply)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | .map_err(|_| Error::interrupted())?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 100 | response.await.map_err(|_| Error::interrupted())? |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 101 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 103 | pub(crate) async fn stop(&self) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 104 | let (reply, response) = oneshot::channel(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 105 | if self.0.send(Request::Stop(reply)).await.is_ok() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 106 | let _ = response.await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 107 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 108 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 109 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 110 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 111 | impl Service { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | pub(crate) async fn new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | client: Client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | media: Arc<MediaService>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | store: Option<MentionsStore>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | config: EngineConfig, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 117 | ) -> Result<(Handle, Self, watch::Receiver<MentionsPage>), Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 118 | let account = client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 119 | .user_id() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 120 | .ok_or_else(|| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 121 | Error::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 122 | ErrorKind::Unauthenticated, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 123 | "mentions require an authenticated account", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 124 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 125 | })? |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 126 | .to_owned(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 127 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 128 | let data: Data = store |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 129 | .as_ref() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 130 | .map(|store| store.read("inbox")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 131 | .transpose()? |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 132 | .flatten() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 133 | .unwrap_or_default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 134 | #[cfg(not(any(feature = "sqlite", feature = "indexeddb")))] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 135 | let data = Data::default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 136 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 137 | if store |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 138 | .as_ref() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 139 | .is_some_and(|store| store.revision() != data.revision) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 140 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 141 | return Err(Error::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 142 | ErrorKind::Store, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 143 | "mentions revision does not match its checkpoint", |
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 | let page = data.page(&account, None, 50)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 147 | media |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 148 | .register( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 149 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 150 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 151 | .filter_map(|entry| data.events.get(&id(&entry.key))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 152 | .flat_map(|event| event.media.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 153 | .collect(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 154 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 155 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 156 | let (updates, receiver) = watch::channel(page); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 157 | let (sender, requests) = mpsc::channel(config.command_capacity.max(1)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 158 | Ok(( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 159 | Handle(sender), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 160 | Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 161 | client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 162 | account, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 163 | media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 164 | store, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 165 | data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 166 | config, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 167 | requests, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 168 | updates, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 169 | recovery_budget: 20, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 170 | latest_scan: true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 171 | recovery_retry: 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 172 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 173 | receiver, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 174 | )) |
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 save(&mut self, mut next: Data) -> Result<(), Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 178 | next.revision = self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 179 | .data |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 180 | .revision |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 181 | .checked_add(1) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 182 | .ok_or_else(|| invalid("mentions revision exhausted"))?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 183 | next.bound( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 184 | self.config.max_mentions_entries, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 185 | self.config.max_mention_context_events, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 186 | self.config.max_mention_candidates, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 187 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 188 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 189 | if let Some(store) = &mut self.store { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 190 | loop { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 191 | let value = serde_json::to_value(&next).map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 192 | Error::with_source( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 193 | ErrorKind::Store, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 194 | "encoding the mentions inbox failed", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 195 | error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 196 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 197 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 198 | match store |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 199 | .commit( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 200 | self.data.revision, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 201 | vec![super::store::Write::Set("inbox".to_owned(), value)], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 202 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 203 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 204 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 205 | Ok(_) => break, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 206 | Err(error) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 207 | if error.message == "mentions storage limit exceeded" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 208 | && next.evict_one() => {} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 209 | Err(error) => return Err(error), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 210 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 211 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 212 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 213 | self.data = next; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 214 | let page = self.data.page(&self.account, None, 50)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 215 | self.media |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 216 | .register( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 217 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 218 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 219 | .filter_map(|entry| self.data.events.get(&id(&entry.key))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 220 | .flat_map(|event| event.media.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 221 | .collect(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 222 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 223 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 224 | self.updates.send_replace(page); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 225 | Ok(()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 226 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 227 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 228 | async fn command(&mut self, command: MentionsCommand) -> Result<CommandOutput, Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 229 | match command { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 230 | MentionsCommand::Page { cursor, limit } => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 231 | let page = self.data.page(&self.account, cursor.as_deref(), limit)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 232 | self.media |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 233 | .register( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 234 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 235 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 236 | .filter_map(|entry| self.data.events.get(&id(&entry.key))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 237 | .flat_map(|event| event.media.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 238 | .collect(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 239 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 240 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 241 | Ok(CommandOutput::MentionsPage(page)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 242 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 243 | MentionsCommand::Context(key) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 244 | let (context, media) = self.data.context(&key)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 245 | self.media.register(media).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 246 | Ok(CommandOutput::MentionContext(context)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 247 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 248 | MentionsCommand::RetryContext(key) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 249 | let room = self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 250 | .client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 251 | .get_room(&key.room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 252 | .filter(|room| room.state() == RoomState::Joined) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 253 | .ok_or_else(|| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 254 | Error::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 255 | ErrorKind::Forbidden, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 256 | "saved context cannot be refetched after leaving the room", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 257 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 258 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 259 | drop(room); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 260 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 261 | let entry = next |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 262 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 263 | .get_mut(&id(&key)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 264 | .ok_or_else(|| Error::new(ErrorKind::EventNotFound, "mention was not found"))?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 265 | entry.capture = MentionCaptureState::Pending; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 266 | entry.revision = self.data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 267 | next.windows.entry(id(&key)).or_insert(Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 268 | before: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 269 | target: Some(id(&key)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 270 | after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 271 | start: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 272 | end: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 273 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 274 | self.save(next).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 275 | let (context, media) = self.data.context(&key)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 276 | self.media.register(media).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 277 | Ok(CommandOutput::MentionContext(context)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 278 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 279 | MentionsCommand::SetRead { keys, read } => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 280 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 281 | for key in keys { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 282 | if let Some(entry) = next.entries.get_mut(&id(&key)) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 283 | entry.read = read; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 284 | entry.revision = self.data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 285 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 286 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 287 | self.save(next).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 288 | Ok(CommandOutput::Done) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 289 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 290 | MentionsCommand::ReadThrough(boundary) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 291 | if boundary.account != self.account || boundary.revision > self.data.revision { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 292 | return Err(invalid("invalid mentions read boundary")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 293 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 294 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 295 | for entry in next |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 296 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 297 | .values_mut() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 298 | .filter(|entry| entry.discovery_revision <= boundary.revision) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 299 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 300 | entry.read = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 301 | entry.revision = self.data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 302 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 303 | self.save(next).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 304 | Ok(CommandOutput::Done) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 305 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 306 | MentionsCommand::Recover => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 307 | self.recovery_budget = 20; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 308 | self.latest_scan = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 309 | self.recovery_retry = 0; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 310 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 311 | next.coverage.recovering = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 312 | next.coverage.historical_supported = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 313 | self.save(next).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 314 | Ok(CommandOutput::Done) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 315 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 316 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 317 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 318 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 319 | fn gap(data: &mut Data, room_id: Option<OwnedRoomId>, reason: &str, token: Option<String>) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 320 | if !data |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 321 | .coverage |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 322 | .gaps |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 323 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 324 | .any(|gap| gap.room_id == room_id && gap.reason == reason && gap.token == token) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 325 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 326 | data.coverage.gaps.push(MentionGap { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 327 | room_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 328 | reason: reason.to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 329 | token, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 330 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 331 | if data.coverage.gaps.len() > 128 { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 332 | data.coverage.gaps.remove(0); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 333 | data.coverage.truncated = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 334 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 335 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 336 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 337 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 338 | async fn batch(&mut self, batch: ResponseCommitBatch) -> Result<(), Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 339 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 340 | if next.checkpoints.get(&batch.connection_id) == Some(&batch.next_pos) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 341 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 342 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 343 | if next.checkpoints.get(&batch.connection_id) != batch.previous_pos.as_ref() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 344 | Self::gap( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 345 | &mut next, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 346 | None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 347 | "sync checkpoint continuity is unknown", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 348 | None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 349 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 350 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 351 | for (room_id, raw) in &batch.raw_rooms { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 352 | if raw.limited || !next.observed_rooms.contains(room_id) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 353 | Self::gap( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 354 | &mut next, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 355 | Some(room_id.clone()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 356 | if raw.limited { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 357 | "limited sync timeline" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 358 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 359 | "initial room history is incomplete" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 360 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 361 | raw.prev_batch.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 362 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 363 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 364 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 365 | let room_ids: BTreeSet<_> = batch |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 366 | .raw_rooms |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 367 | .keys() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 368 | .chain(batch.response.rooms.joined.keys()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 369 | .cloned() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 370 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 371 | for room_id in &room_ids { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 372 | if !self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 373 | .client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 374 | .get_room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 375 | .is_some_and(|room| room.state() == RoomState::Joined) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 376 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 377 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 378 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 379 | next.observed_rooms.insert(room_id.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 380 | let mut observed = BTreeMap::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 381 | if let Some(raw) = batch.raw_rooms.get(room_id) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 382 | for event in &raw.timeline { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 383 | let value: Value = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 384 | serde_json::from_str(event.json().get()).map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 385 | Error::with_source(ErrorKind::Invalid, "invalid observed event", error) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 386 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 387 | if let Some(event_id) = value["event_id"].as_str() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 388 | observed.insert(event_id.to_owned(), value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 389 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 390 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 391 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 392 | if let Some(update) = batch.response.rooms.joined.get(room_id) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 393 | for event in &update.timeline.events { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 394 | let value: Value = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 395 | serde_json::from_str(event.raw().json().get()).map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 396 | Error::with_source(ErrorKind::Invalid, "invalid observed event", error) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 397 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 398 | if let Some(event_id) = value["event_id"].as_str() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 399 | observed.insert(event_id.to_owned(), value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 400 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 401 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 402 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 403 | let mut observed: Vec<_> = observed.into_values().collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 404 | observed.sort_by_key(|value| value["origin_server_ts"].as_u64().unwrap_or(0)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 405 | for value in observed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 406 | self.observe(&mut next, room_id, value).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 407 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 408 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 409 | for room_id in batch.response.rooms.left.keys() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 410 | Self::gap( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 411 | &mut next, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 412 | Some(room_id.clone()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 413 | "room left, retained context is read only", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 414 | None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 415 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 416 | next.candidates |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 417 | .retain(|_, candidate| &candidate.room_id != room_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 418 | for entry in next |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 419 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 420 | .values_mut() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 421 | .filter(|entry| &entry.key.room_id == room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 422 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 423 | entry.capture = MentionCaptureState::Unavailable; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 424 | entry.revision = next.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 425 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 426 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 427 | let reevaluate: Vec<_> = next |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 428 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 429 | .values() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 430 | .filter(|entry| room_ids.contains(&entry.key.room_id)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 431 | .filter_map(|entry| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 432 | next.events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 433 | .get(&id(&entry.key)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 434 | .and_then(|event| event.raw.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 435 | .map(|raw| (entry.key.room_id.clone(), raw)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 436 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 437 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 438 | for (room_id, raw) in reevaluate { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 439 | self.observe(&mut next, &room_id, raw).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 440 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 441 | self.filter_ignored(&mut next).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 442 | next.checkpoints.insert(batch.connection_id, batch.next_pos); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 443 | self.save(next).await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 444 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 445 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 446 | async fn filter_ignored(&self, next: &mut Data) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 447 | let mut removed = Vec::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 448 | for (key, entry) in &next.entries { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 449 | if let TimelineItem::Event(event) = &entry.event { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 450 | if self.client.is_user_ignored(&event.sender.user_id).await { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 451 | removed.push(key.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 452 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 453 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 454 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 455 | for key in removed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 456 | next.entries.remove(&key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 457 | next.windows.remove(&key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 458 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 459 | next.collect_events(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 460 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 461 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 462 | async fn observe( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 463 | &self, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 464 | data: &mut Data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 465 | room_id: &OwnedRoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 466 | mut value: Value, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 467 | ) -> Result<(), Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 468 | let Some(event_id) = value["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 469 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 470 | .and_then(|value| value.parse().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 471 | else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 472 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 473 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 474 | let key = MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 475 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 476 | event_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 477 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 478 | let identity = id(&key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 479 | let Some(timestamp) = value["origin_server_ts"].as_u64() else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 480 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 481 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 482 | data.coverage.earliest_timestamp_ms = Some( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 483 | data.coverage |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 484 | .earliest_timestamp_ms |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 485 | .map_or(timestamp, |old| old.min(timestamp)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 486 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 487 | if value["type"] == "m.room.redaction" { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 488 | if let Some(target) = value["redacts"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 489 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 490 | .or_else(|| value["content"]["redacts"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 491 | .and_then(|value| value.parse().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 492 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 493 | let target = id(&MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 494 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 495 | event_id: target, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 496 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 497 | data.redacted.insert(target.clone(), timestamp); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 498 | data.entries.remove(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 499 | data.windows.remove(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 500 | data.candidates.remove(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 501 | data.edits.remove(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 502 | data.candidates.retain(|_, candidate| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 503 | candidate.room_id != *room_id |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 504 | || candidate.raw["content"]["m.relates_to"]["event_id"].as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 505 | != target.split_once('\n').map(|(_, event)| event) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 506 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 507 | if let Some(saved) = data.events.get_mut(&target) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 508 | redact(saved); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 509 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 510 | data.touch_contexts(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 511 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 512 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 513 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 514 | if data.redacted.contains_key(&identity) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 515 | || value["unsigned"].get("redacted_because").is_some() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 516 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 517 | data.redacted.insert(identity.clone(), timestamp); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 518 | data.entries.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 519 | data.windows.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 520 | data.candidates.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 521 | if let Some(saved) = data.events.get_mut(&identity) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 522 | redact(saved); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 523 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 524 | data.touch_contexts(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 525 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 526 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 527 | if value["content"]["m.relates_to"]["rel_type"] == "m.replace" { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 528 | if let Some(target) = value["content"]["m.relates_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 529 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 530 | .and_then(|value| value.parse().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 531 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 532 | let target = id(&MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 533 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 534 | event_id: target, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 535 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 536 | if data.redacted.contains_key(&target) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 537 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 538 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 539 | let newer = data.edits.get(&target).is_none_or(|old| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 540 | (old["origin_server_ts"].as_u64(), old["event_id"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 541 | < (Some(timestamp), value["event_id"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 542 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 543 | if newer { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 544 | if let Some(original) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 545 | data.events.get(&target).and_then(|saved| saved.raw.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 546 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 547 | if original["sender"] != value["sender"] { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 548 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 549 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 550 | data.edits.insert(target.clone(), value.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 551 | value = edited(original, &value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 552 | return Box::pin(self.observe(data, room_id, value)).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 553 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 554 | data.candidates.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 555 | identity, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 556 | Candidate { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 557 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 558 | raw: value, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 559 | attempts: 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 560 | retry_at_ms: 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 561 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 562 | ); |
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 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 566 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 567 | if let Some(edit) = value["unsigned"]["m.relations"].get("m.replace").cloned() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 568 | if edit["sender"] == value["sender"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 569 | && edit["content"]["m.new_content"].is_object() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 570 | && edit["content"]["m.relates_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 571 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 572 | .is_none_or(|target| target == key.event_id.as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 573 | && edit["origin_server_ts"].as_u64().is_some() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 574 | && data.edits.get(&identity).is_none_or(|old| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 575 | (old["origin_server_ts"].as_u64(), old["event_id"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 576 | < (edit["origin_server_ts"].as_u64(), edit["event_id"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 577 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 578 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 579 | data.edits.insert(identity.clone(), edit); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 580 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 581 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 582 | if let Some(edit) = data.edits.get(&identity) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 583 | if value["sender"] == edit["sender"] { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 584 | value = edited(value, edit); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 585 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 586 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 587 | if let Some(edit) = value["unsigned"].get("macltok_edit").cloned() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 588 | data.edits.insert(identity.clone(), edit); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 589 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 590 | if value["type"] == "m.room.encrypted" { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 591 | data.candidates.entry(identity).or_insert(Candidate { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 592 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 593 | raw: value, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 594 | attempts: 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 595 | retry_at_ms: 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 596 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 597 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 598 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 599 | let Some((item, media)) = normalise_observed(&value, Some(&self.account), &identity) else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 600 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 601 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 602 | self.supplement(data, &key, &item, &media, &value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 603 | let (reasons, suppressed, unresolved) = self.classify(data, room_id, &value).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 604 | if unresolved { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 605 | data.candidates |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 606 | .entry(identity.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 607 | .or_insert(Candidate { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 608 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 609 | raw: value.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 610 | attempts: 0, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 611 | retry_at_ms: now() + 30_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 612 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 613 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 614 | data.candidates.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 615 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 616 | if reasons.is_empty() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 617 | data.entries.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 618 | data.windows.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 619 | if data.events.contains_key(&identity) || data.edits.contains_key(&identity) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 620 | data.events.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 621 | identity, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 622 | SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 623 | item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 624 | media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 625 | raw: Some(value), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 626 | }, |
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 | return Ok(()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 630 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 631 | let previous = data.entries.get(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 632 | let changed = previous.is_none_or(|entry| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 633 | entry.event != item |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 634 | || entry.reasons != reasons |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 635 | || entry.notification_suppressed != suppressed |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 636 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 637 | let entry = MentionEntry { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 638 | key, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 639 | timestamp_ms: timestamp, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 640 | discovered_at_ms: previous.map_or_else(now, |entry| entry.discovered_at_ms), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 641 | discovery_revision: previous |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 642 | .map_or(data.revision + 1, |entry| entry.discovery_revision), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 643 | revision: if changed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 644 | data.revision + 1 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 645 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 646 | previous.unwrap().revision |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 647 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 648 | event: item.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 649 | reasons, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 650 | read: previous.is_some_and(|entry| entry.read), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 651 | capture: previous.map_or(MentionCaptureState::Pending, |entry| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 652 | if changed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 653 | MentionCaptureState::Pending |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 654 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 655 | entry.capture.clone() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 656 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 657 | }), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 658 | room_name: self.client.get_room(room_id).and_then(|room| room.name()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 659 | notification_suppressed: suppressed, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 660 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 661 | data.entries.insert(identity.clone(), entry); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 662 | data.windows.entry(identity.clone()).or_insert(Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 663 | before: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 664 | target: Some(identity.clone()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 665 | after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 666 | start: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 667 | end: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 668 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 669 | data.events.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 670 | identity, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 671 | SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 672 | item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 673 | media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 674 | raw: Some(value), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 675 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 676 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 677 | Ok(()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 678 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 679 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 680 | fn supplement( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 681 | &self, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 682 | data: &mut Data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 683 | key: &MentionKey, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 684 | item: &TimelineItem, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 685 | media: &[MediaRegistration], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 686 | raw: &Value, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 687 | ) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 688 | let TimelineItem::Event(event) = item else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 689 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 690 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 691 | let identity = id(key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 692 | if data |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 693 | .events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 694 | .get(&identity) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 695 | .is_some_and(|saved| saved.item != *item) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 696 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 697 | data.touch_contexts(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 698 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 699 | let mut retained = data.events.contains_key(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 700 | let saved_events = &data.events; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 701 | for (target, window) in &mut data.windows { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 702 | let Some(entry) = data.entries.get_mut(target) else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 703 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 704 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 705 | if entry.key.room_id != key.room_id |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 706 | || (entry.timestamp_ms, target.as_str()) >= (event.timestamp_ms, identity.as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 707 | || window.after.contains(&identity) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 708 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 709 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 710 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 711 | let previous = window.after.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 712 | window.after.push(identity.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 713 | window.after.sort_by_key(|key| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 714 | if key == &identity { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 715 | (event.timestamp_ms, key.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 716 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 717 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 718 | saved_events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 719 | .get(key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 720 | .and_then(|saved| match &saved.item { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 721 | TimelineItem::Event(event) => Some(event.timestamp_ms), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 722 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 723 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 724 | .unwrap_or(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 725 | key.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 726 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 727 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 728 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 729 | window.after.truncate(10); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 730 | if window.after == previous { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 731 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 732 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 733 | window.end = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 734 | retained |= window.after.contains(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 735 | if entry.capture == MentionCaptureState::Partial |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 736 | && window.after.len() == 10 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 737 | && (window.before.len() == 10 || window.start.is_none()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 738 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 739 | entry.capture = MentionCaptureState::Ready; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 740 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 741 | entry.revision = data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 742 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 743 | if retained { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 744 | data.events.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 745 | identity, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 746 | SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 747 | item: item.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 748 | media: media.to_vec(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 749 | raw: Some(raw.clone()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 750 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 751 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 752 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 753 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 754 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 755 | async fn classify( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 756 | &self, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 757 | data: &Data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 758 | room_id: &OwnedRoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 759 | value: &Value, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 760 | ) -> (Vec<MentionReason>, bool, bool) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 761 | let Some(sender) = value["sender"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 762 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 763 | .and_then(|sender| sender.parse::<OwnedUserId>().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 764 | else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 765 | return (vec![], false, false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 766 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 767 | if sender == self.account |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 768 | || self.client.is_user_ignored(&sender).await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 769 | || value["type"] != "m.room.message" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 770 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 771 | return (vec![], false, false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 772 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 773 | let Some(room) = self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 774 | .client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 775 | .get_room(room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 776 | .filter(|room| room.state() == RoomState::Joined) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 777 | else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 778 | return (vec![], false, false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 779 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 780 | let raw: Raw<AnySyncTimelineEvent> = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 781 | Raw::from_json_string(value.to_string()).expect("serialized event"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 782 | let actions = room.event_push_actions(&raw).await.ok().flatten(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 783 | let notify = actions.as_ref().is_some_and(|actions| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 784 | actions |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 785 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 786 | .any(|action| matches!(action, ruma::push::Action::Notify)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 787 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 788 | let highlight = actions.as_ref().is_some_and(|actions| actions.iter().any(|action| matches!(action, ruma::push::Action::SetTweak(ruma::push::Tweak::Highlight(value)) if value != &ruma::push::HighlightTweakValue::No))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 789 | let direct = value["content"]["m.mentions"]["user_ids"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 790 | .as_array() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 791 | .is_some_and(|users| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 792 | users |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 793 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 794 | .any(|user| user.as_str() == Some(self.account.as_str())) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 795 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 796 | let mut reasons = Vec::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 797 | if direct { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 798 | reasons.push(MentionReason::Direct); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 799 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 800 | let mut unresolved = false; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 801 | if let Some(reply) = value["content"]["m.relates_to"]["m.in_reply_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 802 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 803 | .and_then(|value| value.parse().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 804 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 805 | let target = data.events.get(&id(&MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 806 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 807 | event_id: reply, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 808 | })); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 809 | let reply_to_self = value["unsigned"]["macltok_reply_sender"].as_str() == Some(self.account.as_str()) || target.is_some_and(|target| matches!(&target.item, TimelineItem::Event(event) if event.sender.user_id == self.account)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 810 | if reply_to_self && (direct || (notify && highlight)) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 811 | reasons.push(MentionReason::Reply); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 812 | } else if target.is_none() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 813 | && value["unsigned"]["macltok_reply_sender"].as_str().is_none() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 814 | && (direct || (notify && highlight)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 815 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 816 | unresolved = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 817 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 818 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 819 | if value["content"]["m.mentions"]["room"] == true { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 820 | match room.power_levels().await { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 821 | Ok(levels) if levels.user_can_trigger_room_notification(&sender) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 822 | if notify && highlight { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 823 | reasons.push(MentionReason::Room); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 824 | } else if actions.is_none() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 825 | unresolved = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 826 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 827 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 828 | Err(_) => unresolved = true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 829 | _ => {} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 830 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 831 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 832 | (reasons, !notify, unresolved) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 833 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 834 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 835 | pub(crate) async fn run(mut self) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 836 | let reports_client = self.client.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 837 | let reports = reports_client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 838 | .event_cache() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 839 | .subscribe_to_decryption_reports(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 840 | futures_util::pin_mut!(reports); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 841 | let mut ignores = self.client.subscribe_to_ignore_user_list_changes(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 842 | let mut work: FuturesUnordered<EngineBoxFuture<'static, Work>> = FuturesUnordered::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 843 | let mut captures = BTreeSet::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 844 | let mut retries = BTreeSet::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 845 | let mut history = false; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 846 | let mut next_scan = now() + 60_000; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 847 | let mut tick = Box::pin(matrix_sdk::sleep::sleep(Duration::from_secs(1))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 848 | loop { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 849 | self.schedule(&mut work, &mut captures, &mut retries, &mut history); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 850 | tokio::select! { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 851 | biased; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 852 | request = self.requests.recv() => match request { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 853 | Some(Request::Stop(reply)) => { let _ = reply.send(()); return; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 854 | Some(Request::Batch(batch, reply)) => { let result = self.batch(batch).await; let _ = reply.send(result); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 855 | Some(Request::Command(command, reply)) => { let result = self.command(command).await; let _ = reply.send(result); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 856 | None => return, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 857 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 858 | Some(result) = work.next(), if !work.is_empty() => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 859 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 860 | let recovery = (self.latest_scan, self.recovery_budget); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 861 | match result { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 862 | Work::Capture(key, revision, result) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 863 | captures.remove(&key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 864 | let observed = result.as_ref().ok().map(|(_, _, observed)| observed.clone()).unwrap_or_default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 865 | let room_id = next.entries.get(&key).filter(|entry| entry.revision == revision) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 866 | .map(|entry| entry.key.room_id.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 867 | .filter(|room_id| self.client.get_room(room_id).is_some_and(|room| room.state() == RoomState::Joined)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 868 | self.captured(&mut next, key, revision, result); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 869 | if let Some(room_id) = room_id { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 870 | for value in observed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 871 | let identity = value["event_id"].as_str().map(|event_id| format!("{room_id}\n{event_id}")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 872 | if identity.as_ref().is_some_and(|key| next.events.get(key).is_some_and(|event| event.raw.is_some())) { continue; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 873 | self.observe(&mut next, &room_id, value).await.ok(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 874 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 875 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 876 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 877 | Work::Candidate(key, result) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 878 | retries.remove(&key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 879 | if let Some(mut candidate) = next.candidates.remove(&key) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 880 | match result { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 881 | Ok(value) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 882 | if let Err(error) = self.observe(&mut next, &candidate.room_id, value).await { tracing::warn!(%error, "mention retry failed"); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 883 | if let Some(unresolved) = next.candidates.get_mut(&key) { unresolved.attempts = candidate.attempts.saturating_add(1); unresolved.retry_at_ms = now() + (1000u64 << unresolved.attempts.min(8)); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 884 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 885 | Err(error) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 886 | if candidate.raw["type"] != "m.room.encrypted" && (matches!(error.kind, ErrorKind::EventNotFound | ErrorKind::Forbidden) || error.server.as_ref().is_some_and(|server| matches!(server.status, 403 | 404))) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 887 | Self::gap(&mut next, Some(candidate.room_id), "mention notification context is unavailable", None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 888 | } else { candidate.attempts = candidate.attempts.saturating_add(1); candidate.retry_at_ms = retry_at(&error, 1000u64 << candidate.attempts.min(8)); next.candidates.insert(key, candidate); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 889 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 890 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 891 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 892 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 893 | Work::History(latest, from, result) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 894 | history = false; self.recovery_budget = self.recovery_budget.saturating_sub(1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 895 | match result { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 896 | Ok(page) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 897 | next.coverage.historical_supported = Some(true); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 898 | let mut reached_anchor = false; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 899 | for notification in page.notifications { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 900 | if let Some(event_id) = notification.event.get_field::<String>("event_id").ok().flatten() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 901 | let identity = format!("{}\n{}", notification.room_id, event_id); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 902 | if latest && from.is_none() && next.latest_head.is_none() { next.latest_head = Some(identity.clone()); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 903 | if latest && next.latest_anchor.as_ref() == Some(&identity) { reached_anchor = true; break; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 904 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 905 | if self.client.get_room(¬ification.room_id).is_some_and(|room| room.state() == RoomState::Joined) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 906 | if let Ok(value) = serde_json::from_str(notification.event.json().get()) { let _ = self.observe(&mut next, ¬ification.room_id, value).await; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 907 | }} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 908 | let token = page.next_token.filter(|token| Some(token) != from.as_ref() && !reached_anchor); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 909 | if latest { next.latest_cursor = token.clone(); if token.is_none() { self.latest_scan = false; next.latest_anchor = next.latest_head.take().or(next.latest_anchor); } } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 910 | else { next.history_cursor = token.clone(); next.history_exhausted = token.is_none(); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 911 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 912 | Err(error) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 913 | if error.server.as_ref().is_some_and(|server| matches!(server.status, 404 | 405 | 501)) { next.coverage.historical_supported = Some(false); self.recovery_budget = 0; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 914 | if error.server.as_ref().is_some_and(|server| server.status == 400) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 915 | if latest { next.latest_cursor = None; next.latest_head = None; } else { next.history_cursor = None; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 916 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 917 | Self::gap(&mut next, None, "notification history could not be recovered", from); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 918 | self.recovery_retry = retry_at(&error, 60_000); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 919 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 920 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 921 | next.coverage.recovering = self.recovery_budget > 0 && (self.latest_scan || !next.history_exhausted) && next.coverage.historical_supported != Some(false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 922 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 923 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 924 | if let Err(error) = self.save(next).await { tracing::error!(%error, "saving mention background work failed"); self.latest_scan = recovery.0; self.recovery_budget = recovery.1; self.recovery_retry = now() + 60_000; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 925 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 926 | Some(_) = reports.next() => { for candidate in self.data.candidates.values_mut() { candidate.retry_at_ms = 0; } } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 927 | Some(_) = ignores.next() => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 928 | let mut next = self.data.clone(); self.filter_ignored(&mut next).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 929 | if let Err(error) = self.save(next).await { tracing::error!(%error, "saving ignored mention removals failed"); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 930 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 931 | () = &mut tick => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 932 | tick = Box::pin(matrix_sdk::sleep::sleep(Duration::from_secs(1))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 933 | if now() >= next_scan && !history { self.recovery_budget = 20; self.latest_scan = true; next_scan = now() + 60_000; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 934 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 935 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 936 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 937 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 938 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 939 | fn schedule( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 940 | &mut self, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 941 | work: &mut FuturesUnordered<EngineBoxFuture<'static, Work>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 942 | captures: &mut BTreeSet<String>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 943 | retries: &mut BTreeSet<String>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 944 | history: &mut bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 945 | ) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 946 | for (key, entry) in self.data.entries.iter().filter(|(_, entry)| matches!(entry.capture, MentionCaptureState::Pending) || matches!(entry.capture, MentionCaptureState::Failed {retry_at_ms, ..} if retry_at_ms <= now())).take(128) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 947 | if captures.len() >= 2 { break; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 948 | if captures.contains(key) { continue; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 949 | let Some(room) = self.client.get_room(&entry.key.room_id).filter(|room| room.state() == RoomState::Joined) else { continue; }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 950 | captures.insert(key.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 951 | let key = key.clone(); let event_id = entry.key.event_id.clone(); let account = self.account.clone(); let revision = entry.revision; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 952 | work.push(Box::pin(async move { Work::Capture(key, revision, context::capture(room, Some(account), event_id, 10, 10).await) })); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 953 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 954 | for (key, candidate) in &self.data.candidates { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 955 | if retries.len() >= 2 { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 956 | break; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 957 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 958 | if candidate.retry_at_ms > now() || retries.contains(key) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 959 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 960 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 961 | let Some(room) = self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 962 | .client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 963 | .get_room(&candidate.room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 964 | .filter(|room| room.state() == RoomState::Joined) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 965 | else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 966 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 967 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 968 | let key = key.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 969 | let candidate = candidate.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 970 | let key_for_work = key.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 971 | retries.insert(key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 972 | work.push(Box::pin(async move { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 973 | let result = async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 974 | if candidate.raw["type"] == "m.room.encrypted" { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 975 | let raw: Raw<OriginalSyncRoomEncryptedEvent> = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 976 | Raw::from_json_string(candidate.raw.to_string()).map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 977 | Error::with_source( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 978 | ErrorKind::Invalid, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 979 | "invalid encrypted mention candidate", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 980 | error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 981 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 982 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 983 | let event = room.decrypt_event(&raw, None).await.map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 984 | Error::from_sdk("decrypting a mention candidate failed", error) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 985 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 986 | let value: Value = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 987 | serde_json::from_str(event.raw().json().get()).map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 988 | Error::with_source( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 989 | ErrorKind::Invalid, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 990 | "invalid decrypted mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 991 | error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 992 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 993 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 994 | if value["type"] == "m.room.encrypted" { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 995 | return Err(Error::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 996 | ErrorKind::Encryption, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 997 | "mention candidate still awaits keys", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 998 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 999 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1000 | return Ok(value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1001 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1002 | let target = candidate.raw["content"]["m.relates_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1003 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1004 | .filter(|_| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1005 | candidate.raw["content"]["m.relates_to"]["rel_type"] == "m.replace" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1006 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1007 | if let Some(target) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1008 | target.and_then(|target| target.parse::<ruma::OwnedEventId>().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1009 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1010 | let event = room.event(&target, None).await.map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1011 | Error::from_sdk("fetching an edited mention failed", error) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1012 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1013 | let original: Value = serde_json::from_str(event.raw().json().get()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1014 | .map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1015 | Error::with_source( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1016 | ErrorKind::Invalid, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1017 | "invalid edited mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1018 | error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1019 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1020 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1021 | if original["sender"] == candidate.raw["sender"] { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1022 | let mut original = edited(original, &candidate.raw); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1023 | original["unsigned"]["macltok_edit"] = candidate.raw; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1024 | return Ok(original); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1025 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1026 | return Err(Error::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1027 | ErrorKind::Forbidden, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1028 | "an edit belongs to a different sender", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1029 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1030 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1031 | let mut value = candidate.raw; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1032 | if let Some(target) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1033 | value["content"]["m.relates_to"]["m.in_reply_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1034 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1035 | .and_then(|target| target.parse::<ruma::OwnedEventId>().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1036 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1037 | let event = room.event(&target, None).await.map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1038 | Error::from_sdk("fetching a mention reply target failed", error) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1039 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1040 | let target: Value = serde_json::from_str(event.raw().json().get()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1041 | .map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1042 | Error::with_source( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1043 | ErrorKind::Invalid, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1044 | "invalid mention reply target", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1045 | error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1046 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1047 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1048 | value["unsigned"]["macltok_reply_sender"] = target["sender"].clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1049 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1050 | Ok(value) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1051 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1052 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1053 | Work::Candidate(key_for_work, result) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1054 | })); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1055 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1056 | if !*history |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1057 | && self.recovery_budget > 0 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1058 | && now() >= self.recovery_retry |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1059 | && (self.latest_scan || !self.data.history_exhausted) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1060 | && self.data.coverage.historical_supported != Some(false) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1061 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1062 | *history = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1063 | let latest = self.latest_scan; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1064 | let from = if latest { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1065 | self.data.latest_cursor.clone() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1066 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1067 | self.data.history_cursor.clone() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1068 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1069 | let client = self.client.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1070 | work.push(Box::pin(async move { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1071 | let mut request = ruma::api::client::push::get_notifications::v3::Request::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1072 | request.from = from.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1073 | request.limit = Some(ruma::uint!(50)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1074 | let result = client.send(request).await.map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1075 | Error::from_sdk("fetching mention history failed", error.into()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1076 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1077 | Work::History(latest, from, result) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1078 | })); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1079 | } |
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 | fn captured( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1083 | &self, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1084 | data: &mut Data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1085 | key: String, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1086 | revision: u64, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1087 | result: Result<(crate::EventContext, Vec<MediaRegistration>, Vec<Value>), Error>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1088 | ) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1089 | let Some(entry) = data.entries.get(&key) else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1090 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1091 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1092 | if entry.revision != revision { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1093 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1094 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1095 | let room_id = entry.key.room_id.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1096 | if !self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1097 | .client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1098 | .get_room(&room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1099 | .is_some_and(|room| room.state() == RoomState::Joined) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1100 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1101 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1102 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1103 | match result { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1104 | Ok((context, media, _)) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1105 | data.capture_attempts.remove(&key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1106 | let live_after = data |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1107 | .windows |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1108 | .get(&key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1109 | .map(|window| window.after.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1110 | .unwrap_or_default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1111 | let mut insert = |item: TimelineItem| -> Option<String> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1112 | let TimelineItem::Event(event) = &item else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1113 | return None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1114 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1115 | let event_id = event.event_id.clone()?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1116 | let event_key = id(&MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1117 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1118 | event_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1119 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1120 | let registrations = media_for_item(&item, &media); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1121 | let mut saved = SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1122 | item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1123 | media: registrations, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1124 | raw: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1125 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1126 | if let Some(existing) = data.events.get(&event_key) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1127 | saved = existing.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1128 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1129 | if data.redacted.contains_key(&event_key) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1130 | redact(&mut saved); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1131 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1132 | data.events.insert(event_key.clone(), saved); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1133 | Some(event_key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1134 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1135 | let before: Vec<_> = context |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1136 | .events_before |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1137 | .into_iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1138 | .filter_map(&mut insert) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1139 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1140 | let target = context.event.and_then(&mut insert); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1141 | let mut after: Vec<_> = context |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1142 | .events_after |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1143 | .into_iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1144 | .filter_map(&mut insert) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1145 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1146 | let extended = live_after.iter().any(|key| !after.contains(key)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1147 | after.extend(live_after); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1148 | after.sort_by_key(|key| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1149 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1150 | data.events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1151 | .get(key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1152 | .and_then(|saved| match &saved.item { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1153 | TimelineItem::Event(event) => Some(event.timestamp_ms), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1154 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1155 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1156 | .unwrap_or(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1157 | key.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1158 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1159 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1160 | after.dedup(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1161 | after.truncate(10); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1162 | let entry = data.entries.get_mut(&key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1163 | entry.capture = if target.is_none() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1164 | MentionCaptureState::Unavailable |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1165 | } else if after.len() < 10 || (before.len() < 10 && context.start_token.is_some()) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1166 | MentionCaptureState::Partial |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1167 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1168 | MentionCaptureState::Ready |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1169 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1170 | entry.revision = data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1171 | data.windows.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1172 | key, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1173 | Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1174 | before, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1175 | target, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1176 | after, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1177 | start: context.start_token, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1178 | end: if extended { None } else { context.end_token }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1179 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1180 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1181 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1182 | Err(error) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1183 | let attempts = data.capture_attempts.entry(key.clone()).or_default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1184 | *attempts = attempts.saturating_add(1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1185 | let delay = (1000u64 << (*attempts).min(11)).min(3_600_000); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1186 | let entry = data.entries.get_mut(&key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1187 | entry.capture = if matches!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1188 | error.kind, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1189 | ErrorKind::EventNotFound | ErrorKind::Forbidden | ErrorKind::RoomUnknown |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1190 | ) || error |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1191 | .server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1192 | .as_ref() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1193 | .is_some_and(|server| matches!(server.status, 403 | 404)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1194 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1195 | MentionCaptureState::Unavailable |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1196 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1197 | MentionCaptureState::Failed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1198 | message: error.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1199 | retry_at_ms: retry_at(&error, delay), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1200 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1201 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1202 | entry.revision = data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1203 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1204 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1205 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1206 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1207 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1208 | fn edited(mut original: Value, edit: &Value) -> Value { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1209 | original["content"] = edit["content"]["m.new_content"].clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1210 | original["unsigned"]["macltok_edited"] = Value::Bool(true); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1211 | original |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1212 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1213 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1214 | fn retry_at(error: &Error, fallback_ms: u64) -> u64 { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1215 | let mut source = std::error::Error::source(error); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1216 | while let Some(current) = source { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1217 | if let Some(sdk) = current.downcast_ref::<matrix_sdk::Error>() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1218 | if let Some(ruma::api::error::ErrorKind::LimitExceeded(data)) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1219 | sdk.client_api_error_kind() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1220 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1221 | if let Some(delay) = data.retry_after { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1222 | let delay = match delay { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1223 | ruma::api::error::RetryAfter::Delay(delay) => delay, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1224 | ruma::api::error::RetryAfter::DateTime(time) => time |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1225 | .duration_since(ruma::time::SystemTime::now()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1226 | .unwrap_or_default(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1227 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1228 | return now().saturating_add( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1229 | u64::try_from(delay.as_millis()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1230 | .unwrap_or(u64::MAX) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1231 | .max(fallback_ms), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1232 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1233 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1234 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1235 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1236 | source = current.source(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1237 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1238 | now().saturating_add(fallback_ms) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1239 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1240 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1241 | fn redact(event: &mut SavedEvent) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1242 | if let TimelineItem::Event(item) = &mut event.item { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1243 | item.content = TimelineContent::Redacted; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1244 | item.reply_to = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1245 | item.reactions.clear(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1246 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1247 | event.media.clear(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1248 | event.raw = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1249 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1250 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1251 | fn media_for_item( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1252 | item: &TimelineItem, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1253 | registrations: &[MediaRegistration], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1254 | ) -> Vec<MediaRegistration> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1255 | let TimelineItem::Event(event) = item else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1256 | return Vec::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1257 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1258 | let mut ids = Vec::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1259 | match &event.content { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1260 | TimelineContent::Message(message) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1261 | ids.extend(message.media.iter().map(|media| &media.id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1262 | ids.extend( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1263 | message |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1264 | .link_previews |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1265 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1266 | .filter_map(|preview| preview.image.as_ref().map(|image| &image.media_id)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1267 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1268 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1269 | TimelineContent::Sticker { media, .. } => ids.push(&media.id), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1270 | _ => {} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1271 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1272 | registrations |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1273 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1274 | .filter(|registration| ids.contains(&®istration.id)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1275 | .cloned() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1276 | .collect() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1277 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1278 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1279 | pub(crate) async fn watch_updates(mut updates: watch::Receiver<MentionsPage>, events: EventSender) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1280 | let mut previous: BTreeMap<String, MentionEntry> = BTreeMap::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1281 | loop { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1282 | let snapshot = updates.borrow_and_update().clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1283 | let current: BTreeMap<_, _> = snapshot |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1284 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1285 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1286 | .map(|entry| (id(&entry.key), entry.clone())) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1287 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1288 | let upserts = current |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1289 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1290 | .filter(|(key, entry)| previous.get(*key) != Some(entry)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1291 | .map(|(_, entry)| entry.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1292 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1293 | let removed = previous |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1294 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1295 | .filter(|(key, _)| !current.contains_key(*key)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1296 | .map(|(_, entry)| entry.key.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1297 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1298 | previous = current; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1299 | if events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1300 | .send(Event::MentionsChanged(MentionsUpdate { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1301 | snapshot, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1302 | upserts, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1303 | removed, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1304 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1305 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1306 | .is_err() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1307 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1308 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1309 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1310 | if updates.changed().await.is_err() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1311 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1312 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1313 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1314 | } |