Commit 3b86be0545
Unregistered key
Layout: unified · split
src/base.rs +1
| @@ -261,6 +261,7 @@ pub enum SendAction { | ||
| 261 | 261 | pub enum HomeserverAction { |
| 262 | 262 | /// Create a new room with an optional localpart. |
| 263 | 263 | CreateRoom(Option<String>, CreateRoomType, CreateRoomFlags), |
| 264 | Logout(String, bool), | |
| 264 | 265 | } |
| 265 | 266 | |
| 266 | 267 | /// An action that the main program loop should. |
src/commands.rs +18
| @@ -471,6 +471,19 @@ fn iamb_open(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | ||
| 471 | 471 | return Ok(step); |
| 472 | 472 | } |
| 473 | 473 | |
| 474 | fn iamb_logout(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult { | |
| 475 | let args = desc.arg.strings()?; | |
| 476 | ||
| 477 | if args.len() != 1 { | |
| 478 | return Result::Err(CommandError::InvalidArgument); | |
| 479 | } | |
| 480 | ||
| 481 | let iact = IambAction::from(HomeserverAction::Logout(args[0].clone(), desc.bang)); | |
| 482 | let step = CommandStep::Continue(iact.into(), ctx.context.take()); | |
| 483 | ||
| 484 | return Ok(step); | |
| 485 | } | |
| 486 | ||
| 474 | 487 | fn add_iamb_commands(cmds: &mut ProgramCommands) { |
| 475 | 488 | cmds.add_command(ProgramCommand { |
| 476 | 489 | name: "cancel".into(), |
| @@ -557,6 +570,11 @@ fn add_iamb_commands(cmds: &mut ProgramCommands) { | ||
| 557 | 570 | aliases: vec![], |
| 558 | 571 | f: iamb_editor, |
| 559 | 572 | }); |
| 573 | cmds.add_command(ProgramCommand { | |
| 574 | name: "logout".into(), | |
| 575 | aliases: vec![], | |
| 576 | f: iamb_logout, | |
| 577 | }); | |
| 560 | 578 | } |
| 561 | 579 | |
| 562 | 580 | /// Initialize the default command state. |
src/main.rs +17 −2
| @@ -115,12 +115,12 @@ use modalkit::{ | ||
| 115 | 115 | WindowAction, |
| 116 | 116 | WindowContainer, |
| 117 | 117 | }, |
| 118 | base::{MoveDir1D, OpenTarget, RepeatType}, | |
| 118 | base::{CloseFlags, MoveDir1D, OpenTarget, RepeatType, TabTarget}, | |
| 119 | 119 | context::Resolve, |
| 120 | 120 | key::KeyManager, |
| 121 | 121 | store::Store, |
| 122 | 122 | }, |
| 123 | input::{bindings::BindingMachine, dialog::Pager, key::TerminalKey}, | |
| 123 | input::{bindings::BindingMachine, dialog::Pager, dialog::PromptYesNo, key::TerminalKey}, | |
| 124 | 124 | widgets::{ |
| 125 | 125 | cmdbar::CommandBarState, |
| 126 | 126 | screen::{FocusList, Screen, ScreenState, TabLayoutDescription}, |
| @@ -571,6 +571,21 @@ impl Application { | ||
| 571 | 571 | |
| 572 | 572 | Ok(vec![(action.into(), ctx)]) |
| 573 | 573 | }, |
| 574 | HomeserverAction::Logout(user, true) => { | |
| 575 | self.worker.logout(user)?; | |
| 576 | let flags = CloseFlags::QUIT | CloseFlags::FORCE; | |
| 577 | let act = TabAction::Close(TabTarget::All, flags); | |
| 578 | ||
| 579 | Ok(vec![(act.into(), ctx)]) | |
| 580 | }, | |
| 581 | HomeserverAction::Logout(user, false) => { | |
| 582 | let msg = "Would you like to logout?"; | |
| 583 | let act = IambAction::from(HomeserverAction::Logout(user, true)); | |
| 584 | let prompt = PromptYesNo::new(msg, vec![Action::from(act)]); | |
| 585 | let prompt = Box::new(prompt); | |
| 586 | ||
| 587 | Err(UIError::NeedConfirm(prompt)) | |
| 588 | }, | |
| 574 | 589 | } |
| 575 | 590 | } |
| 576 | 591 | |
src/worker.rs +41
| @@ -455,6 +455,7 @@ pub type FetchedRoom = (MatrixRoom, DisplayName, Option<Tags>); | ||
| 455 | 455 | pub enum WorkerTask { |
| 456 | 456 | Init(AsyncProgramStore, ClientReply<()>), |
| 457 | 457 | Login(LoginStyle, ClientReply<IambResult<EditInfo>>), |
| 458 | Logout(String, ClientReply<IambResult<EditInfo>>), | |
| 458 | 459 | GetInviter(Invited, ClientReply<IambResult<Option<RoomMember>>>), |
| 459 | 460 | GetRoom(OwnedRoomId, ClientReply<IambResult<FetchedRoom>>), |
| 460 | 461 | JoinRoom(String, ClientReply<IambResult<OwnedRoomId>>), |
| @@ -480,6 +481,9 @@ impl Debug for WorkerTask { | ||
| 480 | 481 | .field(&format_args!("_")) |
| 481 | 482 | .finish() |
| 482 | 483 | }, |
| 484 | WorkerTask::Logout(user_id, _) => { | |
| 485 | f.debug_tuple("WorkerTask::Logout").field(user_id).finish() | |
| 486 | }, | |
| 483 | 487 | WorkerTask::GetInviter(invite, _) => { |
| 484 | 488 | f.debug_tuple("WorkerTask::GetInviter").field(invite).finish() |
| 485 | 489 | }, |
| @@ -550,6 +554,14 @@ impl Requester { | ||
| 550 | 554 | return response.recv(); |
| 551 | 555 | } |
| 552 | 556 | |
| 557 | pub fn logout(&self, user_id: String) -> IambResult<EditInfo> { | |
| 558 | let (reply, response) = oneshot(); | |
| 559 | ||
| 560 | self.tx.send(WorkerTask::Logout(user_id, reply)).unwrap(); | |
| 561 | ||
| 562 | return response.recv(); | |
| 563 | } | |
| 564 | ||
| 553 | 565 | pub fn get_inviter(&self, invite: Invited) -> IambResult<Option<RoomMember>> { |
| 554 | 566 | let (reply, response) = oneshot(); |
| 555 | 567 | |
| @@ -704,6 +716,10 @@ impl ClientWorker { | ||
| 704 | 716 | assert!(self.initialized); |
| 705 | 717 | reply.send(self.login_and_sync(style).await); |
| 706 | 718 | }, |
| 719 | WorkerTask::Logout(user_id, reply) => { | |
| 720 | assert!(self.initialized); | |
| 721 | reply.send(self.logout(user_id).await); | |
| 722 | }, | |
| 707 | 723 | WorkerTask::Members(room_id, reply) => { |
| 708 | 724 | assert!(self.initialized); |
| 709 | 725 | reply.send(self.members(room_id).await); |
| @@ -1073,6 +1089,31 @@ impl ClientWorker { | ||
| 1073 | 1089 | Ok(Some(InfoMessage::from("Successfully logged in!"))) |
| 1074 | 1090 | } |
| 1075 | 1091 | |
| 1092 | async fn logout(&mut self, user_id: String) -> IambResult<EditInfo> { | |
| 1093 | // Verify that the user is logging out of the correct profile. | |
| 1094 | let curr = self.settings.profile.user_id.as_ref(); | |
| 1095 | ||
| 1096 | if user_id != curr { | |
| 1097 | let msg = format!("Incorrect user ID (currently logged in as {curr})"); | |
| 1098 | let err = UIError::Failure(msg); | |
| 1099 | ||
| 1100 | return Err(err); | |
| 1101 | } | |
| 1102 | ||
| 1103 | // Send the logout request. | |
| 1104 | if let Err(e) = self.client.logout().await { | |
| 1105 | let msg = format!("Failed to logout: {e}"); | |
| 1106 | let err = UIError::Failure(msg); | |
| 1107 | ||
| 1108 | return Err(err); | |
| 1109 | } | |
| 1110 | ||
| 1111 | // Remove the session.json file. | |
| 1112 | std::fs::remove_file(&self.settings.session_json)?; | |
| 1113 | ||
| 1114 | Ok(Some(InfoMessage::from("Sucessfully logged out"))) | |
| 1115 | } | |
| 1116 | ||
| 1076 | 1117 | async fn direct_message(&mut self, user: OwnedUserId) -> IambResult<OwnedRoomId> { |
| 1077 | 1118 | for room in self.client.rooms() { |
| 1078 | 1119 | if !room.is_direct() { |