Skip to content

Commit

Permalink
backup: Move GroupData-related processing to a new file
Browse files Browse the repository at this point in the history
  • Loading branch information
jrose-signal committed Aug 19, 2024
1 parent c155166 commit 9f6ceee
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 48 deletions.
53 changes: 5 additions & 48 deletions rust/message-backup/src/backup/recipient.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use derive_where::derive_where;
use itertools::Itertools as _;
use libsignal_core::{Aci, Pni, ServiceIdKind};
use uuid::Uuid;
use zkgroup::{GroupMasterKeyBytes, ProfileKeyBytes};
use zkgroup::ProfileKeyBytes;

use crate::backup::call::{CallLink, CallLinkError};
use crate::backup::frame::RecipientId;
Expand All @@ -22,6 +22,9 @@ use crate::backup::{ReferencedTypes, TryFromWith, TryIntoWith};
use crate::proto::backup as proto;
use crate::proto::backup::recipient::Destination as RecipientDestination;

mod group;
use group::*;

#[derive(Debug, thiserror::Error, displaydoc::Display)]
#[cfg_attr(test, derive(PartialEq))]
pub enum RecipientError {
Expand Down Expand Up @@ -149,18 +152,6 @@ pub struct ContactData {
pub hide_story: bool,
}

#[derive(Debug, serde::Serialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct GroupData {
pub master_key: GroupMasterKeyBytes,
pub whitelisted: bool,
pub hide_story: bool,
#[serde(serialize_with = "serialize::enum_as_string")]
pub story_send_mode: proto::group::StorySendMode,
#[serde(serialize_with = "serialize::optional_proto_message_as_bytes")]
pub snapshot: Option<Box<proto::group::GroupSnapshot>>,
}

#[derive(Debug, serde::Serialize)]
#[cfg_attr(test, derive(PartialEq))]
pub enum DistributionListItem<Recipient> {
Expand Down Expand Up @@ -386,41 +377,6 @@ impl TryFrom<proto::Contact> for ContactData {
}
}

impl TryFrom<proto::Group> for GroupData {
type Error = RecipientError;
fn try_from(value: proto::Group) -> Result<Self, Self::Error> {
let proto::Group {
masterKey,
whitelisted,
hideStory,
storySendMode,
snapshot,
special_fields: _,
} = value;

let master_key = masterKey
.try_into()
.map_err(|_| RecipientError::InvalidMasterKey)?;

let story_send_mode = match storySendMode.enum_value_or_default() {
s @ (proto::group::StorySendMode::DEFAULT
| proto::group::StorySendMode::DISABLED
| proto::group::StorySendMode::ENABLED) => s,
};

// TODO consider additional group snapshot validation.
let snapshot = snapshot.0;

Ok(GroupData {
master_key,
whitelisted,
hide_story: hideStory,
story_send_mode,
snapshot,
})
}
}

impl<R: Clone, C: LookupPair<RecipientId, DestinationKind, R>>
TryFromWith<proto::DistributionListItem, C> for DistributionListItem<R>
{
Expand Down Expand Up @@ -518,6 +474,7 @@ mod test {
use once_cell::sync::Lazy;
use protobuf::EnumOrUnknown;
use test_case::test_case;
use zkgroup::GroupMasterKeyBytes;

use crate::backup::method::{Contains, Lookup, Store};
use crate::backup::FullRecipientData;
Expand Down
57 changes: 57 additions & 0 deletions rust/message-backup/src/backup/recipient/group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// Copyright (C) 2024 Signal Messenger, LLC.
// SPDX-License-Identifier: AGPL-3.0-only
//

use zkgroup::GroupMasterKeyBytes;

use crate::backup::recipient::RecipientError;
use crate::backup::serialize;
use crate::proto::backup as proto;

#[derive(Debug, serde::Serialize)]
#[cfg_attr(test, derive(PartialEq))]
pub struct GroupData {
pub master_key: GroupMasterKeyBytes,
pub whitelisted: bool,
pub hide_story: bool,
#[serde(serialize_with = "serialize::enum_as_string")]
pub story_send_mode: proto::group::StorySendMode,
#[serde(serialize_with = "serialize::optional_proto_message_as_bytes")]
pub snapshot: Option<Box<proto::group::GroupSnapshot>>,
}

impl TryFrom<proto::Group> for GroupData {
type Error = RecipientError;
fn try_from(value: proto::Group) -> Result<Self, Self::Error> {
let proto::Group {
masterKey,
whitelisted,
hideStory,
storySendMode,
snapshot,
special_fields: _,
} = value;

let master_key = masterKey
.try_into()
.map_err(|_| RecipientError::InvalidMasterKey)?;

let story_send_mode = match storySendMode.enum_value_or_default() {
s @ (proto::group::StorySendMode::DEFAULT
| proto::group::StorySendMode::DISABLED
| proto::group::StorySendMode::ENABLED) => s,
};

// TODO consider additional group snapshot validation.
let snapshot = snapshot.0;

Ok(GroupData {
master_key,
whitelisted,
hide_story: hideStory,
story_send_mode,
snapshot,
})
}
}

0 comments on commit 9f6ceee

Please sign in to comment.