Skip to main content

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