89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 1 | use std::sync::Arc; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 2 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 3 | use matrix_sdk::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 4 | Room, deserialized_responses::TimelineEvent as SdkTimelineEvent, room::MessagesOptions, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 5 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 6 | use ruma::{OwnedEventId, OwnedUserId, RoomId, UInt}; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 7 | |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 8 | use crate::{ |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 9 | Error, ErrorKind, EventContext, EventContextCommand, EventContextDirection, EventContextPage, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 10 | TimelineItem, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 11 | engine::MediaService, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 12 | normalise::{MediaRegistration, normalise_observed}, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 13 | }; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 14 | |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 15 | const MAX_CONTEXT_LIMIT: u16 = 50; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 16 | |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 17 | pub(crate) async fn execute( |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 18 | room: Room, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 19 | current_user_id: Option<OwnedUserId>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 20 | media: Arc<MediaService>, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 21 | command: EventContextCommand, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 22 | ) -> Result<crate::CommandOutput, Error> { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 23 | if let EventContextCommand::Before { token, limit, .. } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 24 | | EventContextCommand::After { token, limit, .. } = &command |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 25 | { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 26 | validate_limit(*limit)?; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 27 | if token.is_empty() { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 28 | return Err(Error::new( |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 29 | ErrorKind::Invalid, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 30 | "context pagination token is empty", |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 31 | )); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 32 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 33 | return execute_page( |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 34 | room, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 35 | current_user_id, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 36 | media, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 37 | token, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 38 | *limit, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 39 | matches!(command, EventContextCommand::Before { .. }), |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 40 | ) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 41 | .await; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 42 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 43 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 44 | let (event_id, before_limit, after_limit) = match &command { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 45 | EventContextCommand::Fetch { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 46 | event_id, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 47 | before_limit, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 48 | after_limit, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 49 | .. |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 50 | } => { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 51 | validate_limit(*before_limit)?; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 52 | validate_limit(*after_limit)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 53 | (event_id.clone(), *before_limit, *after_limit) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 54 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 55 | EventContextCommand::Before { .. } | EventContextCommand::After { .. } => unreachable!(), |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 56 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 57 | let (context, registrations, _) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 58 | capture(room, current_user_id, event_id, before_limit, after_limit).await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 59 | media.register(registrations).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 60 | Ok(crate::CommandOutput::EventContext(context)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 61 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 62 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 63 | pub(crate) async fn capture( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 64 | room: Room, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 65 | current_user_id: Option<OwnedUserId>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 66 | event_id: OwnedEventId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 67 | before_limit: u16, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 68 | after_limit: u16, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 69 | ) -> Result<(EventContext, Vec<MediaRegistration>, Vec<serde_json::Value>), Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 70 | validate_limit(before_limit)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 71 | validate_limit(after_limit)?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 72 | let raw = room |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 73 | .event_with_context(&event_id, false, UInt::from(0_u16), None) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 74 | .await |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 75 | .map_err(|source| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 76 | let error = Error::from_sdk("fetching event context failed", source); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 77 | if error |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | .server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 79 | .as_ref() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 80 | .is_some_and(|server| server.status == 404) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 81 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 82 | Error::new(ErrorKind::EventNotFound, "event was not found") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 84 | error |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 85 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 86 | })?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | if raw.event.is_none() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | return Err(Error::new(ErrorKind::EventNotFound, "event was not found")); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 89 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 90 | let before = fetch_side( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 91 | &room, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 92 | raw.prev_batch_token.as_deref(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 93 | before_limit, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | current_user_id.as_deref(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | let after = fetch_side( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | &room, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | raw.next_batch_token.as_deref(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 100 | after_limit, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 101 | false, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | current_user_id.as_deref(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 103 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 104 | let (events_before, mut before_media, start_token, mut observed) = before.await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 105 | let (events_after, after_media, end_token, following) = after.await?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 106 | observed.extend(following); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 107 | if let Some(value) = raw |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 108 | .event |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 109 | .as_ref() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 110 | .and_then(|event| serde_json::from_str(event.raw().json().get()).ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 111 | { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | observed.push(value); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | let event = raw.event.as_ref().and_then(|event| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | normalise_sdk_event(event, current_user_id.as_deref(), room.room_id()).map( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | |(item, media)| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 117 | before_media.extend(media); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 118 | item |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 119 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 120 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 121 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 122 | before_media.extend(after_media); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 123 | Ok(( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 124 | EventContext { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 125 | events_before, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 126 | event, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 127 | events_after, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 128 | start_token, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 129 | end_token, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 130 | }, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 131 | before_media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 132 | observed, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 133 | )) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 134 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 135 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 136 | async fn fetch_side( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 137 | room: &Room, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 138 | token: Option<&str>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 139 | limit: u16, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 140 | backwards: bool, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 141 | current_user_id: Option<&ruma::UserId>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 142 | ) -> Result< |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 143 | ( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 144 | Vec<TimelineItem>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 145 | Vec<MediaRegistration>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 146 | Option<String>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 147 | Vec<serde_json::Value>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 148 | ), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 149 | Error, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 150 | > { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 151 | if limit == 0 { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 152 | return Ok((Vec::new(), Vec::new(), token.map(str::to_owned), Vec::new())); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 153 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 154 | let Some(token) = token else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 155 | return Ok((Vec::new(), Vec::new(), None, Vec::new())); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 156 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 157 | let mut options = if backwards { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 158 | MessagesOptions::backward() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 159 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 160 | MessagesOptions::forward() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 161 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 162 | options = options.from(token); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 163 | options.limit = UInt::from(limit); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 164 | let messages = room |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 165 | .messages(options) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 166 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 167 | .map_err(|source| Error::from_sdk("fetching event context events failed", source))?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 168 | let mut events = messages.chunk; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 169 | events.truncate(usize::from(limit)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 170 | if backwards { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 171 | events.reverse(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 172 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 173 | let next_token = messages.end; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 174 | let observed = events |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 175 | .iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 176 | .filter_map(|event| serde_json::from_str(event.raw().json().get()).ok()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 177 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 178 | let (items, media) = normalise_events(events, current_user_id, room.room_id()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 179 | Ok((items, media, next_token, observed)) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 180 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 181 | |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 182 | async fn execute_page( |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 183 | room: Room, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 184 | current_user_id: Option<OwnedUserId>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 185 | media: Arc<MediaService>, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 186 | token: &str, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 187 | limit: u16, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 188 | backwards: bool, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 189 | ) -> Result<crate::CommandOutput, Error> { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 190 | let mut options = if backwards { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 191 | MessagesOptions::backward() |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 192 | } else { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 193 | MessagesOptions::forward() |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 194 | }; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 195 | options = options.from(token); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 196 | options.limit = UInt::from(limit); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 197 | let messages = room |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 198 | .messages(options) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 199 | .await |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 200 | .map_err(|source| Error::from_sdk("fetching event context page failed", source))?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 201 | let next_token = messages.end.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 202 | let mut events = messages.chunk; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 203 | events.truncate(usize::from(limit)); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 204 | if backwards { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 205 | events.reverse(); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 206 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 207 | let (events, registrations) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 208 | normalise_events(events, current_user_id.as_deref(), room.room_id()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 209 | media.register(registrations).await; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 210 | let end_reached = next_token.is_none(); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 211 | let direction = if backwards { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 212 | EventContextDirection::Before |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 213 | } else { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 214 | EventContextDirection::After |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 215 | }; |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 216 | Ok(crate::CommandOutput::EventContextPage(EventContextPage { |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 217 | direction, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 218 | events, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 219 | next_token, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 220 | end_reached, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 221 | })) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 222 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 223 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 224 | fn normalise_events( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 225 | events: Vec<SdkTimelineEvent>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 226 | current_user_id: Option<&ruma::UserId>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 227 | room_id: &RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 228 | ) -> (Vec<TimelineItem>, Vec<MediaRegistration>) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 229 | let mut items = Vec::with_capacity(events.len()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 230 | let mut media = Vec::new(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 231 | for event in events { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 232 | if let Some((item, registrations)) = normalise_sdk_event(&event, current_user_id, room_id) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 233 | items.push(item); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 234 | media.extend(registrations); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 235 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 236 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 237 | (items, media) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 238 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 239 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 240 | fn normalise_sdk_event( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 241 | event: &SdkTimelineEvent, |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 242 | current_user_id: Option<&ruma::UserId>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 243 | room_id: &RoomId, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 244 | ) -> Option<(TimelineItem, Vec<MediaRegistration>)> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 245 | let value: serde_json::Value = serde_json::from_str(event.raw().json().get()).ok()?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 246 | let event_id = value.get("event_id")?.as_str()?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 247 | let identity = format!("{room_id}\n{event_id}"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 248 | normalise_observed(&value, current_user_id, &identity) |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 249 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 250 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 251 | fn validate_limit(limit: u16) -> Result<(), Error> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 252 | if limit > MAX_CONTEXT_LIMIT { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 253 | Err(Error::new(ErrorKind::Invalid, "context limit exceeds 50")) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 254 | } else { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 255 | Ok(()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 256 | } |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 257 | } |