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