| @@ -92,6 +92,7 @@ pub struct MatrixRoom { |
| 92 | 92 | room_id: Rc<RoomId>, |
| 93 | 93 | own_user_id: Rc<UserId>, |
| 94 | 94 | room: Arc<RwLock<Room>>, |
| 95 | buffer: Rc<Option<BufferHandle>>, |
| 95 | 96 | |
| 96 | 97 | config: Rc<RefCell<Config>>, |
| 97 | 98 | connection: Rc<RefCell<Option<Connection>>>, |
| @@ -131,7 +132,7 @@ impl MessageQueue { |
| 131 | 132 | |
| 132 | 133 | #[async_trait(?Send)] |
| 133 | 134 | impl BufferInputCallbackAsync for MatrixRoom { |
| 134 | | async fn callback(&mut self, buffer_handle: BufferHandle, input: String) { |
| 135 | async fn callback(&mut self, _: BufferHandle, input: String) { |
| 135 | 136 | // TODO parse the input here and produce a formatted body. |
| 136 | 137 | let content = MessageEventContent::Text(TextMessageEventContent { |
| 137 | 138 | body: input, |
| @@ -139,20 +140,37 @@ impl BufferInputCallbackAsync for MatrixRoom { |
| 139 | 140 | relates_to: None, |
| 140 | 141 | }); |
| 141 | 142 | |
| 142 | | self.send_message(buffer_handle, content).await; |
| 143 | self.send_message(content).await; |
| 143 | 144 | } |
| 144 | 145 | } |
| 145 | 146 | |
| 146 | 147 | impl MatrixRoom { |
| 148 | /// Send the given content to the server. |
| 149 | /// |
| 150 | /// # Arguments |
| 151 | /// |
| 152 | /// * `content` - The content that should be sent to the server. |
| 153 | /// |
| 154 | /// # Examples |
| 155 | /// |
| 156 | /// ``` |
| 157 | /// let content = MessageEventContent::Text(TextMessageEventContent { |
| 158 | /// body: "Hello world".to_owned(), |
| 159 | /// formatted: None, |
| 160 | /// relates_to: None, |
| 161 | /// }); |
| 162 | /// let content = AnyMessageEventContent::RoomMessage(content); |
| 163 | /// |
| 164 | /// buffer.send_message(content).await |
| 165 | /// ``` |
| 147 | 166 | pub async fn send_message( |
| 148 | 167 | &self, |
| 149 | | buffer: BufferHandle, |
| 150 | 168 | content: MessageEventContent, |
| 151 | 169 | ) { |
| 152 | 170 | let uuid = Uuid::new_v4(); |
| 153 | 171 | |
| 154 | 172 | if let Some(c) = &*self.connection.borrow() { |
| 155 | | self.queue_outgoing_message(buffer.clone(), uuid, &content); |
| 173 | self.queue_outgoing_message(uuid, &content); |
| 156 | 174 | match c |
| 157 | 175 | .send_message( |
| 158 | 176 | &self.room_id, |
| @@ -162,7 +180,7 @@ impl MatrixRoom { |
| 162 | 180 | .await |
| 163 | 181 | { |
| 164 | 182 | Ok(r) => { |
| 165 | | self.handle_outgoing_message(buffer, uuid, &r.event_id); |
| 183 | self.handle_outgoing_message(uuid, &r.event_id); |
| 166 | 184 | } |
| 167 | 185 | Err(_e) => { |
| 168 | 186 | // TODO print out an error, remember to modify the local |
| @@ -171,19 +189,15 @@ impl MatrixRoom { |
| 171 | 189 | } |
| 172 | 190 | } |
| 173 | 191 | } else { |
| 174 | | let buffer = buffer |
| 175 | | .upgrade() |
| 176 | | .expect("Trying to send a message while the buffer is closed"); |
| 177 | | |
| 192 | let buffer = self.buffer(); |
| 178 | 193 | buffer.print("Error not connected"); |
| 179 | 194 | } |
| 180 | 195 | } |
| 181 | 196 | |
| 182 | 197 | // Add the content of the message to our outgoing messag queue and print out |
| 183 | 198 | // a local echo line if local echo is enabled. |
| 184 | | pub fn queue_outgoing_message( |
| 199 | fn queue_outgoing_message( |
| 185 | 200 | &self, |
| 186 | | buffer_handle: BufferHandle, |
| 187 | 201 | uuid: Uuid, |
| 188 | 202 | content: &MessageEventContent, |
| 189 | 203 | ) { |
| @@ -194,11 +208,8 @@ impl MatrixRoom { |
| 194 | 208 | panic!("No own member {}", self.own_user_id) |
| 195 | 209 | }); |
| 196 | 210 | |
| 197 | | if let Ok(b) = buffer_handle.upgrade() { |
| 198 | | let local_echo = |
| 199 | | c.render_with_prefix_for_echo(&sender, uuid, &()); |
| 200 | | self.print_rendered_event(&b, local_echo) |
| 201 | | } |
| 211 | let local_echo = c.render_with_prefix_for_echo(&sender, uuid, &()); |
| 212 | self.print_rendered_event(local_echo); |
| 202 | 213 | |
| 203 | 214 | self.outgoing_messages.add_with_echo(uuid, content.clone()); |
| 204 | 215 | } else { |
| @@ -209,11 +220,12 @@ impl MatrixRoom { |
| 209 | 220 | } |
| 210 | 221 | } |
| 211 | 222 | |
| 212 | | pub fn print_rendered_event( |
| 223 | fn print_rendered_event( |
| 213 | 224 | &self, |
| 214 | | buffer: &Buffer, |
| 215 | 225 | rendered: RenderedEvent, |
| 216 | 226 | ) { |
| 227 | let buffer = self.buffer(); |
| 228 | |
| 217 | 229 | for line in rendered.content.lines { |
| 218 | 230 | let message = format!("{}\t{}", &rendered.prefix, &line.message); |
| 219 | 231 | let tags: Vec<&str> = |
| @@ -248,7 +260,6 @@ impl MatrixRoom { |
| 248 | 260 | |
| 249 | 261 | fn handle_outgoing_message( |
| 250 | 262 | &self, |
| 251 | | buffer_handle: BufferHandle, |
| 252 | 263 | uuid: Uuid, |
| 253 | 264 | event_id: &EventId, |
| 254 | 265 | ) { |
| @@ -267,11 +278,11 @@ impl MatrixRoom { |
| 267 | 278 | .render_message_event(&event) |
| 268 | 279 | .expect("Sent out an event that we don't know how to render"); |
| 269 | 280 | |
| 270 | | if let Ok(buffer) = buffer_handle.upgrade() { |
| 281 | if let Ok(buffer) = self.buffer_handle().upgrade() { |
| 271 | 282 | if echo { |
| 272 | 283 | self.replace_local_echo(uuid, &buffer, rendered); |
| 273 | 284 | } else { |
| 274 | | self.print_rendered_event(&buffer, rendered); |
| 285 | self.print_rendered_event(rendered); |
| 275 | 286 | } |
| 276 | 287 | } |
| 277 | 288 | } |
| @@ -330,156 +341,23 @@ impl MatrixRoom { |
| 330 | 341 | |
| 331 | 342 | Some(rendered) |
| 332 | 343 | } |
| 333 | | } |
| 334 | | |
| 335 | | impl RoomBuffer { |
| 336 | | pub fn new( |
| 337 | | connection: &Rc<RefCell<Option<Connection>>>, |
| 338 | | config: Rc<RefCell<Config>>, |
| 339 | | room: Arc<RwLock<Room>>, |
| 340 | | homeserver: &Url, |
| 341 | | room_id: RoomId, |
| 342 | | own_user_id: &UserId, |
| 343 | | ) -> Self { |
| 344 | | let members = Members::new(room.clone()); |
| 345 | | let mut room = MatrixRoom { |
| 346 | | homeserver: Rc::new(homeserver.clone()), |
| 347 | | room_id: Rc::new(room_id.clone()), |
| 348 | | connection: connection.clone(), |
| 349 | | typing_notice_time: Rc::new(RefCell::new(None)), |
| 350 | | typing_in_flight: Rc::new(Mutex::new(())), |
| 351 | | config, |
| 352 | | room, |
| 353 | | own_user_id: Rc::new(own_user_id.to_owned()), |
| 354 | | members: members.clone(), |
| 355 | | outgoing_messages: MessageQueue::new(), |
| 356 | | }; |
| 357 | | |
| 358 | | let buffer_handle = BufferBuilderAsync::new(&room_id.to_string()) |
| 359 | | .input_callback(room.clone()) |
| 360 | | .close_callback(|_weechat: &Weechat, _buffer: &Buffer| { |
| 361 | | // TODO remove the roombuffer from the server here. |
| 362 | | // TODO leave the room if the plugin isn't unloading. |
| 363 | | Ok(()) |
| 364 | | }) |
| 365 | | .build() |
| 366 | | .expect("Can't create new room buffer"); |
| 367 | | |
| 368 | | let buffer = buffer_handle |
| 369 | | .upgrade() |
| 370 | | .expect("Can't upgrade newly created buffer"); |
| 371 | | |
| 372 | | buffer.enable_nicklist(); |
| 373 | | |
| 374 | | // This is fine since we're only given the room to the buffer input and |
| 375 | | // the callback can only run once we yield controll back to Weechat. |
| 376 | | unsafe { |
| 377 | | *Rc::get_mut_unchecked(&mut room.members.buffer) = |
| 378 | | Some(buffer_handle.clone()); |
| 379 | | } |
| 380 | | |
| 381 | | RoomBuffer { |
| 382 | | inner: room, |
| 383 | | buffer_handle, |
| 384 | | } |
| 385 | | } |
| 386 | | |
| 387 | | pub fn restore( |
| 388 | | room: Arc<RwLock<Room>>, |
| 389 | | connection: &Rc<RefCell<Option<Connection>>>, |
| 390 | | config: Rc<RefCell<Config>>, |
| 391 | | homeserver: &Url, |
| 392 | | ) -> Self { |
| 393 | | let room_clone = room.clone(); |
| 394 | | let room_lock = block_on(room.read()); |
| 395 | | let room_id = room_lock.room_id.to_owned(); |
| 396 | | let own_user_id = &room_lock.own_user_id; |
| 397 | | |
| 398 | | let room_buffer = RoomBuffer::new( |
| 399 | | connection, |
| 400 | | config, |
| 401 | | room_clone, |
| 402 | | homeserver, |
| 403 | | room_id, |
| 404 | | own_user_id, |
| 405 | | ); |
| 406 | | |
| 407 | | debug!("Restoring room {}", room_lock.room_id); |
| 408 | | |
| 409 | | let matrix_members = room_lock |
| 410 | | .joined_members |
| 411 | | .values() |
| 412 | | .chain(room_lock.invited_members.values()); |
| 413 | | |
| 414 | | for member in matrix_members { |
| 415 | | let display_name = room_lock |
| 416 | | .get_member(&member.user_id) |
| 417 | | .unwrap() |
| 418 | | .display_name |
| 419 | | .clone(); |
| 420 | | |
| 421 | | trace!("Restoring member {}", member.user_id); |
| 422 | | let member = WeechatRoomMember::new( |
| 423 | | &member.user_id, |
| 424 | | member.disambiguated_name(), |
| 425 | | display_name, |
| 426 | | ); |
| 427 | | |
| 428 | | room_buffer.members.add(member); |
| 429 | | } |
| 430 | | |
| 431 | | room_buffer.update_buffer_name(); |
| 432 | | room_buffer.restore_messages(); |
| 433 | | |
| 434 | | room_buffer |
| 435 | | } |
| 436 | | |
| 437 | | pub fn restore_messages(&self) { |
| 438 | | use AnyPossiblyRedactedSyncMessageEvent::*; |
| 439 | | |
| 440 | | let room = self.room(); |
| 441 | | let buffer = self.weechat_buffer(); |
| 442 | | |
| 443 | | if buffer.num_lines() == 0 { |
| 444 | | for event in room.messages.iter() { |
| 445 | | match event { |
| 446 | | Regular(e) => self.handle_room_message(e), |
| 447 | | Redacted(e) => self.handle_redacted_events(e), |
| 448 | | } |
| 449 | | } |
| 450 | | } |
| 451 | | } |
| 452 | 344 | |
| 453 | | /// Send the given content to the server. |
| 454 | | /// |
| 455 | | /// # Arguments |
| 456 | | /// |
| 457 | | /// * `content` - The content that should be sent to the server. |
| 458 | | /// |
| 459 | | /// # Examples |
| 460 | | /// |
| 461 | | /// ``` |
| 462 | | /// let content = MessageEventContent::Text(TextMessageEventContent { |
| 463 | | /// body: "Hello world".to_owned(), |
| 464 | | /// formatted: None, |
| 465 | | /// relates_to: None, |
| 466 | | /// }); |
| 467 | | /// let content = AnyMessageEventContent::RoomMessage(content); |
| 468 | | /// |
| 469 | | /// buffer.send_message(content).await |
| 470 | | /// ``` |
| 471 | | pub async fn send_message(&self, content: MessageEventContent) { |
| 472 | | let buffer = self.buffer_handle.clone(); |
| 473 | | self.inner.send_message(buffer, content).await |
| 345 | fn buffer_handle(&self) -> BufferHandle { |
| 346 | (&*self.buffer).as_ref().expect("Room struct wasn't initialized properly").clone() |
| 474 | 347 | } |
| 475 | 348 | |
| 476 | | pub fn weechat_buffer(&self) -> Buffer { |
| 477 | | self.buffer_handle.upgrade().expect(BUFFER_CLOSED_ERROR) |
| 349 | pub fn buffer(&self) -> Buffer { |
| 350 | self.buffer |
| 351 | .as_ref() |
| 352 | .as_ref() |
| 353 | .expect("Room struct wasn't initialized properly") |
| 354 | .upgrade() |
| 355 | .expect(BUFFER_CLOSED_ERROR) |
| 478 | 356 | } |
| 479 | 357 | |
| 480 | | pub fn update_buffer_name(&self) { |
| 358 | fn update_buffer_name(&self) { |
| 481 | 359 | let name = self.members.calculate_buffer_name(); |
| 482 | | self.weechat_buffer().set_name(&name) |
| 360 | self.buffer().set_name(&name) |
| 483 | 361 | } |
| 484 | 362 | |
| 485 | 363 | /// Send out a typing notice. |
| @@ -493,8 +371,8 @@ impl RoomBuffer { |
| 493 | 371 | /// |
| 494 | 372 | /// If the input is empty the typing notice is disabled. |
| 495 | 373 | pub fn update_typing_notice(&self) { |
| 496 | | let typing_in_flight = self.inner.typing_in_flight.clone(); |
| 497 | | let buffer = self.weechat_buffer(); |
| 374 | let typing_in_flight = self.typing_in_flight.clone(); |
| 375 | let buffer = self.buffer(); |
| 498 | 376 | let input = buffer.input(); |
| 499 | 377 | |
| 500 | 378 | if input.starts_with('/') && !input.starts_with("//") { |
| @@ -502,9 +380,9 @@ impl RoomBuffer { |
| 502 | 380 | return; |
| 503 | 381 | } |
| 504 | 382 | |
| 505 | | let connection = self.inner.connection.clone(); |
| 506 | | let room_id = self.inner.room_id.clone(); |
| 507 | | let typing_notice_time = self.inner.typing_notice_time.clone(); |
| 383 | let connection = self.connection.clone(); |
| 384 | let room_id = self.room_id.clone(); |
| 385 | let typing_notice_time = self.typing_notice_time.clone(); |
| 508 | 386 | |
| 509 | 387 | let send = async move |typing: bool| { |
| 510 | 388 | let typing_time = typing_notice_time; |
| @@ -542,7 +420,7 @@ impl RoomBuffer { |
| 542 | 420 | drop(guard) |
| 543 | 421 | }; |
| 544 | 422 | |
| 545 | | let typing_time = self.inner.typing_notice_time.borrow_mut(); |
| 423 | let typing_time = self.typing_notice_time.borrow_mut(); |
| 546 | 424 | |
| 547 | 425 | if input.len() < 4 && typing_time.is_some() { |
| 548 | 426 | // If we have an active typing notice and our input is short, e.g. |
| @@ -563,14 +441,13 @@ impl RoomBuffer { |
| 563 | 441 | } |
| 564 | 442 | } |
| 565 | 443 | |
| 566 | | pub fn handle_room_message(&self, event: &AnySyncMessageEvent) { |
| 444 | fn handle_room_message(&self, event: &AnySyncMessageEvent) { |
| 567 | 445 | // If the event has a transaction id it's an event that we sent out |
| 568 | 446 | // ourselves, the content will be in the outgoing message queue and it |
| 569 | 447 | // may have been printed out as a local echo. |
| 570 | 448 | if let Some(id) = &event.unsigned().transaction_id { |
| 571 | 449 | if let Ok(id) = Uuid::parse_str(id) { |
| 572 | 450 | self.handle_outgoing_message( |
| 573 | | self.buffer_handle.clone(), |
| 574 | 451 | id, |
| 575 | 452 | event.event_id(), |
| 576 | 453 | ); |
| @@ -583,11 +460,6 @@ impl RoomBuffer { |
| 583 | 460 | } |
| 584 | 461 | } |
| 585 | 462 | |
| 586 | | fn print_rendered_event(&self, rendered: RenderedEvent) { |
| 587 | | let buffer = self.weechat_buffer(); |
| 588 | | self.inner.print_rendered_event(&buffer, rendered); |
| 589 | | } |
| 590 | | |
| 591 | 463 | fn handle_redacted_events(&self, event: &AnyRedactedSyncMessageEvent) { |
| 592 | 464 | use AnyRedactedSyncMessageEvent::*; |
| 593 | 465 | |
| @@ -608,7 +480,7 @@ impl RoomBuffer { |
| 608 | 480 | } |
| 609 | 481 | } |
| 610 | 482 | |
| 611 | | pub fn handle_sync_room_event(&mut self, event: AnySyncRoomEvent) { |
| 483 | pub fn handle_sync_room_event(&self, event: AnySyncRoomEvent) { |
| 612 | 484 | match &event { |
| 613 | 485 | AnySyncRoomEvent::Message(message) => { |
| 614 | 486 | self.handle_room_message(message) |
| @@ -630,7 +502,7 @@ impl RoomBuffer { |
| 630 | 502 | } |
| 631 | 503 | } |
| 632 | 504 | |
| 633 | | pub fn handle_sync_state_event(&mut self, event: AnySyncStateEvent) { |
| 505 | pub fn handle_sync_state_event(&self, event: AnySyncStateEvent) { |
| 634 | 506 | match &event { |
| 635 | 507 | AnySyncStateEvent::RoomMember(e) => { |
| 636 | 508 | self.members.handle_membership_event(e, true) |
| @@ -640,3 +512,124 @@ impl RoomBuffer { |
| 640 | 512 | } |
| 641 | 513 | } |
| 642 | 514 | } |
| 515 | |
| 516 | impl RoomBuffer { |
| 517 | pub fn new( |
| 518 | connection: &Rc<RefCell<Option<Connection>>>, |
| 519 | config: Rc<RefCell<Config>>, |
| 520 | room: Arc<RwLock<Room>>, |
| 521 | homeserver: &Url, |
| 522 | room_id: RoomId, |
| 523 | own_user_id: &UserId, |
| 524 | ) -> Self { |
| 525 | let members = Members::new(room.clone()); |
| 526 | |
| 527 | let mut room = MatrixRoom { |
| 528 | homeserver: Rc::new(homeserver.clone()), |
| 529 | room_id: Rc::new(room_id.clone()), |
| 530 | connection: connection.clone(), |
| 531 | typing_notice_time: Rc::new(RefCell::new(None)), |
| 532 | typing_in_flight: Rc::new(Mutex::new(())), |
| 533 | config, |
| 534 | room, |
| 535 | own_user_id: Rc::new(own_user_id.to_owned()), |
| 536 | members: members.clone(), |
| 537 | buffer: members.buffer.clone(), |
| 538 | outgoing_messages: MessageQueue::new(), |
| 539 | }; |
| 540 | |
| 541 | let buffer_handle = BufferBuilderAsync::new(&room_id.to_string()) |
| 542 | .input_callback(room.clone()) |
| 543 | .close_callback(|_weechat: &Weechat, _buffer: &Buffer| { |
| 544 | // TODO remove the roombuffer from the server here. |
| 545 | // TODO leave the room if the plugin isn't unloading. |
| 546 | Ok(()) |
| 547 | }) |
| 548 | .build() |
| 549 | .expect("Can't create new room buffer"); |
| 550 | |
| 551 | let buffer = buffer_handle |
| 552 | .upgrade() |
| 553 | .expect("Can't upgrade newly created buffer"); |
| 554 | |
| 555 | buffer.enable_nicklist(); |
| 556 | |
| 557 | // This is fine since we're only given the room to the buffer input and |
| 558 | // the callback can only run once we yield controll back to Weechat. |
| 559 | unsafe { |
| 560 | *Rc::get_mut_unchecked(&mut room.members.buffer) = |
| 561 | Some(buffer_handle.clone()); |
| 562 | } |
| 563 | |
| 564 | RoomBuffer { |
| 565 | inner: room, |
| 566 | buffer_handle, |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | pub fn restore( |
| 571 | room: Arc<RwLock<Room>>, |
| 572 | connection: &Rc<RefCell<Option<Connection>>>, |
| 573 | config: Rc<RefCell<Config>>, |
| 574 | homeserver: &Url, |
| 575 | ) -> Self { |
| 576 | let room_clone = room.clone(); |
| 577 | let room_lock = block_on(room.read()); |
| 578 | let room_id = room_lock.room_id.to_owned(); |
| 579 | let own_user_id = &room_lock.own_user_id; |
| 580 | |
| 581 | let room_buffer = Self::new( |
| 582 | connection, |
| 583 | config, |
| 584 | room_clone, |
| 585 | homeserver, |
| 586 | room_id, |
| 587 | own_user_id, |
| 588 | ); |
| 589 | |
| 590 | debug!("Restoring room {}", room_lock.room_id); |
| 591 | |
| 592 | let matrix_members = room_lock |
| 593 | .joined_members |
| 594 | .values() |
| 595 | .chain(room_lock.invited_members.values()); |
| 596 | |
| 597 | for member in matrix_members { |
| 598 | let display_name = room_lock |
| 599 | .get_member(&member.user_id) |
| 600 | .unwrap() |
| 601 | .display_name |
| 602 | .clone(); |
| 603 | |
| 604 | trace!("Restoring member {}", member.user_id); |
| 605 | let member = WeechatRoomMember::new( |
| 606 | &member.user_id, |
| 607 | member.disambiguated_name(), |
| 608 | display_name, |
| 609 | ); |
| 610 | |
| 611 | room_buffer.members.add(member); |
| 612 | } |
| 613 | |
| 614 | room_buffer.update_buffer_name(); |
| 615 | room_buffer.restore_messages(); |
| 616 | |
| 617 | room_buffer |
| 618 | } |
| 619 | |
| 620 | pub fn restore_messages(&self) { |
| 621 | use AnyPossiblyRedactedSyncMessageEvent::*; |
| 622 | |
| 623 | let room = self.room(); |
| 624 | let buffer = self.buffer(); |
| 625 | |
| 626 | if buffer.num_lines() == 0 { |
| 627 | for event in room.messages.iter() { |
| 628 | match event { |
| 629 | Regular(e) => self.handle_room_message(e), |
| 630 | Redacted(e) => self.handle_redacted_events(e), |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | } |
| 635 | } |