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