use serde::{Deserialize, Serialize};
use tetratto_shared::{snow::Snowflake, unix_epoch_timestamp};
use super::communities_permissions::CommunityPermission;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Community {
pub id: usize,
pub created: usize,
pub title: String,
pub context: CommunityContext,
pub owner: usize,
pub read_access: CommunityReadAccess,
pub write_access: CommunityWriteAccess,
pub join_access: CommunityJoinAccess,
pub likes: isize,
pub dislikes: isize,
pub member_count: usize,
pub is_forge: bool,
pub post_count: usize,
}
impl Community {
pub fn new(title: String, owner: usize) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created: unix_epoch_timestamp(),
title: title.clone(),
context: CommunityContext {
display_name: title,
..Default::default()
},
owner,
read_access: CommunityReadAccess::default(),
write_access: CommunityWriteAccess::default(),
join_access: CommunityJoinAccess::default(),
likes: 0,
dislikes: 0,
member_count: 0,
is_forge: false,
post_count: 0,
}
}
pub fn void() -> Self {
Self {
id: 0,
created: 0,
title: "void".to_string(),
context: CommunityContext::default(),
owner: 0,
read_access: CommunityReadAccess::Joined,
write_access: CommunityWriteAccess::Owner,
join_access: CommunityJoinAccess::Nobody,
likes: 0,
dislikes: 0,
member_count: 0,
is_forge: false,
post_count: 0,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct CommunityContext {
#[serde(default)]
pub display_name: String,
#[serde(default)]
pub description: String,
#[serde(default)]
pub is_nsfw: bool,
#[serde(default)]
pub enable_questions: bool,
#[serde(default)]
pub enable_titles: bool,
#[serde(default)]
pub require_titles: bool,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum CommunityReadAccess {
Everybody,
Joined,
}
impl Default for CommunityReadAccess {
fn default() -> Self {
Self::Everybody
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum CommunityWriteAccess {
Everybody,
Joined,
Owner,
}
impl Default for CommunityWriteAccess {
fn default() -> Self {
Self::Joined
}
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum CommunityJoinAccess {
Nobody,
Everybody,
Request,
}
impl Default for CommunityJoinAccess {
fn default() -> Self {
Self::Everybody
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CommunityMembership {
pub id: usize,
pub created: usize,
pub owner: usize,
pub community: usize,
pub role: CommunityPermission,
}
impl CommunityMembership {
pub fn new(owner: usize, community: usize, role: CommunityPermission) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created: unix_epoch_timestamp(),
owner,
community,
role,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PostContext {
#[serde(default = "default_comments_enabled")]
pub comments_enabled: bool,
#[serde(default)]
pub is_pinned: bool,
#[serde(default)]
pub is_profile_pinned: bool,
#[serde(default)]
pub edited: usize,
#[serde(default)]
pub is_nsfw: bool,
#[serde(default)]
pub repost: Option<RepostContext>,
#[serde(default = "default_reposts_enabled")]
pub reposts_enabled: bool,
#[serde(default)]
pub answering: usize,
#[serde(default = "default_reactions_enabled")]
pub reactions_enabled: bool,
#[serde(default)]
pub content_warning: String,
#[serde(default)]
pub tags: Vec<String>,
#[serde(default)]
pub full_unlist: bool,
}
fn default_comments_enabled() -> bool {
true
}
fn default_reposts_enabled() -> bool {
true
}
fn default_reactions_enabled() -> bool {
true
}
impl Default for PostContext {
fn default() -> Self {
Self {
comments_enabled: default_comments_enabled(),
reposts_enabled: default_reposts_enabled(),
is_pinned: false,
is_profile_pinned: false,
edited: 0,
is_nsfw: false,
repost: None,
answering: 0,
reactions_enabled: default_reactions_enabled(),
content_warning: String::new(),
tags: Vec::new(),
full_unlist: false,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RepostContext {
pub is_repost: bool,
pub reposting: Option<usize>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Post {
pub id: usize,
pub created: usize,
pub content: String,
pub owner: usize,
pub community: usize,
pub context: PostContext,
pub replying_to: Option<usize>,
pub likes: isize,
pub dislikes: isize,
pub comment_count: usize,
pub uploads: Vec<usize>,
pub is_deleted: bool,
pub poll_id: usize,
pub title: String,
pub is_open: bool,
pub stack: usize,
}
impl Post {
pub fn new(
content: String,
community: usize,
replying_to: Option<usize>,
owner: usize,
poll_id: usize,
) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created: unix_epoch_timestamp(),
content,
owner,
community,
context: PostContext::default(),
replying_to,
likes: 0,
dislikes: 0,
comment_count: 0,
uploads: Vec::new(),
is_deleted: false,
poll_id,
title: String::new(),
is_open: true,
stack: 0,
}
}
pub fn repost(content: String, community: usize, owner: usize, post_id: usize) -> Self {
let mut post = Self::new(content, community, None, owner, 0);
post.context.repost = Some(RepostContext {
is_repost: false,
reposting: Some(post_id),
});
post
}
pub fn mark_as_repost(&mut self) {
self.context.repost = Some(RepostContext {
is_repost: true,
reposting: None,
});
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Question {
pub id: usize,
pub created: usize,
pub owner: usize,
pub receiver: usize,
pub content: String,
pub is_global: bool,
pub answer_count: usize,
pub community: usize,
#[serde(default)]
pub likes: isize,
#[serde(default)]
pub dislikes: isize,
#[serde(default)]
pub context: QuestionContext,
#[serde(default)]
pub ip: String,
#[serde(default)]
pub drawings: Vec<usize>,
}
impl Question {
pub fn new(
owner: usize,
receiver: usize,
content: String,
is_global: bool,
ip: String,
) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created: unix_epoch_timestamp(),
owner,
receiver,
content,
is_global,
answer_count: 0,
community: 0,
likes: 0,
dislikes: 0,
context: QuestionContext::default(),
ip,
drawings: Vec::new(),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct QuestionContext {
#[serde(default)]
pub is_nsfw: bool,
#[serde(default)]
pub mask_owner: bool,
#[serde(default)]
pub asking_about: Option<usize>,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PostDraft {
pub id: usize,
pub created: usize,
pub content: String,
pub owner: usize,
}
impl PostDraft {
pub fn new(content: String, owner: usize) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
created: unix_epoch_timestamp(),
content,
owner,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Poll {
pub id: usize,
pub owner: usize,
pub created: usize,
pub expires: usize,
pub option_a: String,
pub option_b: String,
pub option_c: String,
pub option_d: String,
pub votes_a: usize,
pub votes_b: usize,
pub votes_c: usize,
pub votes_d: usize,
}
impl Poll {
pub fn new(
owner: usize,
expires: usize,
option_a: String,
option_b: String,
option_c: String,
option_d: String,
) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
owner,
created: unix_epoch_timestamp(),
expires,
option_a,
option_b,
option_c,
option_d,
votes_a: 0,
votes_b: 0,
votes_c: 0,
votes_d: 0,
}
}
}
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub enum PollOption {
A,
B,
C,
D,
}
impl From<u8> for PollOption {
fn from(value: u8) -> Self {
match value {
0 => Self::A,
1 => Self::B,
2 => Self::C,
3 => Self::D,
_ => Self::A,
}
}
}
impl Into<u8> for PollOption {
fn into(self) -> u8 {
match self {
Self::A => 0,
Self::B => 1,
Self::C => 2,
Self::D => 3,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct PollVote {
pub id: usize,
pub owner: usize,
pub created: usize,
pub poll_id: usize,
pub vote: PollOption,
}
impl PollVote {
pub fn new(owner: usize, poll_id: usize, vote: PollOption) -> Self {
Self {
id: Snowflake::new().to_string().parse::<usize>().unwrap(),
owner,
created: unix_epoch_timestamp(),
poll_id,
vote,
}
}
}