5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 1 | use std::{ |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 2 | collections::{HashMap, HashSet, VecDeque}, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 3 | sync::Arc, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 4 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | use matrix_sdk::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | Client, Room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | ruma::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | Int, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | api::client::room::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | Visibility as SdkVisibility, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | create_room::v3::{Request as CreateRoomRequest, RoomPreset as SdkPreset}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | events::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | AnyGlobalAccountDataEventContent, GlobalAccountDataEventType, MessageLikeEventType, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | StateEventType, room::power_levels::RoomPowerLevelsEventContent, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | serde::Raw, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | use ruma::{OwnedRoomId, OwnedUserId}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | use tokio::{sync::mpsc, task::JoinSet}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | use super::commands::{Job, guarded}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | use crate::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | CommandOutput, CreateRoomOptions, Error, ErrorKind, PowerLevelChanges, RoomCommand, RoomPreset, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | RoomVisibility, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | /// state changes that must not race each other. same room, same alias, same dm |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | /// partner or same account data type run in order, everything else runs at once |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | pub(super) enum Mutation { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | Room(RoomCommand), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | GlobalAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | event_type: GlobalAccountDataEventType, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | content: Raw<AnyGlobalAccountDataEventContent>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | pub(super) type MutationJob = Job<Mutation>; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | enum Key { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | Room(OwnedRoomId), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | Alias(String), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | Direct(OwnedUserId), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | AccountData(String), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | Independent(u64), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | pub(super) async fn run_scheduler( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | mut jobs: mpsc::Receiver<MutationJob>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | queue_limit: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | ) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 56 | let fresh_dms = FreshDms::default(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | let mut waiting = HashMap::<Key, VecDeque<MutationJob>>::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | let mut running = HashSet::<Key>::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | let mut tasks = JoinSet::<Key>::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | let mut queued = 0_usize; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | let mut independent = 0_u64; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | let mut receiving = true; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | while receiving || !tasks.is_empty() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | biased; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | Some(finished) = tasks.join_next(), if !tasks.is_empty() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | // a job's own panic is caught inside it, so this is only ever a key |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | let Ok(key) = finished else { continue }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | let next = waiting.get_mut(&key).and_then(VecDeque::pop_front); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | if waiting.get(&key).is_some_and(VecDeque::is_empty) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | waiting.remove(&key); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | match next { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | Some(job) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | queued -= 1; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 76 | spawn(&mut tasks, &client, &fresh_dms, key, job); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | None => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | running.remove(&key); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | job = jobs.recv(), if receiving => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | let Some(job) = job else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | receiving = false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | continue; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 88 | let key = key(&job.command, client.user_id().map(ruma::UserId::server_name)).unwrap_or_else(|| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | independent += 1; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | Key::Independent(independent) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | if running.insert(key.clone()) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 93 | spawn(&mut tasks, &client, &fresh_dms, key, job); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | } else if queued < queue_limit { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | waiting.entry(key).or_default().push_back(job); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | queued += 1; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | } else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | job.reply.done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | ErrorKind::QueueFull, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | "room command queue is full", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 108 | /// `server` is ours, since a new alias lives there |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 109 | fn key(mutation: &Mutation, server: Option<&ruma::ServerName>) -> Option<Key> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | let room_id = match mutation { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | Mutation::GlobalAccountData { event_type, .. } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | return Some(Key::AccountData(event_type.to_string())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | Mutation::Room(RoomCommand::Join { room, .. }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | return Some(match OwnedRoomId::try_from(room.clone()) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | Ok(room_id) => Key::Room(room_id), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | Err(alias) => Key::Alias(alias.to_string()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | Mutation::Room(RoomCommand::CreateDirect(user_id)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | return Some(Key::Direct(user_id.clone())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 123 | Mutation::Room(RoomCommand::Create(options)) => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 124 | return match (&options.alias_localpart, options.invite.as_slice()) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 125 | (Some(localpart), _) => Some(Key::Alias(format!("#{localpart}:{}", server?))), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 126 | (None, [user_id]) if options.direct => Some(Key::Direct(user_id.clone())), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 127 | _ => None, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 128 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 129 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | Mutation::Room( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | RoomCommand::Leave(room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | | RoomCommand::Forget(room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | | RoomCommand::AcceptInvite(room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | | RoomCommand::RejectInvite(room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | | RoomCommand::InviteUser { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | | RoomCommand::KickUser { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | | RoomCommand::BanUser { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | | RoomCommand::UnbanUser { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | | RoomCommand::SetPowerLevels { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | | RoomCommand::SetUserPowerLevel { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | | RoomCommand::SetAccountData { room_id, .. }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | ) => room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | Mutation::Room(_) => return None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 145 | Some(Key::Room(room_id.clone())) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 146 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 148 | /// dms made here that the next sync hasn't told the sdk about yet, since `get_dm_room` |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 149 | /// only knows `m.direct` once it comes back round |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 150 | type FreshDms = Arc<std::sync::Mutex<HashMap<OwnedUserId, OwnedRoomId>>>; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 151 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 152 | fn spawn( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 153 | tasks: &mut JoinSet<Key>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 154 | client: &Client, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 155 | fresh_dms: &FreshDms, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 156 | key: Key, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 157 | job: MutationJob, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 158 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | let client = client.clone(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 160 | let fresh_dms = fresh_dms.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | tasks.spawn(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | let Job { command, reply } = job; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 163 | reply.send(guarded("room command", execute(&client, &fresh_dms, command)).await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | key |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 165 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 166 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 167 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 168 | async fn execute( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 169 | client: &Client, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 170 | fresh_dms: &FreshDms, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 171 | mutation: Mutation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 172 | ) -> Result<CommandOutput, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 173 | let command = match mutation { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | Mutation::GlobalAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 178 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 179 | .account() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | .set_account_data_raw(event_type, content) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 181 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | .map_err(|source| Error::from_sdk("writing account data failed", source))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 183 | return Ok(CommandOutput::Done); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 184 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 185 | Mutation::Room(command) => command, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | let room = |room_id: &OwnedRoomId| client.get_room(room_id).ok_or_else(Error::room_unknown); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | let joined = |room: Room| CommandOutput::Room(room.room_id().to_owned()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | let sdk = |message: &'static str| move |source| Error::from_sdk(message, source); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | RoomCommand::Join { room, via } => client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | .join_room_by_id_or_alias(&room, &via) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | .map(joined) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | .map_err(sdk("joining room failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | RoomCommand::Leave(room_id) => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | .leave() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | .map_err(sdk("leaving room failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 201 | RoomCommand::Forget(room_id) => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | .forget() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 204 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 205 | .map_err(sdk("forgetting room failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 206 | RoomCommand::AcceptInvite(room_id) => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 207 | .join() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | .map_err(sdk("accepting invite failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | RoomCommand::RejectInvite(room_id) => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | .leave() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | .map_err(sdk("rejecting invite failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | RoomCommand::InviteUser { room_id, user_id } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | .invite_user_by_id(&user_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 219 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | .map_err(sdk("inviting user failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 221 | RoomCommand::KickUser { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 223 | user_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | reason, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 225 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | .kick_user(&user_id, reason.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | .map_err(sdk("kicking user failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | RoomCommand::BanUser { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | user_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | reason, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | .ban_user(&user_id, reason.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | .map_err(sdk("banning user failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | RoomCommand::UnbanUser { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | user_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | reason, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | .unban_user(&user_id, reason.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | .map_err(sdk("unbanning user failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | RoomCommand::SetPowerLevels { room_id, changes } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | apply_power_level_changes(&room(&room_id)?, changes) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | RoomCommand::SetUserPowerLevel { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | user_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | level, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 258 | .update_power_levels(vec![(&user_id, power_level(level)?)]) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 259 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | .map_err(sdk("updating user power level failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | RoomCommand::SetAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | .set_account_data_raw(event_type, content) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | .map_err(sdk("writing room account data failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | RoomCommand::Create(options) => client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | .create_room(create_room_request(options)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | .map(joined) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 275 | .map_err(sdk("creating room failed")), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 276 | RoomCommand::CreateDirect(user_id) => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 277 | if let Some(existing) = client.get_dm_room(&user_id) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 278 | return Ok(joined(existing)); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 279 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 280 | let fresh = fresh_dms |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 281 | .lock() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 282 | .expect("nothing panics holding this") |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 283 | .get(&user_id) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 284 | .cloned(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 285 | if let Some(room_id) = fresh.filter(|room_id| client.get_room(room_id).is_some()) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 286 | return Ok(CommandOutput::Room(room_id)); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 287 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 288 | let room = client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 289 | .create_dm(&user_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | .await |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 291 | .map_err(sdk("creating direct room failed"))?; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 292 | fresh_dms |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 293 | .lock() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 294 | .expect("nothing panics holding this") |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 295 | .insert(user_id, room.room_id().to_owned()); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 296 | Ok(joined(room)) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 297 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | _ => Err(Error::new(ErrorKind::Other, "room command was misrouted")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | /// one state event carrying every requested change, since the sdk's own patcher is private |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | async fn apply_power_level_changes(room: &Room, changes: PowerLevelChanges) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 304 | let mut levels = room |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | .power_levels() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | .map_err(|source| Error::from_sdk("reading power levels failed", source.into()))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | for (target, value) in [ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | (&mut levels.ban, changes.ban), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | (&mut levels.invite, changes.invite), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | (&mut levels.kick, changes.kick), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | (&mut levels.redact, changes.redact), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | (&mut levels.events_default, changes.events_default), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | (&mut levels.state_default, changes.state_default), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | (&mut levels.users_default, changes.users_default), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | (&mut levels.notifications.room, changes.room_notification), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | ] { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | if let Some(value) = value { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | *target = power_level(value)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | for (event_type, value) in [ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | (StateEventType::RoomName.into(), changes.room_name), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | (StateEventType::RoomAvatar.into(), changes.room_avatar), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | (StateEventType::RoomTopic.into(), changes.room_topic), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | (StateEventType::SpaceChild.into(), changes.space_child), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | (MessageLikeEventType::Beacon.into(), changes.beacon), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | (StateEventType::BeaconInfo.into(), changes.beacon_info), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | ] { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | if let Some(value) = value { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | levels.events.insert(event_type, power_level(value)?); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | let content = RoomPowerLevelsEventContent::try_from(levels).map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | "normalising power levels failed", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | source, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | room.send_state_event(content) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | .map(drop) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | .map_err(|source| Error::from_sdk("updating power levels failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | fn done<T>(_: T) -> CommandOutput { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 348 | CommandOutput::Done |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | fn power_level(value: i64) -> Result<Int, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | value.try_into().map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 353 | Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 354 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 355 | "power level is outside the Matrix integer range", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 356 | source, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 359 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | fn create_room_request(options: CreateRoomOptions) -> CreateRoomRequest { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | let mut request = CreateRoomRequest::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | request.name = options.name; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | request.topic = options.topic; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | request.room_alias_name = options.alias_localpart; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | request.invite = options.invite; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | request.is_direct = options.direct; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | request.visibility = match options.visibility { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | RoomVisibility::Private => SdkVisibility::Private, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | RoomVisibility::Public => SdkVisibility::Public, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | request.preset = options.preset.map(|preset| match preset { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | RoomPreset::PrivateChat => SdkPreset::PrivateChat, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | RoomPreset::TrustedPrivateChat => SdkPreset::TrustedPrivateChat, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | RoomPreset::PublicChat => SdkPreset::PublicChat, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | request |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | } |