Skip to content

Commit

Permalink
Avoid using serde(flatten) because it doesn't work with u128 valu…
Browse files Browse the repository at this point in the history
…es (#6958)
  • Loading branch information
hpeebles authored Dec 3, 2024
1 parent 1f8f4f0 commit c5c78fc
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
45 changes: 32 additions & 13 deletions backend/libraries/group_chat_core/src/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<GroupRoleInternal>,
#[serde(
rename = "n",
default = "default_notifications_muted",
skip_serializing_if = "is_default_notifications_muted"
)]
notifications_muted: Timestamped<bool>,
#[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<MessageIndex>,
#[serde(rename = "tu", default, skip_serializing_if = "TimestampedSet::is_empty")]
pub unfollowed_threads: TimestampedSet<MessageIndex>,
#[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<bool>,
#[serde(rename = "ra", default, skip_serializing_if = "is_default")]
pub rules_accepted: Option<Timestamped<Version>>,
#[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<bool>,
}

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
}
Expand Down Expand Up @@ -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<GroupMemberInternal> for GroupMemberStableStorage {
Expand Down
36 changes: 12 additions & 24 deletions backend/libraries/group_chat_core/src/members_map.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -16,38 +16,38 @@ pub trait MembersMap {
}

pub struct HeapMembersMap {
map: BTreeMap<UserId, GroupMemberStableStorage>,
map: BTreeMap<UserId, GroupMemberInternal>,
}

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<Item = &mut GroupMemberStableStorage> + '_ {
pub fn values_mut(&mut self) -> impl Iterator<Item = &mut GroupMemberInternal> + '_ {
self.map.values_mut()
}
}

impl MembersMap for HeapMembersMap {
fn get(&self, user_id: &UserId) -> Option<GroupMemberInternal> {
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<GroupMemberInternal> {
self.map.remove(user_id).map(|g| g.hydrate(*user_id))
self.map.remove(user_id)
}

#[cfg(test)]
fn all_members(&self) -> Vec<GroupMemberInternal> {
self.map.iter().map(|(k, v)| v.clone().hydrate(*k)).collect()
self.map.values().cloned().collect()
}
}

Expand All @@ -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()
}
Expand Down Expand Up @@ -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::<GroupMembersMapEntry>()? {
map.insert(next.user_id, next.details);
while let Some(next) = seq.next_element::<GroupMemberInternal>()? {
map.insert(next.user_id(), next);
}
Ok(HeapMembersMap { map })
}
}

#[derive(Serialize, Deserialize)]
struct GroupMembersMapEntry {
#[serde(rename = "u")]
user_id: UserId,
#[serde(flatten)]
details: GroupMemberStableStorage,
}

0 comments on commit c5c78fc

Please sign in to comment.