add: cloudflare turnstile captcha

add: "popular communities" card in communities list
This commit is contained in:
trisua 2025-04-02 23:26:43 -04:00
parent 53cf75b53c
commit 131a38abb9
15 changed files with 288 additions and 11 deletions

View file

@ -109,6 +109,47 @@ impl Default for DatabaseConfig {
}
}
/// Policies config (TOS/privacy)
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct PoliciesConfig {
/// The link to your terms of service page.
/// This is relative to `/auth/register` on the site.
///
/// If your TOS is an HTML file located in `./public`, you can put
/// `/public/tos.html` here (or something).
pub terms_of_service: String,
/// The link to your privacy policy page.
/// This is relative to `/auth/register` on the site.
///
/// Same deal as terms of service page.
pub privacy: String,
}
impl Default for PoliciesConfig {
fn default() -> Self {
Self {
terms_of_service: "/public/tos.html".to_string(),
privacy: "/public/privacy.html".to_string(),
}
}
}
/// Cloudflare Turnstile configuration
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct TurnstileConfig {
pub site_key: String,
pub secret_key: String,
}
impl Default for TurnstileConfig {
fn default() -> Self {
Self {
site_key: "1x00000000000000000000AA".to_string(), // always passing, visible
secret_key: "1x0000000000000000000000000000000AA".to_string(), // always passing
}
}
}
/// Configuration file
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct Config {
@ -148,6 +189,12 @@ pub struct Config {
/// A list of usernames which cannot be used. This also includes community names.
#[serde(default = "default_banned_usernames")]
pub banned_usernames: Vec<String>,
/// Configuration for your site's policies (terms of service, privacy).
#[serde(default = "default_policies")]
pub policies: PoliciesConfig,
/// Configuration for Cloudflare Turnstile.
#[serde(default = "default_turnstile")]
pub turnstile: TurnstileConfig,
}
fn default_name() -> String {
@ -199,6 +246,14 @@ fn default_banned_usernames() -> Vec<String> {
]
}
fn default_policies() -> PoliciesConfig {
PoliciesConfig::default()
}
fn default_turnstile() -> TurnstileConfig {
TurnstileConfig::default()
}
impl Default for Config {
fn default() -> Self {
Self {
@ -212,6 +267,8 @@ impl Default for Config {
dirs: default_dirs(),
no_track: default_no_track(),
banned_usernames: default_banned_usernames(),
policies: default_policies(),
turnstile: default_turnstile(),
}
}
}

View file

@ -9,7 +9,7 @@ use crate::model::{
communities::{CommunityReadAccess, CommunityWriteAccess},
permissions::FinePermission,
};
use crate::{auto_method, execute, get, query_row};
use crate::{auto_method, execute, get, query_row, query_rows};
use pathbufd::PathBufD;
use std::fs::{exists, remove_file};
@ -119,6 +119,32 @@ impl DataManager {
auto_method!(get_community_by_id_no_void()@get_community_from_row -> "SELECT * FROM communities WHERE id = $1" --name="community" --returns=Community --cache-key-tmpl="atto.community:{}");
auto_method!(get_community_by_title_no_void(&str)@get_community_from_row -> "SELECT * FROM communities WHERE title = $1" --name="community" --returns=Community --cache-key-tmpl="atto.community:{}");
/// Get the top 12 most popular (most likes) communities.
pub async fn get_popular_communities(&self) -> Result<Vec<Community>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
#[cfg(feature = "sqlite")]
let empty = [];
#[cfg(feature = "postgres")]
let empty = &[];
let res = query_rows!(
&conn,
"SELECT * FROM communities ORDER BY likes DESC LIMIT 12",
empty,
|x| { Self::get_community_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("communities".to_string()));
}
Ok(res.unwrap())
}
/// Create a new community in the database.
///
/// # Arguments

View file

@ -41,6 +41,8 @@ impl Default for ThemePreference {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct UserSettings {
#[serde(default)]
pub policy_consent: bool,
#[serde(default)]
pub display_name: String,
#[serde(default)]
@ -58,6 +60,7 @@ pub struct UserSettings {
impl Default for UserSettings {
fn default() -> Self {
Self {
policy_consent: false,
display_name: String::new(),
biography: String::new(),
private_profile: false,