tsar_sdk/structs/user.rs
1use serde::{de::DeserializeOwned, Deserialize, Serialize};
2
3use crate::{errors::TsarError, Client};
4
5/// User object which gets returned from the `client.authenticate()` function.
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct User {
8 pub id: String,
9 /// Name of the user. This could be their display name, username, or null.
10 pub name: Option<String>,
11 /// Avatar of the user. This can either be an image URL or null.
12 pub avatar: Option<String>,
13
14 pub subscription: Subscription,
15
16 /// The current session of the user.
17 /// This is used to authenticate any requests from the user after they've been initialized.
18 pub session: String,
19
20 // A public decryption key which will be used to verify packets signed by a unique private key that was made from `authentication()`
21 // This is NOT the client_key.
22 pub session_key: String,
23}
24
25/// Subscription object which is used within the `User` object.
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub struct Subscription {
28 pub id: String,
29 /// The tier of the user's subscription set by the app's purchase flow. Default is 0.
30 pub tier: u32,
31 /// Timestamp of when the subscription expires. If null then the subscription is lifetime.
32 pub expires: Option<u64>,
33}
34
35impl User {
36 pub fn heartbeat(&self) -> Result<(), TsarError> {
37 self.user_api_call("heartbeat")
38 }
39
40 pub fn user_api_call<T: DeserializeOwned>(&self, path: &str) -> Result<T, TsarError> {
41 let params = vec![("session", self.session.as_str())];
42 Client::encrypted_api_call::<T>(path, &self.session_key, params)
43 }
44}