@@ -2,12 +2,13 @@ use std::{ops::Deref, sync::Arc};
2
2
3
3
use color_eyre:: Result ;
4
4
use crossterm:: event:: { Event , KeyCode , KeyModifiers } ;
5
+ use input:: MessageOrCommand ;
5
6
use invited_room:: InvitedRoomView ;
6
7
use matrix_sdk:: {
7
8
locks:: Mutex ,
8
9
ruma:: {
9
10
api:: client:: receipt:: create_receipt:: v3:: ReceiptType ,
10
- events:: room:: message:: RoomMessageEventContent , OwnedRoomId ,
11
+ events:: room:: message:: RoomMessageEventContent , OwnedRoomId , OwnedUserId ,
11
12
} ,
12
13
RoomState ,
13
14
} ;
@@ -74,16 +75,18 @@ impl RoomView {
74
75
match ( key. modifiers , key. code ) {
75
76
( KeyModifiers :: NONE , Enter ) => {
76
77
if !self . input . is_empty ( ) {
77
- let message = self . input . get_text ( ) ;
78
+ let message_or_command = self . input . get_input ( ) ;
78
79
79
- match self . send_message ( message) . await {
80
- Ok ( _) => {
81
- self . input . clear ( ) ;
80
+ match message_or_command {
81
+ Ok ( MessageOrCommand :: Message ( message) ) => {
82
+ self . send_message ( message) . await
83
+ }
84
+ Ok ( MessageOrCommand :: Command ( command) ) => {
85
+ self . handle_command ( command) . await
82
86
}
83
- Err ( err) => {
84
- self . status_handle . set_message ( format ! (
85
- "error when sending event: {err}"
86
- ) ) ;
87
+ Err ( e) => {
88
+ self . status_handle . set_message ( e. render ( ) . to_string ( ) ) ;
89
+ self . input . clear ( ) ;
87
90
}
88
91
}
89
92
}
@@ -261,7 +264,48 @@ impl RoomView {
261
264
} ;
262
265
}
263
266
264
- pub async fn send_message ( & self , message : String ) -> Result < ( ) > {
267
+ async fn invite_member ( & mut self , user_id : OwnedUserId ) {
268
+ let Some ( room) = self
269
+ . selected_room
270
+ . as_deref ( )
271
+ . and_then ( |room_id| self . ui_rooms . lock ( ) . get ( room_id) . cloned ( ) )
272
+ else {
273
+ self . status_handle
274
+ . set_message ( format ! ( "Coulnd't find the room object to invite {user_id}" ) ) ;
275
+ return ;
276
+ } ;
277
+
278
+ match room. invite_user_by_id ( & user_id) . await {
279
+ Ok ( _) => {
280
+ self . status_handle
281
+ . set_message ( format ! ( "Successfully invited {user_id} to the room" ) ) ;
282
+ self . input . clear ( ) ;
283
+ }
284
+ Err ( e) => {
285
+ self . status_handle
286
+ . set_message ( format ! ( "Failed to invite {user_id} to the room: {e:?}" ) ) ;
287
+ }
288
+ }
289
+ }
290
+
291
+ async fn handle_command ( & mut self , command : input:: Command ) {
292
+ match command {
293
+ input:: Command :: Invite { user_id } => self . invite_member ( user_id) . await ,
294
+ }
295
+ }
296
+
297
+ async fn send_message ( & mut self , message : String ) {
298
+ match self . send_message_impl ( message) . await {
299
+ Ok ( _) => {
300
+ self . input . clear ( ) ;
301
+ }
302
+ Err ( err) => {
303
+ self . status_handle . set_message ( format ! ( "error when sending event: {err}" ) ) ;
304
+ }
305
+ }
306
+ }
307
+
308
+ async fn send_message_impl ( & self , message : String ) -> Result < ( ) > {
265
309
if let Some ( sdk_timeline) = self . selected_room . as_deref ( ) . and_then ( |room_id| {
266
310
self . timelines . lock ( ) . get ( room_id) . map ( |timeline| timeline. timeline . clone ( ) )
267
311
} ) {
0 commit comments