use bitcode::{Decode, Encode}; use crate::constant::{ DEFAULT_LOGINS_PER_MINUTE, DEFAULT_MAX_ATTACHMENT_BYTES, DEFAULT_MESSAGES_PER_MINUTE, MAXIMUM_AVATAR_BYTES, MAXIMUM_MESSAGE_LENGTH, }; pub type Id = i64; pub trait Named: Sized + Copy { fn as_str(self) -> &'static str; fn parse(text: &str) -> Option; } #[macro_export] macro_rules! string_enum { ($(#[$meta:meta])* $name:ident { $($variant:ident => $text:literal),+ $(,)? }) => { $(#[$meta])* pub enum $name { $($variant),+ } impl $name { pub const ALL: [Self; [$(stringify!($variant)),+].len()] = [$(Self::$variant),+]; pub const fn as_str(self) -> &'static str { match self { $(Self::$variant => $text),+ } } pub fn parse(text: &str) -> Option { Self::ALL.into_iter().find(|item| item.as_str() == text) } } impl $crate::model::Named for $name { fn as_str(self) -> &'static str { $name::as_str(self) } fn parse(text: &str) -> Option { $name::parse(text) } } }; } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Encode, Decode)] Hue { Amber => "amber", Sky => "sky", Violet => "violet", Mint => "mint", Rose => "rose", Gold => "gold", Coral => "coral", Teal => "teal", Lilac => "lilac", Paper => "paper", } } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)] Role { User => "user", Builder => "builder", } } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] Presence { Online => "online", Busy => "busy", Offline => "offline", } } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encode, Decode)] Rank { Member => "member", Moderator => "moderator", Admin => "admin", Owner => "owner", Builder => "builder", } } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct User { pub id: Id, pub username: String, pub role: Role, pub status: String, pub hue: Option, pub avatar_id: Option, pub created_at: u64, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] pub enum Suspension { None, Until(u64), Indefinite, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Account { pub user: User, pub suspension: Suspension, pub last_seen: Option, pub ghost: bool, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct SessionInfo { pub id: Id, pub device: String, pub last_seen: Option, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Member { pub user: User, pub rank: Rank, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub enum CommunityKind { Group, Direct { peer: User }, Notes, } impl CommunityKind { pub fn is_group(&self) -> bool { matches!(self, Self::Group) } pub fn is_direct(&self) -> bool { matches!(self, Self::Direct { .. }) } pub fn peer(&self) -> Option<&User> { match self { Self::Direct { peer } => Some(peer), Self::Group | Self::Notes => None, } } } #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] pub enum Sanction { Kick, Ban, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] pub enum BanScope { Account, AccountAndIp, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Community { pub id: Id, pub name: String, pub kind: CommunityKind, pub avatar_id: Option, pub voice_enabled: bool, } #[derive(Debug, Clone, Default, PartialEq, Eq, Encode, Decode)] pub struct Unread { pub community_id: Id, pub messages: u32, pub mentions: u32, pub last_read: Option, } pub fn mentions(body: &str, username: &str) -> bool { let body = body.to_lowercase(); let needle = format!("@{}", username.to_lowercase()); body.match_indices(&needle).any(|(at, _)| { !body[at + needle.len()..] .starts_with(|next: char| next.is_ascii_alphanumeric() || matches!(next, '_' | '-')) }) } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct VoiceRoom { pub community_id: Id, pub community: String, pub users: Vec, pub muted: Vec, pub ghosts: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct CommunitySummary { pub community: Community, pub owner: Option, pub members: u64, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Ban { pub user: User, pub reason: Option, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub enum LogAction { Created, Renamed { name: String }, AvatarChanged, AvatarRemoved, Invited, Joined, Left, Kicked { reason: Option }, Banned { reason: Option }, Unbanned, RankChanged { rank: Rank }, MessageDeleted, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct LogEntry { pub id: Id, pub created_at: u64, pub actor: Option, pub target: Option, pub action: LogAction, } macro_rules! admin_actions { ($($variant:ident $(($inner:ty))? $({ $($field:ident: $ty:ty),* })? => $text:literal),* $(,)?) => { #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub enum AdminAction { $($variant $(($inner))? $({ $($field: $ty),* })?),* } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] AdminActionKind { $($variant => $text),* } } impl AdminAction { pub fn kind(&self) -> AdminActionKind { match self { $(AdminAction::$variant { .. } => AdminActionKind::$variant),* } } } }; } admin_actions! { RoleChanged { role: Role } => "role_changed", UserDeleted => "user_deleted", UserSuspended => "user_suspended", UserUnsuspended => "user_unsuspended", SessionsRevoked => "sessions_revoked", PasswordReset => "password_reset", ProfileCleared => "profile_cleared", UserCreated => "user_created", CommunityDeleted => "community_deleted", OwnershipTransferred => "ownership_transferred", SettingsChanged => "settings_changed", UserRenamed { from: String } => "user_renamed", UserTimedOut { minutes: u32 } => "user_timed_out", UserBanned { reason: Option } => "user_banned", BanRemoved => "ban_removed", InviteCreated => "invite_created", InviteDeleted => "invite_deleted", ReportHandled { status: ReportStatus } => "report_handled", MessageDeleted => "message_deleted", UserPurged { messages: u64 } => "user_purged", VoiceKicked => "voice_kicked", VoiceMuted { muted: bool } => "voice_muted", CommunityVoiceSet { enabled: bool } => "community_voice_set", AttachmentDeleted => "attachment_deleted", GhostEnabled => "ghost_enabled", GhostDisabled => "ghost_disabled", GhostVisit => "ghost_visit", GhostVoice { seconds: u64 } => "ghost_voice", BackupCreated { name: String } => "backup_created", BackupDeleted { name: String } => "backup_deleted", BackupDownloaded { name: String } => "backup_downloaded", ReleasePublished { seq: u64, required: bool } => "release_published", ReleaseRequired { seq: u64, required: bool } => "release_required", Community(LogAction) => "community", } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct AdminLogEntry { pub id: Id, pub actor: Option, pub target: Option, pub created_at: u64, pub community: Option, pub action: AdminAction, } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] ReportStatus { Open => "open", Resolved => "resolved", Dismissed => "dismissed", } } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Report { pub id: Id, pub created_at: u64, pub reporter: Option, pub target: Option, pub message_id: Option, pub community_id: Option, pub community: Option, pub body: Option, pub reason: String, pub status: ReportStatus, pub handled_by: Option, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct ServerSettings { pub name: String, pub registration_open: bool, pub max_attachment_bytes: u64, pub maintenance: bool, pub user_quota_bytes: u64, pub community_quota_bytes: u64, pub message_retention_days: u32, pub attachment_retention_days: u32, pub announcement: String, pub max_communities_per_user: u32, pub max_message_length: u32, pub max_avatar_bytes: u64, pub messages_per_minute: u32, pub logins_per_minute: u32, } impl Default for ServerSettings { fn default() -> Self { Self { name: "yumush".to_string(), registration_open: true, max_attachment_bytes: DEFAULT_MAX_ATTACHMENT_BYTES, maintenance: false, user_quota_bytes: 0, community_quota_bytes: 0, message_retention_days: 0, attachment_retention_days: 0, announcement: String::new(), max_communities_per_user: 0, max_message_length: MAXIMUM_MESSAGE_LENGTH as u32, max_avatar_bytes: MAXIMUM_AVATAR_BYTES as u64, messages_per_minute: DEFAULT_MESSAGES_PER_MINUTE, logins_per_minute: DEFAULT_LOGINS_PER_MINUTE, } } } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct AttachmentInfo { pub attachment: Attachment, pub uploader: Option, pub community: String, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Staged { pub file: crate::update::Sha, pub target: String, pub size: u64, pub packed: u64, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct ReleaseFile { pub target: String, pub size: u64, pub packed: u64, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct ReleaseInfo { pub seq: u64, pub required: bool, pub created_ms: u64, pub files: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] pub enum UpdateState { Unknown, Latest, Available { seq: u64, required: bool }, Downloading { seq: u64, percent: u8 }, Installing { seq: u64 }, Ready { seq: u64 }, Failed { seq: u64 }, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct UpdateClient { pub device: String, pub release: Option, pub target: Option, pub state: UpdateState, pub online: bool, pub seen_ms: u64, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct BackupInfo { pub name: String, pub size_bytes: u64, pub created_ms: u64, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct UserUsage { pub user: User, pub bytes: u64, pub count: u64, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct CommunityUsage { pub community_id: Id, pub community: String, pub bytes: u64, pub count: u64, } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] ServerBanKind { Username => "username", Ip => "ip", } } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct ServerBan { pub id: Id, pub kind: ServerBanKind, pub value: String, pub reason: Option, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct InviteCode { pub code: String, pub uses_left: u32, pub expires_at: Option, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Overview { pub users: u64, pub builders: u64, pub communities: u64, pub online: u64, pub voice: u64, pub storage_bytes: u64, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] pub struct DayStat { pub day: u64, pub active_users: u64, pub messages: u64, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Invitation { pub community: Community, pub inviter: User, } #[derive(Debug, Clone, Default, PartialEq, Eq, Encode, Decode)] pub struct Friends { pub friends: Vec, pub incoming: Vec, pub outgoing: Vec, } string_enum! { #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] AttachmentKind { Image => "image", Video => "video", Audio => "audio", File => "file", } } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Attachment { pub id: Id, pub name: String, pub size: u64, pub kind: AttachmentKind, pub created_at: u64, pub width: Option, pub height: Option, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Quote { pub message_id: Id, pub user_id: Id, pub body: String, pub attachments: u32, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub enum Reply { Quote(Quote), Deleted(Option), } impl Reply { pub fn quote(&self) -> Option<&Quote> { match self { Reply::Quote(quote) => Some(quote), Reply::Deleted(_) => None, } } pub fn redact(&mut self) { if let Reply::Deleted(quote) = self { *quote = None; } } } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Edit { pub body: String, pub edited_at: u64, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct SnippetMessage { pub id: Id, pub user_id: Id, pub username: String, pub avatar_id: Option, pub created_at: u64, pub body: String, pub attachments: Vec, pub poll: Option, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Snippet { pub origin: Option, pub messages: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Reaction { pub emoji: String, pub user_ids: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct PollOption { pub text: String, pub votes: u32, pub voter_ids: Vec, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Poll { pub question: String, pub options: Vec, pub multiple: bool, pub closed: bool, pub public: bool, pub voters: u32, pub ends_at: Option, } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct Message { pub id: Id, pub user_id: Id, pub community_id: Id, pub created_at: u64, pub body: String, pub attachments: Vec, pub reactions: Vec, pub reply: Option, pub snippet: Option, pub poll: Option, pub edited: bool, pub pinned: bool, pub deleted: bool, pub call: Option, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)] pub enum CallLog { Started, Declined, Missed, Ended { seconds: u64 }, } impl CallLog { pub fn as_str(self) -> &'static str { match self { CallLog::Started => "started", CallLog::Declined => "declined", CallLog::Missed => "missed", CallLog::Ended { .. } => "ended", } } pub fn parse(text: &str, seconds: u64) -> Option { match text { "started" => Some(CallLog::Started), "declined" => Some(CallLog::Declined), "missed" => Some(CallLog::Missed), "ended" => Some(CallLog::Ended { seconds }), _ => None, } } pub fn seconds(self) -> u64 { match self { CallLog::Ended { seconds } => seconds, CallLog::Started | CallLog::Declined | CallLog::Missed => 0, } } } #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode)] pub struct MessageHit { pub message: Message, pub community: String, pub author: Option, }