31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | mod commands; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 2 | pub(crate) mod context; |
a319bae745d8 | atagen | 2026-09-26 09:59 UTC | 3 | pub(crate) mod executor; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | mod media; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | mod mutations; |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 6 | mod packs; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 7 | mod prepare; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 8 | mod repair; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | mod room; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 10 | mod session; |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 11 | mod thread_list; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | mod timeline; |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 13 | mod url_preview; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | mod watchers; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | use std::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | collections::{HashMap, VecDeque}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | future::Future, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 19 | panic::AssertUnwindSafe, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | sync::Arc, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 23 | use futures_util::FutureExt; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 24 | use matrix_sdk::{Client, SendOutsideWasm, SessionChange}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | use matrix_sdk_ui::sync_service::SyncService; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 26 | use ruma::{OwnedDeviceId, OwnedRoomId}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | use thiserror::Error as ThisError; |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 28 | use tokio::sync::{broadcast, mpsc, watch}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | pub(crate) use media::MediaService; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 31 | pub(crate) use session::Persistence; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 33 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 34 | pub(crate) use crate::store_lock::StoreLock; |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 35 | #[cfg(not(any(feature = "sqlite", feature = "indexeddb")))] |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 36 | pub(crate) enum StoreLock {} |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 37 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 38 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 39 | pub(crate) use crate::mentions::store::MentionsStore; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 40 | #[cfg(not(any(feature = "sqlite", feature = "indexeddb")))] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 41 | pub(crate) enum MentionsStore {} |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 42 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 43 | #[cfg(any(feature = "sqlite", feature = "indexeddb"))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 44 | pub(crate) use crate::search::store::SearchStore; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 45 | #[cfg(not(any(feature = "sqlite", feature = "indexeddb")))] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 46 | pub(crate) enum SearchStore {} |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 47 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | use self::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | commands::Job, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 50 | executor::{EngineBoxFuture, TaskSet}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | mutations::{Mutation, MutationJob}, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 52 | packs::PackEdit, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 53 | room::{ActorNote, ActorRequest, RoomActor}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | timeline::{SessionContext, Supervisor}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | use crate::{ |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 57 | AccountCommand, ClientHandle, Command, CommandOutput, ConnectionState, EncryptionCommand, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 58 | EngineErrorKind, EngineFailure, Error, ErrorKind, Event, EventStream, ImagePackSource, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 59 | LifecycleCommand, MediaCommand, NotificationCommand, PackCommand, RoomCommand, Termination, |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 60 | ThreadListCommand, TimelineCommand, |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 61 | normalise::{ROOM_PACKS, ROOM_PREVIEW_URLS}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | transport::{DriverPort, Request, Responder, transport_pair}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | #[derive(Clone, Debug, Eq, PartialEq)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | #[non_exhaustive] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | pub struct EngineConfig { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | pub command_capacity: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | pub event_capacity: usize, |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 70 | /// what a thread paginates as it opens, and a room if its recent load fails. 0 for neither |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | pub initial_history_limit: u16, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | pub max_media_bytes: usize, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 73 | /// media one event context session keeps registered, least recently used dropped first |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 74 | pub max_context_media: usize, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 75 | pub max_mentions_bytes: usize, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 76 | pub max_mentions_entries: usize, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 77 | pub max_mention_context_events: usize, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | pub max_mention_candidates: usize, |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 79 | /// warm a room up for sending when it's opened or someone starts typing in it |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 80 | pub prepare_to_send: bool, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 81 | /// `/messages` pages each joined room gets per engine run for search backfill. 0 for none |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 82 | pub search_backfill_pages_per_room: u16, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 83 | pub search_backfill_page_size: u16, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 84 | /// the least time between two backfill requests, across every room |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 85 | pub search_backfill_interval_ms: u64, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 86 | /// undecryptable messages kept to index once their keys turn up, newest first |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 87 | pub max_search_undecrypted: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | impl EngineConfig { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | #[must_use] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 92 | pub const fn with_max_mentions_bytes(mut self, bytes: usize) -> Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 93 | self.max_mentions_bytes = bytes; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | #[must_use] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | pub const fn with_max_mentions_entries(mut self, entries: usize) -> Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | self.max_mentions_entries = entries; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 100 | self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 101 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 103 | #[must_use] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 104 | pub const fn with_max_mention_context_events(mut self, events: usize) -> Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 105 | self.max_mention_context_events = events; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 106 | self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 107 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 108 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 109 | #[must_use] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 110 | pub const fn with_max_mention_candidates(mut self, candidates: usize) -> Self { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 111 | self.max_mention_candidates = candidates; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | self |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | pub const fn with_command_capacity(mut self, capacity: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | self.command_capacity = capacity; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | pub const fn with_event_capacity(mut self, capacity: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | self.event_capacity = capacity; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | pub const fn with_initial_history_limit(mut self, limit: u16) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | self.initial_history_limit = limit; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | #[must_use] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | pub const fn with_max_media_bytes(mut self, bytes: usize) -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | self.max_media_bytes = bytes; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | #[must_use] |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 140 | pub const fn with_max_context_media(mut self, media: usize) -> Self { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 141 | self.max_context_media = media; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | } |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 144 | |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 145 | #[must_use] |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 146 | pub const fn with_prepare_to_send(mut self, prepare: bool) -> Self { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 147 | self.prepare_to_send = prepare; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 148 | self |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 149 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 150 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 151 | #[must_use] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 152 | pub const fn with_search_backfill_pages_per_room(mut self, pages: u16) -> Self { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 153 | self.search_backfill_pages_per_room = pages; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 154 | self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 155 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 156 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 157 | #[must_use] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 158 | pub const fn with_search_backfill_page_size(mut self, events: u16) -> Self { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 159 | self.search_backfill_page_size = events; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 160 | self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 161 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 162 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 163 | #[must_use] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 164 | pub const fn with_search_backfill_interval_ms(mut self, interval_ms: u64) -> Self { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 165 | self.search_backfill_interval_ms = interval_ms; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 166 | self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 167 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 168 | |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 169 | #[must_use] |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 170 | pub const fn with_max_search_undecrypted(mut self, events: usize) -> Self { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 171 | self.max_search_undecrypted = events; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 172 | self |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 173 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | impl Default for EngineConfig { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | fn default() -> Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 178 | Self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 179 | command_capacity: 64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | event_capacity: 128, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 181 | initial_history_limit: 30, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | max_media_bytes: 20 * 1024 * 1024, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 183 | max_context_media: 1_024, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 184 | max_mentions_bytes: 64 * 1024 * 1024, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 185 | max_mentions_entries: 5_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 186 | max_mention_context_events: 25_000, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 187 | max_mention_candidates: 5_000, |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 188 | prepare_to_send: true, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 189 | search_backfill_pages_per_room: 10, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 190 | search_backfill_page_size: 100, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 191 | search_backfill_interval_ms: 2_000, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 192 | max_search_undecrypted: 2_000, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | #[derive(Debug, ThisError)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | pub enum EngineError { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | #[error("the Matrix client is not authenticated")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | Unauthenticated, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 201 | #[error("command, event, media source, and mentions capacities must be nonzero")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | ZeroCapacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | #[error("could not build the sync service")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 204 | SyncService(#[source] matrix_sdk_ui::sync_service::Error), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 205 | #[error("the event receiver has closed")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 206 | EventReceiverClosed, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 207 | #[error("engine task {task} panicked")] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | TaskPanicked { task: String }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | impl EngineError { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | fn failure(&self) -> EngineFailure { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | EngineFailure { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | kind: match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | Self::EventReceiverClosed => EngineErrorKind::EventReceiverClosed, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | Self::TaskPanicked { .. } => EngineErrorKind::Watcher, |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 217 | Self::Unauthenticated | Self::ZeroCapacity | Self::SyncService(_) => { |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 218 | EngineErrorKind::Other |
913832a327a8 | atagen | 2026-10-25 09:33 UTC | 219 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 221 | message: self.to_string(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 223 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 225 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | #[derive(Clone)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | enum TaskContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | /// a panic here costs the caller an `Interrupted` answer and nothing else |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | Command(&'static str), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | /// a panic here takes the engine with it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | Watcher(&'static str), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | room_id: OwnedRoomId, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | generation: u64, |
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 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | impl TaskContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | fn label(&self) -> String { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | Self::Command(name) | Self::Watcher(name) => (*name).to_owned(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | Self::RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | } => format!("room actor {room_id} generation {generation}"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | |
7b376d620a3d | atagen | 2026-09-26 12:53 UTC | 250 | /// tasks catch their own panics so each comes back with its context, since joining |
7b376d620a3d | atagen | 2026-09-26 12:53 UTC | 251 | /// through the sdk's executor gives no task id to look one up by. wasm has nothing to catch |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 252 | fn panic_message(payload: Box<dyn std::any::Any + Send>) -> String { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 253 | payload |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 254 | .downcast_ref::<&str>() |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 255 | .map(ToString::to_string) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 256 | .or_else(|| payload.downcast_ref::<String>().cloned()) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 257 | .unwrap_or_else(|| "task panicked with a non-string payload".to_owned()) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 258 | } |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 259 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | struct TaskRegistry { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 261 | tasks: TaskSet<(TaskContext, Option<String>)>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | impl TaskRegistry { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | fn spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | context: TaskContext, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 268 | task: impl Future<Output = ()> + SendOutsideWasm + 'static, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 269 | ) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 270 | self.tasks.spawn(async move { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 271 | let panic = AssertUnwindSafe(task) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 272 | .catch_unwind() |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 273 | .await |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 274 | .err() |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 275 | .map(panic_message); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 276 | (context, panic) |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 277 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 278 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 280 | fn is_empty(&self) -> bool { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 281 | self.tasks.is_empty() |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 282 | } |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 283 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 284 | async fn join_next(&mut self) -> Option<(TaskContext, Option<String>)> { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 285 | loop { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 286 | if let Ok(done) = self.tasks.join_next().await? { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 287 | return Some(done); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 288 | } |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 289 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 291 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 292 | async fn shutdown(&mut self) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 293 | self.tasks.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 294 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 295 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 296 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 297 | enum RoomSlot { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | Active { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | sender: mpsc::Sender<ActorRequest>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | /// opens and closes that arrive while the actor winds down, replayed in order once it's gone |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | Closing { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 304 | generation: u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | pending: VecDeque<Deferred>, |
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 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | impl RoomSlot { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | const fn generation(&self) -> u64 { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | match self { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | Self::Active { generation, .. } | Self::Closing { generation, .. } => *generation, |
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 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | enum Deferred { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | Open(Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | Close(Responder), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 322 | /// logouts in flight, and a token rejection held back because one of them caused it |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 323 | #[derive(Default)] |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 324 | pub(super) struct Logouts { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 325 | pub(super) in_flight: usize, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 326 | pub(super) rejected: Option<Option<OwnedDeviceId>>, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 327 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 328 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | struct Queues { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | mutations: mpsc::Receiver<MutationJob>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | notifications: mpsc::Receiver<Job<NotificationCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | encryption: mpsc::Receiver<Job<EncryptionCommand>>, |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 333 | thread_lists: mpsc::Receiver<Job<ThreadListCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | session_changes: broadcast::Receiver<SessionChange>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 335 | persistence: Option<Persistence>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 338 | /// requests other devices send us, handed from the sdk's event handler to the encryption worker |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 339 | type IncomingVerifications = |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 340 | mpsc::UnboundedSender<matrix_sdk::encryption::verification::VerificationRequest>; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 341 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | pub struct Engine { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | port: DriverPort, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | config: EngineConfig, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | media: Arc<MediaService>, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 348 | contexts: Arc<context::Sessions>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | tasks: TaskRegistry, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | rooms: HashMap<OwnedRoomId, RoomSlot>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | next_generation: u64, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 352 | notes: mpsc::UnboundedReceiver<ActorNote>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 353 | note_sender: mpsc::UnboundedSender<ActorNote>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 354 | /// the rooms sliding sync should follow, applied off the run loop since it touches the store |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 355 | subscriptions: watch::Sender<Vec<OwnedRoomId>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 356 | mutations: mpsc::Sender<MutationJob>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | notifications: mpsc::Sender<Job<NotificationCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | encryption: mpsc::Sender<Job<EncryptionCommand>>, |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 359 | thread_lists: mpsc::Sender<Job<ThreadListCommand>>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | queues: Option<Queues>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 361 | /// the client can outlive us, so the handler has to come off it when we stop |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 362 | verification_handler: Option<matrix_sdk::event_handler::EventHandlerHandle>, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 363 | stop: mpsc::UnboundedSender<Termination>, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 364 | stops: mpsc::UnboundedReceiver<Termination>, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 365 | logouts: Arc<watch::Sender<Logouts>>, |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 366 | preparations: prepare::Preparations, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 367 | /// shared with every `ClientHandle`, so the store stays locked while the client is reachable |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 368 | _store_lock: Option<Arc<StoreLock>>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 369 | mentions: crate::mentions::service::Handle, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 370 | mentions_service: Option<crate::mentions::service::Service>, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 371 | mentions_updates: Option<watch::Receiver<crate::MentionsPage>>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 372 | search: crate::search::Handle, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 373 | search_service: Option<crate::search::Service>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | impl Engine { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | /// the escape hatch for a client you built yourself. `Session` is the usual way in |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | pub async fn from_client( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | config: EngineConfig, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | ) -> Result<(Self, ClientHandle, EventStream), EngineError> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 382 | let session_changes = client.subscribe_to_session_changes(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 383 | Self::with_persistence(client, config, session_changes, None, None, None, None).await |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 384 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 385 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 386 | /// `session_changes` is taken when the client is built, since the sdk can refresh |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 387 | /// before an engine exists |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 388 | pub(crate) async fn with_persistence( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 389 | client: Client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 390 | config: EngineConfig, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 391 | session_changes: broadcast::Receiver<SessionChange>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 392 | persistence: Option<Persistence>, |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 393 | store_lock: Option<StoreLock>, |
82d37c369846 | atagen | 2026-10-25 10:55 UTC | 394 | mentions: Option<Result<MentionsStore, crate::mentions::service::Unopened>>, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 395 | search: Option<Result<SearchStore, crate::Error>>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 396 | ) -> Result<(Self, ClientHandle, EventStream), EngineError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 397 | if client.user_id().is_none() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | return Err(EngineError::Unauthenticated); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | if config.command_capacity == 0 |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | || config.event_capacity == 0 |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 402 | || config.max_context_media == 0 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 403 | || config.max_mentions_entries == 0 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 404 | || config.max_mention_context_events == 0 |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 405 | || config.max_mention_candidates == 0 |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 406 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | return Err(EngineError::ZeroCapacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 409 | let store_lock = store_lock.map(Arc::new); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 410 | let media = Arc::new(MediaService::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 411 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 412 | config.max_media_bytes, |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 413 | config.max_context_media, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 414 | )); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 415 | let (mentions, mentions_service, mentions_updates) = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 416 | crate::mentions::service::Service::new( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 417 | client.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 418 | media.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 419 | mentions, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 420 | config.clone(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 421 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 422 | .await |
0d763e4284ef | atagen | 2026-10-25 09:31 UTC | 423 | // a missing user id is all it still refuses, and that was turned away above |
0d763e4284ef | atagen | 2026-10-25 09:31 UTC | 424 | .map_err(|_| EngineError::Unauthenticated)?; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 425 | let (search, search_service) = |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 426 | crate::search::Service::new(client.clone(), media.clone(), search, &config); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 427 | let sync = Arc::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 428 | SyncService::builder(client.clone()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | .with_offline_mode() |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 430 | .with_room_list_extra_required_state(vec![ |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 431 | (ROOM_PACKS.into(), "*".to_owned()), |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 432 | (ROOM_PREVIEW_URLS.into(), String::new()), |
0291516d40b5 | atagen | 2026-10-22 08:31 UTC | 433 | ]) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 434 | .with_room_list_response_commit_hook(mentions.hook()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 435 | .build() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 436 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 437 | .map_err(EngineError::SyncService)?, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 438 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 439 | let (handle, port, events) = transport_pair( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 440 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 441 | sync.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 442 | media.clone(), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 443 | store_lock.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 444 | config.command_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 445 | config.event_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 446 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 447 | client.send_queue().enable_upload_progress(true); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 448 | client.send_queue().enable_global_upload_progress(true); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 449 | let (note_sender, notes) = mpsc::unbounded_channel(); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 450 | let (stop, stops) = mpsc::unbounded_channel(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 451 | let (mutations, mutation_queue) = mpsc::channel(config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 452 | let (notifications, notification_queue) = mpsc::channel(config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 453 | let (encryption, encryption_queue) = mpsc::channel(config.command_capacity); |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 454 | let (thread_lists, thread_list_queue) = mpsc::channel(config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 455 | let engine = Self { |
1f875b103b27 | atagen | 2026-10-25 10:44 UTC | 456 | contexts: Arc::new(context::Sessions::new(port.event_sender())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 457 | client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 458 | sync, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 459 | port, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 460 | config, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 461 | media, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 462 | tasks: TaskRegistry { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 463 | tasks: TaskSet::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 464 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 465 | rooms: HashMap::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 466 | next_generation: 0, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 467 | notes, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 468 | note_sender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 469 | subscriptions: watch::Sender::new(Vec::new()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 470 | mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 471 | notifications, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 472 | encryption, |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 473 | thread_lists, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 474 | queues: Some(Queues { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 475 | mutations: mutation_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 476 | notifications: notification_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 477 | encryption: encryption_queue, |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 478 | thread_lists: thread_list_queue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 479 | session_changes, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 480 | persistence, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 481 | }), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 482 | verification_handler: None, |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 483 | preparations: prepare::Preparations::default(), |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 484 | stop, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 485 | stops, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 486 | logouts: Arc::new(watch::Sender::new(Logouts::default())), |
a250c7fa43fb | atagen | 2026-09-26 13:35 UTC | 487 | _store_lock: store_lock, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 488 | mentions, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 489 | mentions_service: Some(mentions_service), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 490 | mentions_updates: Some(mentions_updates), |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 491 | search, |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 492 | search_service: Some(search_service), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 493 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 494 | Ok((engine, handle, events)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 495 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 496 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 497 | /// boxed because the engine's future is deep enough to blow the compiler's query depth |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 498 | /// in every crate that spawns it, which isn't something a client should have to fix |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 499 | pub fn run(self) -> EngineBoxFuture<'static, Result<Termination, EngineError>> { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 500 | Box::pin(self.run_inner()) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 501 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 502 | |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 503 | async fn run_inner(mut self) -> Result<Termination, EngineError> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 504 | let events = self.port.event_sender(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 505 | let user_id = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 506 | .client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 507 | .user_id() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 508 | .expect("authentication was checked during construction") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 509 | .to_owned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 510 | self.port |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 511 | .connection_sender() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 512 | .send_replace(ConnectionState::Connecting); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 513 | for event in [ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 514 | Event::Authenticated { user_id }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 515 | Event::ConnectionChanged(ConnectionState::Connecting), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 516 | ] { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 517 | if events.send(event).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 518 | self.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 519 | return Err(EngineError::EventReceiverClosed); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 520 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 521 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 522 | self.start_tasks(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 523 | self.sync.start().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 524 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 525 | let mut shutdown_reply = None; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 526 | let mut handles_dropped = false; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 527 | let mut logouts = self.logouts.subscribe(); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 528 | let ending = loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 529 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 530 | biased; |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 531 | Some(termination) = self.stops.recv() => break Ok(termination), |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 532 | () = events.closed() => break Err(EngineError::EventReceiverClosed), |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 533 | () = settled(&mut logouts), if handles_dropped => { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 534 | break Ok(Termination::HandlesDropped); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 535 | } |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 536 | Some((context, panic)) = self.tasks.join_next(), if !self.tasks.is_empty() => { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 537 | let Some(message) = panic else { continue }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 538 | if let TaskContext::Command(label) = context { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 539 | tracing::error!(label, panic = %message, "command task panicked"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 540 | continue; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 541 | } |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 542 | break Err(EngineError::TaskPanicked { task: context.label() }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 543 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 544 | Some(note) = self.notes.recv() => self.actor_note(note).await, |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 545 | request = self.port.recv(), if !handles_dropped => { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 546 | // nobody's left to ask, but a logout they asked for still deserves to land |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 547 | let Some(request) = request else { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 548 | handles_dropped = true; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 549 | continue; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 550 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 551 | if let Some(reply) = self.dispatch(request).await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 552 | shutdown_reply = Some(reply); |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 553 | break Ok(Termination::ShutDown); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 554 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 555 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 556 | } |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 557 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 558 | |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 559 | if let Err(error) = &ending { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 560 | let _ = events.send(Event::Failed(error.failure())).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 561 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 562 | self.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 563 | let _ = events.send(Event::Stopped).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 564 | if let Some(reply) = shutdown_reply { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 565 | reply.done(Ok(())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 566 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 567 | // everything else still queued answers `Interrupted` as it drops, but a shutdown did happen |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 568 | while let Some(Request { command, reply }) = self.port.try_recv() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 569 | if matches!(command, Command::Lifecycle(LifecycleCommand::Shutdown)) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 570 | reply.done(Ok(())); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 571 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 572 | } |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 573 | ending |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 574 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 575 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 576 | fn start_tasks(&mut self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 577 | let queues = self.queues.take().expect("engine tasks start once"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 578 | if let Some(service) = self.mentions_service.take() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 579 | self.tasks |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 580 | .spawn(TaskContext::Watcher("mentions service"), service.run()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 581 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 582 | if let Some(service) = self.search_service.take() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 583 | self.tasks |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 584 | .spawn(TaskContext::Watcher("search service"), service.run()); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 585 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 586 | if let Some(updates) = self.mentions_updates.take() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 587 | let events = self.port.event_sender(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 588 | self.tasks.spawn( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 589 | TaskContext::Watcher("mentions updates"), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 590 | crate::mentions::service::watch_updates(updates, events), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 591 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 592 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 593 | let (incoming, incoming_verifications) = mpsc::unbounded_channel(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 594 | self.verification_handler = Some(self.verification_handler(incoming)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 595 | let client = &self.client; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 596 | let events = || self.port.event_sender(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 597 | let (notices, notice_queue) = mpsc::unbounded_channel(); |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 598 | let watchers: [(&'static str, EngineBoxFuture<'static, ()>); 15] = [ |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 599 | ( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 600 | "upload tracker", |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 601 | Box::pin(watchers::uploads( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 602 | client.clone(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 603 | client.send_queue().subscribe(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 604 | events(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 605 | )), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 606 | ), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 607 | ( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 608 | "room subscription watcher", |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 609 | Box::pin(watchers::room_subscriptions( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 610 | self.sync.clone(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 611 | self.subscriptions.subscribe(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 612 | )), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 613 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 614 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 615 | "session change watcher", |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 616 | Box::pin(session::session( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 617 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 618 | queues.session_changes, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 619 | queues.persistence, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 620 | notices, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 621 | client |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 622 | .device_id() |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 623 | .expect("authentication was checked during construction") |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 624 | .to_owned(), |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 625 | self.logouts.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 626 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 627 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 628 | ( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 629 | "session notice watcher", |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 630 | Box::pin(session::session_notices( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 631 | notice_queue, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 632 | events(), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 633 | self.stop.clone(), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 634 | )), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 635 | ), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 636 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 637 | "connection state watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 638 | Box::pin(watchers::connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 639 | self.sync.state(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 640 | self.sync.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 641 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 642 | events(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 643 | self.port.connection_sender(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 644 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 645 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 646 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 647 | "send queue watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 648 | Box::pin(watchers::send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 649 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 650 | client.send_queue().subscribe_errors(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 651 | self.port.connection_sender().subscribe(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 652 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 653 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 654 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 655 | "encryption state watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 656 | Box::pin(watchers::encryption(client.clone(), events())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 657 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 658 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 659 | "room list watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 660 | Box::pin(watchers::rooms(client.clone(), events())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 661 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 662 | ( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 663 | "event context watcher", |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 664 | Box::pin(context::close_on_leave( |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 665 | client.clone(), |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 666 | self.media.clone(), |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 667 | self.contexts.clone(), |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 668 | )), |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 669 | ), |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 670 | ( |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 671 | "image pack watcher", |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 672 | Box::pin(watchers::image_packs(client.clone(), events())), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 673 | ), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 674 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 675 | "notification settings watcher", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 676 | Box::pin(watchers::notification_settings(client.clone(), events())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 677 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 678 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 679 | "room command worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 680 | Box::pin(mutations::run_scheduler( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 681 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 682 | queues.mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 683 | self.config.command_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 684 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 685 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 686 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 687 | "notification command worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 688 | Box::pin(commands::run_notifications( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 689 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 690 | queues.notifications, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 691 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 692 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 693 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 694 | "encryption command worker", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 695 | Box::pin(commands::run_encryption( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 696 | client.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 697 | events(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 698 | queues.encryption, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 699 | incoming_verifications, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 700 | )), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 701 | ), |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 702 | ( |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 703 | "thread list worker", |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 704 | Box::pin(thread_list::run( |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 705 | client.clone(), |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 706 | events(), |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 707 | self.media.clone(), |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 708 | queues.thread_lists, |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 709 | )), |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 710 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 711 | ]; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 712 | for (label, task) in watchers { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 713 | self.tasks.spawn(TaskContext::Watcher(label), task); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 714 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 715 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 716 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 717 | fn verification_handler( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 718 | &self, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 719 | incoming: IncomingVerifications, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 720 | ) -> matrix_sdk::event_handler::EventHandlerHandle { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 721 | use matrix_sdk::ruma::events::key::verification::request::ToDeviceKeyVerificationRequestEvent; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 722 | self.client.add_event_handler( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 723 | move |event: ToDeviceKeyVerificationRequestEvent, client: Client| { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 724 | let incoming = incoming.clone(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 725 | async move { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 726 | let request = client |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 727 | .encryption() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 728 | .get_verification_request( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 729 | &event.sender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 730 | event.content.transaction_id.as_str(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 731 | ) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 732 | .await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 733 | if let Some(request) = request { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 734 | let _ = incoming.send(request); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 735 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 736 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 737 | }, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 738 | ) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 739 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 740 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 741 | /// hands back the shutdown request's responder, which is answered once we've stopped |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 742 | async fn dispatch(&mut self, Request { command, reply }: Request) -> Option<Responder> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 743 | let client = self.client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 744 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 745 | Command::Lifecycle(LifecycleCommand::Shutdown) => return Some(reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 746 | Command::Account(AccountCommand::Logout) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 747 | let events = self.port.event_sender(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 748 | let stop = self.stop.clone(); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 749 | let logouts = self.logouts.clone(); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 750 | logouts.send_modify(|logouts| logouts.in_flight += 1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 751 | self.spawn_command("logout", reply, async move { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 752 | let result = client.logout().await; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 753 | if result.is_ok() { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 754 | let _ = events.send(Event::LoggedOut).await; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 755 | let _ = stop.send(Termination::LoggedOut); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 756 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 757 | let mut rejected = None; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 758 | logouts.send_if_modified(|logouts| { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 759 | if result.is_err() && logouts.in_flight == 1 { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 760 | rejected = logouts.rejected.take(); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 761 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 762 | false |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 763 | }); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 764 | if let Some(soft_logout) = rejected { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 765 | session::reject_session(&events, &stop, soft_logout).await; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 766 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 767 | logouts.send_modify(|logouts| logouts.in_flight -= 1); |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 768 | result.map_err(|source| Error::from_sdk("logout failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 769 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 770 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 771 | Command::Account(AccountCommand::SetAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 772 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 773 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 774 | }) => enqueue( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 775 | &self.mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 776 | Job { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 777 | command: Mutation::GlobalAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 778 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 779 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 780 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 781 | reply, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 782 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 783 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 784 | Command::Account(AccountCommand::SetDisplayName(name)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 785 | self.spawn_command("set display name", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 786 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 787 | .account() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 788 | .set_display_name(name.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 789 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 790 | .map_err(|source| Error::from_sdk("setting display name failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 791 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 792 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 793 | Command::Account(AccountCommand::SetAvatar(avatar)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 794 | self.spawn_command("set avatar", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 795 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 796 | .account() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 797 | .set_avatar_url(avatar.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 798 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 799 | .map_err(|source| Error::from_sdk("setting avatar failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 800 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 801 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 802 | Command::Account(AccountCommand::UploadAvatar { mimetype, bytes }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 803 | let max_bytes = self.config.max_media_bytes; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 804 | self.spawn_command("upload avatar", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 805 | commands::upload_avatar(&client, &mimetype, bytes, max_bytes).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 806 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 807 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 808 | Command::Account(AccountCommand::UploadMedia { mimetype, bytes }) => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 809 | let max_bytes = self.config.max_media_bytes; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 810 | self.tasks |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 811 | .spawn(TaskContext::Command("upload media"), async move { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 812 | let uploaded = |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 813 | commands::upload_media(&client, &mimetype, bytes, max_bytes).await; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 814 | reply.send(uploaded.map(CommandOutput::Mxc)); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 815 | }); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 816 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 817 | Command::Account(AccountCommand::IgnoreUser(user_id)) => enqueue( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 818 | &self.mutations, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 819 | Job { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 820 | command: Mutation::Ignore { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 821 | user_id, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 822 | ignored: true, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 823 | }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 824 | reply, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 825 | }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 826 | ), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 827 | Command::Account(AccountCommand::UnignoreUser(user_id)) => enqueue( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 828 | &self.mutations, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 829 | Job { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 830 | command: Mutation::Ignore { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 831 | user_id, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 832 | ignored: false, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 833 | }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 834 | reply, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 835 | }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 836 | ), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 837 | Command::Account(AccountCommand::ReorderSpaces(order)) => enqueue( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 838 | &self.mutations, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 839 | Job { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 840 | command: Mutation::ReorderSpaces(order), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 841 | reply, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 842 | }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 843 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 844 | Command::Account(AccountCommand::RenameDevice { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 845 | device_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 846 | display_name, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 847 | }) => self.spawn_command("rename device", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 848 | commands::rename_device(&client, &device_id, &display_name).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 849 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 850 | Command::Account(AccountCommand::DeleteDevices { device_ids, auth }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 851 | self.spawn_command("delete devices", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 852 | commands::delete_devices(&client, &device_ids, auth).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 853 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 854 | } |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 855 | Command::Room(RoomCommand::Open(room_id)) => { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 856 | if self.config.prepare_to_send { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 857 | self.prepare_quietly(room_id.clone()); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 858 | } |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 859 | self.open_room(room_id, reply).await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 860 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 861 | Command::Room(RoomCommand::Close(room_id)) => self.close_room(room_id, reply).await, |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 862 | Command::Room(RoomCommand::PrepareToSend(room_id)) => { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 863 | let flight = self.preparations.join(&client, room_id); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 864 | self.spawn_command("prepare to send", reply, flight); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 865 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 866 | Command::Room(RoomCommand::SetTyping { room_id, typing }) => { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 867 | if typing && self.config.prepare_to_send { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 868 | self.prepare_quietly(room_id.clone()); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 869 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 870 | self.spawn_command("typing", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 871 | commands::set_typing(&client, &room_id, typing).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 872 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 873 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 874 | Command::Room(RoomCommand::CancelSend { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 875 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 876 | transaction_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 877 | }) => self.spawn_command("cancel send", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 878 | commands::cancel_send(&client, &room_id, &transaction_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 879 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 880 | Command::Room(RoomCommand::RetrySend { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 881 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 882 | transaction_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 883 | }) => self.spawn_command("retry send", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 884 | commands::retry_send(&client, &room_id, &transaction_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 885 | }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 886 | Command::Room(command) => enqueue( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 887 | &self.mutations, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 888 | Job { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 889 | command: Mutation::Room(command), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 890 | reply, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 891 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 892 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 893 | Command::Timeline(command) => self.route_timeline(command, reply), |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 894 | Command::EventContext(command) => { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 895 | let (client, media, contexts) = |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 896 | (client.clone(), self.media.clone(), self.contexts.clone()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 897 | self.tasks |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 898 | .spawn(TaskContext::Command("event context"), async move { |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 899 | reply.send(context::execute(client, media, contexts, command).await); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 900 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 901 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 902 | Command::Mentions(command) => { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 903 | let mentions = self.mentions.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 904 | self.tasks |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 905 | .spawn(TaskContext::Command("mentions command"), async move { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 906 | reply.send(mentions.execute(command).await); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 907 | }); |
89bc9f8d246d | atagen | 2026-10-24 13:40 UTC | 908 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 909 | Command::Search(command) => { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 910 | let search = self.search.clone(); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 911 | self.tasks |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 912 | .spawn(TaskContext::Command("search command"), async move { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 913 | reply.send(search.execute(command).await); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 914 | }); |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 915 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 916 | Command::HistoryRepair(command) => self.route_repair(command, reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 917 | Command::Media(MediaCommand::Upload(upload)) => self.upload(upload, reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 918 | Command::Encryption(command) => enqueue(&self.encryption, Job { command, reply }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 919 | Command::Notification(command) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 920 | enqueue(&self.notifications, Job { command, reply }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 921 | } |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 922 | Command::Pack(command) => self.pack(command, reply), |
66db1b3cd895 | atagen | 2026-10-25 10:28 UTC | 923 | Command::ThreadList(command) => enqueue(&self.thread_lists, Job { command, reply }), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 924 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 925 | None |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 926 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 927 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 928 | fn spawn_command( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 929 | &mut self, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 930 | label: &'static str, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 931 | reply: Responder, |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 932 | work: impl Future<Output = Result<(), Error>> + SendOutsideWasm + 'static, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 933 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 934 | self.tasks.spawn(TaskContext::Command(label), async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 935 | reply.done(work.await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 936 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 937 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 938 | |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 939 | fn prepare_quietly(&mut self, room_id: OwnedRoomId) { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 940 | let flight = self.preparations.join(&self.client, room_id); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 941 | self.tasks |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 942 | .spawn(TaskContext::Command("prepare to send"), async move { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 943 | let _ = flight.await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 944 | }); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 945 | } |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 946 | |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 947 | fn pack(&mut self, command: PackCommand, reply: Responder) { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 948 | let edit = |pack, edit| Mutation::Pack { pack, edit }; |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 949 | let mutation = match command { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 950 | PackCommand::CreateRoomPack { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 951 | room_id, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 952 | state_key, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 953 | display_name, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 954 | } => edit( |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 955 | ImagePackSource::Room { room_id, state_key }, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 956 | PackEdit::Create { display_name }, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 957 | ), |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 958 | PackCommand::Delete(pack) => edit(pack, PackEdit::Delete), |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 959 | PackCommand::SetMeta { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 960 | pack, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 961 | display_name, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 962 | avatar, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 963 | usage, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 964 | } => edit( |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 965 | pack, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 966 | PackEdit::Meta { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 967 | display_name, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 968 | avatar, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 969 | usage, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 970 | }, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 971 | ), |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 972 | PackCommand::PutImage { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 973 | pack, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 974 | shortcode, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 975 | mxc, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 976 | body, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 977 | usage, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 978 | info, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 979 | } => edit( |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 980 | pack, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 981 | PackEdit::Put { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 982 | shortcode, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 983 | mxc, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 984 | body, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 985 | usage, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 986 | info, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 987 | }, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 988 | ), |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 989 | PackCommand::RenameImage { pack, from, to } => { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 990 | edit(pack, PackEdit::Rename { from, to }) |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 991 | } |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 992 | PackCommand::RemoveImage { pack, shortcode } => { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 993 | edit(pack, PackEdit::Remove { shortcode }) |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 994 | } |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 995 | PackCommand::SetEnabledEverywhere { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 996 | room_id, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 997 | state_key, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 998 | enabled, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 999 | } => Mutation::PackEnabledEverywhere { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1000 | room_id, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1001 | state_key, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1002 | enabled, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1003 | }, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1004 | command => { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1005 | let client = self.client.clone(); |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1006 | let mutations = self.mutations.clone(); |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1007 | let max_bytes = self.config.max_media_bytes; |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1008 | self.tasks.spawn(TaskContext::Command("pack"), async move { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1009 | reply.send(packs::run_detached(client, mutations, max_bytes, command).await); |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1010 | }); |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1011 | return; |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1012 | } |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1013 | }; |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1014 | enqueue( |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1015 | &self.mutations, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1016 | Job { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1017 | command: mutation, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1018 | reply, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1019 | }, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1020 | ); |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1021 | } |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 1022 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1023 | fn upload(&mut self, upload: crate::AttachmentUpload, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1024 | let Some(room) = self.client.get_room(upload.timeline_id.room_id()) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1025 | return reply.done(Err(Error::room_unknown())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1026 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1027 | let max_bytes = self.config.max_media_bytes; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1028 | self.spawn_command("upload", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1029 | media::queue_upload(room, upload, max_bytes).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1030 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1031 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1032 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1033 | fn route_timeline(&mut self, command: TimelineCommand, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1034 | let sender = match self.rooms.get(command.room_id()) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1035 | Some(RoomSlot::Active { sender, .. }) => Some(sender.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1036 | _ => None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1037 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1038 | match (sender, command) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1039 | (Some(sender), command) => send_actor(&sender, ActorRequest::Execute(command, reply)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1040 | ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1041 | None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1042 | TimelineCommand::MarkRead { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1043 | timeline_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1044 | event_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1045 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1046 | ) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1047 | let client = self.client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1048 | self.spawn_command("mark read", reply, async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1049 | commands::mark_read(&client, &timeline_id, event_id).await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1050 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1051 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1052 | (None, _) => reply.done(Err(Error::new(ErrorKind::RoomNotOpen, "room is not open"))), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1053 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1054 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1055 | |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1056 | fn route_repair(&mut self, command: crate::HistoryRepairCommand, reply: Responder) { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1057 | use crate::HistoryRepairCommand; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1058 | let room_id = match &command { |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 1059 | HistoryRepairCommand::Cancel { room_id } |
d9b96952ec60 | atagen | 2026-10-25 09:07 UTC | 1060 | | HistoryRepairCommand::RetryRecent { room_id } |
c9f0fd0ee1cc | atagen | 2026-10-25 06:16 UTC | 1061 | | HistoryRepairCommand::FillGap { room_id, .. } => room_id, |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1062 | }; |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1063 | match self.rooms.get(room_id) { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1064 | Some(RoomSlot::Active { sender, .. }) => { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1065 | send_actor(sender, ActorRequest::Repair(command, reply)) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1066 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1067 | Some(RoomSlot::Closing { .. }) => { |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1068 | reply.done(Err(Error::new(ErrorKind::RoomBusy, "room is closing"))) |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1069 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1070 | None => reply.done(Err(Error::new(ErrorKind::RoomNotOpen, "room is not open"))), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1071 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1072 | } |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1073 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1074 | async fn open_room(&mut self, room_id: OwnedRoomId, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1075 | match self.rooms.get_mut(&room_id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1076 | Some(RoomSlot::Active { sender, .. }) => send_actor(sender, ActorRequest::Open(reply)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1077 | Some(RoomSlot::Closing { pending, .. }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1078 | defer(pending, Deferred::Open(reply), self.config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1079 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1080 | None => self.start_actor(room_id, reply).await, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1081 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1082 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1083 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1084 | async fn close_room(&mut self, room_id: OwnedRoomId, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1085 | let (generation, sender) = match self.rooms.get_mut(&room_id) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1086 | None => return reply.done(Ok(())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1087 | Some(RoomSlot::Closing { pending, .. }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1088 | return defer( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1089 | pending, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1090 | Deferred::Close(reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1091 | self.config.command_capacity, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1092 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1093 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1094 | Some(RoomSlot::Active { generation, sender }) => (*generation, sender.clone()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1095 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1096 | match sender.try_send(ActorRequest::Close(reply)) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1097 | Ok(()) => {} |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1098 | // the actor already left on its own, which is closed enough |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1099 | Err(mpsc::error::TrySendError::Closed(request)) => request.into_reply().done(Ok(())), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1100 | Err(mpsc::error::TrySendError::Full(request)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1101 | return request.into_reply().done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1102 | ErrorKind::RoomBusy, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1103 | "room command queue is full", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1104 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1105 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1106 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1107 | self.rooms.insert( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1108 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1109 | RoomSlot::Closing { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1110 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1111 | pending: VecDeque::new(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1112 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1113 | ); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1114 | self.update_room_subscriptions(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1115 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1116 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1117 | async fn start_actor(&mut self, room_id: OwnedRoomId, reply: Responder) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1118 | let Some(room) = self.client.get_room(&room_id) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1119 | return reply.done(Err(Error::room_unknown())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1120 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1121 | self.next_generation = self.next_generation.wrapping_add(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1122 | let generation = self.next_generation; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1123 | let (sender, requests) = mpsc::channel(self.config.command_capacity); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1124 | let (supervisor, panics) = Supervisor::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1125 | let actor = RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1126 | room_id: room_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1127 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1128 | room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1129 | requests, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1130 | notes: self.note_sender.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1131 | context: SessionContext { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1132 | events: self.port.event_sender(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1133 | media: self.media.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1134 | current_user_id: self.client.user_id().map(ToOwned::to_owned), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1135 | initial_history_limit: self.config.initial_history_limit, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1136 | supervisor, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1137 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1138 | panics, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1139 | opening: vec![reply], |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1140 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1141 | self.rooms |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1142 | .insert(room_id.clone(), RoomSlot::Active { generation, sender }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1143 | self.tasks.spawn( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1144 | TaskContext::RoomActor { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1145 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1146 | generation, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1147 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1148 | actor.run(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1149 | ); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1150 | self.update_room_subscriptions(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1151 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1152 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1153 | async fn actor_note(&mut self, note: ActorNote) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1154 | let (room_id, generation) = match ¬e { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1155 | ActorNote::Leaving { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1156 | room_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1157 | generation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1158 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1159 | | ActorNote::Exited { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1160 | room_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1161 | generation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1162 | .. |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1163 | } => (room_id.clone(), *generation), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1164 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1165 | if self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1166 | .rooms |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1167 | .get(&room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1168 | .is_none_or(|slot| slot.generation() != generation) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1169 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1170 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1171 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1172 | let ActorNote::Exited { leftovers, .. } = note else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1173 | if let Some(slot @ RoomSlot::Active { .. }) = self.rooms.get_mut(&room_id) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1174 | *slot = RoomSlot::Closing { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1175 | generation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1176 | pending: VecDeque::new(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1177 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1178 | self.update_room_subscriptions(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1179 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1180 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1181 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1182 | let pending = match self.rooms.remove(&room_id) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1183 | Some(RoomSlot::Closing { pending, .. }) => pending, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1184 | _ => VecDeque::new(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1185 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1186 | // leftovers were sent before anything deferred, so they go first |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1187 | for request in leftovers { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1188 | match request { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1189 | ActorRequest::Open(reply) => self.open_room(room_id.clone(), reply).await, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1190 | ActorRequest::Close(reply) => reply.done(Ok(())), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1191 | ActorRequest::Execute(command, reply) => self.route_timeline(command, reply), |
80381e06a573 | atagen | 2026-10-25 03:02 UTC | 1192 | ActorRequest::Repair(command, reply) => self.route_repair(command, reply), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1193 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1194 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1195 | for deferred in pending { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1196 | match deferred { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1197 | Deferred::Open(reply) => self.open_room(room_id.clone(), reply).await, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1198 | Deferred::Close(reply) => self.close_room(room_id.clone(), reply).await, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1199 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1200 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1201 | self.update_room_subscriptions(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1202 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1203 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1204 | fn update_room_subscriptions(&self) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1205 | let room_ids = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1206 | .rooms |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1207 | .iter() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1208 | .filter(|(_, slot)| matches!(slot, RoomSlot::Active { .. })) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1209 | .map(|(room_id, _)| room_id.clone()) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1210 | .collect(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1211 | self.subscriptions.send_replace(room_ids); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1212 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1213 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1214 | async fn shutdown(&mut self) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1215 | if let Some(handler) = self.verification_handler.take() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1216 | self.client.remove_event_handler(handler); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1217 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1218 | self.port.close(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1219 | self.sync.stop().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1220 | if self.mentions_service.is_none() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1221 | self.mentions.stop().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1222 | } |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1223 | if self.search_service.is_none() { |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1224 | self.search.stop().await; |
c9effeac326a | atagen | 2026-10-25 10:32 UTC | 1225 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1226 | self.tasks.shutdown().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1227 | self.rooms.clear(); |
da8896938cf0 | atagen | 2026-10-25 10:04 UTC | 1228 | self.contexts.clear().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1229 | self.media.clear().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1230 | self.port |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1231 | .connection_sender() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1232 | .send_replace(ConnectionState::Stopped); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1233 | let _ = self |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1234 | .port |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1235 | .event_sender() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1236 | .send(Event::ConnectionChanged(ConnectionState::Stopped)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1237 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1238 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1239 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1240 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1241 | fn send_actor(sender: &mpsc::Sender<ActorRequest>, request: ActorRequest) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1242 | if let Err(error) = sender.try_send(request) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1243 | let (request, error) = match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1244 | mpsc::error::TrySendError::Full(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1245 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1246 | Error::new(ErrorKind::RoomBusy, "room command queue is full"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1247 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1248 | mpsc::error::TrySendError::Closed(request) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1249 | request, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1250 | Error::new(ErrorKind::RoomNotOpen, "room is not open"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1251 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1252 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1253 | request.into_reply().done(Err(error)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1254 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1255 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1256 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1257 | fn defer(pending: &mut VecDeque<Deferred>, deferred: Deferred, capacity: usize) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1258 | if pending.len() < capacity { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1259 | return pending.push_back(deferred); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1260 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1261 | let (Deferred::Open(reply) | Deferred::Close(reply)) = deferred; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1262 | reply.done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1263 | ErrorKind::RoomBusy, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1264 | "room lifecycle queue is full", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1265 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1266 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1267 | |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 1268 | async fn settled(logouts: &mut watch::Receiver<Logouts>) { |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 1269 | let _ = logouts.wait_for(|logouts| logouts.in_flight == 0).await; |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 1270 | } |
673563a76e43 | atagen | 2026-09-26 12:30 UTC | 1271 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1272 | fn enqueue<T>(sender: &mpsc::Sender<Job<T>>, job: Job<T>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1273 | if let Err(error) = sender.try_send(job) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1274 | let (job, error) = match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1275 | mpsc::error::TrySendError::Full(job) => ( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1276 | job, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1277 | Error::new(ErrorKind::QueueFull, "command queue is full"), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1278 | ), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1279 | mpsc::error::TrySendError::Closed(job) => (job, Error::interrupted()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1280 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1281 | job.reply.done(Err(error)); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1282 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1283 | } |