1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 1 | use std::{collections::HashSet, future::Future, panic::AssertUnwindSafe, 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}; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 4 | use matrix_sdk::SendOutsideWasm; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | use matrix_sdk::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | room::edit::EditedContent, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | room::reply::{EnforceThread, Reply as SdkReply}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | ruma::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | api::client::receipt::create_receipt::v3::ReceiptType, |
0827ad2d24e9 | atagen | 2026-10-23 06:49 UTC | 10 | events::room::message::{ |
0827ad2d24e9 | atagen | 2026-10-23 06:49 UTC | 11 | AddMentions, Relation, ReplyWithinThread, RoomMessageEventContentWithoutRelation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | }, |
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 | use matrix_sdk_ui::timeline::{Timeline, TimelineEventItemId}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | use ruma::{OwnedEventId, OwnedUserId}; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 17 | use tokio::sync::{mpsc, oneshot}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 19 | use super::executor::{EngineBoxFuture, JoinHandle, TaskSet, spawn}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | use crate::{ |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 21 | Error, ErrorKind, Event, EventRef, HistoryState, MessageContent, MessageEditContent, |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 22 | MessageMentions, RichText, TimelineCommand, TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | engine::MediaService, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | normalise::{MediaScope, normalise_timeline, normalise_timeline_diffs}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | transport::{EventSender, Responder, SendEventError}, |
0827ad2d24e9 | atagen | 2026-10-23 06:49 UTC | 26 | upload::sdk_mentions, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | const PAGINATION_QUEUE: usize = 32; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | /// long-lived tasks inside a room actor report panics here so the actor can die loudly |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | #[derive(Clone)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | pub(super) struct Supervisor(mpsc::UnboundedSender<&'static str>); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | impl Supervisor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | pub(super) fn new() -> (Self, mpsc::UnboundedReceiver<&'static str>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | let (sender, receiver) = mpsc::unbounded_channel(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | (Self(sender), receiver) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | pub(super) fn spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | &self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | label: &'static str, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 44 | task: impl Future<Output = ()> + SendOutsideWasm + 'static, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | ) -> JoinHandle<()> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | let panics = self.0.clone(); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 47 | spawn(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | if AssertUnwindSafe(task).catch_unwind().await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | let _ = panics.send(label); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 55 | /// aborts its task when dropped, so an actor that dies mid-flight takes its helpers with it |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 56 | pub(super) struct TaskGuard(pub(super) JoinHandle<()>); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 57 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 58 | impl Drop for TaskGuard { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 59 | fn drop(&mut self) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 60 | self.0.abort(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 61 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 62 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 63 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | /// what a timeline session needs from the room that owns it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | #[derive(Clone)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | pub(super) struct SessionContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | pub(super) events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | pub(super) media: Arc<MediaService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | pub(super) current_user_id: Option<OwnedUserId>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | pub(super) initial_history_limit: u16, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | pub(super) supervisor: Supervisor, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | pub(super) struct TimelineSession { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | pub(super) id: TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | timeline: Arc<Timeline>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | context: SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | scope: MediaScope, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | watcher: JoinHandle<()>, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 80 | fences: mpsc::UnboundedSender<Fence>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | pagination: mpsc::Sender<PaginationRequest>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | pagination_worker: JoinHandle<()>, |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 83 | member_fetch: JoinHandle<()>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | struct PaginationRequest { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | limit: u16, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | reply: Option<Responder>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | impl TimelineSession { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | pub(super) async fn start( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | id: TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | timeline: Timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | context: SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | ) -> Result<Self, SendEventError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | let timeline = Arc::new(timeline); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | let scope = MediaScope::new(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 99 | let (watcher, fences) = start_watcher(&id, &timeline, &context, &scope).await?; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 100 | let (pagination, pagination_worker) = start_pagination_worker(&id, &timeline, &context); |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 101 | // under lazy-loaded members the sdk leaves sender profiles unavailable until someone |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 102 | // asks, and a /members fetched any other way never reaches the timeline's items |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 103 | let member_fetch = context.supervisor.spawn("member fetch", { |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 104 | let timeline = timeline.clone(); |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 105 | async move { timeline.fetch_members().await } |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 106 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | let session = Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | timeline, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | scope, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | watcher, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 112 | fences, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | pagination, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | pagination_worker, |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 115 | member_fetch, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | context, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | }; |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 118 | if session.context.initial_history_limit > 0 && !matches!(session.id, TimelineId::Room(_)) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | session.paginate(session.context.initial_history_limit, None); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | Ok(session) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | /// swaps in a fresh watcher. the old one is dead before the reset goes out, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | /// so no stale diff can land after it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | pub(super) async fn resync(&mut self) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | self.watcher.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | let _ = (&mut self.watcher).await; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 129 | (self.watcher, self.fences) = |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 130 | start_watcher(&self.id, &self.timeline, &self.context, &self.scope) |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 131 | .await |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 132 | .map_err(|_| Error::new(ErrorKind::Timeline, "timeline reset could not be sent"))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | Ok(()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 136 | /// returns once the watcher has passed on the history the room cache committed |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 137 | pub(super) async fn publish_history(&self) -> Result<(), Error> { |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 138 | self.timeline |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 139 | .flush_history() |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 140 | .await |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 141 | .map_err(timeline_error)?; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 142 | let (fence, drained) = oneshot::channel(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 143 | self.fences.send(fence).map_err(|_| Error::interrupted())?; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 144 | drained.await.map_err(|_| Error::interrupted()) |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 145 | } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 146 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | pub(super) fn paginate(&self, limit: u16, reply: Option<Responder>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 148 | if let Err(error) = self.pagination.try_send(PaginationRequest { limit, reply }) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | let (request, error) = match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | mpsc::error::TrySendError::Full(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | Error::new(ErrorKind::QueueFull, "pagination queue is full"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | mpsc::error::TrySendError::Closed(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | Error::new(ErrorKind::TimelineNotOpen, "timeline is closing"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 158 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | if let Some(reply) = request.reply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | reply.done(Err(error)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 163 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 165 | async fn stop_without_close(&mut self) { |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 166 | self.member_fetch.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 167 | self.pagination_worker.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 168 | self.watcher.abort(); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 169 | let _ = (&mut self.member_fetch).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 170 | let _ = (&mut self.pagination_worker).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 171 | let _ = (&mut self.watcher).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 172 | // after the watcher, or a last diff could register media for a timeline that's gone |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 173 | self.context.media.release(&self.scope).await; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 174 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 175 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 176 | /// pending paginations answer `Interrupted` as their responders drop |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 177 | pub(super) async fn close(mut self) { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 178 | self.stop_without_close().await; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 179 | let _ = self |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 180 | .context |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 181 | .events |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 182 | .send(Event::TimelineClosed { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 183 | timeline_id: self.id.clone(), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 184 | }) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 185 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | pub(super) async fn execute( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | &self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | command: TimelineCommand, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | reply: Responder, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 192 | work: &mut TaskSet<()>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | let timeline = self.timeline.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | let thread_root = self.id.thread_root().cloned(); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 196 | let background: EngineBoxFuture<'static, Result<(), Error>> = match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | TimelineCommand::Paginate { limit, .. } => return self.paginate(limit, Some(reply)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | // network-bound, so it runs beside the actor instead of queueing the room behind it |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 199 | TimelineCommand::Redact { target, reason, .. } => Box::pin(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | timeline |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 201 | .redact(&item_id(target), reason.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | .map_err(timeline_error) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 204 | }), |
8fa8a7cde89c | atagen | 2026-10-08 12:39 UTC | 205 | TimelineCommand::ToggleReaction { |
8fa8a7cde89c | atagen | 2026-10-08 12:39 UTC | 206 | target, |
8fa8a7cde89c | atagen | 2026-10-08 12:39 UTC | 207 | key, |
8fa8a7cde89c | atagen | 2026-10-08 12:39 UTC | 208 | shortcode, |
8fa8a7cde89c | atagen | 2026-10-08 12:39 UTC | 209 | .. |
8fa8a7cde89c | atagen | 2026-10-08 12:39 UTC | 210 | } => Box::pin(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | timeline |
8fa8a7cde89c | atagen | 2026-10-08 12:39 UTC | 212 | .toggle_reaction_with_shortcode(&item_id(target), &key, shortcode.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | .map(drop) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | .map_err(timeline_error) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | TimelineCommand::MarkRead { event_id, .. } => Box::pin(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | let receipt_failed = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 219 | |source| Error::from_sdk("sending read receipt failed", source); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 221 | .send_single_receipt(ReceiptType::Read, event_id.clone()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 223 | .map_err(receipt_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | if thread_root.is_none() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 225 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | .send_single_receipt(ReceiptType::FullyRead, event_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | .map_err(receipt_failed)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | Ok(()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | command => return reply.done(self.execute_serial(command).await), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | work.spawn(async move { reply.done(background.await) }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | /// 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 | 238 | async fn execute_serial(&self, command: TimelineCommand) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | let timeline = &self.timeline; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | TimelineCommand::Send { content, .. } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | let body = match &content { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | MessageContent::Text { content, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | | MessageContent::Emote { content, .. } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 245 | | MessageContent::Notice { content, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | | MessageContent::Reply { content, .. } => &content.body, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | if body.trim().is_empty() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | return Err(Error::new(ErrorKind::EmptyBody, "message body is empty")); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | let content = match content { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | MessageContent::Text { content, mentions } => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 253 | message_content(content, mentions, Msgtype::Text).with_relation(None) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | MessageContent::Emote { content, mentions } => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 256 | message_content(content, mentions, Msgtype::Emote).with_relation(None) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 257 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 258 | MessageContent::Notice { content, mentions } => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 259 | message_content(content, mentions, Msgtype::Notice).with_relation(None) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | MessageContent::Reply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | event_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | mentions, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | notify_replied_to, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | self.reply_content(event_id, content, mentions, notify_replied_to) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | .await? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | .send(content.into()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | .map(drop) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 275 | .map_err(timeline_error) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 276 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 277 | TimelineCommand::Edit { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 278 | target, content, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | } => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 280 | let (content, mentions, msgtype) = match content { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 281 | MessageEditContent::Text { content, mentions } => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 282 | (content, mentions, Msgtype::Text) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 283 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 284 | MessageEditContent::Emote { content, mentions } => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 285 | (content, mentions, Msgtype::Emote) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 286 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 287 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 288 | if content.body.trim().is_empty() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 289 | return Err(Error::new(ErrorKind::EmptyBody, "message body is empty")); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 291 | timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 292 | .edit( |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 293 | &item_id(target), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 294 | EditedContent::RoomMessage(message_content(content, mentions, msgtype)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 295 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 296 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 297 | .map_err(timeline_error) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | _ => Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | ErrorKind::Other, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | "timeline command was misrouted", |
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 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | async fn reply_content( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | &self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | event_id: OwnedEventId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | content: RichText, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | mentions: MessageMentions, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | notify_replied_to: bool, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | ) -> Result<matrix_sdk::ruma::events::room::message::RoomMessageEventContent, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | let thread_root = self.id.thread_root(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | let content = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | .timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | .room() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | .make_reply_event( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 318 | message_content(content, mentions, Msgtype::Text), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | SdkReply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | event_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | enforce_thread: if thread_root.is_some() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | EnforceThread::Threaded(ReplyWithinThread::Yes) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | } else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | EnforceThread::MaybeThreaded |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | add_mentions: if notify_replied_to { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | AddMentions::Yes |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | } else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | AddMentions::No |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | .map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | Error::with_source(ErrorKind::Timeline, "reply operation failed", source) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | if let Some(thread_root) = thread_root |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | && !matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | content.relates_to.as_ref(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | Some(Relation::Thread(thread)) if &thread.event_id == thread_root |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | return Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | "reply target does not belong to the selected thread", |
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 | Ok(content) |
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 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 352 | fn start_pagination_worker( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 353 | id: &TimelineId, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 354 | timeline: &Arc<Timeline>, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 355 | context: &SessionContext, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 356 | ) -> (mpsc::Sender<PaginationRequest>, JoinHandle<()>) { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 357 | let (pagination, requests) = mpsc::channel(PAGINATION_QUEUE); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 358 | let worker = context.supervisor.spawn( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 359 | "pagination worker", |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 360 | run_pagination( |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 361 | requests, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 362 | timeline.clone(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 363 | context.events.clone(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 364 | id.clone(), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 365 | ), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 366 | ); |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 367 | (pagination, worker) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 368 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 369 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | impl Drop for TimelineSession { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | fn drop(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | self.watcher.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | self.pagination_worker.abort(); |
6ed8c0ef3a2a | atagen | 2026-10-24 10:13 UTC | 374 | self.member_fetch.abort(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 378 | fn item_id(target: EventRef) -> TimelineEventItemId { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 379 | match target { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 380 | EventRef::Remote(event_id) => TimelineEventItemId::EventId(event_id), |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 381 | EventRef::Local(transaction_id) => TimelineEventItemId::TransactionId(transaction_id), |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 382 | } |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 383 | } |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 384 | |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 385 | /// the sdk leaves a reply's target unloaded until someone asks, so the watcher asks, once per event |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 386 | struct ReplyFetcher { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 387 | timeline: Arc<Timeline>, |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 388 | asked: HashSet<OwnedEventId>, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 389 | fetches: TaskSet<()>, |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 390 | } |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 391 | |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 392 | impl ReplyFetcher { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 393 | fn fetch(&mut self, missing: Vec<OwnedEventId>) { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 394 | for event_id in missing { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 395 | if self.asked.insert(event_id.clone()) { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 396 | let timeline = self.timeline.clone(); |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 397 | self.fetches.spawn(async move { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 398 | if let Err(error) = timeline.fetch_details_for_event(&event_id).await { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 399 | tracing::debug!(%event_id, %error, "fetching reply details failed"); |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 400 | } |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 401 | }); |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 402 | } |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 403 | } |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 404 | } |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 405 | } |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 406 | |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 407 | /// asks the watcher to pass on every diff the timeline already holds, then answers |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 408 | type Fence = oneshot::Sender<()>; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 409 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 410 | async fn start_watcher( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 411 | id: &TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | timeline: &Arc<Timeline>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 413 | context: &SessionContext, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | scope: &MediaScope, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 415 | ) -> Result<(JoinHandle<()>, mpsc::UnboundedSender<Fence>), SendEventError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | let (items, mut stream) = timeline.subscribe().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 417 | let current_user_id = context.current_user_id.clone(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 418 | let (normalised, mut hidden) = |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 419 | normalise_timeline(items.iter(), current_user_id.as_deref(), scope); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 420 | context.media.register(scope, normalised.media).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 421 | context |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 422 | .events |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | .send(Event::TimelineReset { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | timeline_id: id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 425 | items: normalised.items, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 426 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 427 | .await?; |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 428 | let mut replies = ReplyFetcher { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 429 | timeline: timeline.clone(), |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 430 | asked: HashSet::new(), |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 431 | fetches: TaskSet::new(), |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 432 | }; |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 433 | replies.fetch(normalised.missing_replies); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 434 | let (fences, mut fence_requests) = mpsc::unbounded_channel::<Fence>(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 435 | let id = id.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 436 | let events = context.events.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 437 | let media = context.media.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 438 | let scope = scope.clone(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 439 | let task = context.supervisor.spawn("timeline watcher", async move { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 440 | loop { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 441 | let (diffs, fence) = tokio::select! { |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 442 | diffs = stream.next() => match diffs { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 443 | Some(diffs) => (vec![diffs], None), |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 444 | None => return, |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 445 | }, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 446 | Some(fence) = fence_requests.recv() => { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 447 | let mut ready = Vec::new(); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 448 | while let Some(diffs) = stream.next().now_or_never() { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 449 | match diffs { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 450 | Some(diffs) => ready.push(diffs), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 451 | None => return, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 452 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 453 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 454 | (ready, Some(fence)) |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 455 | } |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 456 | Some(_) = replies.fetches.join_next(), if !replies.fetches.is_empty() => continue, |
960ac0721079 | atagen | 2026-09-25 00:24 UTC | 457 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 458 | for diffs in diffs { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 459 | let normalised = normalise_timeline_diffs( |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 460 | diffs, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 461 | current_user_id.as_deref(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 462 | &scope, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 463 | &mut hidden, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 464 | ); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 465 | media.register(&scope, normalised.media).await; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 466 | replies.fetch(normalised.missing_replies); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 467 | if normalised.diffs.is_empty() { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 468 | continue; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 469 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 470 | let event = Event::TimelineChanged { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 471 | timeline_id: id.clone(), |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 472 | diffs: normalised.diffs, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 473 | }; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 474 | if events.send(event).await.is_err() { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 475 | return; |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 476 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 477 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 478 | if let Some(fence) = fence { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 479 | let _ = fence.send(()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 480 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 481 | } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 482 | }); |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 483 | Ok((task, fences)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 484 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 485 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 486 | async fn run_pagination( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 487 | mut requests: mpsc::Receiver<PaginationRequest>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 488 | timeline: Arc<Timeline>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 489 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 490 | timeline_id: TimelineId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 491 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 492 | while let Some(PaginationRequest { limit, reply }) = requests.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 493 | let history = |state| Event::HistoryChanged { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 494 | timeline_id: timeline_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 495 | state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 496 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 497 | if events.send(history(HistoryState::Loading)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 498 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 499 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 500 | let result = timeline |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 501 | .paginate_backwards(limit.max(1)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 502 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 503 | .map_err(timeline_error); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 504 | let state = match &result { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 505 | Ok(true) => HistoryState::StartReached, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 506 | Ok(false) => HistoryState::Idle, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 507 | Err(error) => HistoryState::Failed(error.to_string()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 508 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 509 | let _ = events.send(history(state)).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 510 | if let Some(reply) = reply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 511 | reply.done(result.map(drop)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 512 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 513 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 514 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 515 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 516 | #[derive(Clone, Copy)] |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 517 | enum Msgtype { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 518 | Text, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 519 | Emote, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 520 | Notice, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 521 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 522 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 523 | fn message_content( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 524 | content: RichText, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 525 | mentions: MessageMentions, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 526 | msgtype: Msgtype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 527 | ) -> RoomMessageEventContentWithoutRelation { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 528 | type Content = RoomMessageEventContentWithoutRelation; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 529 | let (body, formatted_body) = (content.body, content.formatted_body); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 530 | let content = match (msgtype, formatted_body) { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 531 | (Msgtype::Text, Some(html)) => Content::text_html(body, html), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 532 | (Msgtype::Text, None) => Content::text_plain(body), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 533 | (Msgtype::Emote, Some(html)) => Content::emote_html(body, html), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 534 | (Msgtype::Emote, None) => Content::emote_plain(body), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 535 | (Msgtype::Notice, Some(html)) => Content::notice_html(body, html), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 536 | (Msgtype::Notice, None) => Content::notice_plain(body), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 537 | }; |
0827ad2d24e9 | atagen | 2026-10-23 06:49 UTC | 538 | content.add_mentions(sdk_mentions(mentions)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 539 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 540 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 541 | pub(super) fn timeline_error(source: matrix_sdk_ui::timeline::Error) -> Error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 542 | Error::with_source(ErrorKind::Timeline, "timeline operation failed", source) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 543 | } |