Commit 7725b07f85
Unregistered key
Layout: unified · split
src/windows/room/message.rs +61 −1
| @@ -7,6 +7,22 @@ use crate::preview::ImageStatus; | |||
| 7 | use crate::util::space; | 7 | use crate::util::space; |
| 8 | use crate::windows::room::chat; | 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 | /// State needed for rendering [MessageWidget]. | 26 | /// State needed for rendering [MessageWidget]. |
| 11 | pub(crate) struct MessageState { | 27 | pub(crate) struct MessageState { |
| 12 | room: MatrixRoom, | 28 | room: MatrixRoom, |
| @@ -347,6 +363,12 @@ impl StatefulWidget for MessageWidget<'_> { | |||
| 347 | } | 363 | } |
| 348 | 364 | ||
| 349 | let section = Cow::from(content); | 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 | items.extend(users.into_iter().map(|user_id| { | 372 | items.extend(users.into_iter().map(|user_id| { |
| 351 | MessageInfoItem::User( | 373 | MessageInfoItem::User( |
| 352 | section.clone(), | 374 | section.clone(), |
| @@ -447,6 +469,9 @@ pub(crate) enum MessageInfoItem { | |||
| 447 | /// The message itself | 469 | /// The message itself |
| 448 | Message(OwnedRoomId, MessageId), | 470 | Message(OwnedRoomId, MessageId), |
| 449 | 471 | ||
| 472 | /// The line to interactively un-/react | ||
| 473 | Reaction(Cow<'static, str>, String, OwnedRoomId, MessageId), | ||
| 474 | |||
| 450 | User(Cow<'static, str>, OwnedRoomId, OwnedUserId), | 475 | User(Cow<'static, str>, OwnedRoomId, OwnedUserId), |
| 451 | Link(char, Url), | 476 | Link(char, Url), |
| 452 | } | 477 | } |
| @@ -463,6 +488,7 @@ impl Display for MessageInfoItem { | |||
| 463 | write!(f, "{user_id}") | 488 | write!(f, "{user_id}") |
| 464 | }, | 489 | }, |
| 465 | MessageInfoItem::Link(_, url) => write!(f, "{url}"), | 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 | MessageInfoItem::Link(c, url) => { | 571 | MessageInfoItem::Link(c, url) => { |
| 546 | Text::from(Span::styled(format!("[{c}] {url}"), style.bold())) | 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 | Some(user_id.to_string()) | 595 | Some(user_id.to_string()) |
| 559 | }, | 596 | }, |
| 560 | MessageInfoItem::Link(.., url) => Some(url.to_string()), | 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 | MessageInfoItem::Header(..) | MessageInfoItem::Message(..) => None, | 604 | MessageInfoItem::Header(..) | MessageInfoItem::Message(..) => None, |
| 567 | MessageInfoItem::User(section, ..) => Some(section), | 605 | MessageInfoItem::User(section, ..) => Some(section), |
| 568 | MessageInfoItem::Link(..) => Some(&Cow::Borrowed("Links:")), | 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 | &mut self, | 614 | &mut self, |
| 576 | act: &PromptAction, | 615 | act: &PromptAction, |
| 577 | ctx: &ProgramContext, | 616 | ctx: &ProgramContext, |
| 578 | _: &mut ProgramStore, | 617 | store: &mut ProgramStore, |
| 579 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { | 618 | ) -> EditResult<Vec<(ProgramAction, ProgramContext)>, IambInfo> { |
| 580 | match act { | 619 | match act { |
| 581 | PromptAction::Submit => { | 620 | PromptAction::Submit => { |
| @@ -604,6 +643,27 @@ impl Promptable<ProgramContext, ProgramStore, IambInfo> for MessageInfoItem { | |||
| 604 | let act = IambAction::OpenLink(url.to_string()); | 643 | let act = IambAction::OpenLink(url.to_string()); |
| 605 | Ok(vec![(act.into(), ctx.clone())]) | 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 | PromptAction::Abort(..) => { | 669 | PromptAction::Abort(..) => { |