add: communities list page

TODO(ui): implement community creation, community viewing, community posting
TODO(ui): implement profile following, followers, and posts feed
This commit is contained in:
trisua 2025-03-27 18:10:47 -04:00
parent 5cfca49793
commit d6fbfc3cd6
28 changed files with 497 additions and 313 deletions

View file

@ -0,0 +1,174 @@
use serde::{Deserialize, Serialize};
use tetratto_shared::{snow::AlmostSnowflake, unix_epoch_timestamp};
use super::communities_permissions::CommunityPermission;
#[derive(Serialize, Deserialize)]
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,
/// Who can read the community page.
pub read_access: CommunityReadAccess,
/// Who can write to the community page (create posts belonging to it).
///
/// The owner of the community page (and moderators) are the ***only*** people
/// capable of removing posts.
pub write_access: CommunityWriteAccess,
pub likes: isize,
pub dislikes: isize,
}
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,
title,
context: CommunityContext::default(),
owner,
read_access: CommunityReadAccess::default(),
write_access: CommunityWriteAccess::default(),
likes: 0,
dislikes: 0,
}
}
}
#[derive(Serialize, Deserialize)]
pub struct CommunityContext {
pub description: String,
}
impl Default for CommunityContext {
fn default() -> Self {
Self {
description: String::new(),
}
}
}
/// Who can read a [`Community`].
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub enum CommunityReadAccess {
/// Everybody can view the community.
Everybody,
/// Only people with the link to the community.
Unlisted,
/// Only the owner of the community.
Private,
}
impl Default for CommunityReadAccess {
fn default() -> Self {
Self::Everybody
}
}
/// Who can write to a [`Community`].
#[derive(Serialize, Deserialize, PartialEq, Eq)]
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
}
}
#[derive(Serialize, Deserialize)]
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,
}
}
}
#[derive(Serialize, Deserialize)]
pub struct PostContext {
pub comments_enabled: bool,
}
impl Default for PostContext {
fn default() -> Self {
Self {
comments_enabled: true,
}
}
}
#[derive(Serialize, Deserialize)]
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,
}
}
}

View file

