whatsapp_rust/features/
tctoken.rs1use crate::client::Client;
23use crate::request::IqError;
24use crate::store::error::StoreError;
25use thiserror::Error;
26use wacore::iq::tctoken::{IssuePrivacyTokensSpec, ReceivedTcToken};
27use wacore::store::traits::TcTokenEntry;
28use wacore_binary::Jid;
29
30#[derive(Debug, Error)]
32#[non_exhaustive]
33pub enum TcTokenError {
34 #[error("{0}")]
36 Iq(#[from] IqError),
37 #[error("{0}")]
39 Store(#[from] StoreError),
40}
41
42pub struct TcToken<'a> {
44 client: &'a Client,
45}
46
47impl<'a> TcToken<'a> {
48 pub(crate) fn new(client: &'a Client) -> Self {
49 Self { client }
50 }
51
52 pub async fn issue_tokens(&self, jids: &[Jid]) -> Result<Vec<ReceivedTcToken>, TcTokenError> {
57 if jids.is_empty() {
58 return Ok(Vec::new());
59 }
60
61 let spec = IssuePrivacyTokensSpec::new(jids);
62 let response = self.client.execute(spec).await?;
63 self.client.store_issued_tc_tokens(&response.tokens).await;
64
65 Ok(response.tokens)
66 }
67
68 pub async fn prune_expired(&self) -> Result<u32, TcTokenError> {
75 use wacore::iq::tctoken::{
76 sender_tc_token_expiration_cutoff_with, tc_token_expiration_cutoff_with,
77 };
78
79 let backend = self.client.persistence_manager.backend();
80 let tc_config = self.client.tc_token_config().await;
81 let token_cutoff = tc_token_expiration_cutoff_with(&tc_config);
82 let sender_cutoff = sender_tc_token_expiration_cutoff_with(&tc_config);
83 let deleted = backend
84 .delete_expired_tc_tokens(token_cutoff, sender_cutoff)
85 .await?;
86
87 if deleted > 0 {
88 log::info!(target: "Client/TcToken", "Pruned {} expired tc_tokens", deleted);
89 }
90
91 Ok(deleted)
92 }
93
94 pub async fn get(&self, jid: &str) -> Result<Option<TcTokenEntry>, TcTokenError> {
96 let backend = self.client.persistence_manager.backend();
97 Ok(backend.get_tc_token(jid).await?)
98 }
99
100 pub async fn get_all_jids(&self) -> Result<Vec<String>, TcTokenError> {
102 let backend = self.client.persistence_manager.backend();
103 Ok(backend.get_all_tc_token_jids().await?)
104 }
105}
106
107impl Client {
108 pub fn tc_token(&self) -> TcToken<'_> {
110 TcToken::new(self)
111 }
112}