Skip to main content

imessage_database/tables/messages/models/
group_action.rs

1/*!
2 Group actions encoded by a message row.
3*/
4
5use crate::tables::messages::message::Message;
6
7/// Group action encoded by a message row.
8#[derive(Debug, PartialEq, Eq)]
9pub enum GroupAction<'a> {
10    /// Participant was added to the group.
11    ParticipantAdded(i32),
12    /// Participant was removed from the group.
13    ParticipantRemoved(i32),
14    /// Group name changed.
15    NameChange(&'a str),
16    /// Participant left the group.
17    ParticipantLeft,
18    /// Group icon/avatar changed.
19    GroupIconChanged,
20    /// Group icon/avatar was removed.
21    GroupIconRemoved,
22    /// Chat background changed.
23    ChatBackgroundChanged,
24    /// Chat background was removed.
25    ChatBackgroundRemoved,
26    /// Participant changed their phone number.
27    PhoneNumberChanged(i32),
28}
29
30impl<'a> GroupAction<'a> {
31    /// Parse group action fields from a message row.
32    #[must_use]
33    pub(crate) fn from_message(message: &'a Message) -> Option<Self> {
34        match (
35            message.item_type,
36            message.group_action_type,
37            message.other_handle,
38            &message.group_title,
39        ) {
40            // If the handle_id of the message matches the other_handle, the sender changed their own phone number
41            (1, 0, Some(who), _) if message.handle_id == Some(who) => {
42                Some(Self::PhoneNumberChanged(who))
43            }
44            (1, 0, Some(who), _) => Some(Self::ParticipantAdded(who)),
45            (1, 1, Some(who), _) => Some(Self::ParticipantRemoved(who)),
46            (2, _, _, Some(name)) => Some(Self::NameChange(name)),
47            (3, 0, _, _) => Some(Self::ParticipantLeft),
48            (3, 1, _, _) => Some(Self::GroupIconChanged),
49            (3, 2, _, _) => Some(Self::GroupIconRemoved),
50            (3, 4, _, _) => Some(Self::ChatBackgroundChanged),
51            (3, 6, _, _) => Some(Self::ChatBackgroundRemoved),
52            _ => None,
53        }
54    }
55}