da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 1 | use std::{ |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 2 | collections::{HashMap, VecDeque}, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 3 | sync::Arc, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 4 | }; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 5 | |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 6 | use futures_util::future::{join_all, try_join}; |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 7 | use matrix_sdk::{ |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 8 | Client, Room, RoomState, deserialized_responses::TimelineEvent as SdkTimelineEvent, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 9 | room::MessagesOptions, |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 10 | }; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 11 | use ruma::{OwnedEventId, OwnedRoomId, OwnedUserId, UInt, push::Action}; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 12 | use serde_json::Value; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 13 | use tokio::sync::{Mutex, broadcast::error::RecvError}; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 14 | |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 15 | use crate::{ |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 16 | Error, ErrorKind, Event, EventContext, EventContextCloseReason, EventContextCommand, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 17 | EventContextPage, EventContextSession, Sender, TimelineEvent, TimelineItem, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 18 | engine::MediaService, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 19 | normalise::{ |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 20 | MediaRegistration, MediaScope, apply_profiles, member_sender, normalise_context, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 21 | normalise_observed, resolve_replies, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 22 | }, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 23 | transport::EventSender, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 24 | }; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 25 | |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 26 | const MAX_CONTEXT_LIMIT: u16 = 50; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 27 | /// a consumer that never closes its jump views still holds at most this many |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 28 | const MAX_SESSIONS: usize = 32; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 29 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 30 | /// open event contexts, oldest first, each owning the media its pages registered |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 31 | pub(crate) struct Sessions { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 32 | open: Mutex<VecDeque<Session>>, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 33 | events: EventSender, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 34 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 35 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 36 | struct Session { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 37 | id: String, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 38 | room_id: OwnedRoomId, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 39 | scope: MediaScope, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 40 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 41 | |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 42 | impl Session { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 43 | /// media ids carry the session, so two owners of one event never share an entry |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 44 | fn media_prefix(scope: &MediaScope) -> String { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 45 | format!("{}#", scope.nonce()) |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 46 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 47 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 48 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 49 | impl Sessions { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 50 | pub(crate) fn new(events: EventSender) -> Self { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 51 | Self { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 52 | open: Mutex::default(), |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 53 | events, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 54 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 55 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 56 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 57 | async fn open( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 58 | &self, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 59 | media: &MediaService, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 60 | room_id: OwnedRoomId, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 61 | scope: MediaScope, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 62 | registrations: Vec<MediaRegistration>, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 63 | ) -> String { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 64 | let id = format!("context-{}", scope.nonce()); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 65 | let mut evicted = Vec::new(); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 66 | { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 67 | let mut open = self.open.lock().await; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 68 | media.register(&scope, registrations).await; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 69 | open.push_back(Session { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 70 | id: id.clone(), |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 71 | room_id, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 72 | scope, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 73 | }); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 74 | while open.len() > MAX_SESSIONS { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 75 | if let Some(oldest) = open.pop_front() { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 76 | media.release(&oldest.scope).await; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 77 | evicted.push(oldest.id); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 78 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 79 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 80 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 81 | self.announce(evicted, EventContextCloseReason::Evicted) |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 82 | .await; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 83 | id |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 84 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 85 | |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 86 | /// the room and the media prefix the session's pages use |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 87 | async fn find(&self, id: &str) -> Result<(OwnedRoomId, String), Error> { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 88 | let open = self.open.lock().await; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 89 | let session = open |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 90 | .iter() |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 91 | .find(|session| session.id == id) |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 92 | .ok_or_else(not_open)?; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 93 | Ok(( |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 94 | session.room_id.clone(), |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 95 | Session::media_prefix(&session.scope), |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 96 | )) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 97 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 98 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 99 | /// under the lock, so a page that lands after its close registers nothing |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 100 | async fn register( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 101 | &self, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 102 | media: &MediaService, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 103 | id: &str, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 104 | registrations: Vec<MediaRegistration>, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 105 | ) -> Result<(), Error> { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 106 | let open = self.open.lock().await; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 107 | let session = open |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 108 | .iter() |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 109 | .find(|session| session.id == id) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 110 | .ok_or_else(not_open)?; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 111 | media.register(&session.scope, registrations).await; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 112 | Ok(()) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 113 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 114 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 115 | async fn close(&self, media: &MediaService, id: &str) -> Result<(), Error> { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 116 | let mut open = self.open.lock().await; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 117 | let index = open |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 118 | .iter() |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 119 | .position(|session| session.id == id) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 120 | .ok_or_else(not_open)?; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 121 | if let Some(session) = open.remove(index) { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 122 | media.release(&session.scope).await; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 123 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 124 | Ok(()) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 125 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 126 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 127 | async fn close_left(&self, media: &MediaService, client: &Client) { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 128 | let mut closed = Vec::new(); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 129 | { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 130 | let mut open = self.open.lock().await; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 131 | let mut kept = VecDeque::with_capacity(open.len()); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 132 | for session in open.drain(..) { |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 133 | let left = client |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 134 | .get_room(&session.room_id) |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 135 | .is_none_or(|room| matches!(room.state(), RoomState::Left | RoomState::Banned)); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 136 | if left { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 137 | media.release(&session.scope).await; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 138 | closed.push(session.id); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 139 | } else { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 140 | kept.push_back(session); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 141 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 142 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 143 | *open = kept; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 144 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 145 | self.announce(closed, EventContextCloseReason::RoomLeft) |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 146 | .await; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 147 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 148 | |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 149 | async fn announce(&self, ids: Vec<String>, reason: EventContextCloseReason) { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 150 | for context_id in ids { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 151 | let event = Event::EventContextClosed { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 152 | context_id, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 153 | reason: reason.clone(), |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 154 | }; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 155 | if self.events.send(event).await.is_err() { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 156 | return; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 157 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 158 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 159 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 160 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 161 | /// the media service is cleared beside this, so nothing is released one by one |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 162 | pub(crate) async fn clear(&self) { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 163 | self.open.lock().await.clear(); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 164 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 165 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 166 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 167 | fn not_open() -> Error { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 168 | Error::new( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 169 | ErrorKind::EventContextNotOpen, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 170 | "event context is closed or was never opened", |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 171 | ) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 172 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 173 | |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 174 | /// leaving a room closes its event contexts |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 175 | pub(super) async fn close_on_leave( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 176 | client: Client, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 177 | media: Arc<MediaService>, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 178 | sessions: Arc<Sessions>, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 179 | ) { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 180 | let mut info = client.room_info_notable_update_receiver(); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 181 | loop { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 182 | match info.recv().await { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 183 | Ok(_) | Err(RecvError::Lagged(_)) => sessions.close_left(&media, &client).await, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 184 | Err(RecvError::Closed) => return, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 185 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 186 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 187 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 188 | |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 189 | pub(crate) async fn execute( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 190 | client: Client, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 191 | media: Arc<MediaService>, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 192 | sessions: Arc<Sessions>, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 193 | command: EventContextCommand, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 194 | ) -> Result<crate::CommandOutput, Error> { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 195 | let current_user_id = client.user_id().map(ToOwned::to_owned); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 196 | let room = |room_id: &OwnedRoomId| client.get_room(room_id).ok_or_else(Error::room_unknown); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 197 | let (context_id, token, limit, backwards) = match command { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 198 | EventContextCommand::Fetch { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 199 | room_id, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 200 | event_id, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 201 | before_limit, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 202 | after_limit, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 203 | } => { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 204 | let scope = media.context_scope(); |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 205 | let (context, registrations, _) = capture_in( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 206 | room(&room_id)?, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 207 | current_user_id, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 208 | event_id, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 209 | before_limit, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 210 | after_limit, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 211 | &Session::media_prefix(&scope), |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 212 | ) |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 213 | .await?; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 214 | let context_id = sessions.open(&media, room_id, scope, registrations).await; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 215 | return Ok(crate::CommandOutput::EventContext(EventContextSession { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 216 | context_id, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 217 | context, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 218 | })); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 219 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 220 | EventContextCommand::Close { context_id } => { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 221 | sessions.close(&media, &context_id).await?; |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 222 | return Ok(crate::CommandOutput::Done); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 223 | } |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 224 | EventContextCommand::Before { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 225 | context_id, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 226 | token, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 227 | limit, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 228 | } => (context_id, token, limit, true), |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 229 | EventContextCommand::After { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 230 | context_id, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 231 | token, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 232 | limit, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 233 | } => (context_id, token, limit, false), |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 234 | }; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 235 | if limit == 0 { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 236 | return Err(Error::new( |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 237 | ErrorKind::Invalid, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 238 | "context page limit must be at least 1", |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 239 | )); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 240 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 241 | validate_limit(limit)?; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 242 | if token.is_empty() { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 243 | return Err(Error::new( |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 244 | ErrorKind::Invalid, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 245 | "context pagination token is empty", |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 246 | )); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 247 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 248 | let (room_id, media_prefix) = sessions.find(&context_id).await?; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 249 | let room = room(&room_id)?; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 250 | let side = fetch_side(&room, Some(&token), limit, backwards).await?; |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 251 | let mut page = Assembly::new( |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 252 | &room, |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 253 | current_user_id.as_deref(), |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 254 | &side.values, |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 255 | &side.highlights, |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 256 | None, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 257 | &media_prefix, |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 258 | ) |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 259 | .await; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 260 | let events = page.take(0..side.values.len()); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 261 | sessions.register(&media, &context_id, page.media).await?; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 262 | Ok(crate::CommandOutput::EventContextPage(EventContextPage { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 263 | events, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 264 | next_token: side.token, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 265 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 266 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 267 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 268 | pub(crate) async fn capture( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 269 | room: Room, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 270 | current_user_id: Option<OwnedUserId>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 271 | event_id: OwnedEventId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 272 | before_limit: u16, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 273 | after_limit: u16, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 274 | ) -> Result<(EventContext, Vec<MediaRegistration>, Vec<Value>), Error> { |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 275 | capture_in( |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 276 | room, |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 277 | current_user_id, |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 278 | event_id, |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 279 | before_limit, |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 280 | after_limit, |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 281 | "", |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 282 | ) |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 283 | .await |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 284 | } |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 285 | |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 286 | async fn capture_in( |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 287 | room: Room, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 288 | current_user_id: Option<OwnedUserId>, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 289 | event_id: OwnedEventId, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 290 | before_limit: u16, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 291 | after_limit: u16, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 292 | media_prefix: &str, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 293 | ) -> Result<(EventContext, Vec<MediaRegistration>, Vec<Value>), Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 294 | validate_limit(before_limit)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 295 | validate_limit(after_limit)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 296 | let raw = room |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 297 | .event_with_context(&event_id, false, UInt::from(0_u16), None) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 298 | .await |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 299 | .map_err(|source| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 300 | let error = Error::from_sdk("fetching event context failed", source); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 301 | if error |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 302 | .server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 303 | .as_ref() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 304 | .is_some_and(|server| server.status == 404) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 305 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 306 | Error::new(ErrorKind::EventNotFound, "event was not found") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 307 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 308 | error |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 309 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 310 | })?; |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 311 | let (target, target_highlight) = raw |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 312 | .event |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 313 | .as_ref() |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 314 | .and_then(raw_event) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 315 | .ok_or_else(|| Error::new(ErrorKind::EventNotFound, "event was not found"))?; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 316 | let (before, after) = try_join( |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 317 | fetch_side(&room, raw.prev_batch_token.as_deref(), before_limit, true), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 318 | fetch_side(&room, raw.next_batch_token.as_deref(), after_limit, false), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 319 | ) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 320 | .await?; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 321 | let target_index = before.values.len(); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 322 | let mut values = before.values; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 323 | values.push(target); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 324 | values.extend(after.values); |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 325 | let mut highlights = before.highlights; |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 326 | highlights.push(target_highlight); |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 327 | highlights.extend(after.highlights); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 328 | let mut page = Assembly::new( |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 329 | &room, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 330 | current_user_id.as_deref(), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 331 | &values, |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 332 | &highlights, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 333 | Some(target_index), |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 334 | media_prefix, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 335 | ) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 336 | .await; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 337 | let events_before = page.take(0..target_index); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 338 | let event = page |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 339 | .take(target_index..target_index + 1) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 340 | .pop() |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 341 | .ok_or_else(|| Error::new(ErrorKind::EventNotFound, "event could not be read"))?; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 342 | let events_after = page.take(target_index + 1..values.len()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 343 | Ok(( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 344 | EventContext { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 345 | events_before, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 346 | event, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 347 | events_after, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 348 | start_token: before.token, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 349 | end_token: after.token, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 350 | }, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 351 | page.media, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 352 | values, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 353 | )) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 354 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 355 | |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 356 | struct Side { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 357 | /// in timeline order |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 358 | values: Vec<Value>, |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 359 | /// beside `values`, from the push actions the sdk worked out as it fetched |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 360 | highlights: Vec<bool>, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 361 | token: Option<String>, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 362 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 363 | |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 364 | fn raw_event(event: &SdkTimelineEvent) -> Option<(Value, bool)> { |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 365 | let value = serde_json::from_str(event.raw().json().get()).ok()?; |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 366 | let highlight = event |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 367 | .push_actions() |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 368 | .is_some_and(|actions| actions.iter().any(Action::is_highlight)); |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 369 | Some((value, highlight)) |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 370 | } |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 371 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 372 | async fn fetch_side( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 373 | room: &Room, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 374 | token: Option<&str>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 375 | limit: u16, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 376 | backwards: bool, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 377 | ) -> Result<Side, Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 378 | let Some(token) = token else { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 379 | return Ok(Side { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 380 | values: Vec::new(), |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 381 | highlights: Vec::new(), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 382 | token: None, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 383 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 384 | }; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 385 | if limit == 0 { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 386 | return Ok(Side { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 387 | values: Vec::new(), |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 388 | highlights: Vec::new(), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 389 | token: Some(token.to_owned()), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 390 | }); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 391 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 392 | let mut options = if backwards { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 393 | MessagesOptions::backward() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 394 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 395 | MessagesOptions::forward() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 396 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 397 | options = options.from(token); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 398 | options.limit = UInt::from(limit); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 399 | let messages = room |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 400 | .messages(options) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 401 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 402 | .map_err(|source| Error::from_sdk("fetching event context events failed", source))?; |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 403 | let mut events: Vec<_> = messages.chunk.iter().filter_map(raw_event).collect(); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 404 | if backwards { |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 405 | events.reverse(); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 406 | } |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 407 | let (values, highlights) = events.into_iter().unzip(); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 408 | Ok(Side { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 409 | values, |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 410 | highlights, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 411 | token: messages.end, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 412 | }) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 413 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 414 | |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 415 | /// a window of raw events normalised together, so relations inside it land on their targets |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 416 | struct Assembly { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 417 | items: Vec<(usize, TimelineItem)>, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 418 | media: Vec<MediaRegistration>, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 419 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 420 | |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 421 | impl Assembly { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 422 | async fn new( |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 423 | room: &Room, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 424 | current_user_id: Option<&ruma::UserId>, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 425 | values: &[Value], |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 426 | highlights: &[bool], |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 427 | keep: Option<usize>, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 428 | media_prefix: &str, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 429 | ) -> Self { |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 430 | let page = normalise_context(values, keep, current_user_id, room.room_id(), media_prefix); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 431 | let mut items = page.items; |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 432 | for (index, item) in &mut items { |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 433 | if let TimelineItem::Event(event) = item { |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 434 | event.highlight = highlights[*index]; |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 435 | } |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 436 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 437 | let mut media = page.media; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 438 | let mut targets: HashMap<OwnedEventId, TimelineEvent> = HashMap::new(); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 439 | let mut missing = Vec::new(); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 440 | for (_, item) in &items { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 441 | if let TimelineItem::Event(event) = item |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 442 | && let Some(event_id) = &event.event_id |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 443 | { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 444 | targets.insert(event_id.clone(), (**event).clone()); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 445 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 446 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 447 | for event in events(&mut items) { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 448 | if let Some(reply) = &event.reply_to |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 449 | && !targets.contains_key(&reply.event_id) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 450 | && !missing.contains(&reply.event_id) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 451 | { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 452 | missing.push(reply.event_id.clone()); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 453 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 454 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 455 | let fetched = join_all( |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 456 | missing |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 457 | .into_iter() |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 458 | .map(|event_id| fetch_reply(room, current_user_id, event_id, media_prefix)), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 459 | ) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 460 | .await; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 461 | for (event, registrations) in fetched.into_iter().flatten() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 462 | media.extend(registrations); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 463 | if let Some(event_id) = event.event_id.clone() { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 464 | targets.insert(event_id, event); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 465 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 466 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 467 | let profiles = profiles( |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 468 | room, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 469 | items |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 470 | .iter() |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 471 | .filter_map(|(_, item)| match item { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 472 | TimelineItem::Event(event) => Some(&event.sender.user_id), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 473 | _ => None, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 474 | }) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 475 | .chain(targets.values().map(|event| &event.sender.user_id)), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 476 | ) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 477 | .await; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 478 | apply_profiles(targets.values_mut(), &profiles); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 479 | apply_profiles(events(&mut items), &profiles); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 480 | resolve_replies(events(&mut items), &targets); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 481 | Self { items, media } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 482 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 483 | |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 484 | /// the items that came from raw events in `range` |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 485 | fn take(&mut self, range: std::ops::Range<usize>) -> Vec<TimelineItem> { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 486 | let (taken, kept) = std::mem::take(&mut self.items) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 487 | .into_iter() |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 488 | .partition(|(index, _)| range.contains(index)); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 489 | self.items = kept; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 490 | taken.into_iter().map(|(_, item)| item).collect() |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 491 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 492 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 493 | |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 494 | fn events(items: &mut [(usize, TimelineItem)]) -> impl Iterator<Item = &mut TimelineEvent> { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 495 | items.iter_mut().filter_map(|(_, item)| match item { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 496 | TimelineItem::Event(event) => Some(&mut **event), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 497 | _ => None, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 498 | }) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 499 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 500 | |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 501 | async fn fetch_reply( |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 502 | room: &Room, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 503 | current_user_id: Option<&ruma::UserId>, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 504 | event_id: OwnedEventId, |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 505 | media_prefix: &str, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 506 | ) -> Option<(TimelineEvent, Vec<MediaRegistration>)> { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 507 | let event = room |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 508 | .load_or_fetch_event(&event_id, None) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 509 | .await |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 510 | .inspect_err(|error| tracing::debug!(%event_id, %error, "fetching a reply target failed")) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 511 | .ok()?; |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 512 | let value: Value = serde_json::from_str(event.raw().json().get()).ok()?; |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 513 | let identity = format!("{media_prefix}{}\n{event_id}", room.room_id()); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 514 | match normalise_observed(&value, current_user_id, &identity)? { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 515 | (TimelineItem::Event(event), media) => Some((*event, media)), |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 516 | _ => None, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 517 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 518 | } |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 519 | |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 520 | /// under lazy-loaded members a sender may be unknown locally, and `get_member` fetches the |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 521 | /// member list once for the room when it is |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 522 | async fn profiles<'a>( |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 523 | room: &Room, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 524 | user_ids: impl Iterator<Item = &'a OwnedUserId>, |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 525 | ) -> HashMap<OwnedUserId, Sender> { |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 526 | let mut unique: Vec<&OwnedUserId> = user_ids.collect(); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 527 | unique.sort(); |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 528 | unique.dedup(); |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 529 | join_all( |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 530 | unique.into_iter().map(|user_id| async move { |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 531 | Some((user_id.clone(), member_sender(room, user_id).await?)) |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 532 | }), |
fbeaaa098107 | atagen | 2026-10-25 12:10 UTC | 533 | ) |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 534 | .await |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 535 | .into_iter() |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 536 | .flatten() |
cb7b3362e1ce | atagen | 2026-10-25 09:08 UTC | 537 | .collect() |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 538 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 539 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 540 | fn validate_limit(limit: u16) -> Result<(), Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 541 | if limit > MAX_CONTEXT_LIMIT { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 542 | Err(Error::new(ErrorKind::Invalid, "context limit exceeds 50")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 543 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 544 | Ok(()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 545 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 546 | } |