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, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 10 | api::client::{ |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 11 | profile::{AvatarUrl, DisplayName}, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 12 | room::{ |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 13 | Visibility as SdkVisibility, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 14 | create_room::v3::{Request as CreateRoomRequest, RoomPreset as SdkPreset}, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 15 | }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 16 | state::get_state_event_for_key, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | events::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | AnyGlobalAccountDataEventContent, GlobalAccountDataEventType, MessageLikeEventType, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 20 | StateEventType, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 21 | ignored_user_list::{IgnoredUser, IgnoredUserListEventContent}, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 22 | room::power_levels::RoomPowerLevelsEventContent, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 24 | serde::{JsonObject, Raw}, |
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 | use ruma::{OwnedRoomId, OwnedUserId}; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 28 | use serde_json::Value as JsonValue; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 29 | use tokio::sync::mpsc; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | use super::commands::{Job, guarded}; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 32 | use super::executor::TaskSet; |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 33 | use super::packs::{self, PackEdit}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | use crate::{ |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 35 | CommandOutput, CreateRoomOptions, Error, ErrorKind, ImagePackSource, PowerLevelChanges, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 36 | RoomCommand, RoomPreset, RoomVisibility, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 37 | normalise::{ENABLED_ROOMS, USER_PACK, stored_space_order}, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 38 | space_order, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | /// state changes that must not race each other. same room, same alias, same dm |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | /// partner or same account data type run in order, everything else runs at once |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | pub(super) enum Mutation { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | Room(RoomCommand), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | GlobalAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | event_type: GlobalAccountDataEventType, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | content: Raw<AnyGlobalAccountDataEventContent>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 49 | Ignore { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 50 | user_id: OwnedUserId, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 51 | ignored: bool, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 52 | }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 53 | ReorderSpaces(Vec<OwnedRoomId>), |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 54 | Pack { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 55 | pack: ImagePackSource, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 56 | edit: PackEdit, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 57 | }, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 58 | PackEnabledEverywhere { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 59 | room_id: OwnedRoomId, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 60 | state_key: String, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 61 | enabled: bool, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 62 | }, |
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 | pub(super) type MutationJob = Job<Mutation>; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | #[derive(Clone, Debug, Eq, Hash, PartialEq)] |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | enum Key { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | Room(OwnedRoomId), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | Alias(String), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | Direct(OwnedUserId), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | AccountData(String), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | Independent(u64), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | pub(super) async fn run_scheduler( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | mut jobs: mpsc::Receiver<MutationJob>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | queue_limit: usize, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | ) { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 81 | let fresh = Fresh::default(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | let mut waiting = HashMap::<Key, VecDeque<MutationJob>>::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | let mut running = HashSet::<Key>::new(); |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 84 | let mut tasks = TaskSet::<Key>::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | let mut queued = 0_usize; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | let mut independent = 0_u64; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | let mut receiving = true; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | while receiving || !tasks.is_empty() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | biased; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | Some(finished) = tasks.join_next(), if !tasks.is_empty() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | // a job's own panic is caught inside it, so this is only ever a key |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | let Ok(key) = finished else { continue }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | let next = waiting.get_mut(&key).and_then(VecDeque::pop_front); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | if waiting.get(&key).is_some_and(VecDeque::is_empty) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | waiting.remove(&key); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | match next { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | Some(job) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | queued -= 1; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 101 | spawn(&mut tasks, &client, &fresh, key, job); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | None => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | running.remove(&key); |
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 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | job = jobs.recv(), if receiving => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | let Some(job) = job else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | receiving = false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | continue; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 113 | let key = key(&job.command, client.user_id().map(ruma::UserId::server_name)).unwrap_or_else(|| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | independent += 1; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | Key::Independent(independent) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | if running.insert(key.clone()) { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 118 | spawn(&mut tasks, &client, &fresh, key, job); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | } else if queued < queue_limit { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | waiting.entry(key).or_default().push_back(job); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | queued += 1; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | } else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | job.reply.done(Err(Error::new( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | ErrorKind::QueueFull, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | "room command queue is full", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | ))); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 133 | /// `server` is ours, since a new alias lives there |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 134 | fn key(mutation: &Mutation, server: Option<&ruma::ServerName>) -> Option<Key> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | let room_id = match mutation { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | Mutation::GlobalAccountData { event_type, .. } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | return Some(Key::AccountData(event_type.to_string())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 139 | Mutation::Ignore { .. } => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 140 | return Some(Key::AccountData( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 141 | GlobalAccountDataEventType::IgnoredUserList.to_string(), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 142 | )); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 143 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 144 | Mutation::ReorderSpaces(_) => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 145 | return Some(Key::AccountData(space_order::EVENT_TYPE.to_owned())); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 146 | } |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 147 | Mutation::Pack { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 148 | pack: ImagePackSource::User, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 149 | .. |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 150 | } => return Some(Key::AccountData(USER_PACK.to_owned())), |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 151 | Mutation::Pack { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 152 | pack: ImagePackSource::Room { room_id, .. }, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 153 | .. |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 154 | } => room_id, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 155 | Mutation::PackEnabledEverywhere { .. } => { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 156 | return Some(Key::AccountData(ENABLED_ROOMS.to_owned())); |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 157 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 158 | Mutation::Room(RoomCommand::Join { room, .. }) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | return Some(match OwnedRoomId::try_from(room.clone()) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | Ok(room_id) => Key::Room(room_id), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | Err(alias) => Key::Alias(alias.to_string()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 163 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | Mutation::Room(RoomCommand::CreateDirect(user_id)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 165 | return Some(Key::Direct(user_id.clone())); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 166 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 167 | Mutation::Room(RoomCommand::Create(options)) => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 168 | return match (&options.alias_localpart, options.invite.as_slice()) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 169 | (Some(localpart), _) => Some(Key::Alias(format!("#{localpart}:{}", server?))), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 170 | (None, [user_id]) if options.direct => Some(Key::Direct(user_id.clone())), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 171 | _ => None, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 172 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 173 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | Mutation::Room( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | RoomCommand::Leave(room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | | RoomCommand::Forget(room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | | RoomCommand::AcceptInvite(room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 178 | | RoomCommand::RejectInvite(room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 179 | | RoomCommand::InviteUser { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | | RoomCommand::KickUser { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 181 | | RoomCommand::BanUser { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | | RoomCommand::UnbanUser { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 183 | | RoomCommand::SetPowerLevels { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 184 | | RoomCommand::SetUserPowerLevel { room_id, .. } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 185 | | RoomCommand::MuteUser { room_id, .. } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 186 | | RoomCommand::SetFavourite { room_id, .. } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 187 | | RoomCommand::SetOwnDisplayName { room_id, .. } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 188 | | RoomCommand::SetOwnAvatar { room_id, .. } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | | RoomCommand::SetAccountData { room_id, .. }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | ) => room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | Mutation::Room(_) => return None, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | Some(Key::Room(room_id.clone())) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 196 | /// 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 | 197 | /// only knows `m.direct` once it comes back round |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 198 | type FreshDms = Arc<std::sync::Mutex<HashMap<OwnedUserId, OwnedRoomId>>>; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 199 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 200 | /// space orders written here that sync hasn't echoed into the store yet |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 201 | type FreshOrders = Arc<std::sync::Mutex<HashMap<OwnedRoomId, String>>>; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 202 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 203 | #[derive(Clone, Default)] |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 204 | struct Fresh { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 205 | dms: FreshDms, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 206 | orders: FreshOrders, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 207 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 208 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 209 | fn spawn(tasks: &mut TaskSet<Key>, client: &Client, fresh: &Fresh, key: Key, job: MutationJob) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | let client = client.clone(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 211 | let fresh = fresh.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | tasks.spawn(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | let Job { command, reply } = job; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 214 | reply.send(guarded("room command", execute(&client, &fresh, command)).await); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | key |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 219 | async fn execute( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 220 | client: &Client, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 221 | fresh: &Fresh, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 222 | mutation: Mutation, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 223 | ) -> Result<CommandOutput, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | let command = match mutation { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 225 | Mutation::GlobalAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | .account() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | .set_account_data_raw(event_type, content) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | .map_err(|source| Error::from_sdk("writing account data failed", source))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | return Ok(CommandOutput::Done); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 236 | Mutation::Ignore { user_id, ignored } => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 237 | return set_ignored(client, user_id, ignored).await.map(done); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 238 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 239 | Mutation::ReorderSpaces(order) => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 240 | return reorder_spaces(client, &fresh.orders, order).await.map(done); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 241 | } |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 242 | Mutation::Pack { pack, edit } => return packs::edit_pack(client, &pack, edit).await, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 243 | Mutation::PackEnabledEverywhere { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 244 | room_id, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 245 | state_key, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 246 | enabled, |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 247 | } => { |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 248 | return packs::set_enabled_everywhere(client, room_id, state_key, enabled) |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 249 | .await |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 250 | .map(done); |
fa51490b6ae9 | atagen | 2026-10-24 08:41 UTC | 251 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | Mutation::Room(command) => command, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | }; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 254 | let fresh_dms = &fresh.dms; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | let room = |room_id: &OwnedRoomId| client.get_room(room_id).ok_or_else(Error::room_unknown); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | let joined = |room: Room| CommandOutput::Room(room.room_id().to_owned()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | let sdk = |message: &'static str| move |source| Error::from_sdk(message, source); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 258 | match command { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 259 | RoomCommand::Join { room, via } => client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | .join_room_by_id_or_alias(&room, &via) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | .map(joined) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | .map_err(sdk("joining room failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | RoomCommand::Leave(room_id) => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | .leave() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | .map_err(sdk("leaving room failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | RoomCommand::Forget(room_id) => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | .forget() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | .map_err(sdk("forgetting room failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | RoomCommand::AcceptInvite(room_id) => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 275 | .join() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 276 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 277 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 278 | .map_err(sdk("accepting invite failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | RoomCommand::RejectInvite(room_id) => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 280 | .leave() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 281 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 282 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 283 | .map_err(sdk("rejecting invite failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 284 | RoomCommand::InviteUser { room_id, user_id } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 285 | .invite_user_by_id(&user_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 286 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 287 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 288 | .map_err(sdk("inviting user failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 289 | RoomCommand::KickUser { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 291 | user_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 292 | reason, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 293 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 294 | .kick_user(&user_id, reason.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 295 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 296 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 297 | .map_err(sdk("kicking user failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | RoomCommand::BanUser { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | user_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | reason, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | .ban_user(&user_id, reason.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 304 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | .map_err(sdk("banning user failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | RoomCommand::UnbanUser { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | user_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | reason, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | .unban_user(&user_id, reason.as_deref()) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | .map_err(sdk("unbanning user failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | RoomCommand::SetPowerLevels { room_id, changes } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | apply_power_level_changes(&room(&room_id)?, changes) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | RoomCommand::SetUserPowerLevel { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | user_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | level, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | .update_power_levels(vec![(&user_id, power_level(level)?)]) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | .map_err(sdk("updating user power level failed")), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 330 | RoomCommand::MuteUser { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 331 | room_id, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 332 | user_id, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 333 | muted, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 334 | } => mute_user(client, &room(&room_id)?, &user_id, muted) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 335 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 336 | .map(done), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 337 | RoomCommand::SetFavourite { room_id, favourite } => room(&room_id)? |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 338 | .set_is_favourite(favourite, None) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 339 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 340 | .map(done) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 341 | .map_err(sdk("setting favourite failed")), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 342 | RoomCommand::SetOwnDisplayName { room_id, name } => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 343 | set_own_member_field(client, &room(&room_id)?, ProfileField::DisplayName, name) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 344 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 345 | .map(done) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 346 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 347 | RoomCommand::SetOwnAvatar { room_id, avatar } => set_own_member_field( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 348 | client, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 349 | &room(&room_id)?, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 350 | ProfileField::Avatar, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 351 | avatar.map(String::from), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 352 | ) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 353 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 354 | .map(done), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 355 | RoomCommand::SetAccountData { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 356 | room_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | event_type, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | content, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 359 | } => room(&room_id)? |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | .set_account_data_raw(event_type, content) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | .map(done) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | .map_err(sdk("writing room account data failed")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | RoomCommand::Create(options) => client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | .create_room(create_room_request(options)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | .map(joined) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | .map_err(sdk("creating room failed")), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 369 | RoomCommand::CreateDirect(user_id) => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 370 | if let Some(existing) = client.get_dm_room(&user_id) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 371 | return Ok(joined(existing)); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 372 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 373 | let fresh = fresh_dms |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 374 | .lock() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 375 | .expect("nothing panics holding this") |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 376 | .get(&user_id) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 377 | .cloned(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 378 | if let Some(room_id) = fresh.filter(|room_id| client.get_room(room_id).is_some()) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 379 | return Ok(CommandOutput::Room(room_id)); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 380 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 381 | let room = client |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | .create_dm(&user_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | .await |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 384 | .map_err(sdk("creating direct room failed"))?; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 385 | fresh_dms |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 386 | .lock() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 387 | .expect("nothing panics holding this") |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 388 | .insert(user_id, room.room_id().to_owned()); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 389 | Ok(joined(room)) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 390 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | _ => Err(Error::new(ErrorKind::Other, "room command was misrouted")), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 394 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 395 | /// one state event carrying every requested change, since the sdk's own patcher is private |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 396 | async fn apply_power_level_changes(room: &Room, changes: PowerLevelChanges) -> Result<(), Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 397 | let mut levels = room |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | .power_levels() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | .map_err(|source| Error::from_sdk("reading power levels failed", source.into()))?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | for (target, value) in [ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 402 | (&mut levels.ban, changes.ban), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 403 | (&mut levels.invite, changes.invite), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 404 | (&mut levels.kick, changes.kick), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 405 | (&mut levels.redact, changes.redact), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 406 | (&mut levels.events_default, changes.events_default), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | (&mut levels.state_default, changes.state_default), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | (&mut levels.users_default, changes.users_default), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 409 | (&mut levels.notifications.room, changes.room_notification), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 410 | ] { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 411 | if let Some(value) = value { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | *target = power_level(value)?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 413 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 415 | for (event_type, value) in [ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | (StateEventType::RoomName.into(), changes.room_name), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 417 | (StateEventType::RoomAvatar.into(), changes.room_avatar), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 418 | (StateEventType::RoomTopic.into(), changes.room_topic), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 419 | (StateEventType::SpaceChild.into(), changes.space_child), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 420 | (MessageLikeEventType::Beacon.into(), changes.beacon), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 421 | (StateEventType::BeaconInfo.into(), changes.beacon_info), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 422 | ] { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | if let Some(value) = value { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | levels.events.insert(event_type, power_level(value)?); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 425 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 426 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 427 | let content = RoomPowerLevelsEventContent::try_from(levels).map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 428 | Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 430 | "normalising power levels failed", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 431 | source, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 432 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 433 | })?; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 434 | room.send_state_event(content) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 435 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 436 | .map(drop) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 437 | .map_err(|source| Error::from_sdk("updating power levels failed", source)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 438 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 439 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 440 | /// reads the list from the server, since the store only catches up on the next sync |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 441 | async fn set_ignored(client: &Client, user_id: OwnedUserId, ignored: bool) -> Result<(), Error> { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 442 | if ignored && client.user_id() == Some(&*user_id) { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 443 | return Err(Error::new(ErrorKind::Invalid, "you can't ignore yourself")); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 444 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 445 | let account = client.account(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 446 | let mut list = account |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 447 | .fetch_account_data_static::<IgnoredUserListEventContent>() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 448 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 449 | .map_err(|source| Error::from_sdk("reading the ignore list failed", source))? |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 450 | .map(|raw| raw.deserialize()) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 451 | .transpose() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 452 | .map_err(|source| { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 453 | Error::with_source(ErrorKind::Invalid, "the ignore list is malformed", source) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 454 | })? |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 455 | .unwrap_or_default(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 456 | let changed = if ignored { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 457 | list.ignored_users |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 458 | .insert(user_id, IgnoredUser::new()) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 459 | .is_none() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 460 | } else { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 461 | list.ignored_users.remove(&user_id).is_some() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 462 | }; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 463 | if !changed { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 464 | return Ok(()); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 465 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 466 | account |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 467 | .set_account_data(list) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 468 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 469 | .map(drop) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 470 | .map_err(|source| Error::from_sdk("writing the ignore list failed", source)) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 471 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 472 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 473 | async fn reorder_spaces( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 474 | client: &Client, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 475 | fresh: &FreshOrders, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 476 | order: Vec<OwnedRoomId>, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 477 | ) -> Result<(), Error> { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 478 | if order.iter().collect::<HashSet<_>>().len() != order.len() { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 479 | return Err(Error::new(ErrorKind::Invalid, "a space is listed twice")); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 480 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 481 | let mut rooms = HashMap::new(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 482 | let mut spaces = Vec::with_capacity(order.len()); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 483 | for room_id in order { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 484 | let room = client.get_room(&room_id).ok_or_else(Error::room_unknown)?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 485 | let stored = stored_space_order(&room).await; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 486 | let current = { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 487 | let mut fresh = fresh.lock().expect("nothing panics holding this"); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 488 | match fresh.get(&room_id) { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 489 | Some(written) if stored.as_ref() != Some(written) => Some(written.clone()), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 490 | Some(_) => fresh.remove(&room_id), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 491 | None => stored, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 492 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 493 | }; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 494 | rooms.insert(room_id.clone(), room); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 495 | spaces.push((room_id, current)); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 496 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 497 | for (room_id, order) in space_order::plan(&spaces) { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 498 | let content = Raw::new(&serde_json::json!({ "order": order })) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 499 | .expect("a string map always serialises") |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 500 | .cast_unchecked(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 501 | rooms[&room_id] |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 502 | .set_account_data_raw(space_order::EVENT_TYPE.into(), content) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 503 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 504 | .map_err(|source| Error::from_sdk("writing a space order failed", source))?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 505 | fresh |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 506 | .lock() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 507 | .expect("nothing panics holding this") |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 508 | .insert(room_id, order); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 509 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 510 | Ok(()) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 511 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 512 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 513 | /// below the cheapest of the levels a message could need, so neither plain nor encrypted |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 514 | /// sends get through |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 515 | async fn mute_user( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 516 | client: &Client, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 517 | room: &Room, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 518 | user_id: &OwnedUserId, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 519 | muted: bool, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 520 | ) -> Result<(), Error> { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 521 | let own_user_id = client |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 522 | .user_id() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 523 | .ok_or_else(|| Error::new(ErrorKind::Unauthenticated, "the client is not logged in"))?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 524 | let mut levels = room |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 525 | .power_levels() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 526 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 527 | .map_err(|source| Error::from_sdk("reading power levels failed", source.into()))?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 528 | if !levels.user_can_change_user_power_level(own_user_id, user_id) { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 529 | return Err(Error::new( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 530 | ErrorKind::Forbidden, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 531 | "you can't change this user's power level", |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 532 | )); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 533 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 534 | let speaking = [ |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 535 | levels.events_default, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 536 | levels.for_message(MessageLikeEventType::RoomMessage), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 537 | levels.for_message(MessageLikeEventType::RoomEncrypted), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 538 | ] |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 539 | .into_iter() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 540 | .min() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 541 | .expect("the list isn't empty"); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 542 | let current = levels |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 543 | .users |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 544 | .get(user_id) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 545 | .copied() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 546 | .unwrap_or(levels.users_default); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 547 | match (muted, current < speaking) { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 548 | (true, true) | (false, false) => return Ok(()), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 549 | (true, false) => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 550 | let level = speaking.checked_sub(Int::from(1)).ok_or_else(|| { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 551 | Error::new( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 552 | ErrorKind::Invalid, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 553 | "there's no power level below this room's lowest", |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 554 | ) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 555 | })?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 556 | levels.users.insert(user_id.clone(), level); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 557 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 558 | (false, true) => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 559 | levels.users.remove(user_id); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 560 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 561 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 562 | let content = RoomPowerLevelsEventContent::try_from(levels).map_err(|source| { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 563 | Error::with_source( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 564 | ErrorKind::Invalid, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 565 | "normalising power levels failed", |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 566 | source, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 567 | ) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 568 | })?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 569 | room.send_state_event(content) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 570 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 571 | .map(drop) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 572 | .map_err(|source| Error::from_sdk("updating power levels failed", source)) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 573 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 574 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 575 | #[derive(Clone, Copy)] |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 576 | enum ProfileField { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 577 | DisplayName, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 578 | Avatar, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 579 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 580 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 581 | /// rewrites your member event from the server's copy so membership and unknown keys survive, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 582 | /// and an earlier change this sync hasn't delivered yet isn't lost |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 583 | async fn set_own_member_field( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 584 | client: &Client, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 585 | room: &Room, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 586 | field: ProfileField, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 587 | value: Option<String>, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 588 | ) -> Result<(), Error> { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 589 | let own_user_id = client |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 590 | .user_id() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 591 | .ok_or_else(|| Error::new(ErrorKind::Unauthenticated, "the client is not logged in"))? |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 592 | .to_owned(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 593 | let key = match field { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 594 | ProfileField::DisplayName => "displayname", |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 595 | ProfileField::Avatar => "avatar_url", |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 596 | }; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 597 | let value = match value { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 598 | Some(value) => Some(value), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 599 | None => { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 600 | let profile = client |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 601 | .account() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 602 | .fetch_user_profile() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 603 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 604 | .map_err(|source| Error::from_sdk("fetching the profile failed", source))?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 605 | let global = match field { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 606 | ProfileField::DisplayName => profile.get_static::<DisplayName>(), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 607 | ProfileField::Avatar => profile |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 608 | .get_static::<AvatarUrl>() |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 609 | .map(|avatar| avatar.map(String::from)), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 610 | }; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 611 | global.map_err(|source| { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 612 | Error::with_source( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 613 | ErrorKind::Server, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 614 | "the server sent a malformed profile", |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 615 | source, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 616 | ) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 617 | })? |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 618 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 619 | }; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 620 | let request = get_state_event_for_key::v3::Request::new( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 621 | room.room_id().to_owned(), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 622 | StateEventType::RoomMember, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 623 | own_user_id.to_string(), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 624 | ); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 625 | let response = client |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 626 | .send(request) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 627 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 628 | .map_err(|source| Error::from_http("reading your member event failed", source))?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 629 | let mut content: JsonObject = |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 630 | serde_json::from_str(response.event_or_content.get()).map_err(|source| { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 631 | Error::with_source(ErrorKind::Invalid, "your member event is malformed", source) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 632 | })?; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 633 | match value { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 634 | Some(value) => content.insert(key.to_owned(), JsonValue::String(value)), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 635 | None => content.remove(key), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 636 | }; |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 637 | room.send_state_event_raw( |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 638 | "m.room.member", |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 639 | own_user_id.as_str(), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 640 | JsonValue::Object(content), |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 641 | ) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 642 | .await |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 643 | .map(drop) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 644 | .map_err(|source| Error::from_sdk("updating your member event failed", source)) |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 645 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 646 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 647 | fn done<T>(_: T) -> CommandOutput { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 648 | CommandOutput::Done |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 649 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 650 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 651 | fn power_level(value: i64) -> Result<Int, Error> { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 652 | value.try_into().map_err(|source| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 653 | Error::with_source( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 654 | ErrorKind::Invalid, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 655 | "power level is outside the Matrix integer range", |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 656 | source, |
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 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 660 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 661 | fn create_room_request(options: CreateRoomOptions) -> CreateRoomRequest { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 662 | let mut request = CreateRoomRequest::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 663 | request.name = options.name; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 664 | request.topic = options.topic; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 665 | request.room_alias_name = options.alias_localpart; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 666 | request.invite = options.invite; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 667 | request.is_direct = options.direct; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 668 | request.visibility = match options.visibility { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 669 | RoomVisibility::Private => SdkVisibility::Private, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 670 | RoomVisibility::Public => SdkVisibility::Public, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 671 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 672 | request.preset = options.preset.map(|preset| match preset { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 673 | RoomPreset::PrivateChat => SdkPreset::PrivateChat, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 674 | RoomPreset::TrustedPrivateChat => SdkPreset::TrustedPrivateChat, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 675 | RoomPreset::PublicChat => SdkPreset::PublicChat, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 676 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 677 | request |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 678 | } |