Commit dbce58ed62
Unsigned
Layout: unified · split
src/commands/nick.rs +37 −40
| @@ -32,49 +32,46 @@ impl CommandRunCallback for NickCommand { | ||
| 32 | 32 | ) -> ReturnCode { |
| 33 | 33 | let new_nick = cmd.strip_prefix("/nick ").map(|s| s.trim()); |
| 34 | 34 | |
| 35 | if let Some(server) = self.servers.find_server(&buffer) { | |
| 36 | match new_nick { | |
| 37 | Some(name) if !name.is_empty() => { | |
| 38 | let name = name.to_owned(); | |
| 39 | Weechat::spawn(async move { | |
| 40 | server.set_display_name(Some(&name)).await; | |
| 41 | }) | |
| 42 | .detach(); | |
| 43 | } | |
| 44 | None => { | |
| 45 | // /nick with no arguments: show current display name | |
| 46 | Weechat::spawn(async move { | |
| 47 | match server.get_display_name().await { | |
| 48 | Some(name) => { | |
| 49 | Weechat::print(&format!( | |
| 50 | "{}: Current display name: {}", | |
| 51 | PLUGIN_NAME, name | |
| 52 | )); | |
| 53 | } | |
| 54 | None => { | |
| 55 | Weechat::print(&format!( | |
| 56 | "{}: No display name set.", | |
| 57 | PLUGIN_NAME | |
| 58 | )); | |
| 59 | } | |
| 35 | let Some(server) = self.servers.find_server(&buffer) else { | |
| 36 | return ReturnCode::Ok; | |
| 37 | }; | |
| 38 | ||
| 39 | match new_nick { | |
| 40 | Some(name) if !name.is_empty() => { | |
| 41 | let name = name.to_owned(); | |
| 42 | Weechat::spawn(async move { | |
| 43 | server.set_display_name(Some(&name)).await; | |
| 44 | }) | |
| 45 | .detach(); | |
| 46 | } | |
| 47 | None => { | |
| 48 | // /nick with no arguments: show current display name | |
| 49 | Weechat::spawn(async move { | |
| 50 | match server.get_display_name().await { | |
| 51 | Some(name) => { | |
| 52 | Weechat::print(&format!( | |
| 53 | "{}: Current display name: {}", | |
| 54 | PLUGIN_NAME, name | |
| 55 | )); | |
| 60 | 56 | } |
| 61 | }) | |
| 62 | .detach(); | |
| 63 | } | |
| 64 | _ => { | |
| 65 | Weechat::print(&format!( | |
| 66 | "{}Usage: /nick <new-display-name>", | |
| 67 | Weechat::prefix(weechat::Prefix::Error), | |
| 68 | )); | |
| 69 | } | |
| 57 | None => { | |
| 58 | Weechat::print(&format!( | |
| 59 | "{}: No display name set.", | |
| 60 | PLUGIN_NAME | |
| 61 | )); | |
| 62 | } | |
| 63 | } | |
| 64 | }) | |
| 65 | .detach(); | |
| 66 | } | |
| 67 | _ => { | |
| 68 | Weechat::print(&format!( | |
| 69 | "{}Usage: /nick <new-display-name>", | |
| 70 | Weechat::prefix(weechat::Prefix::Error), | |
| 71 | )); | |
| 70 | 72 | } |
| 71 | } else { | |
| 72 | Weechat::print(&format!( | |
| 73 | "{}The /nick command must be run in a Matrix buffer.", | |
| 74 | Weechat::prefix(weechat::Prefix::Error), | |
| 75 | )); | |
| 76 | 73 | } |
| 77 | 74 | |
| 78 | ReturnCode::Ok | |
| 75 | ReturnCode::OkEat | |
| 79 | 76 | } |
| 80 | 77 | } |
src/server.rs +19 −4
| @@ -740,19 +740,34 @@ impl InnerServer { | ||
| 740 | 740 | |
| 741 | 741 | /// Set the display name for this account on the homeserver. |
| 742 | 742 | pub async fn set_display_name(&self, name: Option<&str>) { |
| 743 | let Some(client) = self.get_client() else { | |
| 743 | let Some(connection) = self.connection() else { | |
| 744 | 744 | self.print_error("Not connected to a server"); |
| 745 | 745 | return; |
| 746 | 746 | }; |
| 747 | if let Err(e) = client.account().set_display_name(name).await { | |
| 747 | ||
| 748 | let client = connection.client().clone(); | |
| 749 | let name = name.map(str::to_owned); | |
| 750 | let result = connection | |
| 751 | .spawn(async move { | |
| 752 | client.account().set_display_name(name.as_deref()).await | |
| 753 | }) | |
| 754 | .await; | |
| 755 | ||
| 756 | if let Err(e) = result { | |
| 748 | 757 | self.print_error(&format!("Failed to set display name: {}", e)); |
| 749 | 758 | } |
| 750 | 759 | } |
| 751 | 760 | |
| 752 | 761 | /// Get the current display name for this account from the homeserver. |
| 753 | 762 | pub async fn get_display_name(&self) -> Option<String> { |
| 754 | let client = self.get_client()?; | |
| 755 | client.account().get_display_name().await.ok().flatten() | |
| 763 | let connection = self.connection()?; | |
| 764 | let client = connection.client().clone(); | |
| 765 | ||
| 766 | connection | |
| 767 | .spawn(async move { client.account().get_display_name().await }) | |
| 768 | .await | |
| 769 | .ok() | |
| 770 | .flatten() | |
| 756 | 771 | } |
| 757 | 772 | |
| 758 | 773 | pub async fn restore_room_by_id(&self, room_id: OwnedRoomId) { |