tauri_plugin_matrix_svelte/matrix/room/message/
message_details.rs

1use bitflags::bitflags;
2use matrix_sdk::ruma::OwnedEventId;
3use matrix_sdk_ui::timeline::EventTimelineItem;
4
5use crate::matrix::user_power_level::UserPowerLevels;
6
7bitflags! {
8    /// Possible actions that the user can perform on a message.
9    ///
10    /// This is used to determine which buttons to show in the message context menu.
11    #[derive(Copy, Clone, Debug)]
12    pub struct MessageAbilities: u8 {
13        /// Whether the user can react to this message.
14        const CanReact = 1 << 0;
15        /// Whether the user can reply to this message.
16        const CanReplyTo = 1 << 1;
17        /// Whether the user can edit this message.
18        const CanEdit = 1 << 2;
19        /// Whether the user can pin this message.
20        const CanPin = 1 << 3;
21        /// Whether the user can unpin this message.
22        const CanUnpin = 1 << 4;
23        /// Whether the user can delete/redact this message.
24        const CanDelete = 1 << 5;
25        /// Whether this message contains HTML content that the user can copy.
26        const HasHtml = 1 << 6;
27    }
28}
29impl MessageAbilities {
30    pub fn from_user_power_and_event(
31        user_power_levels: &UserPowerLevels,
32        event_tl_item: &EventTimelineItem,
33        has_html: bool,
34    ) -> Self {
35        let mut abilities = Self::empty();
36        abilities.set(Self::CanEdit, event_tl_item.is_editable());
37        // Currently we only support deleting one's own messages.
38        if event_tl_item.is_own() {
39            abilities.set(Self::CanDelete, user_power_levels.can_redact_own());
40        }
41        abilities.set(Self::CanReplyTo, event_tl_item.can_be_replied_to());
42        abilities.set(Self::CanPin, user_power_levels.can_pin());
43        // TODO: currently we don't differentiate between pin and unpin,
44        //       but we should first check whether the given message is already pinned
45        //       before deciding which ability to set.
46        // abilities.set(Self::CanUnPin, user_power_levels.can_pin_unpin());
47        abilities.set(Self::CanReact, user_power_levels.can_send_reaction());
48        abilities.set(Self::HasHtml, has_html);
49        abilities
50    }
51}
52
53/// Details about the message that define its context menu content.
54#[derive(Clone, Debug)]
55pub struct MessageDetails {
56    /// The Event ID of the message. If `None`, it is an unsent local event.
57    pub event_id: Option<OwnedEventId>,
58    /// The index of this message in its room's timeline.
59    pub item_id: usize,
60    /// The event ID of the message that this message is related to, if any,
61    /// such as the replied-to message.
62    pub related_event_id: Option<OwnedEventId>,
63    /// The abilities that the user has on this message.
64    pub abilities: MessageAbilities,
65}