Commit 7725b07f85
Unregistered key
Layout: unified · split
src/windows/room/message.rs +61 −1
| @@ -7,6 +7,22 @@ use crate::preview::ImageStatus; | ||
| 7 | 7 | use crate::util::space; |
| 8 | 8 | use crate::windows::room::chat; |
| 9 | 9 | |
| 10 | fn contains_reaction( | |
| 11 | info: &RoomInfo, | |
| 12 | key: &str, | |
| 13 | message_id: &MessageId, | |
| 14 | settings: &ApplicationSettings, | |
| 15 | ) -> bool { | |
| 16 | message_id | |
| 17 | .as_origin() | |
| 18 | .and_then(|event_id| info.reactions.get(event_id)) | |
| 19 | .is_some_and(|reacts| { | |
| 20 | reacts | |
| 21 | .values() | |
| 22 | .any(|(k, user, _)| user == &settings.profile.user_id && k == key) | |
| 23 | }) | |
| 24 | } | |
| 25 | ||
| 10 | 26 | /// State needed for rendering [MessageWidget]. |
| 11 | 27 | pub(crate) struct MessageState { |
| 12 | 28 | room: MatrixRoom, |
| @@ -347,6 +363,12 @@ impl StatefulWidget for MessageWidget<'_> { | ||
| 347 | 363 | } |
| 348 | 364 | |
| 349 | 365 | let section = Cow::from(content); |
| 366 | items.push(MessageInfoItem::Reaction( | |
| 367 | section.clone(), | |
| 368 | key.to_string(), | |
| 369 | state.room_id().to_owned(), | |
| 370 | state.id(), | |
| 371 | )); | |
| 350 | 372 | items.extend(users.into_iter().map(|user_id| { |
| 351 | 373 | MessageInfoItem::User( |
| 352 | 374 | section.clone(), |
| @@ -447,6 +469,9 @@ pub(crate) enum MessageInfoItem { | ||
| 447 | 469 | /// The message itself |
| 448 | 470 | Message(OwnedRoomId, MessageId), |
| 449 | 471 | |
| 472 | /// The line to interactively un-/react | |
| 473 | Reaction(Cow<'static, str>, String, OwnedRoomId, MessageId), | |
| 474 | ||
| 450 | 475 | User(Cow<'static, str>, OwnedRoomId, OwnedUserId), |
| 451 | 476 | Link(char, Url), |
| 452 | 477 | } |
| @@ -463,6 +488,7 @@ impl Display for MessageInfoItem { | ||
| 463 | 488 | write!(f, "{user_id}") |
| 464 | 489 | }, |
| 465 | 490 | MessageInfoItem::Link(_, url) => write!(f, "{url}"), |
| 491 | MessageInfoItem::Reaction(_, key, ..) => write!(f, "{key}"), | |
| 466 | 492 | } |
| 467 | 493 | } |
| 468 | 494 | } |
| @@ -545,6 +571,17 @@ impl ListItem<IambInfo> for MessageInfoItem { | ||
| 545 | 571 | MessageInfoItem::Link(c, url) => { |
| 546 | 572 | Text::from(Span::styled(format!("[{c}] {url}"), style.bold())) |
| 547 | 573 | }, |
| 574 | MessageInfoItem::Reaction(_, key, room_id, message_id) => { | |
| 575 | let Some(info) = store.application.rooms.get(room_id) else { | |
| 576 | return Span::styled("Loading room...", style.fg(Color::Gray)).into(); | |
| 577 | }; | |
| 578 | ||
| 579 | if contains_reaction(info, key, message_id, &store.application.settings) { | |
| 580 | Text::from(Span::styled(format!("Press Enter to unreact with '{key}'"), style)) | |
| 581 | } else { | |
| 582 | Text::from(Span::styled(format!("Press Enter to react with '{key}'"), style)) | |
| 583 | } | |
| 584 | }, | |
| 548 | 585 | } |
| 549 | 586 | } |
| 550 | 587 | |
| @@ -558,6 +595,7 @@ impl ListItem<IambInfo> for MessageInfoItem { | ||
| 558 | 595 | Some(user_id.to_string()) |
| 559 | 596 | }, |
| 560 | 597 | MessageInfoItem::Link(.., url) => Some(url.to_string()), |
| 598 | MessageInfoItem::Reaction(_, key, ..) => Some(key.to_string()), | |
| 561 | 599 | } |
| 562 | 600 | } |
| 563 | 601 | |
| @@ -566,6 +604,7 @@ impl ListItem<IambInfo> for MessageInfoItem { | ||
| 566 | 604 | MessageInfoItem::Header(..) | MessageInfoItem::Message(..) => None, |
| 567 | 605 | MessageInfoItem::User(section, ..) => Some(section), |
| 568 | 606 | MessageInfoItem::Link(..) => Some(&Cow::Borrowed("Links:")), |
| 607 | MessageInfoItem::Reaction(section, ..) => Some(section), | |
| 569 | 608 | } |
| 570 | 609 | } |
| 571 | 610 | } |
| @@ -575,7 +614,7 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for MessageInfoItem { | ||
| 575 | 614 | &mut self, |
| 576 | 615 | act: &PromptAction, |
| 577 | 616 | ctx: &ProgramContext, |
| 578 | _: &mut ProgramStore, | |
| 617 | store: &mut ProgramStore, | |
| 579 | 618 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { |
| 580 | 619 | match act { |
| 581 | 620 | PromptAction::Submit => { |
| @@ -604,6 +643,27 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for MessageInfoItem { | ||
| 604 | 643 | let act = IambAction::OpenLink(url.to_string()); |
| 605 | 644 | Ok(vec![(act.into(), ctx.clone())]) |
| 606 | 645 | }, |
| 646 | MessageInfoItem::Reaction(_, key, room_id, message_id) => { | |
| 647 | let Some(info) = store.application.rooms.get(room_id) else { | |
| 648 | let msg = "Room not loaded."; | |
| 649 | let err = EditError::Failure(msg.into()); | |
| 650 | ||
| 651 | return Err(err); | |
| 652 | }; | |
| 653 | ||
| 654 | let act = if contains_reaction( | |
| 655 | info, | |
| 656 | key, | |
| 657 | message_id, | |
| 658 | &store.application.settings, | |
| 659 | ) { | |
| 660 | MessageAction::Unreact(Some(key.to_string()), true) | |
| 661 | } else { | |
| 662 | MessageAction::React(key.to_string(), true) | |
| 663 | }; | |
| 664 | let act: IambAction = act.into(); | |
| 665 | Ok(vec![(act.into(), ctx.clone())]) | |
| 666 | }, | |
| 607 | 667 | } |
| 608 | 668 | }, |
| 609 | 669 | PromptAction::Abort(..) => { |