Skip to main content

xmtp_api/
notification.rs

1use crate::{ApiClientWrapper, Result, dyn_err};
2use xmtp_common::time::timeout;
3use xmtp_configuration::NOTIFICATION_REQUEST_TIMEOUT;
4use xmtp_proto::{
5    api_client::XmtpBackendClient,
6    backend_v1::{
7        RecipientState, RegisterRequest, UnregisterRequest, UnregisterResponse,
8        UpdateSubscriptionsRequest,
9    },
10};
11
12impl<C: XmtpBackendClient> ApiClientWrapper<C> {
13    /// Create or renew a notification recipient.
14    #[xmtp_common::rpc_span]
15    pub async fn register(&self, request: RegisterRequest) -> Result<RecipientState> {
16        timeout(
17            NOTIFICATION_REQUEST_TIMEOUT,
18            self.retry_call(|| self.api_client.register(request.clone()), false),
19        )
20        .await
21        .map_err(xmtp_proto::api::ApiClientError::from)
22        .map_err(dyn_err)?
23        .map_err(dyn_err)
24    }
25
26    /// Delete a notification recipient and its subscriptions.
27    #[xmtp_common::rpc_span]
28    pub async fn unregister(&self, request: UnregisterRequest) -> Result<UnregisterResponse> {
29        timeout(
30            NOTIFICATION_REQUEST_TIMEOUT,
31            self.retry_call(|| self.api_client.unregister(request.clone()), false),
32        )
33        .await
34        .map_err(xmtp_proto::api::ApiClientError::from)
35        .map_err(dyn_err)?
36        .map_err(dyn_err)
37    }
38
39    /// Atomically update notification subscriptions and renew the recipient.
40    #[xmtp_common::rpc_span]
41    pub async fn update_subscriptions(
42        &self,
43        request: UpdateSubscriptionsRequest,
44    ) -> Result<RecipientState> {
45        timeout(
46            NOTIFICATION_REQUEST_TIMEOUT,
47            self.retry_call(
48                || self.api_client.update_subscriptions(request.clone()),
49                false,
50            ),
51        )
52        .await
53        .map_err(xmtp_proto::api::ApiClientError::from)
54        .map_err(dyn_err)?
55        .map_err(dyn_err)
56    }
57}