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
use crate::{
    proto::WebSocketRequestMessage,
    push_service::{ServiceError, SignalServiceProfile},
    websocket::SignalWebSocket,
    ServiceAddress,
};

pub struct ProfileService {
    ws: SignalWebSocket,
}

impl ProfileService {
    pub fn from_socket(ws: SignalWebSocket) -> Self {
        ProfileService { ws }
    }

    pub async fn retrieve_profile_by_id(
        &mut self,
        address: ServiceAddress,
        profile_key: Option<zkgroup::profiles::ProfileKey>,
    ) -> Result<SignalServiceProfile, ServiceError> {
        let endpoint = match profile_key {
            Some(key) => {
                let version =
                    bincode::serialize(&key.get_profile_key_version(
                        address.aci().expect("profile by ACI ProtocolAddress"),
                    ))?;
                let version = std::str::from_utf8(&version)
                    .expect("hex encoded profile key version");
                format!("/v1/profile/{}/{}", address.uuid, version)
            },
            None => {
                format!("/v1/profile/{}", address.uuid)
            },
        };

        let request = WebSocketRequestMessage {
            path: Some(endpoint),
            verb: Some("GET".into()),
            // TODO: set locale to en_US
            ..Default::default()
        };

        self.ws.request_json(request).await
    }
}