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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
use bitflags::bitflags;
use serde::{Deserialize, Serialize};
use crate::{
autumn::{Attachment, AttachmentId},
id::UserId,
};
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[serde(transparent)]
pub struct Username(pub String);
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub enum RelationshipStatus {
None,
User,
Friend,
Outgoing,
Incoming,
Blocked,
BlockedOther,
}
#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(deny_unknown_fields)]
pub struct Relationship {
pub status: RelationshipStatus,
#[serde(rename = "_id")]
pub id: UserId,
}
#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(deny_unknown_fields)]
pub enum UserPresence {
Online,
Idle,
Busy,
Invisible,
}
#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(deny_unknown_fields)]
pub struct Status {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub text: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub presence: Option<UserPresence>,
}
bitflags! {
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
#[doc = "User badges"]
pub struct Badges: u32 {
const DEVELOPER = 1;
const TRANSLATOR = 2;
const SUPPORTER = 4;
const RESPONSIBLE_DISCLOSURE = 8;
const REVOLT_TEAM = 16;
const EARLY_ADOPTER = 256;
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(deny_unknown_fields)]
pub struct BotInformation {
pub owner: UserId,
}
#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(deny_unknown_fields)]
pub struct User {
#[serde(rename = "_id")]
pub id: UserId,
pub username: Username,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub avatar: Option<Attachment>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub relations: Vec<Relationship>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub badges: Option<Badges>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<Status>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub relationship: Option<RelationshipStatus>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub online: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flags: Option<UserFlags>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bot: Option<BotInformation>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub profile: Option<Profile>,
}
bitflags! {
#[derive(Serialize, Deserialize)]
#[serde(transparent)]
#[doc = "User flags"]
pub struct UserFlags: u32 {
const SUSPENDED = 0x1;
const DELETED = 0x2;
const BANNED = 0x4;
}
}
#[derive(Serialize, Deserialize, Default, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(deny_unknown_fields)]
pub struct Profile {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub background: Option<Attachment>,
}
#[derive(Serialize, Deserialize, Default, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[serde(deny_unknown_fields)]
pub struct UserPatch {
#[serde(rename = "_id", default, skip_serializing_if = "Option::is_none")]
pub id: Option<UserId>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub username: Option<Username>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub avatar: Option<Attachment>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub relations: Option<Vec<Relationship>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub badges: Option<Badges>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub status: Option<Status>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub relationship: Option<RelationshipStatus>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub online: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub flags: Option<UserFlags>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub bot: Option<BotInformation>,
#[serde(
rename = "profile.content",
default,
skip_serializing_if = "Option::is_none"
)]
pub profile_content: Option<String>,
#[serde(
rename = "profile.background",
default,
skip_serializing_if = "Option::is_none"
)]
pub profile_background: Option<Attachment>,
}
impl UserPatch {
pub fn patch(self, user: &mut User) {
let UserPatch {
id: pid,
username: pusername,
avatar: pavatar,
relations: prelations,
badges: pbadges,
status: pstatus,
relationship: prelationship,
online: ponline,
flags: pflags,
bot: pbot,
profile_content: pprofile_content,
profile_background: pprofile_background,
} = self;
let User {
id,
username,
avatar,
relations,
badges,
status,
relationship,
online,
flags,
bot,
profile,
} = user;
if let Some(pid) = pid {
*id = pid;
}
if let Some(pusername) = pusername {
*username = pusername;
}
if let Some(pavatar) = pavatar {
*avatar = Some(pavatar);
}
if let Some(prelations) = prelations {
*relations = prelations;
}
if let Some(pbadges) = pbadges {
*badges = Some(pbadges);
}
if let Some(pstatus) = pstatus {
*status = Some(pstatus);
}
if let Some(prelationship) = prelationship {
*relationship = Some(prelationship);
}
if let Some(ponline) = ponline {
*online = Some(ponline);
}
if let Some(pflags) = pflags {
*flags = Some(pflags);
}
if let Some(pbot) = pbot {
*bot = Some(pbot);
}
if let Some(pprofile_content) = pprofile_content {
profile.get_or_insert(Default::default()).content = Some(pprofile_content);
}
if let Some(pprofile_background) = pprofile_background {
profile.get_or_insert(Default::default()).background = Some(pprofile_background);
}
}
}
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub enum UserField {
Avatar,
ProfileBackground,
ProfileContent,
StatusText,
}
impl UserField {
pub fn remove_patch(self, user: &mut User) {
match self {
Self::Avatar => user.avatar = None,
Self::ProfileBackground => {}
Self::ProfileContent => {}
Self::StatusText => {
if let Some(Status { text, .. }) = &mut user.status {
*text = None
}
}
}
}
}
#[derive(Serialize, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct UserProfileDataPatch {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub content: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub background: Option<AttachmentId>,
}
#[derive(Serialize, Default, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[non_exhaustive]
pub struct UserEditPatch {
#[serde(skip_serializing_if = "Option::is_none")]
pub status: Option<Status>,
#[serde(skip_serializing_if = "Option::is_none")]
pub profile: Option<UserProfileDataPatch>,
#[serde(skip_serializing_if = "Option::is_none")]
pub avatar: Option<AttachmentId>,
#[serde(skip_serializing_if = "Option::is_none")]
pub remove: Option<UserField>,
}