use bitcode::{Decode, DecodeOwned, Encode}; use crate::{ constant::{MAXIMUM_AVATAR_BYTES, MAXIMUM_MESSAGE_LENGTH}, error::Error, model::Hue, model::{ Account, AdminActionKind, AdminLogEntry, AttachmentInfo, BackupInfo, Ban, BanScope, Community, CommunitySummary, CommunityUsage, DayStat, Edit, Friends, Id, Invitation, InviteCode, LogEntry, Member, Message, MessageHit, Overview, Presence, Rank, ReleaseInfo, Report, ReportStatus, ServerBan, ServerSettings, SessionInfo, Unread, UpdateClient, User, UserUsage, VoiceRoom, }, share::{Codec, Kind, Quality, Share, Source}, stats::Stats, validate::{ validate_avatar_size, validate_community_name, validate_forward, validate_message_edit, validate_new_message, validate_password, validate_poll, validate_reaction, validate_reason, validate_server_settings, validate_status, validate_username, }, }; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Gate { Guest, User, Builder, } pub trait Call: Encode + DecodeOwned + Send + 'static { type Reply: Encode + DecodeOwned + Send + 'static; const GATE: Gate; fn into_request(self) -> Request; fn validate(&self) -> Result<(), Error> { Ok(()) } } macro_rules! calls { ($($name:ident: $gate:ident -> $reply:ty $(, |$c:ident| $check:expr)?;)*) => { #[derive(Debug, Clone, Encode, Decode)] pub enum Request { $($name($name),)* } $( impl Call for $name { type Reply = $reply; const GATE: Gate = Gate::$gate; fn into_request(self) -> Request { Request::$name(self) } $( fn validate(&self) -> Result<(), Error> { let $c = self; $check } )? } )* }; } #[derive(Debug, Clone, Encode, Decode)] pub struct GetSettings; #[derive(Debug, Clone, Encode, Decode)] pub struct GetUser { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct UpdateUser { pub username: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct UpdateStatus { pub status: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct UpdateHue { pub hue: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct UpdatePresence { pub presence: Presence, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListPresences; #[derive(Debug, Clone, Encode, Decode)] pub struct UpdateAvatar { pub image: Vec, } #[derive(Debug, Clone, Encode, Decode)] pub struct RemoveAvatar; #[derive(Debug, Clone, Encode, Decode)] pub struct GetAvatar { pub avatar_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListSessions { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct RevokeSession { pub user_id: Id, pub session_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct CreateCommunity { pub name: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct GetCommunity { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct UpdateCommunity { pub community_id: Id, pub name: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct UpdateCommunityAvatar { pub community_id: Id, pub image: Option>, } #[derive(Debug, Clone, Encode, Decode)] pub struct DeleteCommunity { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct LeaveCommunity { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListCommunities; #[derive(Debug, Clone, Encode, Decode)] pub struct ListMembers { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct Kick { pub community_id: Id, pub user_id: Id, pub reason: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct BanMember { pub community_id: Id, pub user_id: Id, pub reason: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct Unban { pub community_id: Id, pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListBans { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct SetRank { pub community_id: Id, pub user_id: Id, pub rank: Rank, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListLogs { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct CreateMessage { pub community_id: Id, pub body: String, pub attachments: Vec, pub reply_to: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct ForwardMessages { pub message_ids: Vec, pub community_id: Id, pub snippet: bool, } #[derive(Debug, Clone, Encode, Decode)] pub struct UpdateMessage { pub message_id: Id, pub body: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct DeleteMessage { pub message_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListEdits { pub message_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct CreatePoll { pub community_id: Id, pub question: String, pub options: Vec, pub multiple: bool, pub public: bool, pub duration: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct Vote { pub message_id: Id, pub positions: Vec, } #[derive(Debug, Clone, Encode, Decode)] pub struct ClosePoll { pub message_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct MyVotes { pub message_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct React { pub message_id: Id, pub emoji: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct Pin { pub message_id: Id, pub pinned: bool, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListPins { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListMessages { pub community_id: Id, pub before: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListUnreads; #[derive(Debug, Clone, Encode, Decode)] pub struct MarkRead { pub community_id: Id, pub message_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct Typing { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct FindUser { pub username: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct OpenDirect { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct OpenNotes; #[derive(Debug, Clone, Encode, Decode)] pub struct ListFriends; #[derive(Debug, Clone, Encode, Decode)] pub struct AddFriend { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct AcceptFriend { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct RemoveFriend { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct Invite { pub community_id: Id, pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListInvitations; #[derive(Debug, Clone, Encode, Decode)] pub struct AcceptInvitation { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct DeclineInvitation { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ReportMessage { pub message_id: Id, pub reason: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct ReportUser { pub user_id: Id, pub reason: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListReports { pub status: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct HandleReport { pub report_id: Id, pub status: ReportStatus, } #[derive(Debug, Clone, Encode, Decode)] pub struct JoinVoice { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct LeaveVoice; #[derive(Debug, Clone, Encode, Decode)] pub struct StartShare { pub kind: Kind, pub source: Source, } #[derive(Debug, Clone, Encode, Decode)] pub struct StopShare { pub kind: Kind, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListShares { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct WatchShare { pub user_id: Id, pub kind: Kind, pub quality: Quality, pub decode: Vec, } #[derive(Debug, Clone, Encode, Decode)] pub struct UnwatchShare { pub user_id: Id, pub kind: Kind, } #[derive(Debug, Clone, Encode, Decode)] pub struct RequestKeyframe { pub user_id: Id, pub kind: Kind, pub recover: bool, } #[derive(Debug, Clone, Encode, Decode)] pub struct ReportLoss { pub user_id: Id, pub kind: Kind, pub permille: u16, } #[derive(Debug, Clone, Encode, Decode)] pub struct DeclineCall { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListVoiceParticipants { pub community_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListVoiceRooms; #[derive(Debug, Clone, Encode, Decode)] pub struct KickFromVoice { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ServerMute { pub user_id: Id, pub muted: bool, } #[derive(Debug, Clone, Encode, Decode)] pub struct Register { pub username: String, pub password: String, pub invite: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct Login { pub username: String, pub password: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct Logout; #[derive(Debug, Clone, Encode, Decode)] pub struct DeleteUser { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct WatchCommunity { pub community_id: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListUsers; #[derive(Debug, Clone, Encode, Decode)] pub struct ListUserFriends { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListAdminLogs { pub actor_id: Option, pub target_id: Option, pub action: Option, pub after_ms: Option, pub before_ms: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct GetOverview; #[derive(Debug, Clone, Encode, Decode)] pub struct SuspendUser { pub user_id: Id, pub suspended: bool, } #[derive(Debug, Clone, Encode, Decode)] pub struct RevokeSessions { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ResetPassword { pub user_id: Id, pub password: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct ClearProfile { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListAllCommunities; #[derive(Debug, Clone, Encode, Decode)] pub struct TransferOwnership { pub community_id: Id, pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct UpdateServerSettings { pub settings: ServerSettings, } #[derive(Debug, Clone, Encode, Decode)] pub struct CreateUser { pub username: String, pub password: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct RenameUser { pub user_id: Id, pub username: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct TimeoutUser { pub user_id: Id, pub minutes: u32, } #[derive(Debug, Clone, Encode, Decode)] pub struct BanUser { pub user_id: Id, pub reason: Option, pub scope: BanScope, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListServerBans; #[derive(Debug, Clone, Encode, Decode)] pub struct RemoveServerBan { pub id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct CreateInviteCode { pub uses: u32, pub hours: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListInviteCodes; #[derive(Debug, Clone, Encode, Decode)] pub struct DeleteInviteCode { pub code: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct SearchMessages { pub text: Option, pub user_id: Option, pub community_id: Option, pub after_ms: Option, pub before_ms: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct SearchCommunity { pub community_id: Id, pub text: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct PurgeUser { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct SetCommunityVoice { pub community_id: Id, pub enabled: bool, } #[derive(Debug, Clone, Encode, Decode)] pub struct ListAttachments { pub user_id: Option, pub community_id: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct DeleteAttachment { pub attachment_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct GetStorageUsage; #[derive(Debug, Clone, Encode, Decode)] pub struct GetDailyStats { pub days: u32, } #[derive(Debug, Clone, Encode, Decode)] pub struct SetGhost { pub enabled: bool, } #[derive(Debug, Clone, Encode, Decode)] pub struct CreateBackup; #[derive(Debug, Clone, Encode, Decode)] pub struct ListBackups; #[derive(Debug, Clone, Encode, Decode)] pub struct ListReleases; #[derive(Debug, Clone, Encode, Decode)] pub struct ListUpdateClients { pub user_id: Id, } #[derive(Debug, Clone, Encode, Decode)] pub struct PublishRelease { pub files: Vec, pub required: bool, } #[derive(Debug, Clone, Encode, Decode)] pub struct GetForge; #[derive(Debug, Clone, Encode, Decode)] pub struct CreateTag { pub tag: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct ImportRelease { pub tag: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct SetReleaseRequired { pub seq: u64, pub required: bool, } #[derive(Debug, Clone, Encode, Decode)] pub struct FollowStats { pub user_id: Option, } #[derive(Debug, Clone, Encode, Decode)] pub struct GetServerStats; #[derive(Debug, Clone, Encode, Decode)] pub struct RestartServer; #[derive(Debug, Clone, Encode, Decode)] pub struct PushStats { pub stats: Stats, } #[derive(Debug, Clone, Encode, Decode)] pub struct DeleteBackup { pub name: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct Session { pub user: User, pub token: String, } #[derive(Debug, Clone, Encode, Decode)] pub struct StorageUsage { pub users: Vec, pub communities: Vec, } calls! { GetSettings: Guest -> ServerSettings; GetUser: User -> User; UpdateUser: User -> User, |c| validate_username(&c.username); UpdateStatus: User -> User, |c| validate_status(&c.status); UpdateHue: User -> User; UpdatePresence: User -> (); ListPresences: User -> Vec<(Id, Presence)>; UpdateAvatar: User -> User, |c| validate_avatar_size(&c.image, MAXIMUM_AVATAR_BYTES as u64); RemoveAvatar: User -> User; GetAvatar: User -> Vec; ListSessions: Builder -> Vec; RevokeSession: Builder -> (); CreateCommunity: User -> Community, |c| validate_community_name(&c.name); GetCommunity: User -> Community; UpdateCommunity: User -> Community, |c| validate_community_name(&c.name); UpdateCommunityAvatar: User -> Community, |c| c.image.as_deref().map_or(Ok(()), |image| validate_avatar_size(image, MAXIMUM_AVATAR_BYTES as u64)); DeleteCommunity: User -> (); LeaveCommunity: User -> (); ListCommunities: User -> Vec; ListMembers: User -> Vec; Kick: User -> (), |c| validate_reason(c.reason.as_deref()); BanMember: User -> (), |c| validate_reason(c.reason.as_deref()); Unban: User -> (); ListBans: User -> Vec; SetRank: User -> (); ListLogs: User -> Vec; CreateMessage: User -> Message, |c| validate_new_message(&c.body, c.attachments.len(), MAXIMUM_MESSAGE_LENGTH); ForwardMessages: User -> Vec, |c| validate_forward(c.message_ids.len()); UpdateMessage: User -> Message, |c| validate_message_edit(&c.body, MAXIMUM_MESSAGE_LENGTH); DeleteMessage: User -> (); ListEdits: Builder -> Vec; CreatePoll: User -> Message, |c| validate_poll(&c.question, &c.options); Vote: User -> (); ClosePoll: User -> (); MyVotes: User -> Vec; React: User -> (), |c| validate_reaction(&c.emoji); Pin: User -> (); ListPins: User -> Vec; ListMessages: User -> Vec; ListUnreads: User -> Vec; MarkRead: User -> (); Typing: User -> (); FindUser: User -> User; OpenDirect: User -> Community; OpenNotes: User -> Community; ListFriends: User -> Friends; AddFriend: User -> (); AcceptFriend: User -> (); RemoveFriend: User -> (); Invite: User -> (); ListInvitations: User -> Vec; AcceptInvitation: User -> Community; DeclineInvitation: User -> (); ReportMessage: User -> (), |c| validate_reason(Some(&c.reason)); ReportUser: User -> (), |c| validate_reason(Some(&c.reason)); ListReports: Builder -> Vec; HandleReport: Builder -> (), |c| if c.status == ReportStatus::Open { Err(Error::Forbidden) } else { Ok(()) }; JoinVoice: User -> Vec; LeaveVoice: User -> (); StartShare: User -> (); StopShare: User -> (); ListShares: User -> Vec; WatchShare: User -> (); UnwatchShare: User -> (); RequestKeyframe: User -> (); ReportLoss: User -> (); DeclineCall: User -> (); ListVoiceParticipants: User -> Vec; ListVoiceRooms: Builder -> Vec; KickFromVoice: Builder -> (); ServerMute: Builder -> (); Register: Guest -> Session, |c| validate_username(&c.username).and(validate_password(&c.password)); Login: Guest -> Session, |c| validate_password(&c.password); Logout: User -> (); DeleteUser: User -> (); WatchCommunity: User -> (); ListUsers: Builder -> Vec; ListAdminLogs: Builder -> Vec; ListUserFriends: Builder -> Vec; GetOverview: Builder -> Overview; SuspendUser: Builder -> (); RevokeSessions: Builder -> (); ResetPassword: Builder -> (), |c| validate_password(&c.password); ClearProfile: Builder -> User; ListAllCommunities: Builder -> Vec; TransferOwnership: User -> (); UpdateServerSettings: Builder -> ServerSettings, |c| validate_server_settings(&c.settings); CreateUser: Builder -> User, |c| validate_username(&c.username).and(validate_password(&c.password)); RenameUser: Builder -> User, |c| validate_username(&c.username); TimeoutUser: Builder -> (); BanUser: Builder -> (), |c| validate_reason(c.reason.as_deref()); ListServerBans: Builder -> Vec; RemoveServerBan: Builder -> (); CreateInviteCode: Builder -> InviteCode, |c| if c.uses == 0 { Err(Error::Forbidden) } else { Ok(()) }; ListInviteCodes: Builder -> Vec; DeleteInviteCode: Builder -> (); SearchMessages: Builder -> Vec; SearchCommunity: User -> Vec; PurgeUser: Builder -> (); SetCommunityVoice: User -> Community; ListAttachments: Builder -> Vec; DeleteAttachment: Builder -> (); GetStorageUsage: Builder -> StorageUsage; GetDailyStats: Builder -> Vec; SetGhost: Builder -> (); CreateBackup: Builder -> BackupInfo; ListBackups: Builder -> Vec; DeleteBackup: Builder -> (); ListReleases: Builder -> Vec; ListUpdateClients: Builder -> Vec; PublishRelease: Builder -> ReleaseInfo; ImportRelease: Builder -> (); GetForge: Builder -> Option; CreateTag: Builder -> (); SetReleaseRequired: Builder -> (); FollowStats: Builder -> Option; PushStats: User -> (); GetServerStats: Builder -> Stats; RestartServer: Builder -> (); }