Commit dbce58ed62
Unsigned
Layout: unified · split
src/commands/nick.rs +37 −40
| @@ -32,49 +32,46 @@ impl CommandRunCallback for NickCommand { | |||
| 32 | ) -> ReturnCode { | 32 | ) -> ReturnCode { |
| 33 | let new_nick = cmd.strip_prefix("/nick ").map(|s| s.trim()); | 33 | let new_nick = cmd.strip_prefix("/nick ").map(|s| s.trim()); |
| 34 | 34 | ||
| 35 | if let Some(server) = self.servers.find_server(&buffer) { | 35 | let Some(server) = self.servers.find_server(&buffer) else { |
| 36 | match new_nick { | 36 | return ReturnCode::Ok; |
| 37 | Some(name) if !name.is_empty() => { | 37 | }; |
| 38 | let name = name.to_owned(); | 38 | |
| 39 | Weechat::spawn(async move { | 39 | match new_nick { |
| 40 | server.set_display_name(Some(&name)).await; | 40 | Some(name) if !name.is_empty() => { |
| 41 | }) | 41 | let name = name.to_owned(); |
| 42 | .detach(); | 42 | Weechat::spawn(async move { |
| 43 | } | 43 | server.set_display_name(Some(&name)).await; |
| 44 | None => { | 44 | }) |
| 45 | // /nick with no arguments: show current display name | 45 | .detach(); |
| 46 | Weechat::spawn(async move { | 46 | } |
| 47 | match server.get_display_name().await { | 47 | None => { |
| 48 | Some(name) => { | 48 | // /nick with no arguments: show current display name |
| 49 | Weechat::print(&format!( | 49 | Weechat::spawn(async move { |
| 50 | "{}: Current display name: {}", | 50 | match server.get_display_name().await { |
| 51 | PLUGIN_NAME, name | 51 | Some(name) => { |
| 52 | )); | 52 | Weechat::print(&format!( |
| 53 | } | 53 | "{}: Current display name: {}", |
| 54 | None => { | 54 | PLUGIN_NAME, name |
| 55 | Weechat::print(&format!( | 55 | )); |
| 56 | "{}: No display name set.", | ||
| 57 | PLUGIN_NAME | ||
| 58 | )); | ||
| 59 | } | ||
| 60 | } | 56 | } |
| 61 | }) | 57 | None => { |
| 62 | .detach(); | 58 | Weechat::print(&format!( |
| 63 | } | 59 | "{}: No display name set.", |
| 64 | _ => { | 60 | PLUGIN_NAME |
| 65 | Weechat::print(&format!( | 61 | )); |
| 66 | "{}Usage: /nick <new-display-name>", | 62 | } |
| 67 | Weechat::prefix(weechat::Prefix::Error), | 63 | } |
| 68 | )); | 64 | }) |
| 69 | } | 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 | /// Set the display name for this account on the homeserver. | 741 | /// Set the display name for this account on the homeserver. |
| 742 | pub async fn set_display_name(&self, name: Option<&str>) { | 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 | self.print_error("Not connected to a server"); | 744 | self.print_error("Not connected to a server"); |
| 745 | return; | 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 | self.print_error(&format!("Failed to set display name: {}", e)); | 757 | self.print_error(&format!("Failed to set display name: {}", e)); |
| 749 | } | 758 | } |
| 750 | } | 759 | } |
| 751 | 760 | ||
| 752 | /// Get the current display name for this account from the homeserver. | 761 | /// Get the current display name for this account from the homeserver. |
| 753 | pub async fn get_display_name(&self) -> Option<String> { | 762 | pub async fn get_display_name(&self) -> Option<String> { |
| 754 | let client = self.get_client()?; | 763 | let connection = self.connection()?; |
| 755 | client.account().get_display_name().await.ok().flatten() | 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 | pub async fn restore_room_by_id(&self, room_id: OwnedRoomId) { | 773 | pub async fn restore_room_by_id(&self, room_id: OwnedRoomId) { |