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