cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1 | use std::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 2 | collections::{BTreeMap, BTreeSet}, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 3 | sync::{ |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 4 | Arc, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 5 | atomic::{AtomicBool, Ordering}, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 6 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 7 | time::Duration, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 8 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 9 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 10 | use futures_util::{StreamExt, stream::FuturesUnordered}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 11 | use matrix_sdk::{ |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 12 | Client, Room, RoomState, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 13 | event_cache::RedecryptorReport, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 14 | sliding_sync::{ResponseCommitBatch, ResponseCommitHook}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 15 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 16 | use ruma::{ |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 17 | OwnedRoomId, OwnedUserId, RoomId, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 18 | events::{ |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 19 | AnySyncTimelineEvent, ignored_user_list::IgnoredUserListEventContent, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 20 | room::encrypted::OriginalSyncRoomEncryptedEvent, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 21 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 22 | serde::Raw, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 23 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 24 | use serde_json::Value; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 25 | use tokio::sync::{mpsc, oneshot, watch}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 26 | |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 27 | use super::state::{Candidate, Checkpoint, Data, SavedEvent, SyncState, Window, id, invalid, now}; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 28 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 29 | use super::store::Opener; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 30 | use crate::{ |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 31 | CommandOutput, EngineConfig, Error, ErrorKind, Event, MentionCaptureState, MentionEntry, |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 32 | MentionGapReason, MentionKey, MentionReason, MentionsCommand, MentionsPage, MentionsUpdate, |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 33 | TimelineContent, TimelineItem, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 34 | engine::{MediaService, MentionsStore, context, executor::EngineBoxFuture}, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 35 | normalise::{MediaRegistration, MediaScope, normalise_observed}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 36 | transport::EventSender, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 37 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 38 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 39 | #[cfg(all(test, feature = "sqlite"))] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 40 | #[path = "service_tests.rs"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 41 | mod tests; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 42 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 43 | const HOOK_TIMEOUT: Duration = Duration::from_secs(10); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 44 | const FLUSH_RETRY_MS: u64 = 60_000; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 45 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 46 | #[cfg_attr(not(any(feature = "sqlite", feature = "indexeddb")), allow(dead_code))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 47 | pub(crate) struct Unopened { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 48 | pub(crate) error: Error, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 49 | /// set when another attempt may succeed |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 50 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 51 | pub(crate) retry: Option<Opener>, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 52 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 53 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 54 | struct Loaded { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 55 | store: Option<MentionsStore>, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 56 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 57 | reopen: Option<Opener>, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 58 | data: Data, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 59 | sync: SyncState, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 60 | rooms: BTreeSet<OwnedRoomId>, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 61 | gaps: Vec<MentionGapReason>, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 62 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 63 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 64 | impl Loaded { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 65 | fn empty(gaps: Vec<MentionGapReason>) -> Self { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 66 | Self { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 67 | store: None, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 68 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 69 | reopen: None, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 70 | data: Data::default(), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 71 | sync: SyncState::default(), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 72 | rooms: BTreeSet::new(), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 73 | gaps, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 74 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 75 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 76 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 77 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | #[derive(Clone)] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 79 | pub(crate) struct Handle { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 80 | requests: mpsc::Sender<Request>, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 81 | timed_out: Arc<AtomicBool>, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 82 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 84 | enum Request { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 85 | Batch(ResponseCommitBatch, oneshot::Sender<()>), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 86 | Command( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | MentionsCommand, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | oneshot::Sender<Result<CommandOutput, Error>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 89 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 90 | Stop(oneshot::Sender<()>), |
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 | pub(crate) struct Service { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | client: Client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | account: OwnedUserId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | media: Arc<MediaService>, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 97 | /// as many media as the inbox keeps events |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 98 | media_scope: MediaScope, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | store: Option<MentionsStore>, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 100 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 101 | reopen: Option<Opener>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | data: Data, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 103 | sync: SyncState, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 104 | rooms: BTreeSet<OwnedRoomId>, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 105 | dirty: bool, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 106 | rooms_dirty: bool, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 107 | unpublished: bool, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 108 | flush_after: u64, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 109 | ignored: BTreeSet<OwnedUserId>, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 110 | leaving: BTreeSet<OwnedRoomId>, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 111 | timed_out: Arc<AtomicBool>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | config: EngineConfig, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | requests: mpsc::Receiver<Request>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | updates: watch::Sender<MentionsPage>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | recovery_budget: usize, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | latest_scan: bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 117 | recovery_retry: u64, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 118 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 119 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 120 | enum Work { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 121 | Capture( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 122 | String, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 123 | u64, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 124 | Result<(crate::EventContext, Vec<MediaRegistration>, Vec<Value>), Error>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 125 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 126 | Candidate(String, Result<Value, Error>), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 127 | History( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 128 | bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 129 | Option<String>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 130 | Result<ruma::api::client::push::get_notifications::v3::Response, Error>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 131 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 132 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 133 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 134 | impl Work { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 135 | fn rooms(&self) -> BTreeSet<OwnedRoomId> { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 136 | let room = |key: &str| { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 137 | key.split_once('\n') |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 138 | .and_then(|(room, _)| OwnedRoomId::try_from(room).ok()) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 139 | }; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 140 | match self { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 141 | Self::Capture(key, ..) | Self::Candidate(key, _) => room(key).into_iter().collect(), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 142 | Self::History(_, _, Ok(page)) => page |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 143 | .notifications |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 144 | .iter() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 145 | .map(|notification| notification.room_id.clone()) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 146 | .collect(), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 147 | Self::History(..) => BTreeSet::new(), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 148 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 149 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 150 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 151 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 152 | impl Handle { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 153 | /// never fails, a mentions failure becomes a coverage gap rather than a stalled sync |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 154 | pub(crate) fn hook(&self) -> ResponseCommitHook { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 155 | let handle = self.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 156 | ResponseCommitHook::new(move |batch| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 157 | let handle = handle.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 158 | async move { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 159 | let (reply, response) = oneshot::channel(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 160 | let queued = handle.requests.send(Request::Batch(batch, reply)); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 161 | match matrix_sdk::timeout::timeout(queued, HOOK_TIMEOUT).await { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 162 | Ok(Ok(())) => {} |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 163 | Ok(Err(_)) => { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 164 | tracing::warn!("mentions service stopped, sync carries on"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 165 | return Ok(()); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 166 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 167 | Err(_) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 168 | handle.timed_out.store(true, Ordering::Release); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 169 | tracing::warn!("mentions queue stayed full, a response was dropped"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 170 | return Ok(()); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 171 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 172 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 173 | // a queued batch still commits in order, so giving up here loses nothing |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 174 | // unless the process dies first, which the next start sees as a |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 175 | // discontinuity |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 176 | if matrix_sdk::timeout::timeout(response, HOOK_TIMEOUT) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 177 | .await |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 178 | .is_err() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 179 | { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 180 | tracing::warn!("mentions commit is slow, sync carries on"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 181 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 182 | Ok(()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 183 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 184 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 185 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 186 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 187 | pub(crate) async fn execute(&self, command: MentionsCommand) -> Result<CommandOutput, Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 188 | let (reply, response) = oneshot::channel(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 189 | self.requests |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 190 | .send(Request::Command(command, reply)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 191 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 192 | .map_err(|_| Error::interrupted())?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 193 | response.await.map_err(|_| Error::interrupted())? |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 194 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 195 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 196 | pub(crate) async fn stop(&self) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 197 | let (reply, response) = oneshot::channel(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 198 | if self.requests.send(Request::Stop(reply)).await.is_ok() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 199 | let _ = response.await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 200 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 201 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 202 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 203 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 204 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 205 | fn read_store(store: &MentionsStore) -> Result<(Data, SyncState, BTreeSet<OwnedRoomId>), Error> { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 206 | let data: Data = store.read("inbox")?.unwrap_or_default(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 207 | let sync: SyncState = store.read("sync")?.unwrap_or_default(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 208 | let rooms = store.read("rooms")?.unwrap_or_default(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 209 | if sync.inbox_revision != data.revision { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 210 | return Err(Error::new( |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 211 | ErrorKind::Store, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 212 | "mentions checkpoint does not match its inbox", |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 213 | )); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 214 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 215 | Ok((data, sync, rooms)) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 216 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 217 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 218 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 219 | async fn open_store(store: Option<Result<MentionsStore, Unopened>>) -> Loaded { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 220 | let mut store = match store { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 221 | None => return Loaded::empty(Vec::new()), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 222 | Some(Err(unopened)) => { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 223 | tracing::error!(error = %unopened.error, "mentions store unavailable, the inbox runs in memory"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 224 | let mut loaded = Loaded::empty(vec![MentionGapReason::StoreFailed]); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 225 | loaded.reopen = unopened.retry; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 226 | return loaded; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 227 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 228 | Some(Ok(store)) => store, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 229 | }; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 230 | let (data, sync, rooms) = match read_store(&store) { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 231 | Ok(loaded) => loaded, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 232 | Err(error) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 233 | tracing::warn!(%error, "resetting an unreadable mentions inbox"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 234 | if let Err(error) = store.reset().await { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 235 | tracing::error!(%error, "mentions store unavailable, the inbox runs in memory"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 236 | return Loaded::empty(vec![MentionGapReason::StoreFailed]); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 237 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 238 | Default::default() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 239 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 240 | }; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 241 | let gaps = if store.was_reset() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 242 | vec![MentionGapReason::StoreReset] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 243 | } else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 244 | Vec::new() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 245 | }; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 246 | Loaded { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 247 | store: Some(store), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 248 | reopen: None, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 249 | data, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 250 | sync, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 251 | rooms, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 252 | gaps, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 253 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 254 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 255 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 256 | #[cfg(not(any(feature = "sqlite", feature = "indexeddb")))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 257 | async fn open_store(store: Option<Result<MentionsStore, Unopened>>) -> Loaded { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 258 | match store { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 259 | None => Loaded::empty(Vec::new()), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 260 | Some(Err(_)) => Loaded::empty(vec![MentionGapReason::StoreFailed]), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 261 | Some(Ok(store)) => match store {}, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 262 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 263 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 264 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 265 | impl Service { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 266 | pub(crate) async fn new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 267 | client: Client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 268 | media: Arc<MediaService>, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 269 | store: Option<Result<MentionsStore, Unopened>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 270 | config: EngineConfig, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 271 | ) -> Result<(Handle, Self, watch::Receiver<MentionsPage>), Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 272 | let account = client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 273 | .user_id() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 274 | .ok_or_else(|| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 275 | Error::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 276 | ErrorKind::Unauthenticated, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 277 | "mentions require an authenticated account", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 278 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 279 | })? |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 280 | .to_owned(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 281 | let mut loaded = open_store(store).await; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 282 | for reason in &loaded.gaps { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 283 | loaded.data.gap(None, *reason, None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 284 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 285 | let page = loaded.data.page(&account, None, 50)?; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 286 | let media_scope = MediaScope::bounded(config.max_mention_context_events); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 287 | media |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 288 | .register( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 289 | &media_scope, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 290 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 291 | .iter() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 292 | .filter_map(|entry| loaded.data.events.get(&id(&entry.key))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 293 | .flat_map(|event| event.media.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 294 | .collect(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 295 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 296 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 297 | let (updates, receiver) = watch::channel(page); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 298 | let (sender, requests) = mpsc::channel(config.command_capacity.max(1)); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 299 | let timed_out = Arc::new(AtomicBool::new(false)); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 300 | let mut service = Self { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 301 | client, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 302 | account, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 303 | media, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 304 | media_scope, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 305 | store: loaded.store, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 306 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 307 | reopen: loaded.reopen, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 308 | data: loaded.data, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 309 | sync: loaded.sync, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 310 | rooms: loaded.rooms, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 311 | dirty: !loaded.gaps.is_empty(), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 312 | rooms_dirty: false, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 313 | unpublished: false, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 314 | flush_after: 0, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 315 | ignored: BTreeSet::new(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 316 | leaving: BTreeSet::new(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 317 | timed_out: timed_out.clone(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 318 | config, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 319 | requests, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 320 | updates, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 321 | recovery_budget: 20, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 322 | latest_scan: true, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 323 | recovery_retry: 0, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 324 | }; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 325 | service.ignored = service.ignored_users().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 326 | Ok(( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 327 | Handle { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 328 | requests: sender, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 329 | timed_out, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 330 | }, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 331 | service, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 332 | receiver, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 333 | )) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 334 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 335 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 336 | async fn ignored_users(&self) -> BTreeSet<OwnedUserId> { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 337 | self.client |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 338 | .account() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 339 | .account_data::<IgnoredUserListEventContent>() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 340 | .await |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 341 | .ok() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 342 | .flatten() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 343 | .and_then(|raw| raw.deserialize().ok()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 344 | .map(|content| content.ignored_users.into_keys().collect()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 345 | .unwrap_or_default() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 346 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 347 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 348 | async fn publish(&mut self) { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 349 | self.unpublished = false; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 350 | let Ok(page) = self.data.page(&self.account, None, 50) else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 351 | return; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 352 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 353 | self.media |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 354 | .register( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 355 | &self.media_scope, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 356 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 357 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 358 | .filter_map(|entry| self.data.events.get(&id(&entry.key))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 359 | .flat_map(|event| event.media.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 360 | .collect(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 361 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 362 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 363 | self.updates.send_replace(page); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 364 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 365 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 366 | /// writes the sync record, with the inbox when `dirty` and observed rooms when |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 367 | /// `rooms_dirty`, in one commit so a checkpoint never lands without the changes it |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 368 | /// covers |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 369 | async fn flush(&mut self) -> Result<(), Error> { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 370 | if self.dirty { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 371 | self.advance()?; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 372 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 373 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 374 | if let Some(store) = &mut self.store { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 375 | use super::store::Write; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 376 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 377 | let encode = |error| { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 378 | Error::with_source( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 379 | ErrorKind::Store, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 380 | "encoding the mentions inbox failed", |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 381 | error, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 382 | ) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 383 | }; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 384 | self.sync.inbox_revision = self.data.revision; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 385 | let sync = serde_json::value::to_raw_value(&self.sync).map_err(encode)?; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 386 | let rooms = if self.rooms_dirty { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 387 | Some(serde_json::value::to_raw_value(&self.rooms).map_err(encode)?) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 388 | } else { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 389 | None |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 390 | }; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 391 | let mut writes = Vec::new(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 392 | if self.dirty { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 393 | self.data |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 394 | .coverage |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 395 | .gaps |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 396 | .retain(|gap| gap.reason != MentionGapReason::StoreFailed); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 397 | let replaced: &[&str] = if rooms.is_some() { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 398 | &["inbox", "sync", "rooms"] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 399 | } else { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 400 | &["inbox", "sync"] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 401 | }; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 402 | let available = store |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 403 | .available(replaced) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 404 | .saturating_sub(store.sealed_bound("sync", sync.get().len())) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 405 | .saturating_sub( |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 406 | rooms |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 407 | .as_ref() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 408 | .map_or(0, |rooms| store.sealed_bound("rooms", rooms.get().len())), |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 409 | ); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 410 | let mut inbox = serde_json::value::to_raw_value(&self.data).map_err(encode)?; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 411 | let mut size = store.sealed_bound("inbox", inbox.get().len()); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 412 | if size > available { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 413 | while size > available { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 414 | let Some(freed) = self.data.evict_one() else { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 415 | break; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 416 | }; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 417 | size = size.saturating_sub(freed * 3); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 418 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 419 | inbox = serde_json::value::to_raw_value(&self.data).map_err(encode)?; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 420 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 421 | writes.push(Write::Set("inbox".to_owned(), inbox)); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 422 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 423 | writes.push(Write::Set("sync".to_owned(), sync)); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 424 | if let Some(rooms) = rooms { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 425 | writes.push(Write::Set("rooms".to_owned(), rooms)); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 426 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 427 | store.commit(store.revision(), writes).await?; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 428 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 429 | self.rooms_dirty = false; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 430 | if self.dirty { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 431 | self.dirty = false; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 432 | self.publish().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 433 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 434 | Ok(()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 435 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 436 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 437 | fn advance(&mut self) -> Result<(), Error> { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 438 | self.data.revision = self |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 439 | .data |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 440 | .revision |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 441 | .checked_add(1) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 442 | .ok_or_else(|| invalid("mentions revision exhausted"))?; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 443 | self.data.bound( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 444 | self.config.max_mentions_entries, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 445 | self.config.max_mention_context_events, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 446 | self.config.max_mention_candidates, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 447 | ); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 448 | Ok(()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 449 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 450 | |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 451 | async fn apply(&mut self, data: Data) -> Result<(), Error> { |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 452 | let dirty = std::mem::replace(&mut self.dirty, true); |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 453 | let previous = std::mem::replace(&mut self.data, data); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 454 | let result = self.flush().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 455 | if result.is_err() { |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 456 | self.data = previous; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 457 | self.dirty = dirty; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 458 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 459 | result |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 460 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 461 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 462 | async fn store_failed(&mut self, error: Error) { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 463 | tracing::error!(%error, "saving the mentions inbox failed"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 464 | self.data.gap(None, MentionGapReason::StoreFailed, None); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 465 | self.dirty = true; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 466 | self.flush_after = now().saturating_add(FLUSH_RETRY_MS); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 467 | self.publish().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 468 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 469 | |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 470 | /// the store is back after failing at startup, what it held and what was built in |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 471 | /// memory since are merged so nothing is lost |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 472 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 473 | async fn reopen(&mut self) { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 474 | let Some(opener) = &self.reopen else { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 475 | return; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 476 | }; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 477 | let store = match opener.open().await { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 478 | Ok(store) => store, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 479 | Err(super::store::OpenError::Unavailable(error)) => { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 480 | tracing::warn!(%error, "the mentions store is still unavailable"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 481 | self.flush_after = now().saturating_add(FLUSH_RETRY_MS); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 482 | return; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 483 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 484 | Err(super::store::OpenError::Newer(error)) => { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 485 | tracing::error!(%error, "the mentions store belongs to a newer macltok"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 486 | self.reopen = None; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 487 | return; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 488 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 489 | }; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 490 | let mut store = store; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 491 | let (mut data, mut sync, mut rooms) = match read_store(&store) { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 492 | Ok(loaded) => loaded, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 493 | Err(error) => { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 494 | tracing::warn!(%error, "resetting an unreadable mentions inbox"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 495 | if let Err(error) = store.reset().await { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 496 | tracing::warn!(%error, "the mentions store is still unavailable"); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 497 | self.flush_after = now().saturating_add(FLUSH_RETRY_MS); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 498 | return; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 499 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 500 | self.data.gap(None, MentionGapReason::StoreReset, None); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 501 | Default::default() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 502 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 503 | }; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 504 | data.absorb(std::mem::take(&mut self.data)); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 505 | sync.checkpoints |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 506 | .extend(std::mem::take(&mut self.sync.checkpoints)); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 507 | rooms.extend(std::mem::take(&mut self.rooms)); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 508 | self.data = data; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 509 | self.sync = sync; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 510 | self.rooms = rooms; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 511 | self.store = Some(store); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 512 | self.reopen = None; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 513 | self.dirty = true; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 514 | self.rooms_dirty = true; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 515 | self.flush_after = 0; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 516 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 517 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 518 | async fn command(&mut self, command: MentionsCommand) -> Result<CommandOutput, Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 519 | match command { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 520 | MentionsCommand::Page { cursor, limit } => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 521 | let page = self.data.page(&self.account, cursor.as_deref(), limit)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 522 | self.media |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 523 | .register( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 524 | &self.media_scope, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 525 | page.entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 526 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 527 | .filter_map(|entry| self.data.events.get(&id(&entry.key))) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 528 | .flat_map(|event| event.media.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 529 | .collect(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 530 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 531 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 532 | Ok(CommandOutput::MentionsPage(page)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 533 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 534 | MentionsCommand::Context(key) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 535 | let (context, media) = self.data.context(&key)?; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 536 | self.media.register(&self.media_scope, media).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 537 | Ok(CommandOutput::MentionContext(context)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 538 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 539 | MentionsCommand::RetryContext(key) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 540 | let room = self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 541 | .client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 542 | .get_room(&key.room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 543 | .filter(|room| room.state() == RoomState::Joined) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 544 | .ok_or_else(|| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 545 | Error::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 546 | ErrorKind::Forbidden, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 547 | "saved context cannot be refetched after leaving the room", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 548 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 549 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 550 | drop(room); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 551 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 552 | let entry = next |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 553 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 554 | .get_mut(&id(&key)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 555 | .ok_or_else(|| Error::new(ErrorKind::EventNotFound, "mention was not found"))?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 556 | entry.capture = MentionCaptureState::Pending; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 557 | entry.revision = self.data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 558 | next.windows.entry(id(&key)).or_insert(Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 559 | before: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 560 | target: Some(id(&key)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 561 | after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 562 | start: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 563 | end: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 564 | }); |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 565 | self.apply(next).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 566 | let (context, media) = self.data.context(&key)?; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 567 | self.media.register(&self.media_scope, media).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 568 | Ok(CommandOutput::MentionContext(context)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 569 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 570 | MentionsCommand::SetRead { keys, read } => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 571 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 572 | for key in keys { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 573 | if let Some(entry) = next.entries.get_mut(&id(&key)) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 574 | entry.read = read; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 575 | entry.revision = self.data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 576 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 577 | } |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 578 | self.apply(next).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 579 | Ok(CommandOutput::Done) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 580 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 581 | MentionsCommand::ReadThrough(boundary) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 582 | if boundary.account != self.account || boundary.revision > self.data.revision { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 583 | return Err(invalid("invalid mentions read boundary")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 584 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 585 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 586 | for entry in next |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 587 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 588 | .values_mut() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 589 | .filter(|entry| entry.discovery_revision <= boundary.revision) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 590 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 591 | entry.read = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 592 | entry.revision = self.data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 593 | } |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 594 | self.apply(next).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 595 | Ok(CommandOutput::Done) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 596 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 597 | MentionsCommand::Recover => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 598 | self.recovery_budget = 20; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 599 | self.latest_scan = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 600 | self.recovery_retry = 0; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 601 | let mut next = self.data.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 602 | next.coverage.recovering = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 603 | next.coverage.historical_supported = None; |
2c13c8db6174 | atagen | 2026-10-25 09:12 UTC | 604 | self.apply(next).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 605 | Ok(CommandOutput::Done) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 606 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 607 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 608 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 609 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 610 | fn observable(&self, room_id: &RoomId) -> Option<Room> { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 611 | self.client |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 612 | .get_room(room_id) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 613 | .filter(|room| room.state() == RoomState::Joined || self.leaving.contains(room_id)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 614 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 615 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 616 | async fn batch(&mut self, batch: ResponseCommitBatch) { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 617 | let checkpoint = self.sync.checkpoints.get(&batch.connection_id).cloned(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 618 | if checkpoint |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 619 | .as_ref() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 620 | .is_some_and(|checkpoint| checkpoint.pos == batch.next_pos) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 621 | { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 622 | return; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 623 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 624 | self.ignored = self.ignored_users().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 625 | self.leaving = batch.response.rooms.left.keys().cloned().collect(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 626 | let room_ids: BTreeSet<_> = batch |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 627 | .raw_rooms |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 628 | .keys() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 629 | .chain(batch.response.rooms.joined.keys()) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 630 | .chain(batch.response.rooms.left.keys()) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 631 | .cloned() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 632 | .collect(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 633 | let before = self.data.scope(&room_ids); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 634 | let mut next = std::mem::take(&mut self.data); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 635 | let mut changed = false; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 636 | if self.timed_out.swap(false, Ordering::AcqRel) { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 637 | next.gap(None, MentionGapReason::HookTimedOut, None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 638 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 639 | // a crash after the commit but before the sdk saved `pos` replays from the |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 640 | // previous position, and that response is a superset of the one committed |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 641 | let continuous = match &checkpoint { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 642 | None => batch.previous_pos.is_none(), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 643 | Some(checkpoint) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 644 | batch.previous_pos.as_ref() == Some(&checkpoint.pos) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 645 | || batch.previous_pos == checkpoint.previous |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 646 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 647 | }; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 648 | if !continuous { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 649 | next.gap(None, MentionGapReason::SyncDiscontinuity, None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 650 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 651 | for (room_id, raw) in &batch.raw_rooms { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 652 | if raw.limited && self.rooms.contains(room_id) { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 653 | next.gap( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 654 | Some(room_id.clone()), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 655 | MentionGapReason::LimitedTimeline, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 656 | raw.prev_batch.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 657 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 658 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 659 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 660 | for room_id in &room_ids { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 661 | if self.observable(room_id).is_none() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 662 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 663 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 664 | self.rooms_dirty |= self.rooms.insert(room_id.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 665 | let mut observed = BTreeMap::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 666 | if let Some(raw) = batch.raw_rooms.get(room_id) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 667 | for event in &raw.timeline { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 668 | let Ok(value) = serde_json::from_str::<Value>(event.json().get()) else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 669 | continue; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 670 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 671 | if let Some(event_id) = value["event_id"].as_str() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 672 | observed.insert(event_id.to_owned(), value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 673 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 674 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 675 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 676 | let processed = batch |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 677 | .response |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 678 | .rooms |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 679 | .joined |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 680 | .get(room_id) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 681 | .map(|update| &update.timeline.events) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 682 | .into_iter() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 683 | .chain( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 684 | batch |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 685 | .response |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 686 | .rooms |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 687 | .left |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 688 | .get(room_id) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 689 | .map(|update| &update.timeline.events), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 690 | ) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 691 | .flatten(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 692 | for event in processed { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 693 | let Ok(value) = serde_json::from_str::<Value>(event.raw().json().get()) else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 694 | continue; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 695 | }; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 696 | if let Some(event_id) = value["event_id"].as_str() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 697 | observed.insert(event_id.to_owned(), value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 698 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 699 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 700 | let mut observed: Vec<_> = observed.into_values().collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 701 | observed.sort_by_key(|value| value["origin_server_ts"].as_u64().unwrap_or(0)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 702 | for value in observed { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 703 | self.observe(&mut next, room_id, value).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 704 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 705 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 706 | for room_id in batch.response.rooms.left.keys() { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 707 | self.rooms_dirty |= self.rooms.remove(room_id); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 708 | next.gap(Some(room_id.clone()), MentionGapReason::RoomLeft, None); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 709 | next.candidates |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 710 | .retain(|_, candidate| &candidate.room_id != room_id); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 711 | let revision = next.revision + 1; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 712 | for entry in next.entries.values_mut().filter(|entry| { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 713 | &entry.key.room_id == room_id && entry.capture != MentionCaptureState::Unavailable |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 714 | }) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 715 | entry.capture = MentionCaptureState::Unavailable; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 716 | entry.revision = revision; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 717 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 718 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 719 | let reevaluate: Vec<_> = room_ids |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 720 | .iter() |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 721 | .flat_map(|room_id| { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 722 | next.entries |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 723 | .range(format!("{room_id}\n")..format!("{room_id}\u{b}")) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 724 | .filter_map(|(key, _)| next.events.get(key).and_then(|event| event.raw.clone())) |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 725 | .map(move |raw| (room_id.clone(), raw)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 726 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 727 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 728 | for (room_id, raw) in reevaluate { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 729 | self.observe(&mut next, &room_id, raw).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 730 | } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 731 | changed |= self.filter_ignored(&mut next); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 732 | self.leaving.clear(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 733 | self.sync.checkpoints.insert( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 734 | batch.connection_id, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 735 | Checkpoint { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 736 | pos: batch.next_pos, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 737 | previous: batch.previous_pos, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 738 | }, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 739 | ); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 740 | changed |= next.scope(&room_ids) != before; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 741 | self.data = next; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 742 | self.dirty |= changed; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 743 | if now() < self.flush_after { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 744 | if changed && self.advance().is_ok() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 745 | self.publish().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 746 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 747 | } else if let Err(error) = self.flush().await { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 748 | self.store_failed(error).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 749 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 750 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 751 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 752 | fn filter_ignored(&self, next: &mut Data) -> bool { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 753 | let removed: Vec<_> = next |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 754 | .entries |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 755 | .iter() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 756 | .filter(|(_, entry)| { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 757 | matches!(&entry.event, TimelineItem::Event(event) if self.ignored.contains(&event.sender.user_id)) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 758 | }) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 759 | .map(|(key, _)| key.clone()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 760 | .collect(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 761 | if removed.is_empty() { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 762 | return false; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 763 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 764 | for key in &removed { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 765 | next.entries.remove(key); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 766 | next.windows.remove(key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 767 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 768 | next.collect_events(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 769 | true |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 770 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 771 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 772 | async fn observe(&self, data: &mut Data, room_id: &OwnedRoomId, mut value: Value) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 773 | let Some(event_id) = value["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 774 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 775 | .and_then(|value| value.parse().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 776 | else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 777 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 778 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 779 | let key = MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 780 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 781 | event_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 782 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 783 | let identity = id(&key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 784 | let Some(timestamp) = value["origin_server_ts"].as_u64() else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 785 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 786 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 787 | data.coverage.earliest_timestamp_ms = Some( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 788 | data.coverage |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 789 | .earliest_timestamp_ms |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 790 | .map_or(timestamp, |old| old.min(timestamp)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 791 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 792 | if value["type"] == "m.room.redaction" { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 793 | if let Some(target) = value["redacts"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 794 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 795 | .or_else(|| value["content"]["redacts"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 796 | .and_then(|value| value.parse().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 797 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 798 | let target = id(&MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 799 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 800 | event_id: target, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 801 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 802 | data.redacted.insert(target.clone(), timestamp); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 803 | data.entries.remove(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 804 | data.windows.remove(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 805 | data.candidates.remove(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 806 | data.edits.remove(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 807 | data.candidates.retain(|_, candidate| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 808 | candidate.room_id != *room_id |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 809 | || candidate.raw["content"]["m.relates_to"]["event_id"].as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 810 | != target.split_once('\n').map(|(_, event)| event) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 811 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 812 | if let Some(saved) = data.events.get_mut(&target) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 813 | redact(saved); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 814 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 815 | data.touch_contexts(&target); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 816 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 817 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 818 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 819 | if data.redacted.contains_key(&identity) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 820 | || value["unsigned"].get("redacted_because").is_some() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 821 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 822 | data.redacted.insert(identity.clone(), timestamp); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 823 | data.entries.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 824 | data.windows.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 825 | data.candidates.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 826 | if let Some(saved) = data.events.get_mut(&identity) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 827 | redact(saved); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 828 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 829 | data.touch_contexts(&identity); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 830 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 831 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 832 | if value["content"]["m.relates_to"]["rel_type"] == "m.replace" { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 833 | if let Some(target) = value["content"]["m.relates_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 834 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 835 | .and_then(|value| value.parse().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 836 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 837 | let target = id(&MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 838 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 839 | event_id: target, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 840 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 841 | if data.redacted.contains_key(&target) { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 842 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 843 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 844 | let newer = data.edits.get(&target).is_none_or(|old| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 845 | (old["origin_server_ts"].as_u64(), old["event_id"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 846 | < (Some(timestamp), value["event_id"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 847 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 848 | if newer { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 849 | if let Some(original) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 850 | data.events.get(&target).and_then(|saved| saved.raw.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 851 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 852 | if original["sender"] != value["sender"] { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 853 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 854 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 855 | data.edits.insert(target.clone(), value.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 856 | value = edited(original, &value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 857 | return Box::pin(self.observe(data, room_id, value)).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 858 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 859 | data.candidates |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 860 | .entry(identity) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 861 | .and_modify(|candidate| candidate.raw = value.clone()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 862 | .or_insert_with(|| Candidate::new(room_id.clone(), value, 0)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 863 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 864 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 865 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 866 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 867 | if let Some(edit) = value["unsigned"]["m.relations"].get("m.replace").cloned() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 868 | if edit["sender"] == value["sender"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 869 | && edit["content"]["m.new_content"].is_object() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 870 | && edit["content"]["m.relates_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 871 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 872 | .is_none_or(|target| target == key.event_id.as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 873 | && edit["origin_server_ts"].as_u64().is_some() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 874 | && data.edits.get(&identity).is_none_or(|old| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 875 | (old["origin_server_ts"].as_u64(), old["event_id"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 876 | < (edit["origin_server_ts"].as_u64(), edit["event_id"].as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 877 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 878 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 879 | data.edits.insert(identity.clone(), edit); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 880 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 881 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 882 | if let Some(edit) = data.edits.get(&identity) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 883 | if value["sender"] == edit["sender"] { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 884 | value = edited(value, edit); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 885 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 886 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 887 | if let Some(edit) = value["unsigned"].get("macltok_edit").cloned() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 888 | data.edits.insert(identity.clone(), edit); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 889 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 890 | if value["type"] == "m.room.encrypted" { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 891 | data.candidates |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 892 | .entry(identity) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 893 | .or_insert_with(|| Candidate::new(room_id.clone(), value, 0)); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 894 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 895 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 896 | let Some((item, media)) = normalise_observed(&value, Some(&self.account), &identity) else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 897 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 898 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 899 | self.supplement(data, &key, &item, &media, &value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 900 | let (reasons, suppressed, unresolved) = self.classify(data, room_id, &value).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 901 | if unresolved { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 902 | data.candidates |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 903 | .entry(identity.clone()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 904 | .or_insert_with(|| Candidate::new(room_id.clone(), value.clone(), now() + 30_000)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 905 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 906 | data.candidates.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 907 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 908 | if reasons.is_empty() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 909 | data.entries.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 910 | data.windows.remove(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 911 | if data.events.contains_key(&identity) || data.edits.contains_key(&identity) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 912 | data.events.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 913 | identity, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 914 | SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 915 | item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 916 | media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 917 | raw: Some(value), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 918 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 919 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 920 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 921 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 922 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 923 | let previous = data.entries.get(&identity); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 924 | let event_changed = previous.is_none_or(|entry| entry.event != item); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 925 | let changed = event_changed |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 926 | || previous.is_some_and(|entry| { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 927 | entry.reasons != reasons || entry.notification_suppressed != suppressed |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 928 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 929 | let entry = MentionEntry { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 930 | key, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 931 | timestamp_ms: timestamp, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 932 | discovered_at_ms: previous.map_or_else(now, |entry| entry.discovered_at_ms), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 933 | discovery_revision: previous |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 934 | .map_or(data.revision + 1, |entry| entry.discovery_revision), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 935 | revision: if changed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 936 | data.revision + 1 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 937 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 938 | previous.unwrap().revision |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 939 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 940 | event: item.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 941 | reasons, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 942 | read: previous.is_some_and(|entry| entry.read), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 943 | capture: previous.map_or(MentionCaptureState::Pending, |entry| { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 944 | if event_changed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 945 | MentionCaptureState::Pending |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 946 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 947 | entry.capture.clone() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 948 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 949 | }), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 950 | room_name: self.client.get_room(room_id).and_then(|room| room.name()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 951 | notification_suppressed: suppressed, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 952 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 953 | data.entries.insert(identity.clone(), entry); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 954 | data.windows.entry(identity.clone()).or_insert(Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 955 | before: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 956 | target: Some(identity.clone()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 957 | after: Vec::new(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 958 | start: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 959 | end: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 960 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 961 | data.events.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 962 | identity, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 963 | SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 964 | item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 965 | media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 966 | raw: Some(value), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 967 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 968 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 969 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 970 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 971 | fn supplement( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 972 | &self, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 973 | data: &mut Data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 974 | key: &MentionKey, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 975 | item: &TimelineItem, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 976 | media: &[MediaRegistration], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 977 | raw: &Value, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 978 | ) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 979 | let TimelineItem::Event(event) = item else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 980 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 981 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 982 | let identity = id(key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 983 | if data |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 984 | .events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 985 | .get(&identity) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 986 | .is_some_and(|saved| saved.item != *item) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 987 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 988 | data.touch_contexts(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 989 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 990 | let mut retained = data.events.contains_key(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 991 | let saved_events = &data.events; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 992 | for (target, window) in &mut data.windows { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 993 | let Some(entry) = data.entries.get_mut(target) else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 994 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 995 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 996 | if entry.key.room_id != key.room_id |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 997 | || (entry.timestamp_ms, target.as_str()) >= (event.timestamp_ms, identity.as_str()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 998 | || window.after.contains(&identity) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 999 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1000 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1001 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1002 | let previous = window.after.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1003 | window.after.push(identity.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1004 | window.after.sort_by_key(|key| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1005 | if key == &identity { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1006 | (event.timestamp_ms, key.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1007 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1008 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1009 | saved_events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1010 | .get(key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1011 | .and_then(|saved| match &saved.item { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1012 | TimelineItem::Event(event) => Some(event.timestamp_ms), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1013 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1014 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1015 | .unwrap_or(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1016 | key.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1017 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1018 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1019 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1020 | window.after.truncate(10); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1021 | if window.after == previous { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1022 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1023 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1024 | window.end = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1025 | retained |= window.after.contains(&identity); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1026 | if entry.capture == MentionCaptureState::Partial |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1027 | && window.after.len() == 10 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1028 | && (window.before.len() == 10 || window.start.is_none()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1029 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1030 | entry.capture = MentionCaptureState::Ready; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1031 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1032 | entry.revision = data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1033 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1034 | if retained { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1035 | data.events.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1036 | identity, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1037 | SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1038 | item: item.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1039 | media: media.to_vec(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1040 | raw: Some(raw.clone()), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1041 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1042 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1043 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1044 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1045 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1046 | async fn classify( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1047 | &self, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1048 | data: &Data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1049 | room_id: &OwnedRoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1050 | value: &Value, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1051 | ) -> (Vec<MentionReason>, bool, bool) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1052 | let Some(sender) = value["sender"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1053 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1054 | .and_then(|sender| sender.parse::<OwnedUserId>().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1055 | else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1056 | return (vec![], false, false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1057 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1058 | if sender == self.account |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1059 | || self.ignored.contains(&sender) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1060 | || value["type"] != "m.room.message" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1061 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1062 | return (vec![], false, false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1063 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1064 | let Some(room) = self.observable(room_id) else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1065 | return (vec![], false, false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1066 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1067 | let raw: Raw<AnySyncTimelineEvent> = |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1068 | Raw::from_json_string(value.to_string()).expect("serialised event"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1069 | let actions = room.event_push_actions(&raw).await.ok().flatten(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1070 | let notify = actions.as_ref().is_some_and(|actions| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1071 | actions |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1072 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1073 | .any(|action| matches!(action, ruma::push::Action::Notify)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1074 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1075 | 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 | 1076 | let direct = value["content"]["m.mentions"]["user_ids"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1077 | .as_array() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1078 | .is_some_and(|users| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1079 | users |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1080 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1081 | .any(|user| user.as_str() == Some(self.account.as_str())) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1082 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1083 | let mut reasons = Vec::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1084 | if direct { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1085 | reasons.push(MentionReason::Direct); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1086 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1087 | let mut unresolved = false; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1088 | if let Some(reply) = value["content"]["m.relates_to"]["m.in_reply_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1089 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1090 | .and_then(|value| value.parse().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1091 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1092 | let target = data.events.get(&id(&MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1093 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1094 | event_id: reply, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1095 | })); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1096 | 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 | 1097 | if reply_to_self && (direct || (notify && highlight)) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1098 | reasons.push(MentionReason::Reply); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1099 | } else if target.is_none() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1100 | && value["unsigned"]["macltok_reply_sender"].as_str().is_none() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1101 | && (direct || (notify && highlight)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1102 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1103 | unresolved = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1104 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1105 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1106 | if value["content"]["m.mentions"]["room"] == true { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1107 | match room.power_levels().await { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1108 | Ok(levels) if levels.user_can_trigger_room_notification(&sender) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1109 | if notify && highlight { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1110 | reasons.push(MentionReason::Room); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1111 | } else if actions.is_none() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1112 | unresolved = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1113 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1114 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1115 | Err(_) => unresolved = true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1116 | _ => {} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1117 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1118 | } |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 1119 | if reasons.is_empty() && notify && highlight { |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 1120 | reasons.push(MentionReason::Keyword); |
96680d38cc31 | atagen | 2026-10-25 09:20 UTC | 1121 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1122 | (reasons, !notify, unresolved) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1123 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1124 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1125 | pub(crate) async fn run(mut self) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1126 | let reports_client = self.client.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1127 | let reports = reports_client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1128 | .event_cache() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1129 | .subscribe_to_decryption_reports(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1130 | futures_util::pin_mut!(reports); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1131 | let mut ignores = self.client.subscribe_to_ignore_user_list_changes(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1132 | let mut work: FuturesUnordered<EngineBoxFuture<'static, Work>> = FuturesUnordered::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1133 | let mut captures = BTreeSet::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1134 | let mut retries = BTreeSet::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1135 | let mut history = false; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1136 | let mut next_scan = now() + 60_000; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1137 | let mut next_expiry = 0; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1138 | let mut tick = Box::pin(matrix_sdk::sleep::sleep(Duration::from_secs(1))); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1139 | loop { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1140 | self.schedule(&mut work, &mut captures, &mut retries, &mut history); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1141 | tokio::select! { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1142 | biased; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1143 | request = self.requests.recv() => match request { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1144 | Some(Request::Stop(reply)) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1145 | if self.dirty && let Err(error) = self.flush().await { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1146 | tracing::error!(%error, "saving the mentions inbox at shutdown failed"); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1147 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 1148 | self.media.release(&self.media_scope).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1149 | let _ = reply.send(()); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1150 | return; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1151 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1152 | Some(Request::Batch(batch, reply)) => { self.batch(batch).await; let _ = reply.send(()); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1153 | Some(Request::Command(command, reply)) => { let result = self.command(command).await; let _ = reply.send(result); } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1154 | None => return, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1155 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1156 | Some(result) = work.next(), if !work.is_empty() => { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1157 | let rooms = result.rooms(); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1158 | let before = self.data.scope(&rooms); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1159 | let mut next = std::mem::take(&mut self.data); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1160 | // a candidate that failed again only moved its retry time, which needs no write |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1161 | let changed = self.complete(&mut next, result, &mut captures, &mut retries, &mut history).await |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1162 | && next.scope(&rooms) != before; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1163 | self.data = next; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1164 | self.dirty |= changed; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1165 | self.unpublished |= changed; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1166 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1167 | Some(report) = reports.next() => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1168 | let room = match report { Ok(RedecryptorReport::ResolvedUtds { room_id, .. }) => Some(room_id), _ => None }; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1169 | for candidate in self.data.candidates.values_mut().filter(|candidate| candidate.encrypted() && room.as_ref().is_none_or(|room| *room == candidate.room_id)) { candidate.retry_at_ms = 0; } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1170 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1171 | Some(_) = ignores.next() => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1172 | self.ignored = self.ignored_users().await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1173 | let mut next = std::mem::take(&mut self.data); |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1174 | let changed = self.filter_ignored(&mut next); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1175 | self.data = next; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1176 | self.dirty |= changed; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1177 | self.unpublished |= changed; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1178 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1179 | () = &mut tick => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1180 | tick = Box::pin(matrix_sdk::sleep::sleep(Duration::from_secs(1))); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1181 | if now() >= next_scan && !history { self.recovery_budget = 20; self.latest_scan = true; next_scan = now() + 60_000; } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1182 | if now() >= next_expiry { let expired = self.data.expire_candidates(now()); self.dirty |= expired; self.unpublished |= expired; next_expiry = now() + 60_000; } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1183 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1184 | if self.store.is_none() && self.reopen.is_some() && now() >= self.flush_after { self.reopen().await; } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1185 | if self.dirty && now() >= self.flush_after { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1186 | if let Err(error) = self.flush().await { self.store_failed(error).await; } |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1187 | } else if self.unpublished && self.advance().is_ok() { |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1188 | self.publish().await; |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 1189 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1190 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1191 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1192 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1193 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1194 | |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1195 | async fn complete( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1196 | &mut self, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1197 | next: &mut Data, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1198 | result: Work, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1199 | captures: &mut BTreeSet<String>, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1200 | retries: &mut BTreeSet<String>, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1201 | history: &mut bool, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1202 | ) -> bool { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1203 | match result { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1204 | Work::Capture(key, revision, result) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1205 | captures.remove(&key); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1206 | let observed = result |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1207 | .as_ref() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1208 | .ok() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1209 | .map(|(_, _, observed)| observed.clone()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1210 | .unwrap_or_default(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1211 | let room_id = next |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1212 | .entries |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1213 | .get(&key) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1214 | .filter(|entry| entry.revision == revision) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1215 | .map(|entry| entry.key.room_id.clone()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1216 | .filter(|room_id| { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1217 | self.client |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1218 | .get_room(room_id) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1219 | .is_some_and(|room| room.state() == RoomState::Joined) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1220 | }); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1221 | self.captured(next, key, revision, result); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1222 | if let Some(room_id) = room_id { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1223 | for value in observed { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1224 | let identity = value["event_id"] |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1225 | .as_str() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1226 | .map(|event_id| format!("{room_id}\n{event_id}")); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1227 | if identity.as_ref().is_some_and(|key| { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1228 | next.events |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1229 | .get(key) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1230 | .is_some_and(|event| event.raw.is_some()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1231 | }) { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1232 | continue; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1233 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1234 | self.observe(next, &room_id, value).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1235 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1236 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1237 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1238 | Work::Candidate(key, result) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1239 | retries.remove(&key); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1240 | if let Some(mut candidate) = next.candidates.remove(&key) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1241 | match result { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1242 | Ok(value) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1243 | self.observe(next, &candidate.room_id, value).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1244 | if let Some(unresolved) = next.candidates.get_mut(&key) { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1245 | unresolved.attempts = candidate.attempts.saturating_add(1); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1246 | unresolved.first_seen_ms = candidate.first_seen_ms; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1247 | unresolved.retry_at_ms = |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1248 | now() + (1000u64 << unresolved.attempts.min(8)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1249 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1250 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1251 | Err(error) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1252 | if !candidate.encrypted() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1253 | && (matches!( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1254 | error.kind, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1255 | ErrorKind::EventNotFound | ErrorKind::Forbidden |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1256 | ) || error |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1257 | .server |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1258 | .as_ref() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1259 | .is_some_and(|server| matches!(server.status, 403 | 404))) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1260 | { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1261 | next.gap( |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1262 | Some(candidate.room_id), |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1263 | MentionGapReason::ContextUnavailable, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1264 | None, |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1265 | ); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1266 | } else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1267 | candidate.attempts = candidate.attempts.saturating_add(1); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1268 | candidate.retry_at_ms = |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1269 | retry_at(&error, 1000u64 << candidate.attempts.min(8)); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1270 | next.candidates.insert(key, candidate); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1271 | return false; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1272 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1273 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1274 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1275 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1276 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1277 | Work::History(latest, from, result) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1278 | *history = false; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1279 | self.recovery_budget = self.recovery_budget.saturating_sub(1); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1280 | match result { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1281 | Ok(page) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1282 | next.coverage.historical_supported = Some(true); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1283 | let mut reached_anchor = false; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1284 | for notification in page.notifications { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1285 | if let Some(event_id) = notification |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1286 | .event |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1287 | .get_field::<String>("event_id") |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1288 | .ok() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1289 | .flatten() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1290 | { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1291 | let identity = format!("{}\n{}", notification.room_id, event_id); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1292 | if latest && from.is_none() && next.latest_head.is_none() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1293 | next.latest_head = Some(identity.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1294 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1295 | if latest && next.latest_anchor.as_ref() == Some(&identity) { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1296 | reached_anchor = true; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1297 | break; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1298 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1299 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1300 | if self |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1301 | .client |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1302 | .get_room(¬ification.room_id) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1303 | .is_some_and(|room| room.state() == RoomState::Joined) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1304 | && let Ok(value) = |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1305 | serde_json::from_str(notification.event.json().get()) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1306 | { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1307 | self.observe(next, ¬ification.room_id, value).await; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1308 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1309 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1310 | let token = page |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1311 | .next_token |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1312 | .filter(|token| Some(token) != from.as_ref() && !reached_anchor); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1313 | if latest { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1314 | next.latest_cursor = token.clone(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1315 | if token.is_none() { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1316 | self.latest_scan = false; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1317 | next.latest_anchor = |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1318 | next.latest_head.take().or(next.latest_anchor.take()); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1319 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1320 | } else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1321 | next.history_cursor = token.clone(); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1322 | next.history_exhausted = token.is_none(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1323 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1324 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1325 | Err(error) => { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1326 | if error |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1327 | .server |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1328 | .as_ref() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1329 | .is_some_and(|server| matches!(server.status, 404 | 405 | 501)) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1330 | { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1331 | next.coverage.historical_supported = Some(false); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1332 | self.recovery_budget = 0; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1333 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1334 | if error |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1335 | .server |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1336 | .as_ref() |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1337 | .is_some_and(|server| server.status == 400) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1338 | { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1339 | if latest { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1340 | next.latest_cursor = None; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1341 | next.latest_head = None; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1342 | } else { |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1343 | next.history_cursor = None; |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1344 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1345 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1346 | next.gap(None, MentionGapReason::NotificationHistoryUnavailable, from); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1347 | self.recovery_retry = retry_at(&error, 60_000); |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1348 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1349 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1350 | next.coverage.recovering = self.recovery_budget > 0 |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1351 | && (self.latest_scan || !next.history_exhausted) |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1352 | && next.coverage.historical_supported != Some(false); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1353 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1354 | } |
7cdaa86b9489 | atagen | 2026-10-25 08:43 UTC | 1355 | true |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1356 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1357 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1358 | fn schedule( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1359 | &mut self, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1360 | work: &mut FuturesUnordered<EngineBoxFuture<'static, Work>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1361 | captures: &mut BTreeSet<String>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1362 | retries: &mut BTreeSet<String>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1363 | history: &mut bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1364 | ) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1365 | 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 | 1366 | if captures.len() >= 2 { break; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1367 | if captures.contains(key) { continue; } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1368 | 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 | 1369 | captures.insert(key.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1370 | 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 | 1371 | 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 | 1372 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1373 | for (key, candidate) in &self.data.candidates { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1374 | if retries.len() >= 2 { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1375 | break; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1376 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1377 | if candidate.retry_at_ms > now() || retries.contains(key) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1378 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1379 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1380 | let Some(room) = self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1381 | .client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1382 | .get_room(&candidate.room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1383 | .filter(|room| room.state() == RoomState::Joined) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1384 | else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1385 | continue; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1386 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1387 | let key = key.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1388 | let candidate = candidate.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1389 | let key_for_work = key.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1390 | retries.insert(key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1391 | work.push(Box::pin(async move { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1392 | let result = async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1393 | if candidate.raw["type"] == "m.room.encrypted" { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1394 | let raw: Raw<OriginalSyncRoomEncryptedEvent> = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1395 | Raw::from_json_string(candidate.raw.to_string()).map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1396 | Error::with_source( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1397 | ErrorKind::Invalid, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1398 | "invalid encrypted mention candidate", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1399 | error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1400 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1401 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1402 | let event = room.decrypt_event(&raw, None).await.map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1403 | Error::from_sdk("decrypting a mention candidate failed", error) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1404 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1405 | let value: Value = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1406 | serde_json::from_str(event.raw().json().get()).map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1407 | Error::with_source( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1408 | ErrorKind::Invalid, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1409 | "invalid decrypted mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1410 | error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1411 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1412 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1413 | if value["type"] == "m.room.encrypted" { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1414 | return Err(Error::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1415 | ErrorKind::Encryption, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1416 | "mention candidate still awaits keys", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1417 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1418 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1419 | return Ok(value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1420 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1421 | let target = candidate.raw["content"]["m.relates_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1422 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1423 | .filter(|_| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1424 | candidate.raw["content"]["m.relates_to"]["rel_type"] == "m.replace" |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1425 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1426 | if let Some(target) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1427 | target.and_then(|target| target.parse::<ruma::OwnedEventId>().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1428 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1429 | let event = room.event(&target, None).await.map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1430 | Error::from_sdk("fetching an edited mention failed", error) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1431 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1432 | let original: Value = serde_json::from_str(event.raw().json().get()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1433 | .map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1434 | Error::with_source( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1435 | ErrorKind::Invalid, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1436 | "invalid edited mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1437 | error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1438 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1439 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1440 | if original["sender"] == candidate.raw["sender"] { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1441 | let mut original = edited(original, &candidate.raw); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1442 | original["unsigned"]["macltok_edit"] = candidate.raw; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1443 | return Ok(original); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1444 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1445 | return Err(Error::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1446 | ErrorKind::Forbidden, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1447 | "an edit belongs to a different sender", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1448 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1449 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1450 | let mut value = candidate.raw; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1451 | if let Some(target) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1452 | value["content"]["m.relates_to"]["m.in_reply_to"]["event_id"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1453 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1454 | .and_then(|target| target.parse::<ruma::OwnedEventId>().ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1455 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1456 | let event = room.event(&target, None).await.map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1457 | Error::from_sdk("fetching a mention reply target failed", error) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1458 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1459 | let target: Value = serde_json::from_str(event.raw().json().get()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1460 | .map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1461 | Error::with_source( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1462 | ErrorKind::Invalid, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1463 | "invalid mention reply target", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1464 | error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1465 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1466 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1467 | value["unsigned"]["macltok_reply_sender"] = target["sender"].clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1468 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1469 | Ok(value) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1470 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1471 | .await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1472 | Work::Candidate(key_for_work, result) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1473 | })); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1474 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1475 | if !*history |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1476 | && self.recovery_budget > 0 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1477 | && now() >= self.recovery_retry |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1478 | && (self.latest_scan || !self.data.history_exhausted) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1479 | && self.data.coverage.historical_supported != Some(false) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1480 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1481 | *history = true; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1482 | let latest = self.latest_scan; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1483 | let from = if latest { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1484 | self.data.latest_cursor.clone() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1485 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1486 | self.data.history_cursor.clone() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1487 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1488 | let client = self.client.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1489 | work.push(Box::pin(async move { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1490 | let mut request = ruma::api::client::push::get_notifications::v3::Request::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1491 | request.from = from.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1492 | request.limit = Some(ruma::uint!(50)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1493 | let result = client.send(request).await.map_err(|error| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1494 | Error::from_sdk("fetching mention history failed", error.into()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1495 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1496 | Work::History(latest, from, result) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1497 | })); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1498 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1499 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1500 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1501 | fn captured( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1502 | &self, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1503 | data: &mut Data, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1504 | key: String, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1505 | revision: u64, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1506 | result: Result<(crate::EventContext, Vec<MediaRegistration>, Vec<Value>), Error>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1507 | ) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1508 | let Some(entry) = data.entries.get(&key) else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1509 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1510 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1511 | if entry.revision != revision { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1512 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1513 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1514 | let room_id = entry.key.room_id.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1515 | if !self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1516 | .client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1517 | .get_room(&room_id) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1518 | .is_some_and(|room| room.state() == RoomState::Joined) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1519 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1520 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1521 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1522 | match result { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1523 | Ok((context, media, _)) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1524 | data.capture_attempts.remove(&key); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1525 | let live_after = data |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1526 | .windows |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1527 | .get(&key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1528 | .map(|window| window.after.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1529 | .unwrap_or_default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1530 | let mut insert = |item: TimelineItem| -> Option<String> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1531 | let TimelineItem::Event(event) = &item else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1532 | return None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1533 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1534 | let event_id = event.event_id.clone()?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1535 | let event_key = id(&MentionKey { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1536 | room_id: room_id.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1537 | event_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1538 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1539 | let registrations = media_for_item(&item, &media); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1540 | let mut saved = SavedEvent { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1541 | item, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1542 | media: registrations, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1543 | raw: None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1544 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1545 | if let Some(existing) = data.events.get(&event_key) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1546 | saved = existing.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1547 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1548 | if data.redacted.contains_key(&event_key) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1549 | redact(&mut saved); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1550 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1551 | data.events.insert(event_key.clone(), saved); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1552 | Some(event_key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1553 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1554 | let before: Vec<_> = context |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1555 | .events_before |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1556 | .into_iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1557 | .filter_map(&mut insert) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1558 | .collect(); |
258c08ec2168 | atagen | 2026-10-25 09:15 UTC | 1559 | let target = insert(context.event); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1560 | let mut after: Vec<_> = context |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1561 | .events_after |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1562 | .into_iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1563 | .filter_map(&mut insert) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1564 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1565 | let extended = live_after.iter().any(|key| !after.contains(key)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1566 | after.extend(live_after); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1567 | after.sort_by_key(|key| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1568 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1569 | data.events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1570 | .get(key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1571 | .and_then(|saved| match &saved.item { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1572 | TimelineItem::Event(event) => Some(event.timestamp_ms), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1573 | _ => None, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1574 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1575 | .unwrap_or(0), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1576 | key.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1577 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1578 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1579 | after.dedup(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1580 | after.truncate(10); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1581 | let entry = data.entries.get_mut(&key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1582 | entry.capture = if target.is_none() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1583 | MentionCaptureState::Unavailable |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1584 | } else if after.len() < 10 || (before.len() < 10 && context.start_token.is_some()) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1585 | MentionCaptureState::Partial |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1586 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1587 | MentionCaptureState::Ready |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1588 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1589 | entry.revision = data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1590 | data.windows.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1591 | key, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1592 | Window { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1593 | before, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1594 | target, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1595 | after, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1596 | start: context.start_token, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1597 | end: if extended { None } else { context.end_token }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1598 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1599 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1600 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1601 | Err(error) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1602 | let attempts = data.capture_attempts.entry(key.clone()).or_default(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1603 | *attempts = attempts.saturating_add(1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1604 | let delay = (1000u64 << (*attempts).min(11)).min(3_600_000); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1605 | let entry = data.entries.get_mut(&key).unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1606 | entry.capture = if matches!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1607 | error.kind, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1608 | ErrorKind::EventNotFound | ErrorKind::Forbidden | ErrorKind::RoomUnknown |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1609 | ) || error |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1610 | .server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1611 | .as_ref() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1612 | .is_some_and(|server| matches!(server.status, 403 | 404)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1613 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1614 | MentionCaptureState::Unavailable |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1615 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1616 | MentionCaptureState::Failed { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1617 | message: error.to_string(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1618 | retry_at_ms: retry_at(&error, delay), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1619 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1620 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1621 | entry.revision = data.revision + 1; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1622 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1623 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1624 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1625 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1626 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1627 | fn edited(mut original: Value, edit: &Value) -> Value { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1628 | original["content"] = edit["content"]["m.new_content"].clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1629 | original["unsigned"]["macltok_edited"] = Value::Bool(true); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1630 | original |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1631 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1632 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1633 | fn retry_at(error: &Error, fallback_ms: u64) -> u64 { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1634 | let mut source = std::error::Error::source(error); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1635 | while let Some(current) = source { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1636 | if let Some(sdk) = current.downcast_ref::<matrix_sdk::Error>() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1637 | if let Some(ruma::api::error::ErrorKind::LimitExceeded(data)) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1638 | sdk.client_api_error_kind() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1639 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1640 | if let Some(delay) = data.retry_after { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1641 | let delay = match delay { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1642 | ruma::api::error::RetryAfter::Delay(delay) => delay, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1643 | ruma::api::error::RetryAfter::DateTime(time) => time |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1644 | .duration_since(ruma::time::SystemTime::now()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1645 | .unwrap_or_default(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1646 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1647 | return now().saturating_add( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1648 | u64::try_from(delay.as_millis()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1649 | .unwrap_or(u64::MAX) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1650 | .max(fallback_ms), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1651 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1652 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1653 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1654 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1655 | source = current.source(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1656 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1657 | now().saturating_add(fallback_ms) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1658 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1659 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1660 | fn redact(event: &mut SavedEvent) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1661 | if let TimelineItem::Event(item) = &mut event.item { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1662 | item.content = TimelineContent::Redacted; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1663 | item.reply_to = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1664 | item.reactions.clear(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1665 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1666 | event.media.clear(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1667 | event.raw = None; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1668 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1669 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1670 | fn media_for_item( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1671 | item: &TimelineItem, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1672 | registrations: &[MediaRegistration], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1673 | ) -> Vec<MediaRegistration> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1674 | let TimelineItem::Event(event) = item else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1675 | return Vec::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1676 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1677 | let mut ids = Vec::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1678 | match &event.content { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1679 | TimelineContent::Message(message) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1680 | ids.extend(message.media.iter().map(|media| &media.id)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1681 | ids.extend( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1682 | message |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1683 | .link_previews |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1684 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1685 | .filter_map(|preview| preview.image.as_ref().map(|image| &image.media_id)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1686 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1687 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1688 | TimelineContent::Sticker { media, .. } => ids.push(&media.id), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1689 | _ => {} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1690 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1691 | registrations |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1692 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1693 | .filter(|registration| ids.contains(&®istration.id)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1694 | .cloned() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1695 | .collect() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1696 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1697 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1698 | pub(crate) async fn watch_updates(mut updates: watch::Receiver<MentionsPage>, events: EventSender) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1699 | let mut previous: BTreeMap<String, MentionEntry> = BTreeMap::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1700 | loop { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1701 | let snapshot = updates.borrow_and_update().clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1702 | let current: BTreeMap<_, _> = snapshot |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1703 | .entries |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1704 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1705 | .map(|entry| (id(&entry.key), entry.clone())) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1706 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1707 | let upserts = current |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1708 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1709 | .filter(|(key, entry)| previous.get(*key) != Some(entry)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1710 | .map(|(_, entry)| entry.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1711 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1712 | let removed = previous |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1713 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1714 | .filter(|(key, _)| !current.contains_key(*key)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1715 | .map(|(_, entry)| entry.key.clone()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1716 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1717 | previous = current; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1718 | if events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1719 | .send(Event::MentionsChanged(MentionsUpdate { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1720 | snapshot, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1721 | upserts, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1722 | removed, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1723 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1724 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1725 | .is_err() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1726 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1727 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1728 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1729 | if updates.changed().await.is_err() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1730 | return; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1731 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1732 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1733 | } |