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