tetratto_core/model/
requests.rs

1use serde::{Serialize, Deserialize};
2use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
3
4#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
5pub enum ActionData {
6    String(String),
7    Int32(i32),
8    Usize(usize),
9    Many(Vec<ActionData>),
10    Null,
11}
12
13impl ActionData {
14    pub fn read_string(self) -> String {
15        match self {
16            ActionData::String(x) => x,
17            _ => String::default(),
18        }
19    }
20
21    pub fn read_int32(self) -> i32 {
22        match self {
23            ActionData::Int32(x) => x,
24            _ => i32::default(),
25        }
26    }
27
28    pub fn read_usize(self) -> usize {
29        match self {
30            ActionData::Usize(x) => x,
31            _ => usize::default(),
32        }
33    }
34
35    pub fn read_many(self) -> Vec<ActionData> {
36        match self {
37            ActionData::Many(x) => x,
38            _ => Vec::default(),
39        }
40    }
41}
42
43impl Default for ActionData {
44    fn default() -> Self {
45        Self::Null
46    }
47}
48
49#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
50pub enum ActionType {
51    /// A request to join a community.
52    ///
53    /// `users` table.
54    CommunityJoin,
55    /// A request to answer a question with a post.
56    ///
57    /// `questions` table.
58    Answer,
59    /// A request follow a private account.
60    ///
61    /// `users` table.
62    Follow,
63    /// A request for the `owner` user (sender) to send the `linked_asset` user (receiver) coins.
64    ///
65    /// Expects a `data` value of [`ActionData::Int32`] representing the coin amount.
66    Transfer,
67}
68
69#[derive(Debug, Serialize, Deserialize)]
70pub struct ActionRequest {
71    pub id: usize,
72    pub created: usize,
73    pub owner: usize,
74    pub action_type: ActionType,
75    /// The ID of the asset this request links to. Should exist in the correct
76    /// table for the given [`ActionType`].
77    pub linked_asset: usize,
78    /// Optional data attached to the action request.
79    pub data: ActionData,
80}
81
82impl ActionRequest {
83    /// Create a new [`ActionRequest`].
84    pub fn new(
85        owner: usize,
86        action_type: ActionType,
87        linked_asset: usize,
88        data: Option<ActionData>,
89    ) -> Self {
90        Self {
91            id: Snowflake::new().to_string().parse::<usize>().unwrap(),
92            created: unix_epoch_timestamp(),
93            owner,
94            action_type,
95            linked_asset,
96            data: match data {
97                Some(x) => x,
98                None => ActionData::default(),
99            },
100        }
101    }
102
103    /// Create a new [`ActionRequest`] with the given `id`.
104    pub fn with_id(
105        id: usize,
106        owner: usize,
107        action_type: ActionType,
108        linked_asset: usize,
109        data: Option<ActionData>,
110    ) -> Self {
111        Self {
112            id,
113            created: unix_epoch_timestamp(),
114            owner,
115            action_type,
116            linked_asset,
117            data: match data {
118                Some(x) => x,
119                None => ActionData::default(),
120            },
121        }
122    }
123}