tetratto_core/model/
requests.rs1use 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 CommunityJoin,
52 Answer,
56 Follow,
60 Transfer,
64 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 pub linked_asset: usize,
79 pub data: ActionData,
81}
82
83impl ActionRequest {
84 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 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}