Skip to main content

tetratto_core2/model/
requests.rs

1use serde::{Serialize, Deserialize};
2use tritools::time::unix_epoch_timestamp;
3use super::id::Id;
4
5#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, 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#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)]
46pub enum ActionType {
47    /// A request to answer a question with a post.
48    ///
49    /// `questions` table.
50    Answer,
51    /// A request follow a private account.
52    ///
53    /// `users` table.
54    Follow,
55}
56
57#[derive(Debug, Serialize, Deserialize)]
58pub struct ActionRequest {
59    pub id: Id,
60    pub created: u128,
61    pub owner: crate::model::id::Id,
62    pub action_type: ActionType,
63    /// The ID of the asset this request links to. Should exist in the correct
64    /// table for the given [`ActionType`].
65    pub linked_asset: Id,
66    /// Optional data attached to the action request.
67    pub data: ActionData,
68}
69
70impl ActionRequest {
71    /// Create a new [`ActionRequest`].
72    pub fn new(
73        owner: crate::model::id::Id,
74        action_type: ActionType,
75        linked_asset: Id,
76        data: Option<ActionData>,
77    ) -> Self {
78        Self {
79            id: Id::new(),
80            created: unix_epoch_timestamp(),
81            owner,
82            action_type,
83            linked_asset,
84            data: data.unwrap_or_default(),
85        }
86    }
87
88    /// Create a new [`ActionRequest`] with the given `id`.
89    pub fn with_id(
90        id: Id,
91        owner: crate::model::id::Id,
92        action_type: ActionType,
93        linked_asset: crate::model::id::Id,
94        data: Option<ActionData>,
95    ) -> Self {
96        Self {
97            id,
98            created: unix_epoch_timestamp(),
99            owner,
100            action_type,
101            linked_asset,
102            data: data.unwrap_or_default(),
103        }
104    }
105}