forge
Explore
Log in

Don't you dare go Hollow

yumush

Clone
★ 0

0 watching0 forksRustAGPL-3.06.8 MiB27ceb05Tahinli8 hours ago399 commits

model.rs710 lines, 19.0 KiBHistoryRawDownload
1use bitcode::{Decode, Encode};
2
3use crate::constant::{
4 DEFAULT_LOGINS_PER_MINUTE, DEFAULT_MAX_ATTACHMENT_BYTES, DEFAULT_MESSAGES_PER_MINUTE,
5 MAXIMUM_AVATAR_BYTES, MAXIMUM_MESSAGE_LENGTH,
6};
7
8pub type Id = i64;
9
10pub trait Named: Sized + Copy {
11 fn as_str(self) -> &'static str;
12 fn parse(text: &str) -> Option<Self>;
13}
14
15#[macro_export]
16macro_rules! string_enum {
17 ($(#[$meta:meta])* $name:ident { $($variant:ident => $text:literal),+ $(,)? }) => {
18 $(#[$meta])*
19 pub enum $name {
20 $($variant),+
21 }
22
23 impl $name {
24 pub const ALL: [Self; [$(stringify!($variant)),+].len()] = [$(Self::$variant),+];
25
26 pub const fn as_str(self) -> &'static str {
27 match self {
28 $(Self::$variant => $text),+
29 }
30 }
31
32 pub fn parse(text: &str) -> Option<Self> {
33 Self::ALL.into_iter().find(|item| item.as_str() == text)
34 }
35 }
36
37 impl $crate::model::Named for $name {
38 fn as_str(self) -> &'static str {
39 $name::as_str(self)
40 }
41
42 fn parse(text: &str) -> Option<Self> {
43 $name::parse(text)
44 }
45 }
46 };
47}
48
49string_enum! {
50 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encode, Decode)]
51 Hue {
52 Amber => "amber",
53 Sky => "sky",
54 Violet => "violet",
55 Mint => "mint",
56 Rose => "rose",
57 Gold => "gold",
58 Coral => "coral",
59 Teal => "teal",
60 Lilac => "lilac",
61 Paper => "paper",
62 }
63}
64
65string_enum! {
66 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)]
67 Role {
68 User => "user",
69 Builder => "builder",
70 }
71}
72
73string_enum! {
74 #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
75 Presence {
76 Online => "online",
77 Busy => "busy",
78 Offline => "offline",
79 }
80}
81
82string_enum! {
83 #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)]
84 Rank {
85 Member => "member",
86 Moderator => "moderator",
87 Admin => "admin",
88 Owner => "owner",
89 Builder => "builder",
90 }
91}
92
93#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
94pub struct User {
95 pub id: Id,
96 pub username: String,
97 pub role: Role,
98 pub status: String,
99 pub hue: Option<Hue>,
100 pub avatar_id: Option<Id>,
101 pub created_at: u64,
102}
103
104#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
105pub struct Account {
106 pub user: User,
107 pub suspended: bool,
108 pub last_seen: Option<u64>,
109 pub suspended_until: Option<u64>,
110 pub ghost: bool,
111}
112
113#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
114pub struct SessionInfo {
115 pub id: Id,
116 pub device: String,
117 pub last_seen: Option<u64>,
118}
119
120#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
121pub struct Member {
122 pub user: User,
123 pub rank: Rank,
124}
125
126#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
127pub enum CommunityKind {
128 Group,
129 Direct { peer: User },
130 Notes,
131}
132
133impl CommunityKind {
134 pub fn is_group(&self) -> bool {
135 matches!(self, Self::Group)
136 }
137
138 pub fn is_direct(&self) -> bool {
139 matches!(self, Self::Direct { .. })
140 }
141
142 pub fn peer(&self) -> Option<&User> {
143 match self {
144 Self::Direct { peer } => Some(peer),
145 Self::Group | Self::Notes => None,
146 }
147 }
148}
149
150#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
151pub enum Sanction {
152 Kick,
153 Ban,
154}
155
156#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
157pub enum BanScope {
158 Account,
159 AccountAndIp,
160}
161
162#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
163pub struct Community {
164 pub id: Id,
165 pub name: String,
166 pub kind: CommunityKind,
167 pub avatar_id: Option<Id>,
168 pub voice_enabled: bool,
169}
170
171#[derive(Debug, Clone, Default, PartialEq, Eq, Encode, Decode)]
172pub struct Unread {
173 pub community_id: Id,
174 pub messages: u32,
175 pub mentions: u32,
176 pub last_read: Option<Id>,
177}
178
179pub fn mentions(body: &str, username: &str) -> bool {
180 let body = body.to_lowercase();
181 let needle = format!("@{}", username.to_lowercase());
182 body.match_indices(&needle).any(|(at, _)| {
183 !body[at + needle.len()..]
184 .starts_with(|next: char| next.is_ascii_alphanumeric() || matches!(next, '_' | '-'))
185 })
186}
187
188#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
189pub struct VoiceRoom {
190 pub community_id: Id,
191 pub community: String,
192 pub users: Vec<User>,
193 pub muted: Vec<Id>,
194 pub ghosts: Vec<Id>,
195}
196
197#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
198pub struct CommunitySummary {
199 pub community: Community,
200 pub owner: Option<User>,
201 pub members: u64,
202}
203
204#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
205pub struct Ban {
206 pub user: User,
207 pub reason: Option<String>,
208}
209
210#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
211pub enum LogAction {
212 Created,
213 Renamed { name: String },
214 AvatarChanged,
215 AvatarRemoved,
216 Invited,
217 Joined,
218 Left,
219 Kicked { reason: Option<String> },
220 Banned { reason: Option<String> },
221 Unbanned,
222 RankChanged { rank: Rank },
223 MessageDeleted,
224}
225
226#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
227pub struct LogEntry {
228 pub id: Id,
229 pub created_at: u64,
230 pub actor: Option<User>,
231 pub target: Option<User>,
232 pub action: LogAction,
233}
234
235#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
236pub enum AdminAction {
237 RoleChanged { role: Role },
238 UserDeleted,
239 UserSuspended,
240 UserUnsuspended,
241 SessionsRevoked,
242 PasswordReset,
243 ProfileCleared,
244 UserCreated,
245 CommunityDeleted,
246 OwnershipTransferred,
247 SettingsChanged,
248 UserRenamed { from: String },
249 UserTimedOut { minutes: u32 },
250 UserBanned { reason: Option<String> },
251 BanRemoved,
252 InviteCreated,
253 InviteDeleted,
254 ReportHandled { status: ReportStatus },
255 MessageDeleted,
256 UserPurged { messages: u64 },
257 VoiceKicked,
258 VoiceMuted { muted: bool },
259 CommunityVoiceSet { enabled: bool },
260 AttachmentDeleted,
261 GhostEnabled,
262 GhostDisabled,
263 GhostVisit,
264 GhostVoice { seconds: u64 },
265 BackupCreated { name: String },
266 BackupDeleted { name: String },
267 BackupDownloaded { name: String },
268 ReleasePublished { seq: u64, required: bool },
269 ReleaseRequired { seq: u64, required: bool },
270 Community(LogAction),
271}
272
273string_enum! {
274 #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
275 AdminActionKind {
276 RoleChanged => "role_changed",
277 UserDeleted => "user_deleted",
278 UserSuspended => "user_suspended",
279 UserUnsuspended => "user_unsuspended",
280 SessionsRevoked => "sessions_revoked",
281 PasswordReset => "password_reset",
282 ProfileCleared => "profile_cleared",
283 UserCreated => "user_created",
284 CommunityDeleted => "community_deleted",
285 OwnershipTransferred => "ownership_transferred",
286 SettingsChanged => "settings_changed",
287 UserRenamed => "user_renamed",
288 UserTimedOut => "user_timed_out",
289 UserBanned => "user_banned",
290 BanRemoved => "ban_removed",
291 InviteCreated => "invite_created",
292 InviteDeleted => "invite_deleted",
293 ReportHandled => "report_handled",
294 MessageDeleted => "message_deleted",
295 UserPurged => "user_purged",
296 VoiceKicked => "voice_kicked",
297 VoiceMuted => "voice_muted",
298 CommunityVoiceSet => "community_voice_set",
299 AttachmentDeleted => "attachment_deleted",
300 GhostEnabled => "ghost_enabled",
301 GhostDisabled => "ghost_disabled",
302 GhostVisit => "ghost_visit",
303 GhostVoice => "ghost_voice",
304 BackupCreated => "backup_created",
305 BackupDeleted => "backup_deleted",
306 BackupDownloaded => "backup_downloaded",
307 ReleasePublished => "release_published",
308 ReleaseRequired => "release_required",
309 Community => "community",
310 }
311}
312
313impl AdminAction {
314 pub fn kind(&self) -> AdminActionKind {
315 match self {
316 AdminAction::RoleChanged { .. } => AdminActionKind::RoleChanged,
317 AdminAction::UserDeleted => AdminActionKind::UserDeleted,
318 AdminAction::UserSuspended => AdminActionKind::UserSuspended,
319 AdminAction::UserUnsuspended => AdminActionKind::UserUnsuspended,
320 AdminAction::SessionsRevoked => AdminActionKind::SessionsRevoked,
321 AdminAction::PasswordReset => AdminActionKind::PasswordReset,
322 AdminAction::ProfileCleared => AdminActionKind::ProfileCleared,
323 AdminAction::UserCreated => AdminActionKind::UserCreated,
324 AdminAction::CommunityDeleted => AdminActionKind::CommunityDeleted,
325 AdminAction::OwnershipTransferred => AdminActionKind::OwnershipTransferred,
326 AdminAction::SettingsChanged => AdminActionKind::SettingsChanged,
327 AdminAction::UserRenamed { .. } => AdminActionKind::UserRenamed,
328 AdminAction::UserTimedOut { .. } => AdminActionKind::UserTimedOut,
329 AdminAction::UserBanned { .. } => AdminActionKind::UserBanned,
330 AdminAction::BanRemoved => AdminActionKind::BanRemoved,
331 AdminAction::InviteCreated => AdminActionKind::InviteCreated,
332 AdminAction::InviteDeleted => AdminActionKind::InviteDeleted,
333 AdminAction::ReportHandled { .. } => AdminActionKind::ReportHandled,
334 AdminAction::MessageDeleted => AdminActionKind::MessageDeleted,
335 AdminAction::UserPurged { .. } => AdminActionKind::UserPurged,
336 AdminAction::VoiceKicked => AdminActionKind::VoiceKicked,
337 AdminAction::VoiceMuted { .. } => AdminActionKind::VoiceMuted,
338 AdminAction::CommunityVoiceSet { .. } => AdminActionKind::CommunityVoiceSet,
339 AdminAction::AttachmentDeleted => AdminActionKind::AttachmentDeleted,
340 AdminAction::GhostEnabled => AdminActionKind::GhostEnabled,
341 AdminAction::GhostDisabled => AdminActionKind::GhostDisabled,
342 AdminAction::GhostVisit => AdminActionKind::GhostVisit,
343 AdminAction::GhostVoice { .. } => AdminActionKind::GhostVoice,
344 AdminAction::BackupCreated { .. } => AdminActionKind::BackupCreated,
345 AdminAction::BackupDeleted { .. } => AdminActionKind::BackupDeleted,
346 AdminAction::BackupDownloaded { .. } => AdminActionKind::BackupDownloaded,
347 AdminAction::ReleasePublished { .. } => AdminActionKind::ReleasePublished,
348 AdminAction::ReleaseRequired { .. } => AdminActionKind::ReleaseRequired,
349 AdminAction::Community(_) => AdminActionKind::Community,
350 }
351 }
352}
353
354#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
355pub struct AdminLogEntry {
356 pub id: Id,
357 pub actor: Option<User>,
358 pub target: Option<User>,
359 pub created_at: u64,
360 pub community: Option<String>,
361 pub action: AdminAction,
362}
363
364string_enum! {
365 #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
366 ReportStatus {
367 Open => "open",
368 Resolved => "resolved",
369 Dismissed => "dismissed",
370 }
371}
372
373#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
374pub struct Report {
375 pub id: Id,
376 pub created_at: u64,
377 pub reporter: Option<User>,
378 pub target: Option<User>,
379 pub message_id: Option<Id>,
380 pub community_id: Option<Id>,
381 pub community: Option<String>,
382 pub body: Option<String>,
383 pub reason: String,
384 pub status: ReportStatus,
385 pub handled_by: Option<User>,
386}
387
388#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
389pub struct ServerSettings {
390 pub name: String,
391 pub registration_open: bool,
392 pub max_attachment_bytes: u64,
393 pub maintenance: bool,
394 pub user_quota_bytes: u64,
395 pub community_quota_bytes: u64,
396 pub message_retention_days: u32,
397 pub attachment_retention_days: u32,
398 pub announcement: String,
399 pub max_communities_per_user: u32,
400 pub max_message_length: u32,
401 pub max_avatar_bytes: u64,
402 pub messages_per_minute: u32,
403 pub logins_per_minute: u32,
404}
405
406impl Default for ServerSettings {
407 fn default() -> Self {
408 Self {
409 name: "yumush".to_string(),
410 registration_open: true,
411 max_attachment_bytes: DEFAULT_MAX_ATTACHMENT_BYTES,
412 maintenance: false,
413 user_quota_bytes: 0,
414 community_quota_bytes: 0,
415 message_retention_days: 0,
416 attachment_retention_days: 0,
417 announcement: String::new(),
418 max_communities_per_user: 0,
419 max_message_length: MAXIMUM_MESSAGE_LENGTH as u32,
420 max_avatar_bytes: MAXIMUM_AVATAR_BYTES as u64,
421 messages_per_minute: DEFAULT_MESSAGES_PER_MINUTE,
422 logins_per_minute: DEFAULT_LOGINS_PER_MINUTE,
423 }
424 }
425}
426
427#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
428pub struct AttachmentInfo {
429 pub attachment: Attachment,
430 pub uploader: Option<User>,
431 pub community: String,
432}
433
434#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
435pub struct Staged {
436 pub file: crate::update::Sha,
437 pub target: String,
438 pub size: u64,
439 pub packed: u64,
440}
441
442#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
443pub struct ReleaseFile {
444 pub target: String,
445 pub size: u64,
446 pub packed: u64,
447}
448
449#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
450pub struct ReleaseInfo {
451 pub seq: u64,
452 pub required: bool,
453 pub created_ms: u64,
454 pub files: Vec<ReleaseFile>,
455}
456
457#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
458pub enum UpdateState {
459 Unknown,
460 Latest,
461 Available { seq: u64, required: bool },
462 Downloading { seq: u64, percent: u8 },
463 Installing { seq: u64 },
464 Ready { seq: u64 },
465 Failed { seq: u64 },
466}
467
468#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
469pub struct UpdateClient {
470 pub device: String,
471 pub release: Option<u64>,
472 pub target: Option<String>,
473 pub state: UpdateState,
474 pub online: bool,
475 pub seen_ms: u64,
476}
477
478#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
479pub struct BackupInfo {
480 pub name: String,
481 pub size_bytes: u64,
482 pub created_ms: u64,
483}
484
485#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
486pub struct Usage {
487 pub user: User,
488 pub bytes: u64,
489 pub count: u64,
490}
491
492#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
493pub struct CommunityUsage {
494 pub community_id: Id,
495 pub community: String,
496 pub bytes: u64,
497 pub count: u64,
498}
499
500string_enum! {
501 #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
502 ServerBanKind {
503 Username => "username",
504 Ip => "ip",
505 }
506}
507
508#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
509pub struct ServerBan {
510 pub id: Id,
511 pub kind: ServerBanKind,
512 pub value: String,
513 pub reason: Option<String>,
514}
515
516#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
517pub struct InviteCode {
518 pub code: String,
519 pub uses_left: u32,
520 pub expires_at: Option<u64>,
521}
522
523#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
524pub struct Overview {
525 pub users: u64,
526 pub builders: u64,
527 pub communities: u64,
528 pub online: u64,
529 pub voice: u64,
530 pub storage_bytes: u64,
531}
532
533#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
534pub struct DayStat {
535 pub day: u64,
536 pub active_users: u64,
537 pub messages: u64,
538}
539
540#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
541pub struct Invitation {
542 pub community: Community,
543 pub inviter: User,
544}
545
546#[derive(Debug, Clone, Default, PartialEq, Eq, Encode, Decode)]
547pub struct Friends {
548 pub friends: Vec<User>,
549 pub incoming: Vec<User>,
550 pub outgoing: Vec<User>,
551}
552
553string_enum! {
554 #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
555 AttachmentKind {
556 Image => "image",
557 Video => "video",
558 Audio => "audio",
559 File => "file",
560 }
561}
562
563#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
564pub struct Attachment {
565 pub id: Id,
566 pub name: String,
567 pub size: u64,
568 pub kind: AttachmentKind,
569 pub created_at: u64,
570 pub width: Option<u32>,
571 pub height: Option<u32>,
572}
573
574#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
575pub struct Quote {
576 pub message_id: Id,
577 pub user_id: Id,
578 pub body: String,
579 pub attachments: u32,
580}
581
582#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
583pub enum Reply {
584 Quote(Quote),
585 Deleted(Option<Quote>),
586}
587
588impl Reply {
589 pub fn quote(&self) -> Option<&Quote> {
590 match self {
591 Reply::Quote(quote) => Some(quote),
592 Reply::Deleted(_) => None,
593 }
594 }
595
596 pub fn redact(&mut self) {
597 if let Reply::Deleted(quote) = self {
598 *quote = None;
599 }
600 }
601}
602
603#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
604pub struct Edit {
605 pub body: String,
606 pub edited_at: u64,
607}
608
609#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
610pub struct SnippetMessage {
611 pub id: Id,
612 pub user_id: Id,
613 pub username: String,
614 pub avatar_id: Option<Id>,
615 pub created_at: u64,
616 pub body: String,
617 pub attachments: Vec<Attachment>,
618 pub poll: Option<Poll>,
619}
620
621#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
622pub struct Snippet {
623 pub origin: Option<String>,
624 pub messages: Vec<SnippetMessage>,
625}
626
627#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
628pub struct Reaction {
629 pub emoji: String,
630 pub user_ids: Vec<Id>,
631}
632
633#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
634pub struct PollOption {
635 pub text: String,
636 pub votes: u32,
637 pub voter_ids: Vec<Id>,
638}
639
640#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
641pub struct Poll {
642 pub question: String,
643 pub options: Vec<PollOption>,
644 pub multiple: bool,
645 pub closed: bool,
646 pub public: bool,
647 pub voters: u32,
648 pub ends_at: Option<u64>,
649}
650
651#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
652pub struct Message {
653 pub id: Id,
654 pub user_id: Id,
655 pub community_id: Id,
656 pub created_at: u64,
657 pub body: String,
658 pub attachments: Vec<Attachment>,
659 pub reactions: Vec<Reaction>,
660 pub reply: Option<Reply>,
661 pub snippet: Option<Snippet>,
662 pub poll: Option<Poll>,
663 pub edited: bool,
664 pub pinned: bool,
665 pub deleted: bool,
666 pub call: Option<CallLog>,
667}
668
669#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
670pub enum CallLog {
671 Started,
672 Declined,
673 Missed,
674 Ended { seconds: u64 },
675}
676
677impl CallLog {
678 pub fn as_str(self) -> &'static str {
679 match self {
680 CallLog::Started => "started",
681 CallLog::Declined => "declined",
682 CallLog::Missed => "missed",
683 CallLog::Ended { .. } => "ended",
684 }
685 }
686
687 pub fn parse(text: &str, seconds: u64) -> Option<Self> {
688 match text {
689 "started" => Some(CallLog::Started),
690 "declined" => Some(CallLog::Declined),
691 "missed" => Some(CallLog::Missed),
692 "ended" => Some(CallLog::Ended { seconds }),
693 _ => None,
694 }
695 }
696
697 pub fn seconds(self) -> u64 {
698 match self {
699 CallLog::Ended { seconds } => seconds,
700 CallLog::Started | CallLog::Declined | CallLog::Missed => 0,
701 }
702 }
703}
704
705#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)]
706pub struct MessageHit {
707 pub message: Message,
708 pub community: String,
709 pub author: Option<User>,
710}