diff --git a/backend/libraries/group_chat_core/src/members.rs b/backend/libraries/group_chat_core/src/members.rs index 351071edac..f381f70d87 100644 --- a/backend/libraries/group_chat_core/src/members.rs +++ b/backend/libraries/group_chat_core/src/members.rs @@ -652,26 +652,57 @@ pub struct ChangeRoleSuccess { pub prev_role: GroupRoleInternal, } -#[derive(Clone)] +#[derive(Serialize, Deserialize, Clone)] pub struct GroupMemberInternal { + #[serde(rename = "u")] user_id: UserId, + #[serde(rename = "d")] date_added: TimestampMillis, + #[serde(rename = "r", default, skip_serializing_if = "is_default")] role: Timestamped, + #[serde( + rename = "n", + default = "default_notifications_muted", + skip_serializing_if = "is_default_notifications_muted" + )] notifications_muted: Timestamped, + #[serde(rename = "m", default, skip_serializing_if = "Mentions::is_empty")] pub mentions: Mentions, + #[serde(rename = "tf", default, skip_serializing_if = "TimestampedSet::is_empty")] pub followed_threads: TimestampedSet, + #[serde(rename = "tu", default, skip_serializing_if = "TimestampedSet::is_empty")] pub unfollowed_threads: TimestampedSet, + #[serde(rename = "p", default, skip_serializing_if = "BTreeSet::is_empty")] proposal_votes: BTreeSet<(TimestampMillis, MessageIndex)>, + #[serde(rename = "pr", default, skip_serializing_if = "is_default")] latest_proposal_vote_removed: TimestampMillis, + #[serde(rename = "s", default, skip_serializing_if = "is_default")] suspended: Timestamped, + #[serde(rename = "ra", default, skip_serializing_if = "is_default")] pub rules_accepted: Option>, + #[serde(rename = "ut", default, skip_serializing_if = "is_default")] user_type: UserType, + #[serde(rename = "me", default, skip_serializing_if = "is_default")] min_visible_event_index: EventIndex, + #[serde(rename = "mm", default, skip_serializing_if = "is_default")] min_visible_message_index: MessageIndex, + #[serde(rename = "la", default, skip_serializing_if = "is_default")] lapsed: Timestamped, } impl GroupMemberInternal { + pub fn set_default_timestamps(&mut self) { + if self.role.timestamp <= self.date_added { + self.role.timestamp = 0; + } + if self.notifications_muted.timestamp <= self.date_added { + self.notifications_muted.timestamp = 0; + } + if self.lapsed.timestamp <= self.date_added { + self.lapsed.timestamp = 0; + } + } + pub fn user_id(&self) -> UserId { self.user_id } @@ -913,18 +944,6 @@ impl GroupMemberStableStorage { lapsed: self.lapsed, } } - - pub fn set_default_timestamps(&mut self) { - if self.role.timestamp <= self.date_added { - self.role.timestamp = 0; - } - if self.notifications_muted.timestamp <= self.date_added { - self.notifications_muted.timestamp = 0; - } - if self.lapsed.timestamp <= self.date_added { - self.lapsed.timestamp = 0; - } - } } impl From for GroupMemberStableStorage { diff --git a/backend/libraries/group_chat_core/src/members_map.rs b/backend/libraries/group_chat_core/src/members_map.rs index d320253a8c..ee425476a5 100644 --- a/backend/libraries/group_chat_core/src/members_map.rs +++ b/backend/libraries/group_chat_core/src/members_map.rs @@ -1,4 +1,4 @@ -use crate::{GroupMemberInternal, GroupMemberStableStorage}; +use crate::GroupMemberInternal; use serde::de::{SeqAccess, Visitor}; use serde::ser::SerializeSeq; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -16,38 +16,38 @@ pub trait MembersMap { } pub struct HeapMembersMap { - map: BTreeMap, + map: BTreeMap, } impl HeapMembersMap { pub fn new(member: GroupMemberInternal) -> Self { HeapMembersMap { - map: [(member.user_id(), member.into())].into_iter().collect(), + map: [(member.user_id(), member)].into_iter().collect(), } } // TODO Remove after next upgrade - pub fn values_mut(&mut self) -> impl Iterator + '_ { + pub fn values_mut(&mut self) -> impl Iterator + '_ { self.map.values_mut() } } impl MembersMap for HeapMembersMap { fn get(&self, user_id: &UserId) -> Option { - self.map.get(user_id).cloned().map(|g| g.hydrate(*user_id)) + self.map.get(user_id).cloned() } fn insert(&mut self, member: GroupMemberInternal) { - self.map.insert(member.user_id(), member.into()); + self.map.insert(member.user_id(), member); } fn remove(&mut self, user_id: &UserId) -> Option { - self.map.remove(user_id).map(|g| g.hydrate(*user_id)) + self.map.remove(user_id) } #[cfg(test)] fn all_members(&self) -> Vec { - self.map.iter().map(|(k, v)| v.clone().hydrate(*k)).collect() + self.map.values().cloned().collect() } } @@ -57,12 +57,8 @@ impl Serialize for HeapMembersMap { S: Serializer, { let mut seq = serializer.serialize_seq(Some(self.map.len()))?; - for (user_id, member) in self.map.iter() { - let value = GroupMembersMapEntry { - user_id: *user_id, - details: member.clone(), - }; - seq.serialize_element(&value)?; + for member in self.map.values() { + seq.serialize_element(member)?; } seq.end() } @@ -91,17 +87,9 @@ impl<'de> Visitor<'de> for GroupMembersMapVisitor { A: SeqAccess<'de>, { let mut map = BTreeMap::new(); - while let Some(next) = seq.next_element::()? { - map.insert(next.user_id, next.details); + while let Some(next) = seq.next_element::()? { + map.insert(next.user_id(), next); } Ok(HeapMembersMap { map }) } } - -#[derive(Serialize, Deserialize)] -struct GroupMembersMapEntry { - #[serde(rename = "u")] - user_id: UserId, - #[serde(flatten)] - details: GroupMemberStableStorage, -}