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 = [
18821882
18831883[[package]]
18841884name = "modalkit"
1885version = "0.0.12"
1885version = "0.0.13"
18861886source = "registry+https://github.com/rust-lang/crates.io-index"
1887checksum = "9a4e5226a5d33a7bdf5b4fc067baa211c94f21aa6667af5eec8b357a8af5e4ba"
1887checksum = "038bb42efcd659fb123708bf8e4ea12ca9023f07d44514f9358b9334ea7ba80f"
18881888dependencies = [
18891889 "anymap2",
18901890 "arboard",
Cargo.toml +1 −1
@@ -39,7 +39,7 @@ unicode-width = "0.1.10"
3939url = {version = "^2.2.2", features = ["serde"]}
4040
4141[dependencies.modalkit]
42version = "0.0.12"
42version = "0.0.13"
4343
4444[dependencies.matrix-sdk]
4545version = "0.6"
src/base.rs +22
@@ -135,6 +135,12 @@ pub enum MessageAction {
135135pub enum CreateRoomType {
136136 /// A direct message room.
137137 Direct(OwnedUserId),
138
139 /// A standard chat room.
140 Room,
141
142 /// A Matrix space.
143 Space,
138144}
139145
140146bitflags::bitflags! {
@@ -184,8 +190,14 @@ pub enum SendAction {
184190 Upload(String),
185191}
186192
193#[derive(Clone, Debug, Eq, PartialEq)]
194pub enum HomeserverAction {
195 CreateRoom(Option<String>, CreateRoomType, CreateRoomFlags),
196}
197
187198#[derive(Clone, Debug, Eq, PartialEq)]
188199pub enum IambAction {
200 Homeserver(HomeserverAction),
189201 Message(MessageAction),
190202 Room(RoomAction),
191203 Send(SendAction),
@@ -194,6 +206,12 @@ pub enum IambAction {
194206 ToggleScrollbackFocus,
195207}
196208
209impl From<HomeserverAction> for IambAction {
210 fn from(act: HomeserverAction) -> Self {
211 IambAction::Homeserver(act)
212 }
213}
214
197215impl From<MessageAction> for IambAction {
198216 fn from(act: MessageAction) -> Self {
199217 IambAction::Message(act)
@@ -215,6 +233,7 @@ impl From<SendAction> for IambAction {
215233impl ApplicationAction for IambAction {
216234 fn is_edit_sequence<C: EditContext>(&self, _: &C) -> SequenceStatus {
217235 match self {
236 IambAction::Homeserver(..) => SequenceStatus::Break,
218237 IambAction::Message(..) => SequenceStatus::Break,
219238 IambAction::Room(..) => SequenceStatus::Break,
220239 IambAction::Send(..) => SequenceStatus::Break,
@@ -226,6 +245,7 @@ impl ApplicationAction for IambAction {
226245
227246 fn is_last_action<C: EditContext>(&self, _: &C) -> SequenceStatus {
228247 match self {
248 IambAction::Homeserver(..) => SequenceStatus::Atom,
229249 IambAction::Message(..) => SequenceStatus::Atom,
230250 IambAction::Room(..) => SequenceStatus::Atom,
231251 IambAction::Send(..) => SequenceStatus::Atom,
@@ -237,6 +257,7 @@ impl ApplicationAction for IambAction {
237257
238258 fn is_last_selection<C: EditContext>(&self, _: &C) -> SequenceStatus {
239259 match self {
260 IambAction::Homeserver(..) => SequenceStatus::Ignore,
240261 IambAction::Message(..) => SequenceStatus::Ignore,
241262 IambAction::Room(..) => SequenceStatus::Ignore,
242263 IambAction::Send(..) => SequenceStatus::Ignore,
@@ -248,6 +269,7 @@ impl ApplicationAction for IambAction {
248269
249270 fn is_switchable<C: EditContext>(&self, _: &C) -> bool {
250271 match self {
272 IambAction::Homeserver(..) => false,
251273 IambAction::Message(..) => false,
252274 IambAction::Room(..) => false,
253275 IambAction::Send(..) => false,
src/commands.rs +56 −1
@@ -4,13 +4,16 @@ use matrix_sdk::ruma::{events::tag::TagName, OwnedUserId};
44
55use modalkit::{
66 editing::base::OpenTarget,
7 env::vim::command::{CommandContext, CommandDescription},
7 env::vim::command::{CommandContext, CommandDescription, OptionType},
88 input::commands::{CommandError, CommandResult, CommandStep},
99 input::InputContext,
1010};
1111
1212use crate::base::{
13 CreateRoomFlags,
14 CreateRoomType,
1315 DownloadFlags,
16 HomeserverAction,
1417 IambAction,
1518 IambId,
1619 MessageAction,
@@ -297,6 +300,53 @@ fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
297300 return Ok(step);
298301}
299302
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
300350fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
301351 let mut args = desc.arg.strings()?;
302352
@@ -400,6 +450,11 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) {
400450 aliases: vec![],
401451 f: iamb_cancel,
402452 });
453 cmds.add_command(ProgramCommand {
454 name: "create".into(),
455 aliases: vec![],
456 f: iamb_create,
457 });
403458 cmds.add_command(ProgramCommand { name: "dms".into(), aliases: vec![], f: iamb_dms });
404459 cmds.add_command(ProgramCommand {
405460 name: "download".into(),
src/main.rs +28 −2
@@ -53,6 +53,7 @@ use crate::{
5353 base::{
5454 AsyncProgramStore,
5555 ChatStore,
56 HomeserverAction,
5657 IambAction,
5758 IambError,
5859 IambId,
@@ -64,7 +65,7 @@ use crate::{
6465 },
6566 config::{ApplicationSettings, Iamb},
6667 windows::IambWindow,
67 worker::{ClientWorker, LoginStyle, Requester},
68 worker::{create_room, ClientWorker, LoginStyle, Requester},
6869};
6970
7071use modalkit::{
@@ -355,6 +356,12 @@ impl Application {
355356 None
356357 },
357358
359 IambAction::Homeserver(act) => {
360 let acts = self.homeserver_command(act, ctx, store).await?;
361 self.action_prepend(acts);
362
363 None
364 },
358365 IambAction::Message(act) => {
359366 self.screen.current_window_mut()?.message_command(act, ctx, store).await?
360367 },
@@ -387,6 +394,25 @@ impl Application {
387394 Ok(info)
388395 }
389396
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
390416 pub async fn run(&mut self) -> Result<(), std::io::Error> {
391417 self.terminal.clear()?;
392418
@@ -518,7 +544,7 @@ fn main() -> IambResult<()> {
518544
519545 let subscriber = FmtSubscriber::builder()
520546 .with_writer(appender)
521 .with_max_level(Level::TRACE)
547 .with_max_level(Level::INFO)
522548 .finish();
523549 tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
524550
src/worker.rs +13 −4
@@ -20,7 +20,7 @@ use matrix_sdk::{
2020 room::{Invited, Messages, MessagesOptions, Room as MatrixRoom, RoomMember},
2121 ruma::{
2222 api::client::{
23 room::create_room::v3::{Request as CreateRoomRequest, RoomPreset},
23 room::create_room::v3::{CreationContent, Request as CreateRoomRequest, RoomPreset},
2424 room::Visibility,
2525 space::get_hierarchy::v1::Request as SpaceHierarchyRequest,
2626 },
@@ -50,6 +50,7 @@ use matrix_sdk::{
5050 SyncMessageLikeEvent,
5151 SyncStateEvent,
5252 },
53 room::RoomType,
5354 serde::Raw,
5455 EventEncryptionAlgorithm,
5556 OwnedRoomId,
@@ -93,10 +94,10 @@ pub async fn create_room(
9394 rt: CreateRoomType,
9495 flags: CreateRoomFlags,
9596) -> IambResult<OwnedRoomId> {
96 let creation_content = None;
97 let mut creation_content = None;
9798 let mut initial_state = vec![];
98 let is_direct;
99 let preset;
99 let mut is_direct = false;
100 let mut preset = None;
100101 let mut invite = vec![];
101102
102103 let visibility = if flags.contains(CreateRoomFlags::PUBLIC) {
@@ -111,6 +112,14 @@ pub async fn create_room(
111112 is_direct = true;
112113 preset = Some(RoomPreset::TrustedPrivateChat);
113114 },
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 => {},
114123 }
115124
116125 // Set up encryption.