xmtp_api_d14n/endpoints/d14n/
get_inbox_ids.rs

1use derive_builder::Builder;
2use prost::Message;
3use prost::bytes::Bytes;
4use std::borrow::Cow;
5use xmtp_proto::api::{BodyError, Endpoint};
6use xmtp_proto::xmtp::identity::associations::IdentifierKind;
7use xmtp_proto::xmtp::xmtpv4::message_api::{
8    GetInboxIdsRequest, GetInboxIdsResponse, get_inbox_ids_request,
9};
10
11#[derive(Debug, Builder, Default)]
12#[builder(setter(strip_option), build_fn(error = "BodyError"))]
13pub struct GetInboxIds {
14    #[builder(setter(into), default)]
15    addresses: Vec<String>,
16    #[builder(setter(into), default)]
17    passkeys: Vec<String>,
18}
19
20impl GetInboxIds {
21    pub fn builder() -> GetInboxIdsBuilder {
22        Default::default()
23    }
24}
25
26impl Endpoint for GetInboxIds {
27    type Output = GetInboxIdsResponse;
28    fn grpc_endpoint(&self) -> Cow<'static, str> {
29        xmtp_proto::path_and_query::<GetInboxIdsRequest>()
30    }
31
32    fn body(&self) -> Result<Bytes, BodyError> {
33        let addresses = self
34            .addresses
35            .iter()
36            .cloned()
37            .map(|a| (a, IdentifierKind::Ethereum));
38        let passkeys = self
39            .passkeys
40            .iter()
41            .cloned()
42            .map(|p| (p, IdentifierKind::Passkey));
43
44        Ok(GetInboxIdsRequest {
45            requests: addresses
46                .chain(passkeys)
47                .map(|(i, kind)| get_inbox_ids_request::Request {
48                    identifier: i,
49                    identifier_kind: kind as i32,
50                })
51                .collect(),
52        }
53        .encode_to_vec()
54        .into())
55    }
56}
57
58#[cfg(test)]
59mod test {
60    use super::*;
61    use crate::d14n::GetInboxIds;
62    use xmtp_api_grpc::test::XmtpdClient;
63    use xmtp_proto::{api, prelude::*};
64
65    #[xmtp_common::test]
66    fn test_file_descriptor() {
67        let pnq = xmtp_proto::path_and_query::<GetInboxIdsRequest>();
68        println!("{}", pnq);
69    }
70
71    #[xmtp_common::test]
72    fn test_grpc_endpoint_returns_correct_path() {
73        let endpoint = GetInboxIds::default();
74        assert_eq!(
75            endpoint.grpc_endpoint(),
76            "/xmtp.xmtpv4.message_api.ReplicationApi/GetInboxIds"
77        );
78    }
79
80    #[xmtp_common::test]
81    async fn test_get_inbox_ids() {
82        let client = XmtpdClient::create();
83        let client = client.build().unwrap();
84
85        let endpoint = GetInboxIds::builder()
86            .addresses(vec![
87                "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045".to_string(),
88            ])
89            .build()
90            .unwrap();
91
92        api::ignore(endpoint).query(&client).await.unwrap();
93    }
94}