1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use std::convert::TryInto;

use crate::proto::Verified;

use bytes::Bytes;
use phonenumber::PhoneNumber;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use uuid::Uuid;
use zkgroup::profiles::ProfileKey;

/// Attachment represents an attachment received from a peer
#[derive(Debug, Serialize, Deserialize)]
pub struct Attachment<R> {
    pub content_type: String,
    pub reader: R,
}

/// Mirror of the protobuf ContactDetails message
/// but with stronger types (e.g. `ServiceAddress` instead of optional uuid and string phone numbers)
/// and some helper functions
#[derive(Debug, Serialize, Deserialize)]
pub struct Contact {
    pub uuid: Uuid,
    pub phone_number: Option<PhoneNumber>,
    pub name: String,
    pub color: Option<String>,
    #[serde(skip)]
    pub verified: Verified,
    pub profile_key: Vec<u8>,
    pub expire_timer: u32,
    pub expire_timer_version: u32,
    pub inbox_position: u32,
    pub archived: bool,
    #[serde(skip)]
    pub avatar: Option<Attachment<Bytes>>,
}

#[derive(Error, Debug)]
pub enum ParseContactError {
    #[error(transparent)]
    Protobuf(#[from] prost::DecodeError),
    #[error(transparent)]
    Uuid(#[from] uuid::Error),
    #[error("missing UUID")]
    MissingUuid,
    #[error("missing profile key")]
    MissingProfileKey,
    #[error("missing avatar content-type")]
    MissingAvatarContentType,
}

impl Contact {
    pub fn from_proto(
        contact_details: crate::proto::ContactDetails,
        avatar_data: Option<Bytes>,
    ) -> Result<Self, ParseContactError> {
        Ok(Self {
            uuid: contact_details
                .aci
                .as_ref()
                .ok_or(ParseContactError::MissingUuid)?
                .parse()?,
            phone_number: contact_details
                .number
                .as_ref()
                .and_then(|n| phonenumber::parse(None, n).ok()),
            name: contact_details.name().into(),
            color: contact_details.color.clone(),
            verified: contact_details.verified.clone().unwrap_or_default(),
            profile_key: contact_details.profile_key().to_vec(),
            expire_timer: contact_details.expire_timer(),
            expire_timer_version: contact_details.expire_timer_version(),
            inbox_position: contact_details.inbox_position(),
            archived: contact_details.archived(),
            avatar: contact_details.avatar.and_then(|avatar| {
                if let (Some(content_type), Some(avatar_data)) =
                    (avatar.content_type, avatar_data)
                {
                    Some(Attachment {
                        content_type,
                        reader: avatar_data,
                    })
                } else {
                    tracing::warn!("missing avatar content-type, skipping.");
                    None
                }
            }),
        })
    }

    pub fn profile_key(&self) -> Result<ProfileKey, ParseContactError> {
        Ok(ProfileKey::create(
            self.profile_key
                .clone()
                .try_into()
                .map_err(|_| ParseContactError::MissingProfileKey)?,
        ))
    }
}