Commit 472581ce58
Unsigned
Layout: unified · split
src/commands/invite.rs added +91
| @@ -0,0 +1,91 @@ | |||
| 1 | use clap::{App as Argparse, AppSettings as ArgParseSettings, Arg}; | ||
| 2 | use matrix_sdk::ruma::{OwnedUserId, UserId}; | ||
| 3 | use weechat::{ | ||
| 4 | buffer::Buffer, | ||
| 5 | hooks::{Command, CommandCallback, CommandSettings}, | ||
| 6 | Args, Prefix, Weechat, | ||
| 7 | }; | ||
| 8 | |||
| 9 | use super::parse_and_run; | ||
| 10 | use crate::Servers; | ||
| 11 | |||
| 12 | pub struct InviteCommand { | ||
| 13 | servers: Servers, | ||
| 14 | } | ||
| 15 | |||
| 16 | impl InviteCommand { | ||
| 17 | pub fn create(servers: &Servers) -> Result<Command, ()> { | ||
| 18 | let settings = CommandSettings::new("invite") | ||
| 19 | .description("Invite a Matrix user to the current room.") | ||
| 20 | .add_argument("<user-id>") | ||
| 21 | .arguments_description( | ||
| 22 | "user-id: The Matrix user ID to invite to the current room.", | ||
| 23 | ) | ||
| 24 | .add_completion("%(matrix-users)"); | ||
| 25 | |||
| 26 | Command::new( | ||
| 27 | settings, | ||
| 28 | InviteCommand { | ||
| 29 | servers: servers.clone(), | ||
| 30 | }, | ||
| 31 | ) | ||
| 32 | } | ||
| 33 | |||
| 34 | fn invite(&self, buffer: &Buffer, user_id: OwnedUserId) { | ||
| 35 | if let Some(room) = self.servers.find_room(buffer) { | ||
| 36 | let room = room.room().clone(); | ||
| 37 | let invited_user = user_id.clone(); | ||
| 38 | |||
| 39 | Weechat::spawn(async move { | ||
| 40 | match room.invite_user_by_id(&user_id).await { | ||
| 41 | Ok(()) => Weechat::print(&format!( | ||
| 42 | "{}Invited {} to the room.", | ||
| 43 | Weechat::prefix(Prefix::Network), | ||
| 44 | invited_user | ||
| 45 | )), | ||
| 46 | Err(error) => Weechat::print(&format!( | ||
| 47 | "{}Failed to invite {}: {}", | ||
| 48 | Weechat::prefix(Prefix::Error), | ||
| 49 | invited_user, | ||
| 50 | error | ||
| 51 | )), | ||
| 52 | } | ||
| 53 | }) | ||
| 54 | .detach(); | ||
| 55 | } else { | ||
| 56 | Weechat::print( | ||
| 57 | "The /invite command needs to be run in a Matrix room buffer.", | ||
| 58 | ); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | impl CommandCallback for InviteCommand { | ||
| 64 | fn callback(&mut self, _: &Weechat, buffer: &Buffer, arguments: Args) { | ||
| 65 | let parser = Argparse::new("invite") | ||
| 66 | .about("Invite a Matrix user to the current room.") | ||
| 67 | .settings(&[ | ||
| 68 | ArgParseSettings::DisableHelpFlags, | ||
| 69 | ArgParseSettings::DisableVersion, | ||
| 70 | ]) | ||
| 71 | .arg(Arg::with_name("user-id").required(true).validator(|u| { | ||
| 72 | UserId::parse(u) | ||
| 73 | .map_err(|_| { | ||
| 74 | "The given user isn't a valid user ID".to_owned() | ||
| 75 | }) | ||
| 76 | .map(|_| ()) | ||
| 77 | })); | ||
| 78 | |||
| 79 | parse_and_run(parser, arguments, |matches| { | ||
| 80 | let user_id = matches | ||
| 81 | .value_of("user-id") | ||
| 82 | .map(|u| { | ||
| 83 | UserId::parse(u) | ||
| 84 | .expect("Argument was already validated as a user ID") | ||
| 85 | }) | ||
| 86 | .expect("User ID not set but was required"); | ||
| 87 | |||
| 88 | self.invite(buffer, user_id); | ||
| 89 | }); | ||
| 90 | } | ||
| 91 | } | ||
src/commands/mod.rs +4
| @@ -9,6 +9,7 @@ use crate::{config::ConfigHandle, Servers}; | |||
| 9 | 9 | ||
| 10 | mod buffer_clear; | 10 | mod buffer_clear; |
| 11 | mod devices; | 11 | mod devices; |
| 12 | mod invite; | ||
| 12 | mod join; | 13 | mod join; |
| 13 | mod keys; | 14 | mod keys; |
| 14 | mod matrix; | 15 | mod matrix; |
| @@ -23,6 +24,7 @@ mod verification; | |||
| 23 | 24 | ||
| 24 | use buffer_clear::BufferClearCommand; | 25 | use buffer_clear::BufferClearCommand; |
| 25 | use devices::DevicesCommand; | 26 | use devices::DevicesCommand; |
| 27 | use invite::InviteCommand; | ||
| 26 | use join::JoinCommand; | 28 | use join::JoinCommand; |
| 27 | use keys::KeysCommand; | 29 | use keys::KeysCommand; |
| 28 | use matrix::MatrixCommand; | 30 | use matrix::MatrixCommand; |
| @@ -38,6 +40,7 @@ pub struct Commands { | |||
| 38 | _matrix: Command, | 40 | _matrix: Command, |
| 39 | _keys: Command, | 41 | _keys: Command, |
| 40 | _devices: Command, | 42 | _devices: Command, |
| 43 | _invite: Command, | ||
| 41 | _page_up: CommandRun, | 44 | _page_up: CommandRun, |
| 42 | _redact: Command, | 45 | _redact: Command, |
| 43 | _verification: Command, | 46 | _verification: Command, |
| @@ -57,6 +60,7 @@ impl Commands { | |||
| 57 | Ok(Commands { | 60 | Ok(Commands { |
| 58 | _matrix: MatrixCommand::create(servers, config)?, | 61 | _matrix: MatrixCommand::create(servers, config)?, |
| 59 | _devices: DevicesCommand::create(servers)?, | 62 | _devices: DevicesCommand::create(servers)?, |
| 63 | _invite: InviteCommand::create(servers)?, | ||
| 60 | _keys: KeysCommand::create(servers)?, | 64 | _keys: KeysCommand::create(servers)?, |
| 61 | _page_up: PageUpCommand::create(servers)?, | 65 | _page_up: PageUpCommand::create(servers)?, |
| 62 | _redact: RedactCommand::create(servers)?, | 66 | _redact: RedactCommand::create(servers)?, |