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