@ -7,7 +7,7 @@ use serde::{
bitflags! {
/// Fine-grained journal permissions built using bitwise operations.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct JournalPermission: u32 {
pub struct CommunityPermission: u32 {
const DEFAULT = 1 << 0;
const ADMINISTRATOR = 1 << 1;
const MEMBER = 1 << 2;
@ -18,7 +18,7 @@ bitflags! {
}
}
impl Serialize for JournalPermission {
impl Serialize for CommunityPermission {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
@ -29,7 +29,7 @@ impl Serialize for JournalPermission {
struct JournalPermissionVisitor;
impl<'de> Visitor<'de> for JournalPermissionVisitor {
type Value = JournalPermission;
type Value = CommunityPermission;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("u32")
@ -39,10 +39,10 @@ impl<'de> Visitor<'de> for JournalPermissionVisitor {
where
E: DeError,
{
if let Some(permission) = JournalPermission::from_bits(value) {
if let Some(permission) = CommunityPermission::from_bits(value) {
Ok(permission)
} else {
Ok(JournalPermission::from_bits_retain(value))
Ok(CommunityPermission::from_bits_retain(value))
}
}
@ -50,10 +50,10 @@ impl<'de> Visitor<'de> for JournalPermissionVisitor {
where
E: DeError,
{
if let Some(permission) = JournalPermission::from_bits(value as u32) {
if let Some(permission) = CommunityPermission::from_bits(value as u32) {
Ok(permission)
} else {
Ok(JournalPermission::from_bits_retain(value as u32))
Ok(CommunityPermission::from_bits_retain(value as u32))
}
}
@ -61,15 +61,15 @@ impl<'de> Visitor<'de> for JournalPermissionVisitor {
where
E: DeError,
{
if let Some(permission) = JournalPermission::from_bits(value as u32) {
if let Some(permission) = CommunityPermission::from_bits(value as u32) {
Ok(permission)
} else {
Ok(JournalPermission::from_bits_retain(value as u32))
Ok(CommunityPermission::from_bits_retain(value as u32))
}
}
}
impl<'de> Deserialize<'de> for JournalPermission {
impl<'de> Deserialize<'de> for CommunityPermission {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
@ -78,15 +78,15 @@ impl<'de> Deserialize<'de> for JournalPermission {
}
}
impl JournalPermission {
impl CommunityPermission {
/// Join two [`JournalPermission`]s into a single `u32`.
pub fn join(lhs: JournalPermission, rhs: JournalPermission) -> JournalPermission {
pub fn join(lhs: CommunityPermission, rhs: CommunityPermission) -> CommunityPermission {
lhs | rhs
}
/// Check if the given `input` contains the given [`JournalPermission`].
pub fn check(self, permission: JournalPermission) -> bool {
if (self & JournalPermission::ADMINISTRATOR) == JournalPermission::ADMINISTRATOR {
pub fn check(self, permission: CommunityPermission) -> bool {
if (self & CommunityPermission::ADMINISTRATOR) == CommunityPermission::ADMINISTRATOR {
// has administrator permission, meaning everything else is automatically true
return true;
}
@ -96,16 +96,16 @@ impl JournalPermission {
/// Check if the given [`JournalPermission`] qualifies as "Member" status.
pub fn check_member(self) -> bool {
self.check(JournalPermission::MEMBER)
self.check(CommunityPermission::MEMBER)
}
/// Check if the given [`JournalPermission`] qualifies as "Moderator" status.
pub fn check_moderator(self) -> bool {
self.check(JournalPermission::MANAGE_POSTS)
self.check(CommunityPermission::MANAGE_POSTS)
}
}
impl Default for JournalPermission {
impl Default for CommunityPermission {
fn default() -> Self {
Self::DEFAULT
}

View file

@ -1,155 +0,0 @@
use serde::{Deserialize, Serialize};
use tetratto_shared::{snow::AlmostSnowflake, unix_epoch_timestamp};
use super::journal_permissions::JournalPermission;
#[derive(Serialize, Deserialize)]
pub struct Journal {
pub id: usize,
pub created: usize,
pub title: String,
pub prompt: String,
/// The ID of the owner of the journal page.
pub owner: usize,
/// Who can read the journal page.
pub read_access: JournalReadAccess,
/// Who can write to the journal page (create journal entries belonging to it).
///
/// The owner of the journal page (and moderators) are the ***only*** people
/// capable of removing entries.
pub write_access: JournalWriteAccess,
pub likes: isize,
pub dislikes: isize,
}
impl Journal {
/// Create a new [`Journal`].
pub fn new(title: String, prompt: String, owner: usize) -> Self {
Self {
id: AlmostSnowflake::new(1234567890)
.to_string()
.parse::<usize>()
.unwrap(),
created: unix_epoch_timestamp() as usize,
title,
prompt,
owner,
read_access: JournalReadAccess::default(),
write_access: JournalWriteAccess::default(),
likes: 0,
dislikes: 0,
}
}
}
/// Who can read a [`Journal`].
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub enum JournalReadAccess {
/// Everybody can view the journal page from the owner's profile.
Everybody,
/// Only people with the link to the journal page.
Unlisted,
/// Only the owner of the journal page.
Private,
}
impl Default for JournalReadAccess {
fn default() -> Self {
Self::Everybody
}
}
/// Who can write to a [`Journal`].
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub enum JournalWriteAccess {
/// Everybody (authenticated users only still).
Everybody,
/// Only people who joined the journal page can write to it.
///
/// Memberships can be managed by the owner of the journal page.
Joined,
/// Only the owner of the journal page.
Owner,
}
impl Default for JournalWriteAccess {
fn default() -> Self {
Self::Joined
}
}
#[derive(Serialize, Deserialize)]
pub struct JournalMembership {
pub id: usize,
pub created: usize,
pub owner: usize,
pub journal: usize,
pub role: JournalPermission,
}
impl JournalMembership {
pub fn new(owner: usize, journal: usize, role: JournalPermission) -> Self {
Self {
id: AlmostSnowflake::new(1234567890)
.to_string()
.parse::<usize>()
.unwrap(),
created: unix_epoch_timestamp() as usize,
owner,
journal,
role,
}
}
}
#[derive(Serialize, Deserialize)]
pub struct JournalPostContext {
pub comments_enabled: bool,
}
impl Default for JournalPostContext {
fn default() -> Self {
Self {
comments_enabled: true,
}
}
}
#[derive(Serialize, Deserialize)]
pub struct JournalPost {
pub id: usize,
pub created: usize,
pub content: String,
/// The ID of the owner of this entry.
pub owner: usize,
/// The ID of the [`Journal`] this entry belongs to.
pub journal: usize,
/// Extra information about the journal entry.
pub context: JournalPostContext,
/// 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 JournalPost {
/// Create a new [`JournalEntry`].
pub fn new(content: String, journal: 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,
journal,
context: JournalPostContext::default(),
replying_to,
likes: 0,
dislikes: 0,
comment_count: 0,
}
}
}

View file

@ -1,6 +1,6 @@
pub mod auth;
pub mod journal;
pub mod journal_permissions;
pub mod communities;
pub mod communities_permissions;
pub mod permissions;
pub mod reactions;