Commit 93eff79f79

93eff79f790c4e405096a49aad120d2240fd83bd

parent: 11625262f1

Unregistered key

Ulyssa <git@ulyssa.dev> · 2023-03-04 20:23 UTC

Support creating new rooms and spaces (#40)

Layout: unified · split

Cargo.lock +2 −2
@@ -1882,9 +1882,9 @@ dependencies = [
1882 1882
1883[[package]] 1883[[package]]
1884name = "modalkit" 1884name = "modalkit"
1885version = "0.0.12" 1885version = "0.0.13"
1886source = "registry+https://github.com/rust-lang/crates.io-index" 1886source = "registry+https://github.com/rust-lang/crates.io-index"
1887checksum = "9a4e5226a5d33a7bdf5b4fc067baa211c94f21aa6667af5eec8b357a8af5e4ba" 1887checksum = "038bb42efcd659fb123708bf8e4ea12ca9023f07d44514f9358b9334ea7ba80f"
1888dependencies = [ 1888dependencies = [
1889 "anymap2", 1889 "anymap2",
1890 "arboard", 1890 "arboard",
Cargo.toml +1 −1
@@ -39,7 +39,7 @@ unicode-width = "0.1.10"
39url = {version = "^2.2.2", features = ["serde"]} 39url = {version = "^2.2.2", features = ["serde"]}
40 40
41[dependencies.modalkit] 41[dependencies.modalkit]
42version = "0.0.12" 42version = "0.0.13"
43 43
44[dependencies.matrix-sdk] 44[dependencies.matrix-sdk]
45version = "0.6" 45version = "0.6"
src/base.rs +22
@@ -135,6 +135,12 @@ pub enum MessageAction {
135pub enum CreateRoomType { 135pub enum CreateRoomType {
136 /// A direct message room. 136 /// A direct message room.
137 Direct(OwnedUserId), 137 Direct(OwnedUserId),
138
139 /// A standard chat room.
140 Room,
141
142 /// A Matrix space.
143 Space,
138} 144}
139 145
140bitflags::bitflags! { 146bitflags::bitflags! {
@@ -184,8 +190,14 @@ pub enum SendAction {
184 Upload(String), 190 Upload(String),
185} 191}
186 192
193#[derive(Clone, Debug, Eq, PartialEq)]
194pub enum HomeserverAction {
195 CreateRoom(Option<String>, CreateRoomType, CreateRoomFlags),
196}
197
187#[derive(Clone, Debug, Eq, PartialEq)] 198#[derive(Clone, Debug, Eq, PartialEq)]
188pub enum IambAction { 199pub enum IambAction {
200 Homeserver(HomeserverAction),
189 Message(MessageAction), 201 Message(MessageAction),
190 Room(RoomAction), 202 Room(RoomAction),
191 Send(SendAction), 203 Send(SendAction),
@@ -194,6 +206,12 @@ pub enum IambAction {
194 ToggleScrollbackFocus, 206 ToggleScrollbackFocus,
195} 207}
196 208
209impl From<HomeserverAction> for IambAction {
210 fn from(act: HomeserverAction) -> Self {
211 IambAction::Homeserver(act)
212 }
213}
214
197impl From<MessageAction> for IambAction { 215impl From<MessageAction> for IambAction {
198 fn from(act: MessageAction) -> Self { 216 fn from(act: MessageAction) -> Self {
199 IambAction::Message(act) 217 IambAction::Message(act)
@@ -215,6 +233,7 @@ impl From<SendAction> for IambAction {
215impl ApplicationAction for IambAction { 233impl ApplicationAction for IambAction {
216 fn is_edit_sequence<C: EditContext>(&self, _: &C) -> SequenceStatus { 234 fn is_edit_sequence<C: EditContext>(&self, _: &C) -> SequenceStatus {
217 match self { 235 match self {
236 IambAction::Homeserver(..) => SequenceStatus::Break,
218 IambAction::Message(..) => SequenceStatus::Break, 237 IambAction::Message(..) => SequenceStatus::Break,
219 IambAction::Room(..) => SequenceStatus::Break, 238 IambAction::Room(..) => SequenceStatus::Break,
220 IambAction::Send(..) => SequenceStatus::Break, 239 IambAction::Send(..) => SequenceStatus::Break,
@@ -226,6 +245,7 @@ impl ApplicationAction for IambAction {
226 245
227 fn is_last_action<C: EditContext>(&self, _: &C) -> SequenceStatus { 246 fn is_last_action<C: EditContext>(&self, _: &C) -> SequenceStatus {
228 match self { 247 match self {
248 IambAction::Homeserver(..) => SequenceStatus::Atom,
229 IambAction::Message(..) => SequenceStatus::Atom, 249 IambAction::Message(..) => SequenceStatus::Atom,
230 IambAction::Room(..) => SequenceStatus::Atom, 250 IambAction::Room(..) => SequenceStatus::Atom,
231 IambAction::Send(..) => SequenceStatus::Atom, 251 IambAction::Send(..) => SequenceStatus::Atom,
@@ -237,6 +257,7 @@ impl ApplicationAction for IambAction {
237 257
238 fn is_last_selection<C: EditContext>(&self, _: &C) -> SequenceStatus { 258 fn is_last_selection<C: EditContext>(&self, _: &C) -> SequenceStatus {
239 match self { 259 match self {
260 IambAction::Homeserver(..) => SequenceStatus::Ignore,
240 IambAction::Message(..) => SequenceStatus::Ignore, 261 IambAction::Message(..) => SequenceStatus::Ignore,
241 IambAction::Room(..) => SequenceStatus::Ignore, 262 IambAction::Room(..) => SequenceStatus::Ignore,
242 IambAction::Send(..) => SequenceStatus::Ignore, 263 IambAction::Send(..) => SequenceStatus::Ignore,
@@ -248,6 +269,7 @@ impl ApplicationAction for IambAction {
248 269
249 fn is_switchable<C: EditContext>(&self, _: &C) -> bool { 270 fn is_switchable<C: EditContext>(&self, _: &C) -> bool {
250 match self { 271 match self {
272 IambAction::Homeserver(..) => false,
251 IambAction::Message(..) => false, 273 IambAction::Message(..) => false,
252 IambAction::Room(..) => false, 274 IambAction::Room(..) => false,
253 IambAction::Send(..) => false, 275 IambAction::Send(..) => false,
src/commands.rs +56 −1
@@ -4,13 +4,16 @@ use matrix_sdk::ruma::{events::tag::TagName, OwnedUserId};
4 4
5use modalkit::{ 5use modalkit::{
6 editing::base::OpenTarget, 6 editing::base::OpenTarget,
7 env::vim::command::{CommandContext, CommandDescription}, 7 env::vim::command::{CommandContext, CommandDescription, OptionType},
8 input::commands::{CommandError, CommandResult, CommandStep}, 8 input::commands::{CommandError, CommandResult, CommandStep},
9 input::InputContext, 9 input::InputContext,
10}; 10};
11 11
12use crate::base::{ 12use crate::base::{
13 CreateRoomFlags,
14 CreateRoomType,
13 DownloadFlags, 15 DownloadFlags,
16 HomeserverAction,
14 IambAction, 17 IambAction,
15 IambId, 18 IambId,
16 MessageAction, 19 MessageAction,
@@ -297,6 +300,53 @@ fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
297 return Ok(step); 300 return Ok(step);
298} 301}
299 302
303fn iamb_create(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
304 let args = desc.arg.options()?;
305 let mut flags = CreateRoomFlags::NONE;
306 let mut alias = None;
307 let mut ct = CreateRoomType::Room;
308
309 for arg in args {
310 match arg {
311 OptionType::Flag(name, Some(arg)) => {
312 match name.as_str() {
313 "alias" => {
314 if alias.is_some() {
315 let msg = "Multiple ++alias arguments are not allowed";
316 let err = CommandError::Error(msg.into());
317
318 return Err(err);
319 } else {
320 alias = Some(arg);
321 }
322 },
323 _ => return Err(CommandError::InvalidArgument),
324 }
325 },
326 OptionType::Flag(name, None) => {
327 match name.as_str() {
328 "public" => flags |= CreateRoomFlags::PUBLIC,
329 "space" => ct = CreateRoomType::Space,
330 "enc" | "encrypted" => flags |= CreateRoomFlags::ENCRYPTED,
331 _ => return Err(CommandError::InvalidArgument),
332 }
333 },
334 OptionType::Positional(_) => {
335 let msg = ":create doesn't take any positional arguments";
336 let err = CommandError::Error(msg.into());
337
338 return Err(err);
339 },
340 }
341 }
342
343 let hact = HomeserverAction::CreateRoom(alias, ct, flags);
344 let iact = IambAction::from(hact);
345 let step = CommandStep::Continue(iact.into(), ctx.context.take());
346
347 return Ok(step);
348}
349
300fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { 350fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
301 let mut args = desc.arg.strings()?; 351 let mut args = desc.arg.strings()?;
302 352
@@ -400,6 +450,11 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
400 aliases: vec![], 450 aliases: vec![],
401 f: iamb_cancel, 451 f: iamb_cancel,
402 }); 452 });
453 cmds.add_command(ProgramCommand {
454 name: "create".into(),
455 aliases: vec![],
456 f: iamb_create,
457 });
403 cmds.add_command(ProgramCommand { name: "dms".into(), aliases: vec![], f: iamb_dms }); 458 cmds.add_command(ProgramCommand { name: "dms".into(), aliases: vec![], f: iamb_dms });
404 cmds.add_command(ProgramCommand { 459 cmds.add_command(ProgramCommand {
405 name: "download".into(), 460 name: "download".into(),
src/main.rs +28 −2
@@ -53,6 +53,7 @@ use crate::{
53 base::{ 53 base::{
54 AsyncProgramStore, 54 AsyncProgramStore,
55 ChatStore, 55 ChatStore,
56 HomeserverAction,
56 IambAction, 57 IambAction,
57 IambError, 58 IambError,
58 IambId, 59 IambId,
@@ -64,7 +65,7 @@ use crate::{
64 }, 65 },
65 config::{ApplicationSettings, Iamb}, 66 config::{ApplicationSettings, Iamb},
66 windows::IambWindow, 67 windows::IambWindow,
67 worker::{ClientWorker, LoginStyle, Requester}, 68 worker::{create_room, ClientWorker, LoginStyle, Requester},
68}; 69};
69 70
70use modalkit::{ 71use modalkit::{
@@ -355,6 +356,12 @@ impl Application {
355 None 356 None
356 }, 357 },
357 358
359 IambAction::Homeserver(act) => {
360 let acts = self.homeserver_command(act, ctx, store).await?;
361 self.action_prepend(acts);
362
363 None
364 },
358 IambAction::Message(act) => { 365 IambAction::Message(act) => {
359 self.screen.current_window_mut()?.message_command(act, ctx, store).await? 366 self.screen.current_window_mut()?.message_command(act, ctx, store).await?
360 }, 367 },
@@ -387,6 +394,25 @@ impl Application {
387 Ok(info) 394 Ok(info)
388 } 395 }
389 396
397 async fn homeserver_command(
398 &mut self,
399 action: HomeserverAction,
400 ctx: ProgramContext,
401 store: &mut ProgramStore,
402 ) -> IambResult<Vec<(Action<IambInfo>, ProgramContext)>> {
403 match action {
404 HomeserverAction::CreateRoom(alias, vis, flags) => {
405 let client = &store.application.worker.client;
406 let room_id = create_room(client, alias.as_deref(), vis, flags).await?;
407 let room = IambId::Room(room_id);
408 let target = OpenTarget::Application(room);
409 let action = WindowAction::Switch(target);
410
411 Ok(vec![(action.into(), ctx)])
412 },
413 }
414 }
415
390 pub async fn run(&mut self) -> Result<(), std::io::Error> { 416 pub async fn run(&mut self) -> Result<(), std::io::Error> {
391 self.terminal.clear()?; 417 self.terminal.clear()?;
392 418
@@ -518,7 +544,7 @@ fn main() -> IambResult<()> {
518 544
519 let subscriber = FmtSubscriber::builder() 545 let subscriber = FmtSubscriber::builder()
520 .with_writer(appender) 546 .with_writer(appender)
521 .with_max_level(Level::TRACE) 547 .with_max_level(Level::INFO)
522 .finish(); 548 .finish();
523 tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); 549 tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
524 550
src/worker.rs +13 −4
@@ -20,7 +20,7 @@ use matrix_sdk::{
20 room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember}, 20 room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
21 ruma::{ 21 ruma::{
22 api::client::{ 22 api::client::{
23 room::create_room::v3::{Request as CreateRoomRequest, RoomPreset}, 23 room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset},
24 room::Visibility, 24 room::Visibility,
25 space::get_hierarchy::v1::Request as SpaceHierarchyRequest, 25 space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
26 }, 26 },
@@ -50,6 +50,7 @@ use matrix_sdk::{
50 SyncMessageLikeEvent, 50 SyncMessageLikeEvent,
51 SyncStateEvent, 51 SyncStateEvent,
52 }, 52 },
53 room::RoomType,
53 serde::Raw, 54 serde::Raw,
54 EventEncryptionAlgorithm, 55 EventEncryptionAlgorithm,
55 OwnedRoomId, 56 OwnedRoomId,
@@ -93,10 +94,10 @@ pub async fn create_room(
93 rt: CreateRoomType, 94 rt: CreateRoomType,
94 flags: CreateRoomFlags, 95 flags: CreateRoomFlags,
95) -> IambResult<OwnedRoomId> { 96) -> IambResult<OwnedRoomId> {
96 let creation_content = None; 97 let mut creation_content = None;
97 let mut initial_state = vec![]; 98 let mut initial_state = vec![];
98 let is_direct; 99 let mut is_direct = false;
99 let preset; 100 let mut preset = None;
100 let mut invite = vec![]; 101 let mut invite = vec![];
101 102
102 let visibility = if flags.contains(CreateRoomFlags::PUBLIC) { 103 let visibility = if flags.contains(CreateRoomFlags::PUBLIC) {
@@ -111,6 +112,14 @@ pub async fn create_room(
111 is_direct = true; 112 is_direct = true;
112 preset = Some(RoomPreset::TrustedPrivateChat); 113 preset = Some(RoomPreset::TrustedPrivateChat);
113 }, 114 },
115 CreateRoomType::Space => {
116 let mut cc = CreationContent::new();
117 cc.room_type = Some(RoomType::Space);
118
119 let raw_cc = Raw::new(&cc).map_err(IambError::from)?;
120 creation_content = Some(raw_cc);
121 },
122 CreateRoomType::Room => {},
114 } 123 }
115 124
116 // Set up encryption. 125 // Set up encryption.