31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | use std::{future::Future, panic::AssertUnwindSafe, pin::Pin, sync::Arc}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 3 | use futures_util::{FutureExt, StreamExt}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | use matrix_sdk::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | room::edit::EditedContent, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | room::reply::{EnforceThread, Reply as SdkReply}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | ruma::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | api::client::receipt::create_receipt::v3::ReceiptType, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | events::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | Mentions, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | room::message::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | AddMentions, Relation, ReplyWithinThread, RoomMessageEventContentWithoutRelation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | use matrix_sdk_ui::timeline::{Timeline, TimelineEventItemId}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | use ruma::{OwnedEventId, OwnedUserId}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | use tokio::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | sync::mpsc, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | task::{JoinHandle, JoinSet}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | use crate::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | Error, ErrorKind, Event, HistoryState, MessageContent, MessageEditContent, MessageMentions, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | RichText, TimelineCommand, TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | engine::MediaService, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | normalise::{MediaScope, normalise_timeline, normalise_timeline_diffs}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | transport::{EventSender, Responder, SendEventError}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | const PAGINATION_QUEUE: usize = 32; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | /// long-lived tasks inside a room actor report panics here so the actor can die loudly |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | #[derive(Clone)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | pub(super) struct Supervisor(mpsc::UnboundedSender<&'static str>); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | impl Supervisor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | pub(super) fn new() -> (Self, mpsc::UnboundedReceiver<&'static str>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | let (sender, receiver) = mpsc::unbounded_channel(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | (Self(sender), receiver) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | pub(super) fn spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | &self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | label: &'static str, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | task: impl Future<Output = ()> + Send + 'static, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | ) -> JoinHandle<()> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | let panics = self.0.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | tokio::spawn(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | if AssertUnwindSafe(task).catch_unwind().await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | let _ = panics.send(label); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | /// what a timeline session needs from the room that owns it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | #[derive(Clone)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | pub(super) struct SessionContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | pub(super) events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | pub(super) media: Arc<MediaService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | pub(super) current_user_id: Option<OwnedUserId>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | pub(super) initial_history_limit: u16, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | pub(super) supervisor: Supervisor, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | pub(super) struct TimelineSession { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | pub(super) id: TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | timeline: Arc<Timeline>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | context: SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | scope: MediaScope, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | watcher: JoinHandle<()>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | pagination: mpsc::Sender<PaginationRequest>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | pagination_worker: JoinHandle<()>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | struct PaginationRequest { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | limit: u16, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | reply: Option<Responder>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | impl TimelineSession { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | pub(super) async fn start( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | id: TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | timeline: Timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | context: SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | ) -> Result<Self, SendEventError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | let timeline = Arc::new(timeline); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | let scope = MediaScope::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | let watcher = start_watcher(&id, &timeline, &context, &scope).await?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | let (pagination, requests) = mpsc::channel(PAGINATION_QUEUE); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | let pagination_worker = context.supervisor.spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | "pagination worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | run_pagination( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | requests, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | timeline.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | context.events.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | let session = Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | scope, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | watcher, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | pagination, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | pagination_worker, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | context, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | if session.context.initial_history_limit > 0 { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | session.paginate(session.context.initial_history_limit, None); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | Ok(session) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | /// swaps in a fresh watcher. the old one is dead before the reset goes out, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | /// so no stale diff can land after it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | pub(super) async fn resync(&mut self) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | self.watcher.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | let _ = (&mut self.watcher).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | self.watcher = start_watcher(&self.id, &self.timeline, &self.context, &self.scope) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | .map_err(|_| Error::new(ErrorKind::Timeline, "timeline reset could not be sent"))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | Ok(()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | pub(super) fn paginate(&self, limit: u16, reply: Option<Responder>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | if let Err(error) = self.pagination.try_send(PaginationRequest { limit, reply }) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | let (request, error) = match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | mpsc::error::TrySendError::Full(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | Error::new(ErrorKind::QueueFull, "pagination queue is full"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | mpsc::error::TrySendError::Closed(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | Error::new(ErrorKind::TimelineNotOpen, "timeline is closing"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | if let Some(reply) = request.reply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | reply.done(Err(error)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 145 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 146 | /// pending paginations answer `Interrupted` as their responders drop |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | pub(super) async fn close(mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 148 | self.context.media.release(&self.scope).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | self.pagination_worker.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | let _ = (&mut self.pagination_worker).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | self.watcher.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | let _ = (&mut self.watcher).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | pub(super) async fn execute( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | &self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | command: TimelineCommand, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 158 | reply: Responder, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | work: &mut JoinSet<()>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | let timeline = self.timeline.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | let thread_root = self.id.thread_root().cloned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 163 | let background: Pin<Box<dyn Future<Output = Result<(), Error>> + Send>> = match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | TimelineCommand::Paginate { limit, .. } => return self.paginate(limit, Some(reply)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 165 | // network-bound, so it runs beside the actor instead of queueing the room behind it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 166 | TimelineCommand::Redact { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 167 | event_id, reason, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 168 | } => Box::pin(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 169 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 170 | .redact(&TimelineEventItemId::EventId(event_id), reason.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 171 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 172 | .map_err(timeline_error) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 173 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | TimelineCommand::ToggleReaction { event_id, key, .. } => Box::pin(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | .toggle_reaction(&TimelineEventItemId::EventId(event_id), &key) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 178 | .map(drop) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 179 | .map_err(timeline_error) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 181 | TimelineCommand::MarkRead { event_id, .. } => Box::pin(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | let receipt_failed = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 183 | |source| Error::from_sdk("sending read receipt failed", source); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 184 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 185 | .send_single_receipt(ReceiptType::Read, event_id.clone()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | .map_err(receipt_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | if thread_root.is_none() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | .send_single_receipt(ReceiptType::FullyRead, event_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | .map_err(receipt_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | Ok(()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | command => return reply.done(self.execute_serial(command).await), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | work.spawn(async move { reply.done(background.await) }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 201 | /// sends stay on the actor so they hit the send queue in the order they were asked for |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | async fn execute_serial(&self, command: TimelineCommand) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | let timeline = &self.timeline; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 204 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 205 | TimelineCommand::Send { content, .. } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 206 | let body = match &content { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 207 | MessageContent::Text { content, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | | MessageContent::Emote { content, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | | MessageContent::Reply { content, .. } => &content.body, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | if body.trim().is_empty() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | return Err(Error::new(ErrorKind::EmptyBody, "message body is empty")); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | let content = match content { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | MessageContent::Text { content, mentions } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | message_content(content, mentions, false).with_relation(None) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | MessageContent::Emote { content, mentions } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 219 | message_content(content, mentions, true).with_relation(None) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 221 | MessageContent::Reply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | event_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 223 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | mentions, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 225 | notify_replied_to, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | self.reply_content(event_id, content, mentions, notify_replied_to) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | .await? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | .send(content.into()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | .map(drop) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | .map_err(timeline_error) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | TimelineCommand::Edit { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | event_id, content, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | let (content, mentions, emote) = match content { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | MessageEditContent::Text { content, mentions } => (content, mentions, false), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | MessageEditContent::Emote { content, mentions } => (content, mentions, true), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | if content.body.trim().is_empty() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | return Err(Error::new(ErrorKind::EmptyBody, "message body is empty")); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | .edit( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | &TimelineEventItemId::EventId(event_id), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | EditedContent::RoomMessage(message_content(content, mentions, emote)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | .map_err(timeline_error) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | _ => Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | ErrorKind::Other, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | "timeline command was misrouted", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 258 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 259 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | async fn reply_content( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | &self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | event_id: OwnedEventId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | content: RichText, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | mentions: MessageMentions, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | notify_replied_to: bool, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | ) -> Result<matrix_sdk::ruma::events::room::message::RoomMessageEventContent, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | let thread_root = self.id.thread_root(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | let content = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | .timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | .room() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | .make_reply_event( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | message_content(content, mentions, false), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 275 | SdkReply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 276 | event_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 277 | enforce_thread: if thread_root.is_some() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 278 | EnforceThread::Threaded(ReplyWithinThread::Yes) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | } else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 280 | EnforceThread::MaybeThreaded |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 281 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 282 | add_mentions: if notify_replied_to { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 283 | AddMentions::Yes |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 284 | } else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 285 | AddMentions::No |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 286 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 287 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 288 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 289 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 291 | Error::with_source(ErrorKind::Timeline, "reply operation failed", source) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 292 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 293 | if let Some(thread_root) = thread_root |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 294 | && !matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 295 | content.relates_to.as_ref(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 296 | Some(Relation::Thread(thread)) if &thread.event_id == thread_root |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 297 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | "reply target does not belong to the selected thread", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | )); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 304 | Ok(content) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | impl Drop for TimelineSession { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | fn drop(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | self.watcher.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | self.pagination_worker.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | async fn start_watcher( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | id: &TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | timeline: &Arc<Timeline>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | context: &SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | scope: &MediaScope, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | ) -> Result<JoinHandle<()>, SendEventError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | let (items, mut stream) = timeline.subscribe().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | let current_user_id = context.current_user_id.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | let normalised = normalise_timeline(items.iter(), current_user_id.as_deref(), scope); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | context.media.register(normalised.media).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | context |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | .events |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | .send(Event::TimelineReset { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | timeline_id: id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | items: normalised.items, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | .await?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | let id = id.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | let events = context.events.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | let media = context.media.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | let scope = scope.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | Ok(context.supervisor.spawn("timeline watcher", async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | while let Some(diffs) = stream.next().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | let normalised = normalise_timeline_diffs(diffs, current_user_id.as_deref(), &scope); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | media.register(normalised.media).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | let event = Event::TimelineChanged { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | timeline_id: id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | diffs: normalised.diffs, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | if events.send(event).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 348 | })) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | async fn run_pagination( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | mut requests: mpsc::Receiver<PaginationRequest>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 353 | timeline: Arc<Timeline>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 354 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 355 | timeline_id: TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 356 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | while let Some(PaginationRequest { limit, reply }) = requests.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | let history = |state| Event::HistoryChanged { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 359 | timeline_id: timeline_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | if events.send(history(HistoryState::Loading)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | let result = timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | .paginate_backwards(limit.max(1)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | .map_err(timeline_error); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | let state = match &result { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | Ok(true) => HistoryState::StartReached, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | Ok(false) => HistoryState::Idle, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | Err(error) => HistoryState::Failed(error.to_string()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | let _ = events.send(history(state)).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | if let Some(reply) = reply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | reply.done(result.map(drop)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | fn message_content( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | content: RichText, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | mentions: MessageMentions, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 384 | emote: bool, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 385 | ) -> RoomMessageEventContentWithoutRelation { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 386 | let content = match content.formatted_body { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 387 | Some(formatted_body) if emote => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 388 | RoomMessageEventContentWithoutRelation::emote_html(content.body, formatted_body) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 389 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 390 | Some(formatted_body) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | RoomMessageEventContentWithoutRelation::text_html(content.body, formatted_body) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | None if emote => RoomMessageEventContentWithoutRelation::emote_plain(content.body), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 394 | None => RoomMessageEventContentWithoutRelation::text_plain(content.body), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 395 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 396 | let content = content.add_mentions(Mentions::with_user_ids(mentions.user_ids)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 397 | if mentions.room { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | content.add_mentions(Mentions::with_room_mention()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | } else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | content |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 402 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 403 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 404 | pub(super) fn timeline_error(source: matrix_sdk_ui::timeline::Error) -> Error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 405 | Error::with_source(ErrorKind::Timeline, "timeline operation failed", source) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 406 | } |