add: questions, requests

This commit is contained in:
trisua 2025-04-12 22:25:54 -04:00
parent 24f67221ca
commit 7960484bf9
52 changed files with 1698 additions and 100 deletions

View file

@ -31,6 +31,10 @@ pub struct User {
/// The TOTP recovery codes for this profile.
#[serde(default)]
pub recovery_codes: Vec<String>,
#[serde(default)]
pub post_count: usize,
#[serde(default)]
pub request_count: usize,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
@ -127,6 +131,8 @@ pub struct UserSettings {
pub disable_other_themes: bool,
#[serde(default)]
pub disable_other_theme_css: bool,
#[serde(default)]
pub enable_questions: bool,
}
impl Default for User {
@ -160,6 +166,8 @@ impl User {
last_seen: unix_epoch_timestamp() as usize,
totp: String::new(),
recovery_codes: Vec::new(),
post_count: 0,
request_count: 0,
}
}

View file

@ -1,6 +1,5 @@
use serde::{Deserialize, Serialize};
use tetratto_shared::{snow::AlmostSnowflake, unix_epoch_timestamp};
use super::communities_permissions::CommunityPermission;
#[derive(Clone, Serialize, Deserialize)]
@ -78,6 +77,8 @@ pub struct CommunityContext {
pub description: String,
#[serde(default)]
pub is_nsfw: bool,
#[serde(default)]
pub enable_questions: bool,
}
/// Who can read a [`Community`].
@ -172,6 +173,9 @@ pub struct PostContext {
pub repost: Option<RepostContext>,
#[serde(default = "default_reposts_enabled")]
pub reposts_enabled: bool,
/// The ID of the question this post is answering.
#[serde(default)]
pub answering: usize,
}
fn default_comments_enabled() -> bool {
@ -192,6 +196,7 @@ impl Default for PostContext {
edited: 0,
is_nsfw: false,
repost: None,
answering: 0,
}
}
}
@ -271,3 +276,42 @@ impl Post {
});
}
}
#[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
/// to the question. Normally, ownly the `receiver` can do so.
///
/// 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,
}
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,
}
}
}

View file

@ -4,6 +4,7 @@ pub mod communities_permissions;
pub mod moderation;
pub mod permissions;
pub mod reactions;
pub mod requests;
use serde::{Deserialize, Serialize};
@ -32,6 +33,7 @@ pub enum Error {
DataTooShort(String),
UsernameInUse,
TitleInUse,
QuestionsDisabled,
Unknown,
}
@ -51,6 +53,7 @@ impl ToString for Error {
Self::DataTooShort(name) => format!("Given {name} is too short!"),
Self::UsernameInUse => "Username in use".to_string(),
Self::TitleInUse => "Title in use".to_string(),
Self::QuestionsDisabled => "You are not allowed to ask questions there".to_string(),
_ => format!("An unknown error as occurred: ({:?})", self),
}
}

View file

@ -28,6 +28,8 @@ bitflags! {
const BANNED = 1 << 17;
const INFINITE_COMMUNITIES = 1 << 18;
const SUPPORTER = 1 << 19;
const MANAGE_REQUESTS = 1 << 20;
const MANAGE_QUESTIONS = 1 << 21;
const _ = !0;
}

View file

@ -0,0 +1,52 @@
use serde::{Serialize, Deserialize};
use tetratto_shared::{snow::AlmostSnowflake, unix_epoch_timestamp};
#[derive(Serialize, Deserialize, PartialEq, Eq)]
pub enum ActionType {
/// A request to join a community.
///
/// `users` table.
CommunityJoin,
/// A request to answer a question with a post.
///
/// `questions` table.
Answer,
}
#[derive(Serialize, Deserialize)]
pub struct ActionRequest {
pub id: usize,
pub created: usize,
pub owner: usize,
pub action_type: ActionType,
/// The ID of the asset this request links to. Should exist in the correct
/// table for the given [`ActionType`].
pub linked_asset: usize,
}
impl ActionRequest {
/// Create a new [`ActionRequest`].
pub fn new(owner: usize, action_type: ActionType, linked_asset: usize) -> Self {
Self {
id: AlmostSnowflake::new(1234567890)
.to_string()
.parse::<usize>()
.unwrap(),
created: unix_epoch_timestamp() as usize,
owner,
action_type,
linked_asset,
}
}
/// Create a new [`ActionRequest`] with the given `id`.
pub fn with_id(id: usize, owner: usize, action_type: ActionType, linked_asset: usize) -> Self {
Self {
id,
created: unix_epoch_timestamp() as usize,
owner,
action_type,
linked_asset,
}
}
}