2025-03-27 18:10:47 -04:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use tetratto_shared::{snow::AlmostSnowflake, unix_epoch_timestamp};
|
|
|
|
use super::communities_permissions::CommunityPermission;
|
|
|
|
|
2025-04-03 12:43:36 -04:00
|
|
|
#[derive(Clone, Serialize, Deserialize)]
|
2025-03-27 18:10:47 -04:00
|
|
|
pub struct Community {
|
|
|
|
pub id: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub title: String,
|
|
|
|
pub context: CommunityContext,
|
|
|
|
/// The ID of the owner of the community.
|
|
|
|
pub owner: usize,
|
2025-04-01 15:03:56 -04:00
|
|
|
/// Who can read the community.
|
2025-03-27 18:10:47 -04:00
|
|
|
pub read_access: CommunityReadAccess,
|
2025-04-01 15:03:56 -04:00
|
|
|
/// Who can write to the community (create posts belonging to it).
|
2025-03-27 18:10:47 -04:00
|
|
|
///
|
2025-04-01 15:03:56 -04:00
|
|
|
/// The owner of the community (and moderators) are the ***only*** people
|
2025-03-27 18:10:47 -04:00
|
|
|
/// capable of removing posts.
|
|
|
|
pub write_access: CommunityWriteAccess,
|
2025-04-01 15:03:56 -04:00
|
|
|
/// Who can join the community.
|
|
|
|
pub join_access: CommunityJoinAccess,
|
2025-03-29 00:26:56 -04:00
|
|
|
// likes
|
2025-03-27 18:10:47 -04:00
|
|
|
pub likes: isize,
|
|
|
|
pub dislikes: isize,
|
2025-03-29 00:26:56 -04:00
|
|
|
// counts
|
|
|
|
pub member_count: usize,
|
2025-03-27 18:10:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Community {
|
|
|
|
/// Create a new [`Community`].
|
|
|
|
pub fn new(title: String, owner: usize) -> Self {
|
|
|
|
Self {
|
|
|
|
id: AlmostSnowflake::new(1234567890)
|
|
|
|
.to_string()
|
|
|
|
.parse::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
2025-03-29 00:26:56 -04:00
|
|
|
title: title.clone(),
|
|
|
|
context: CommunityContext {
|
|
|
|
display_name: title,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2025-03-27 18:10:47 -04:00
|
|
|
owner,
|
|
|
|
read_access: CommunityReadAccess::default(),
|
|
|
|
write_access: CommunityWriteAccess::default(),
|
2025-04-01 15:03:56 -04:00
|
|
|
join_access: CommunityJoinAccess::default(),
|
|
|
|
likes: 0,
|
|
|
|
dislikes: 0,
|
|
|
|
member_count: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the "void" community. This is where all posts with a deleted community
|
|
|
|
/// resolve to.
|
|
|
|
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,
|
2025-03-27 18:10:47 -04:00
|
|
|
likes: 0,
|
|
|
|
dislikes: 0,
|
2025-03-29 00:26:56 -04:00
|
|
|
member_count: 0,
|
2025-03-27 18:10:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-10 18:16:52 -04:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
|
2025-03-27 18:10:47 -04:00
|
|
|
pub struct CommunityContext {
|
2025-04-07 16:32:09 -04:00
|
|
|
#[serde(default)]
|
2025-03-29 00:26:56 -04:00
|
|
|
pub display_name: String,
|
2025-04-07 16:32:09 -04:00
|
|
|
#[serde(default)]
|
2025-03-27 18:10:47 -04:00
|
|
|
pub description: String,
|
2025-04-07 16:32:09 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub is_nsfw: bool,
|
2025-04-12 22:25:54 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub enable_questions: bool,
|
2025-03-27 18:10:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Who can read a [`Community`].
|
2025-04-03 12:43:36 -04:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
2025-03-27 18:10:47 -04:00
|
|
|
pub enum CommunityReadAccess {
|
|
|
|
/// Everybody can view the community.
|
|
|
|
Everybody,
|
2025-04-01 15:03:56 -04:00
|
|
|
/// Only people in the community can view the community.
|
|
|
|
Joined,
|
2025-03-27 18:10:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CommunityReadAccess {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Everybody
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Who can write to a [`Community`].
|
2025-04-03 12:43:36 -04:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
2025-03-27 18:10:47 -04:00
|
|
|
pub enum CommunityWriteAccess {
|
|
|
|
/// Everybody.
|
|
|
|
Everybody,
|
|
|
|
/// Only people who joined the community can write to it.
|
|
|
|
///
|
|
|
|
/// Memberships can be managed by the owner of the community.
|
|
|
|
Joined,
|
|
|
|
/// Only the owner of the community.
|
|
|
|
Owner,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CommunityWriteAccess {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Joined
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-01 15:03:56 -04:00
|
|
|
/// Who can join a [`Community`].
|
2025-04-03 12:43:36 -04:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
2025-04-01 15:03:56 -04:00
|
|
|
pub enum CommunityJoinAccess {
|
|
|
|
/// Joins are closed. Nobody can join the community.
|
|
|
|
Nobody,
|
|
|
|
/// All authenticated users can join the community.
|
|
|
|
Everybody,
|
|
|
|
/// People must send a request to join.
|
|
|
|
Request,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CommunityJoinAccess {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::Everybody
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-01 23:16:09 -04:00
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
2025-03-27 18:10:47 -04:00
|
|
|
pub struct CommunityMembership {
|
|
|
|
pub id: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub owner: usize,
|
|
|
|
pub community: usize,
|
|
|
|
pub role: CommunityPermission,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommunityMembership {
|
|
|
|
/// Create a new [`CommunityMembership`].
|
|
|
|
pub fn new(owner: usize, community: usize, role: CommunityPermission) -> Self {
|
|
|
|
Self {
|
|
|
|
id: AlmostSnowflake::new(1234567890)
|
|
|
|
.to_string()
|
|
|
|
.parse::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
owner,
|
|
|
|
community,
|
|
|
|
role,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-10 18:16:52 -04:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2025-03-27 18:10:47 -04:00
|
|
|
pub struct PostContext {
|
2025-04-03 12:43:36 -04:00
|
|
|
#[serde(default = "default_comments_enabled")]
|
2025-03-27 18:10:47 -04:00
|
|
|
pub comments_enabled: bool,
|
2025-04-03 12:43:36 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub is_pinned: bool,
|
2025-04-07 16:07:01 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub is_profile_pinned: bool,
|
|
|
|
#[serde(default)]
|
|
|
|
pub edited: usize,
|
2025-04-07 16:32:09 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub is_nsfw: bool,
|
2025-04-10 18:16:52 -04:00
|
|
|
#[serde(default)]
|
|
|
|
pub repost: Option<RepostContext>,
|
|
|
|
#[serde(default = "default_reposts_enabled")]
|
|
|
|
pub reposts_enabled: bool,
|
2025-04-12 22:25:54 -04:00
|
|
|
/// The ID of the question this post is answering.
|
|
|
|
#[serde(default)]
|
|
|
|
pub answering: usize,
|
2025-04-13 00:42:10 -04:00
|
|
|
#[serde(default = "default_reactions_enabled")]
|
|
|
|
pub reactions_enabled: bool,
|
2025-04-03 12:43:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn default_comments_enabled() -> bool {
|
|
|
|
true
|
2025-03-27 18:10:47 -04:00
|
|
|
}
|
|
|
|
|
2025-04-10 18:16:52 -04:00
|
|
|
fn default_reposts_enabled() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2025-04-13 00:42:10 -04:00
|
|
|
fn default_reactions_enabled() -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
2025-03-27 18:10:47 -04:00
|
|
|
impl Default for PostContext {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
2025-04-03 12:43:36 -04:00
|
|
|
comments_enabled: default_comments_enabled(),
|
2025-04-13 00:42:10 -04:00
|
|
|
reposts_enabled: default_reposts_enabled(),
|
2025-04-03 12:43:36 -04:00
|
|
|
is_pinned: false,
|
2025-04-07 16:07:01 -04:00
|
|
|
is_profile_pinned: false,
|
|
|
|
edited: 0,
|
2025-04-07 16:32:09 -04:00
|
|
|
is_nsfw: false,
|
2025-04-10 18:16:52 -04:00
|
|
|
repost: None,
|
2025-04-12 22:25:54 -04:00
|
|
|
answering: 0,
|
2025-04-13 00:42:10 -04:00
|
|
|
reactions_enabled: default_reactions_enabled(),
|
2025-03-27 18:10:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-04-10 18:16:52 -04:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct RepostContext {
|
2025-04-11 18:08:51 -04:00
|
|
|
/// Should be `false` is `reposting` is `Some`.
|
2025-04-10 18:16:52 -04:00
|
|
|
///
|
|
|
|
/// Declares the post to be a repost of another post.
|
|
|
|
pub is_repost: bool,
|
|
|
|
/// Should be `None` if `is_repost` is true.
|
|
|
|
///
|
|
|
|
/// Sets the ID of the other post to load.
|
|
|
|
pub reposting: Option<usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2025-03-27 18:10:47 -04:00
|
|
|
pub struct Post {
|
|
|
|
pub id: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub content: String,
|
|
|
|
/// The ID of the owner of this post.
|
|
|
|
pub owner: usize,
|
|
|
|
/// The ID of the [`Community`] this post belongs to.
|
|
|
|
pub community: usize,
|
|
|
|
/// Extra information about the post.
|
|
|
|
pub context: PostContext,
|
|
|
|
/// The ID of the post this post is a comment on.
|
|
|
|
pub replying_to: Option<usize>,
|
|
|
|
pub likes: isize,
|
|
|
|
pub dislikes: isize,
|
|
|
|
pub comment_count: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Post {
|
|
|
|
/// Create a new [`Post`].
|
|
|
|
pub fn new(
|
|
|
|
content: String,
|
|
|
|
community: usize,
|
|
|
|
replying_to: Option<usize>,
|
|
|
|
owner: usize,
|
|
|
|
) -> Self {
|
|
|
|
Self {
|
|
|
|
id: AlmostSnowflake::new(1234567890)
|
|
|
|
.to_string()
|
|
|
|
.parse::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
content,
|
|
|
|
owner,
|
|
|
|
community,
|
|
|
|
context: PostContext::default(),
|
|
|
|
replying_to,
|
|
|
|
likes: 0,
|
|
|
|
dislikes: 0,
|
|
|
|
comment_count: 0,
|
|
|
|
}
|
|
|
|
}
|
2025-04-10 18:16:52 -04:00
|
|
|
|
|
|
|
/// Create a new [`Post`] (as a repost of the given `post_id`).
|
|
|
|
pub fn repost(content: String, community: usize, owner: usize, post_id: usize) -> Self {
|
|
|
|
let mut post = Self::new(content, community, None, owner);
|
|
|
|
|
|
|
|
post.context.repost = Some(RepostContext {
|
|
|
|
is_repost: false,
|
|
|
|
reposting: Some(post_id),
|
|
|
|
});
|
|
|
|
|
|
|
|
post
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Make the given post a reposted post.
|
|
|
|
pub fn mark_as_repost(&mut self) {
|
|
|
|
self.context.repost = Some(RepostContext {
|
|
|
|
is_repost: true,
|
|
|
|
reposting: None,
|
|
|
|
});
|
|
|
|
}
|
2025-03-27 18:10:47 -04:00
|
|
|
}
|
2025-04-12 22:25:54 -04:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct Question {
|
|
|
|
pub id: usize,
|
|
|
|
pub created: usize,
|
|
|
|
pub owner: usize,
|
|
|
|
pub receiver: usize,
|
|
|
|
pub content: String,
|
|
|
|
/// The `is_global` flag allows any (authenticated) user to respond
|
2025-04-12 22:42:57 -04:00
|
|
|
/// to the question. Normally, only the `receiver` can do so.
|
2025-04-12 22:25:54 -04:00
|
|
|
///
|
|
|
|
/// If `is_global` is true, `receiver` should be 0 (and vice versa).
|
|
|
|
pub is_global: bool,
|
|
|
|
/// The number of answers the question has. Should never really be changed
|
|
|
|
/// unless the question has `is_global` set to true.
|
|
|
|
pub answer_count: usize,
|
|
|
|
/// The ID of the community this question is asked to. This should only be > 0
|
|
|
|
/// if `is_global` is set to true.
|
|
|
|
pub community: usize,
|
2025-04-13 12:44:08 -04:00
|
|
|
// likes
|
|
|
|
#[serde(default)]
|
|
|
|
pub likes: isize,
|
|
|
|
#[serde(default)]
|
|
|
|
pub dislikes: isize,
|
2025-04-12 22:25:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Question {
|
|
|
|
/// Create a new [`Question`].
|
|
|
|
pub fn new(owner: usize, receiver: usize, content: String, is_global: bool) -> Self {
|
|
|
|
Self {
|
|
|
|
id: AlmostSnowflake::new(1234567890)
|
|
|
|
.to_string()
|
|
|
|
.parse::<usize>()
|
|
|
|
.unwrap(),
|
|
|
|
created: unix_epoch_timestamp() as usize,
|
|
|
|
owner,
|
|
|
|
receiver,
|
|
|
|
content,
|
|
|
|
is_global,
|
|
|
|
answer_count: 0,
|
|
|
|
community: 0,
|
2025-04-13 12:44:08 -04:00
|
|
|
likes: 0,
|
|
|
|
dislikes: 0,
|
2025-04-12 22:25:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|