Merge branch 'main' into ban_reason_required
This commit is contained in:
commit
ac173b9a0d
42 changed files with 1654 additions and 227 deletions
|
|
@ -60,6 +60,9 @@ pipeline:
|
|||
-D clippy::unused_self
|
||||
-A clippy::uninlined_format_args
|
||||
-D clippy::get_first
|
||||
-D clippy::explicit_into_iter_loop
|
||||
-D clippy::explicit_iter_loop
|
||||
-D clippy::needless_collect
|
||||
- cargo clippy --workspace --features console --
|
||||
-D clippy::unwrap_used
|
||||
-D clippy::indexing_slicing
|
||||
|
|
|
|||
32
Cargo.lock
generated
32
Cargo.lock
generated
|
|
@ -14,9 +14,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "activitypub_federation"
|
||||
version = "0.4.4"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "27540f6c4b72c91176610ed5279061a021387f972c7c6f42c41032b78a808267"
|
||||
checksum = "4ab3ac148d9c0b4163a6d41040c17de7558a42224b9ecbd4e8f033aef6c254d9"
|
||||
dependencies = [
|
||||
"activitystreams-kinds",
|
||||
"actix-web",
|
||||
|
|
@ -1370,6 +1370,7 @@ dependencies = [
|
|||
"itoa",
|
||||
"pq-sys",
|
||||
"serde_json",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -2569,7 +2570,7 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
|||
|
||||
[[package]]
|
||||
name = "lemmy_api"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
|
|
@ -2577,6 +2578,7 @@ dependencies = [
|
|||
"base64 0.13.1",
|
||||
"bcrypt",
|
||||
"captcha",
|
||||
"chrono",
|
||||
"lemmy_api_common",
|
||||
"lemmy_db_schema",
|
||||
"lemmy_db_views",
|
||||
|
|
@ -2592,7 +2594,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lemmy_api_common"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
|
|
@ -2621,12 +2623,13 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lemmy_api_crud"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"activitypub_federation",
|
||||
"actix-web",
|
||||
"async-trait",
|
||||
"bcrypt",
|
||||
"chrono",
|
||||
"lemmy_api_common",
|
||||
"lemmy_db_schema",
|
||||
"lemmy_db_views",
|
||||
|
|
@ -2635,12 +2638,13 @@ dependencies = [
|
|||
"serde",
|
||||
"tracing",
|
||||
"url",
|
||||
"uuid",
|
||||
"webmention",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lemmy_apub"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"activitypub_federation",
|
||||
"actix-web",
|
||||
|
|
@ -2678,7 +2682,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lemmy_db_schema"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"activitypub_federation",
|
||||
"async-trait",
|
||||
|
|
@ -2710,11 +2714,12 @@ dependencies = [
|
|||
"ts-rs",
|
||||
"typed-builder",
|
||||
"url",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lemmy_db_views"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"diesel",
|
||||
"diesel-async",
|
||||
|
|
@ -2731,7 +2736,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lemmy_db_views_actor"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"diesel",
|
||||
"diesel-async",
|
||||
|
|
@ -2744,7 +2749,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lemmy_db_views_moderator"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"diesel",
|
||||
"diesel-async",
|
||||
|
|
@ -2756,7 +2761,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lemmy_routes"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"activitypub_federation",
|
||||
"actix-web",
|
||||
|
|
@ -2781,11 +2786,12 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lemmy_server"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"activitypub_federation",
|
||||
"actix-cors",
|
||||
"actix-web",
|
||||
"chrono",
|
||||
"clokwerk",
|
||||
"console-subscriber",
|
||||
"diesel",
|
||||
|
|
@ -2822,7 +2828,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "lemmy_utils"
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"anyhow",
|
||||
|
|
|
|||
24
Cargo.toml
24
Cargo.toml
|
|
@ -1,5 +1,5 @@
|
|||
[workspace.package]
|
||||
version = "0.18.0"
|
||||
version = "0.18.1-rc.1"
|
||||
edition = "2021"
|
||||
description = "A link aggregator for the fediverse"
|
||||
license = "AGPL-3.0"
|
||||
|
|
@ -49,16 +49,16 @@ members = [
|
|||
]
|
||||
|
||||
[workspace.dependencies]
|
||||
lemmy_api = { version = "=0.18.0", path = "./crates/api" }
|
||||
lemmy_api_crud = { version = "=0.18.0", path = "./crates/api_crud" }
|
||||
lemmy_apub = { version = "=0.18.0", path = "./crates/apub" }
|
||||
lemmy_utils = { version = "=0.18.0", path = "./crates/utils" }
|
||||
lemmy_db_schema = { version = "=0.18.0", path = "./crates/db_schema" }
|
||||
lemmy_api_common = { version = "=0.18.0", path = "./crates/api_common" }
|
||||
lemmy_routes = { version = "=0.18.0", path = "./crates/routes" }
|
||||
lemmy_db_views = { version = "=0.18.0", path = "./crates/db_views" }
|
||||
lemmy_db_views_actor = { version = "=0.18.0", path = "./crates/db_views_actor" }
|
||||
lemmy_db_views_moderator = { version = "=0.18.0", path = "./crates/db_views_moderator" }
|
||||
lemmy_api = { version = "=0.18.1-rc.1", path = "./crates/api" }
|
||||
lemmy_api_crud = { version = "=0.18.1-rc.1", path = "./crates/api_crud" }
|
||||
lemmy_apub = { version = "=0.18.1-rc.1", path = "./crates/apub" }
|
||||
lemmy_utils = { version = "=0.18.1-rc.1", path = "./crates/utils" }
|
||||
lemmy_db_schema = { version = "=0.18.1-rc.1", path = "./crates/db_schema" }
|
||||
lemmy_api_common = { version = "=0.18.1-rc.1", path = "./crates/api_common" }
|
||||
lemmy_routes = { version = "=0.18.1-rc.1", path = "./crates/routes" }
|
||||
lemmy_db_views = { version = "=0.18.1-rc.1", path = "./crates/db_views" }
|
||||
lemmy_db_views_actor = { version = "=0.18.1-rc.1", path = "./crates/db_views_actor" }
|
||||
lemmy_db_views_moderator = { version = "=0.18.1-rc.1", path = "./crates/db_views_moderator" }
|
||||
activitypub_federation = { version = "0.4.4", default-features = false, features = ["actix-web"] }
|
||||
diesel = "2.1.0"
|
||||
diesel_migrations = "2.1.0"
|
||||
|
|
@ -147,4 +147,4 @@ rustls = { workspace = true }
|
|||
futures-util = { workspace = true }
|
||||
tokio-postgres = { workspace = true }
|
||||
tokio-postgres-rustls = { workspace = true }
|
||||
|
||||
chrono = { workspace = true }
|
||||
|
|
@ -29,6 +29,7 @@ async-trait = { workspace = true }
|
|||
captcha = { workspace = true }
|
||||
anyhow = { workspace = true }
|
||||
tracing = { workspace = true }
|
||||
chrono = { workspace = true }
|
||||
|
||||
[dev-dependencies]
|
||||
serial_test = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
use actix_web::web::Data;
|
||||
use captcha::Captcha;
|
||||
use lemmy_api_common::{context::LemmyContext, utils::local_site_to_slur_regex};
|
||||
use lemmy_db_schema::source::local_site::LocalSite;
|
||||
use lemmy_utils::{error::LemmyError, utils::slurs::check_slurs};
|
||||
|
|
@ -20,6 +21,21 @@ pub trait Perform {
|
|||
async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError>;
|
||||
}
|
||||
|
||||
/// Converts the captcha to a base64 encoded wav audio file
|
||||
pub(crate) fn captcha_as_wav_base64(captcha: &Captcha) -> String {
|
||||
let letters = captcha.as_wav();
|
||||
|
||||
let mut concat_letters: Vec<u8> = Vec::new();
|
||||
|
||||
for letter in letters {
|
||||
let bytes = letter.unwrap_or_default();
|
||||
concat_letters.extend(bytes);
|
||||
}
|
||||
|
||||
// Convert to base64
|
||||
base64::encode(concat_letters)
|
||||
}
|
||||
|
||||
/// Check size of report and remove whitespace
|
||||
pub(crate) fn check_report_reason(reason: &str, local_site: &LocalSite) -> Result<(), LemmyError> {
|
||||
let slur_regex = &local_site_to_slur_regex(local_site);
|
||||
|
|
|
|||
50
crates/api/src/local_user/get_captcha.rs
Normal file
50
crates/api/src/local_user/get_captcha.rs
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
use crate::{captcha_as_wav_base64, Perform};
|
||||
use actix_web::web::Data;
|
||||
use captcha::{gen, Difficulty};
|
||||
use lemmy_api_common::{
|
||||
context::LemmyContext,
|
||||
person::{CaptchaResponse, GetCaptcha, GetCaptchaResponse},
|
||||
};
|
||||
use lemmy_db_schema::source::{
|
||||
captcha_answer::{CaptchaAnswer, CaptchaAnswerForm},
|
||||
local_site::LocalSite,
|
||||
};
|
||||
use lemmy_utils::error::LemmyError;
|
||||
|
||||
#[async_trait::async_trait(?Send)]
|
||||
impl Perform for GetCaptcha {
|
||||
type Response = GetCaptchaResponse;
|
||||
|
||||
#[tracing::instrument(skip(context))]
|
||||
async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError> {
|
||||
let local_site = LocalSite::read(context.pool()).await?;
|
||||
|
||||
if !local_site.captcha_enabled {
|
||||
return Ok(GetCaptchaResponse { ok: None });
|
||||
}
|
||||
|
||||
let captcha = gen(match local_site.captcha_difficulty.as_str() {
|
||||
"easy" => Difficulty::Easy,
|
||||
"hard" => Difficulty::Hard,
|
||||
_ => Difficulty::Medium,
|
||||
});
|
||||
|
||||
let answer = captcha.chars_as_string();
|
||||
|
||||
let png = captcha.as_base64().expect("failed to generate captcha");
|
||||
|
||||
let wav = captcha_as_wav_base64(&captcha);
|
||||
|
||||
let captcha_form: CaptchaAnswerForm = CaptchaAnswerForm { answer };
|
||||
// Stores the captcha item in the db
|
||||
let captcha = CaptchaAnswer::insert(context.pool(), &captcha_form).await?;
|
||||
|
||||
Ok(GetCaptchaResponse {
|
||||
ok: Some(CaptchaResponse {
|
||||
png,
|
||||
wav,
|
||||
uuid: captcha.uuid.to_string(),
|
||||
}),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ mod ban_person;
|
|||
mod block;
|
||||
mod change_password;
|
||||
mod change_password_after_reset;
|
||||
mod get_captcha;
|
||||
mod list_banned;
|
||||
mod login;
|
||||
mod notifications;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ use lemmy_api_common::{
|
|||
person::{PasswordReset, PasswordResetResponse},
|
||||
utils::send_password_reset_email,
|
||||
};
|
||||
use lemmy_db_schema::source::password_reset_request::PasswordResetRequest;
|
||||
use lemmy_db_views::structs::LocalUserView;
|
||||
use lemmy_utils::error::LemmyError;
|
||||
|
||||
|
|
@ -25,6 +26,16 @@ impl Perform for PasswordReset {
|
|||
.await
|
||||
.map_err(|e| LemmyError::from_error_message(e, "couldnt_find_that_username_or_email"))?;
|
||||
|
||||
// Check for too many attempts (to limit potential abuse)
|
||||
let recent_resets_count = PasswordResetRequest::get_recent_password_resets_count(
|
||||
context.pool(),
|
||||
local_user_view.local_user.id,
|
||||
)
|
||||
.await?;
|
||||
if recent_resets_count >= 3 {
|
||||
return Err(LemmyError::from_message("password_reset_limit_reached"));
|
||||
}
|
||||
|
||||
// Email the pure token to the user.
|
||||
send_password_reset_email(&local_user_view, context.pool(), context.settings()).await?;
|
||||
Ok(PasswordResetResponse {})
|
||||
|
|
|
|||
|
|
@ -24,3 +24,10 @@ As you can see, each API endpoint needs a parameter type ( GetPosts), path (/pos
|
|||
For a real example of a Lemmy API client, look at [lemmyBB](https://github.com/LemmyNet/lemmyBB/tree/main/src/api).
|
||||
|
||||
Lemmy also provides a websocket API. You can find the full websocket code in [this file](https://github.com/LemmyNet/lemmy/blob/main/src/api_routes_websocket.rs).
|
||||
|
||||
## Generate TypeScript bindings
|
||||
|
||||
TypeScript bindings (API types) can be generated by running `cargo test --features full`.
|
||||
The ts files be generated into a `bindings` folder.
|
||||
|
||||
This crate uses [`ts_rs`](https://docs.rs/ts-rs/6.2.1/ts_rs/#traits) macros `derive(TS)` and `ts(export)` to attribute types for binding generating.
|
||||
|
|
|
|||
|
|
@ -98,7 +98,6 @@ pub async fn send_local_notifs(
|
|||
for mention in mentions
|
||||
.iter()
|
||||
.filter(|m| m.is_local(&context.settings().hostname) && m.name.ne(&person.name))
|
||||
.collect::<Vec<&MentionData>>()
|
||||
{
|
||||
let mention_name = mention.name.clone();
|
||||
let user_view = LocalUserView::read_from_name(context.pool(), &mention_name).await;
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ pub struct CommunityResponse {
|
|||
pub struct ListCommunities {
|
||||
pub type_: Option<ListingType>,
|
||||
pub sort: Option<SortType>,
|
||||
pub show_nsfw: Option<bool>,
|
||||
pub page: Option<i64>,
|
||||
pub limit: Option<i64>,
|
||||
pub auth: Option<Sensitive<String>>,
|
||||
|
|
|
|||
|
|
@ -299,15 +299,6 @@ pub fn password_length_check(pass: &str) -> Result<(), LemmyError> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Checks the site description length
|
||||
pub fn site_description_length_check(description: &str) -> Result<(), LemmyError> {
|
||||
if description.len() > 150 {
|
||||
Err(LemmyError::from_message("site_description_length_overflow"))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks for a honeypot. If this field is filled, fail the rest of the function
|
||||
pub fn honeypot_check(honeypot: &Option<String>) -> Result<(), LemmyError> {
|
||||
if honeypot.is_some() && honeypot != &Some(String::new()) {
|
||||
|
|
|
|||
|
|
@ -22,3 +22,5 @@ tracing = { workspace = true }
|
|||
url = { workspace = true }
|
||||
async-trait = { workspace = true }
|
||||
webmention = "0.4.0"
|
||||
chrono = { workspace = true }
|
||||
uuid = { workspace = true }
|
||||
|
|
@ -191,7 +191,7 @@ impl PerformCrud for CreateComment {
|
|||
|
||||
pub fn check_comment_depth(comment: &Comment) -> Result<(), LemmyError> {
|
||||
let path = &comment.path.0;
|
||||
let length = path.split('.').collect::<Vec<&str>>().len();
|
||||
let length = path.split('.').count();
|
||||
if length > MAX_COMMENT_DEPTH_LIMIT {
|
||||
Err(LemmyError::from_message("max_comment_depth_reached"))
|
||||
} else {
|
||||
|
|
|
|||
|
|
@ -27,12 +27,14 @@ impl PerformCrud for ListCommunities {
|
|||
|
||||
let sort = data.sort;
|
||||
let listing_type = data.type_;
|
||||
let show_nsfw = data.show_nsfw;
|
||||
let page = data.page;
|
||||
let limit = data.limit;
|
||||
let local_user = local_user_view.map(|l| l.local_user);
|
||||
let communities = CommunityQuery::builder()
|
||||
.pool(context.pool())
|
||||
.listing_type(listing_type)
|
||||
.show_nsfw(show_nsfw)
|
||||
.sort(sort)
|
||||
.local_user(local_user.as_ref())
|
||||
.page(page)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,7 @@
|
|||
use crate::{site::check_application_question, PerformCrud};
|
||||
use crate::{
|
||||
site::{application_question_check, site_default_post_listing_type_check},
|
||||
PerformCrud,
|
||||
};
|
||||
use activitypub_federation::http_signatures::generate_actor_keypair;
|
||||
use actix_web::web::Data;
|
||||
use lemmy_api_common::{
|
||||
|
|
@ -8,9 +11,7 @@ use lemmy_api_common::{
|
|||
generate_site_inbox_url,
|
||||
is_admin,
|
||||
local_site_rate_limit_to_rate_limit_config,
|
||||
local_site_to_slur_regex,
|
||||
local_user_view_from_jwt,
|
||||
site_description_length_check,
|
||||
},
|
||||
};
|
||||
use lemmy_db_schema::{
|
||||
|
|
@ -26,10 +27,16 @@ use lemmy_db_schema::{
|
|||
};
|
||||
use lemmy_db_views::structs::SiteView;
|
||||
use lemmy_utils::{
|
||||
error::LemmyError,
|
||||
error::{LemmyError, LemmyResult},
|
||||
utils::{
|
||||
slurs::{check_slurs, check_slurs_opt},
|
||||
validation::{check_site_visibility_valid, is_valid_body_field},
|
||||
validation::{
|
||||
build_and_check_regex,
|
||||
check_site_visibility_valid,
|
||||
is_valid_body_field,
|
||||
site_description_length_check,
|
||||
site_name_length_check,
|
||||
},
|
||||
},
|
||||
};
|
||||
use url::Url;
|
||||
|
|
@ -41,57 +48,23 @@ impl PerformCrud for CreateSite {
|
|||
#[tracing::instrument(skip(context))]
|
||||
async fn perform(&self, context: &Data<LemmyContext>) -> Result<SiteResponse, LemmyError> {
|
||||
let data: &CreateSite = self;
|
||||
|
||||
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
||||
let local_site = LocalSite::read(context.pool()).await?;
|
||||
|
||||
if local_site.site_setup {
|
||||
return Err(LemmyError::from_message("site_already_exists"));
|
||||
};
|
||||
|
||||
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
||||
|
||||
// Make sure user is an admin
|
||||
// Make sure user is an admin; other types of users should not create site data...
|
||||
is_admin(&local_user_view)?;
|
||||
|
||||
check_site_visibility_valid(
|
||||
local_site.private_instance,
|
||||
local_site.federation_enabled,
|
||||
&data.private_instance,
|
||||
&data.federation_enabled,
|
||||
)?;
|
||||
|
||||
let sidebar = diesel_option_overwrite(&data.sidebar);
|
||||
let description = diesel_option_overwrite(&data.description);
|
||||
let icon = diesel_option_overwrite_to_url(&data.icon)?;
|
||||
let banner = diesel_option_overwrite_to_url(&data.banner)?;
|
||||
|
||||
let slur_regex = local_site_to_slur_regex(&local_site);
|
||||
check_slurs(&data.name, &slur_regex)?;
|
||||
check_slurs_opt(&data.description, &slur_regex)?;
|
||||
|
||||
if let Some(Some(desc)) = &description {
|
||||
site_description_length_check(desc)?;
|
||||
}
|
||||
|
||||
is_valid_body_field(&data.sidebar, false)?;
|
||||
|
||||
let application_question = diesel_option_overwrite(&data.application_question);
|
||||
check_application_question(
|
||||
&application_question,
|
||||
data
|
||||
.registration_mode
|
||||
.unwrap_or(local_site.registration_mode),
|
||||
)?;
|
||||
validate_create_payload(&local_site, data)?;
|
||||
|
||||
let actor_id: DbUrl = Url::parse(&context.settings().get_protocol_and_hostname())?.into();
|
||||
let inbox_url = Some(generate_site_inbox_url(&actor_id)?);
|
||||
let keypair = generate_actor_keypair()?;
|
||||
let site_form = SiteUpdateForm::builder()
|
||||
.name(Some(data.name.clone()))
|
||||
.sidebar(sidebar)
|
||||
.description(description)
|
||||
.icon(icon)
|
||||
.banner(banner)
|
||||
.sidebar(diesel_option_overwrite(&data.sidebar))
|
||||
.description(diesel_option_overwrite(&data.description))
|
||||
.icon(diesel_option_overwrite_to_url(&data.icon)?)
|
||||
.banner(diesel_option_overwrite_to_url(&data.banner)?)
|
||||
.actor_id(Some(actor_id))
|
||||
.last_refreshed_at(Some(naive_now()))
|
||||
.inbox_url(inbox_url)
|
||||
|
|
@ -111,7 +84,7 @@ impl PerformCrud for CreateSite {
|
|||
.enable_nsfw(data.enable_nsfw)
|
||||
.community_creation_admin_only(data.community_creation_admin_only)
|
||||
.require_email_verification(data.require_email_verification)
|
||||
.application_question(application_question)
|
||||
.application_question(diesel_option_overwrite(&data.application_question))
|
||||
.private_instance(data.private_instance)
|
||||
.default_theme(data.default_theme.clone())
|
||||
.default_post_listing_type(data.default_post_listing_type)
|
||||
|
|
@ -163,3 +136,449 @@ impl PerformCrud for CreateSite {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_create_payload(local_site: &LocalSite, create_site: &CreateSite) -> LemmyResult<()> {
|
||||
// Make sure the site hasn't already been set up...
|
||||
if local_site.site_setup {
|
||||
return Err(LemmyError::from_message("site_already_exists"));
|
||||
};
|
||||
|
||||
// Check that the slur regex compiles, and returns the regex if valid...
|
||||
// Prioritize using new slur regex from the request; if not provided, use the existing regex.
|
||||
let slur_regex = build_and_check_regex(
|
||||
&create_site
|
||||
.slur_filter_regex
|
||||
.as_deref()
|
||||
.or(local_site.slur_filter_regex.as_deref()),
|
||||
)?;
|
||||
|
||||
site_name_length_check(&create_site.name)?;
|
||||
check_slurs(&create_site.name, &slur_regex)?;
|
||||
|
||||
if let Some(desc) = &create_site.description {
|
||||
site_description_length_check(desc)?;
|
||||
check_slurs_opt(&create_site.description, &slur_regex)?;
|
||||
}
|
||||
|
||||
site_default_post_listing_type_check(&create_site.default_post_listing_type)?;
|
||||
|
||||
check_site_visibility_valid(
|
||||
local_site.private_instance,
|
||||
local_site.federation_enabled,
|
||||
&create_site.private_instance,
|
||||
&create_site.federation_enabled,
|
||||
)?;
|
||||
|
||||
// Ensure that the sidebar has fewer than the max num characters...
|
||||
is_valid_body_field(&create_site.sidebar, false)?;
|
||||
|
||||
application_question_check(
|
||||
&local_site.application_question,
|
||||
&create_site.application_question,
|
||||
create_site
|
||||
.registration_mode
|
||||
.unwrap_or(local_site.registration_mode),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::site::create::validate_create_payload;
|
||||
use lemmy_api_common::site::CreateSite;
|
||||
use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode};
|
||||
|
||||
#[test]
|
||||
fn test_validate_invalid_create_payload() {
|
||||
let invalid_payloads = [
|
||||
(
|
||||
"CreateSite attempted on set up LocalSite",
|
||||
"site_already_exists",
|
||||
&generate_local_site(
|
||||
true,
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"CreateSite name matches LocalSite slur filter",
|
||||
"slurs",
|
||||
&generate_local_site(
|
||||
false,
|
||||
Some(String::from("(foo|bar)")),
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("foo site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"CreateSite name matches new slur filter",
|
||||
"slurs",
|
||||
&generate_local_site(
|
||||
false,
|
||||
Some(String::from("(foo|bar)")),
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("zeta site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
Some(String::from("(zeta|alpha)")),
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"CreateSite listing type is Subscribed, which is invalid",
|
||||
"invalid_default_post_listing_type",
|
||||
&generate_local_site(
|
||||
false,
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
Some(ListingType::Subscribed),
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"CreateSite is both private and federated",
|
||||
"cant_enable_private_instance_and_federation_together",
|
||||
&generate_local_site(
|
||||
false,
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
Some(true),
|
||||
Some(true),
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"LocalSite is private, but CreateSite also makes it federated",
|
||||
"cant_enable_private_instance_and_federation_together",
|
||||
&generate_local_site(
|
||||
false,
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
Some(true),
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"CreateSite requires application, but neither it nor LocalSite has an application question",
|
||||
"application_question_required",
|
||||
&generate_local_site(
|
||||
false,
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
Some(RegistrationMode::RequireApplication),
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
invalid_payloads.iter().enumerate().for_each(
|
||||
|(
|
||||
idx,
|
||||
&(reason, expected_err, local_site, create_site),
|
||||
)| {
|
||||
match validate_create_payload(
|
||||
local_site,
|
||||
create_site,
|
||||
) {
|
||||
Ok(_) => {
|
||||
panic!(
|
||||
"Got Ok, but validation should have failed with error: {} for reason: {}. invalid_payloads.nth({})",
|
||||
expected_err, reason, idx
|
||||
)
|
||||
}
|
||||
Err(error) => {
|
||||
assert!(
|
||||
error.message.eq(&Some(String::from(expected_err))),
|
||||
"Got Err {:?}, but should have failed with message: {} for reason: {}. invalid_payloads.nth({})",
|
||||
error.message,
|
||||
expected_err,
|
||||
reason,
|
||||
idx
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_valid_create_payload() {
|
||||
let valid_payloads = [
|
||||
(
|
||||
"No changes between LocalSite and CreateSite",
|
||||
&generate_local_site(
|
||||
false,
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"CreateSite allows clearing and changing values",
|
||||
&generate_local_site(
|
||||
false,
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("site_name"),
|
||||
Some(String::new()),
|
||||
Some(String::new()),
|
||||
Some(ListingType::All),
|
||||
Some(String::new()),
|
||||
Some(false),
|
||||
Some(true),
|
||||
Some(String::new()),
|
||||
Some(RegistrationMode::Open),
|
||||
),
|
||||
),
|
||||
(
|
||||
"CreateSite clears existing slur filter regex",
|
||||
&generate_local_site(
|
||||
false,
|
||||
Some(String::from("(foo|bar)")),
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("foo site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
Some(String::new()),
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"LocalSite has application question and CreateSite now requires applications,",
|
||||
&generate_local_site(
|
||||
false,
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
Some(String::from("question")),
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_create_site(
|
||||
String::from("site_name"),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
Some(RegistrationMode::RequireApplication),
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
valid_payloads
|
||||
.iter()
|
||||
.enumerate()
|
||||
.for_each(|(idx, &(reason, local_site, edit_site))| {
|
||||
assert!(
|
||||
validate_create_payload(local_site, edit_site).is_ok(),
|
||||
"Got Err, but should have got Ok for reason: {}. valid_payloads.nth({})",
|
||||
reason,
|
||||
idx
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
fn generate_local_site(
|
||||
site_setup: bool,
|
||||
site_slur_filter_regex: Option<String>,
|
||||
site_is_private: bool,
|
||||
site_is_federated: bool,
|
||||
site_application_question: Option<String>,
|
||||
site_registration_mode: RegistrationMode,
|
||||
) -> LocalSite {
|
||||
LocalSite {
|
||||
id: Default::default(),
|
||||
site_id: Default::default(),
|
||||
site_setup,
|
||||
enable_downvotes: false,
|
||||
enable_nsfw: false,
|
||||
community_creation_admin_only: false,
|
||||
require_email_verification: false,
|
||||
application_question: site_application_question,
|
||||
private_instance: site_is_private,
|
||||
default_theme: String::new(),
|
||||
default_post_listing_type: ListingType::All,
|
||||
legal_information: None,
|
||||
hide_modlog_mod_names: false,
|
||||
application_email_admins: false,
|
||||
slur_filter_regex: site_slur_filter_regex,
|
||||
actor_name_max_length: 0,
|
||||
federation_enabled: site_is_federated,
|
||||
captcha_enabled: false,
|
||||
captcha_difficulty: String::new(),
|
||||
published: Default::default(),
|
||||
updated: None,
|
||||
registration_mode: site_registration_mode,
|
||||
reports_email_admins: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Allow the test helper function to have too many arguments.
|
||||
// It's either this or generate the entire struct each time for testing.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn generate_create_site(
|
||||
site_name: String,
|
||||
site_description: Option<String>,
|
||||
site_sidebar: Option<String>,
|
||||
site_listing_type: Option<ListingType>,
|
||||
site_slur_filter_regex: Option<String>,
|
||||
site_is_private: Option<bool>,
|
||||
site_is_federated: Option<bool>,
|
||||
site_application_question: Option<String>,
|
||||
site_registration_mode: Option<RegistrationMode>,
|
||||
) -> CreateSite {
|
||||
CreateSite {
|
||||
name: site_name,
|
||||
sidebar: site_sidebar,
|
||||
description: site_description,
|
||||
icon: None,
|
||||
banner: None,
|
||||
enable_downvotes: None,
|
||||
enable_nsfw: None,
|
||||
community_creation_admin_only: None,
|
||||
require_email_verification: None,
|
||||
application_question: site_application_question,
|
||||
private_instance: site_is_private,
|
||||
default_theme: None,
|
||||
default_post_listing_type: site_listing_type,
|
||||
legal_information: None,
|
||||
application_email_admins: None,
|
||||
hide_modlog_mod_names: None,
|
||||
discussion_languages: None,
|
||||
slur_filter_regex: site_slur_filter_regex,
|
||||
actor_name_max_length: None,
|
||||
rate_limit_message: None,
|
||||
rate_limit_message_per_second: None,
|
||||
rate_limit_post: None,
|
||||
rate_limit_post_per_second: None,
|
||||
rate_limit_register: None,
|
||||
rate_limit_register_per_second: None,
|
||||
rate_limit_image: None,
|
||||
rate_limit_image_per_second: None,
|
||||
rate_limit_comment: None,
|
||||
rate_limit_comment_per_second: None,
|
||||
rate_limit_search: None,
|
||||
rate_limit_search_per_second: None,
|
||||
federation_enabled: site_is_federated,
|
||||
federation_debug: None,
|
||||
captcha_enabled: None,
|
||||
captcha_difficulty: None,
|
||||
allowed_instances: None,
|
||||
blocked_instances: None,
|
||||
taglines: None,
|
||||
registration_mode: site_registration_mode,
|
||||
auth: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,95 @@
|
|||
use lemmy_db_schema::RegistrationMode;
|
||||
use lemmy_utils::error::LemmyError;
|
||||
use lemmy_db_schema::{ListingType, RegistrationMode};
|
||||
use lemmy_utils::error::{LemmyError, LemmyResult};
|
||||
|
||||
mod create;
|
||||
mod read;
|
||||
mod update;
|
||||
|
||||
pub fn check_application_question(
|
||||
application_question: &Option<Option<String>>,
|
||||
/// Checks whether the default post listing type is valid for a site.
|
||||
pub fn site_default_post_listing_type_check(
|
||||
default_post_listing_type: &Option<ListingType>,
|
||||
) -> LemmyResult<()> {
|
||||
if let Some(listing_type) = default_post_listing_type {
|
||||
// Only allow all or local as default listing types...
|
||||
if listing_type != &ListingType::All && listing_type != &ListingType::Local {
|
||||
Err(LemmyError::from_message(
|
||||
"invalid_default_post_listing_type",
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks whether the application question and registration mode align.
|
||||
pub fn application_question_check(
|
||||
current_application_question: &Option<String>,
|
||||
new_application_question: &Option<String>,
|
||||
registration_mode: RegistrationMode,
|
||||
) -> Result<(), LemmyError> {
|
||||
) -> LemmyResult<()> {
|
||||
let has_no_question: bool =
|
||||
current_application_question.is_none() && new_application_question.is_none();
|
||||
let is_nullifying_question: bool = new_application_question == &Some(String::new());
|
||||
|
||||
if registration_mode == RegistrationMode::RequireApplication
|
||||
&& application_question.as_ref().unwrap_or(&None).is_none()
|
||||
&& (has_no_question || is_nullifying_question)
|
||||
{
|
||||
Err(LemmyError::from_message("application_question_required"))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::site::{application_question_check, site_default_post_listing_type_check};
|
||||
use lemmy_db_schema::{ListingType, RegistrationMode};
|
||||
|
||||
#[test]
|
||||
fn test_site_default_post_listing_type_check() {
|
||||
assert!(site_default_post_listing_type_check(&None::<ListingType>).is_ok());
|
||||
assert!(site_default_post_listing_type_check(&Some(ListingType::All)).is_ok());
|
||||
assert!(site_default_post_listing_type_check(&Some(ListingType::Local)).is_ok());
|
||||
assert!(site_default_post_listing_type_check(&Some(ListingType::Subscribed)).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_application_question_check() {
|
||||
assert!(
|
||||
application_question_check(&Some(String::from("q")), &Some(String::new()), RegistrationMode::RequireApplication).is_err(),
|
||||
"Expected application to be invalid because an application is required, current question: {:?}, new question: {:?}",
|
||||
"q",
|
||||
String::new(),
|
||||
);
|
||||
assert!(
|
||||
application_question_check(&None, &None, RegistrationMode::RequireApplication).is_err(),
|
||||
"Expected application to be invalid because an application is required, current question: {:?}, new question: {:?}",
|
||||
None::<String>,
|
||||
None::<String>
|
||||
);
|
||||
|
||||
assert!(
|
||||
application_question_check(&None, &None, RegistrationMode::Open).is_ok(),
|
||||
"Expected application to be valid because no application required, current question: {:?}, new question: {:?}, mode: {:?}",
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
RegistrationMode::Open
|
||||
);
|
||||
assert!(
|
||||
application_question_check(&None, &Some(String::from("q")), RegistrationMode::RequireApplication).is_ok(),
|
||||
"Expected application to be valid because new application provided, current question: {:?}, new question: {:?}, mode: {:?}",
|
||||
None::<String>,
|
||||
Some(String::from("q")),
|
||||
RegistrationMode::RequireApplication
|
||||
);
|
||||
assert!(
|
||||
application_question_check(&Some(String::from("q")), &None, RegistrationMode::RequireApplication).is_ok(),
|
||||
"Expected application to be valid because application existed, current question: {:?}, new question: {:?}, mode: {:?}",
|
||||
Some(String::from("q")),
|
||||
None::<String>,
|
||||
RegistrationMode::RequireApplication
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,15 +1,12 @@
|
|||
use crate::{site::check_application_question, PerformCrud};
|
||||
use crate::{
|
||||
site::{application_question_check, site_default_post_listing_type_check},
|
||||
PerformCrud,
|
||||
};
|
||||
use actix_web::web::Data;
|
||||
use lemmy_api_common::{
|
||||
context::LemmyContext,
|
||||
site::{EditSite, SiteResponse},
|
||||
utils::{
|
||||
is_admin,
|
||||
local_site_rate_limit_to_rate_limit_config,
|
||||
local_site_to_slur_regex,
|
||||
local_user_view_from_jwt,
|
||||
site_description_length_check,
|
||||
},
|
||||
utils::{is_admin, local_site_rate_limit_to_rate_limit_config, local_user_view_from_jwt},
|
||||
};
|
||||
use lemmy_db_schema::{
|
||||
source::{
|
||||
|
|
@ -24,15 +21,20 @@ use lemmy_db_schema::{
|
|||
},
|
||||
traits::Crud,
|
||||
utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now},
|
||||
ListingType,
|
||||
RegistrationMode,
|
||||
};
|
||||
use lemmy_db_views::structs::SiteView;
|
||||
use lemmy_utils::{
|
||||
error::LemmyError,
|
||||
error::{LemmyError, LemmyResult},
|
||||
utils::{
|
||||
slurs::check_slurs_opt,
|
||||
validation::{check_site_visibility_valid, is_valid_body_field},
|
||||
validation::{
|
||||
build_and_check_regex,
|
||||
check_site_visibility_valid,
|
||||
is_valid_body_field,
|
||||
site_description_length_check,
|
||||
site_name_length_check,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -48,51 +50,17 @@ impl PerformCrud for EditSite {
|
|||
let local_site = site_view.local_site;
|
||||
let site = site_view.site;
|
||||
|
||||
// Make sure user is an admin
|
||||
// Make sure user is an admin; other types of users should not update site data...
|
||||
is_admin(&local_user_view)?;
|
||||
|
||||
check_site_visibility_valid(
|
||||
local_site.private_instance,
|
||||
local_site.federation_enabled,
|
||||
&data.private_instance,
|
||||
&data.federation_enabled,
|
||||
)?;
|
||||
|
||||
let slur_regex = local_site_to_slur_regex(&local_site);
|
||||
|
||||
check_slurs_opt(&data.name, &slur_regex)?;
|
||||
check_slurs_opt(&data.description, &slur_regex)?;
|
||||
|
||||
if let Some(desc) = &data.description {
|
||||
site_description_length_check(desc)?;
|
||||
}
|
||||
|
||||
is_valid_body_field(&data.sidebar, false)?;
|
||||
|
||||
let application_question = diesel_option_overwrite(&data.application_question);
|
||||
check_application_question(
|
||||
&application_question,
|
||||
data
|
||||
.registration_mode
|
||||
.unwrap_or(local_site.registration_mode),
|
||||
)?;
|
||||
|
||||
if let Some(listing_type) = &data.default_post_listing_type {
|
||||
// only allow all or local as default listing types
|
||||
if listing_type != &ListingType::All && listing_type != &ListingType::Local {
|
||||
return Err(LemmyError::from_message(
|
||||
"invalid_default_post_listing_type",
|
||||
));
|
||||
}
|
||||
}
|
||||
validate_update_payload(&local_site, data)?;
|
||||
|
||||
if let Some(discussion_languages) = data.discussion_languages.clone() {
|
||||
SiteLanguage::update(context.pool(), discussion_languages.clone(), &site).await?;
|
||||
}
|
||||
|
||||
let name = data.name.clone();
|
||||
let site_form = SiteUpdateForm::builder()
|
||||
.name(name)
|
||||
.name(data.name.clone())
|
||||
.sidebar(diesel_option_overwrite(&data.sidebar))
|
||||
.description(diesel_option_overwrite(&data.description))
|
||||
.icon(diesel_option_overwrite_to_url(&data.icon)?)
|
||||
|
|
@ -112,7 +80,7 @@ impl PerformCrud for EditSite {
|
|||
.enable_nsfw(data.enable_nsfw)
|
||||
.community_creation_admin_only(data.community_creation_admin_only)
|
||||
.require_email_verification(data.require_email_verification)
|
||||
.application_question(application_question)
|
||||
.application_question(diesel_option_overwrite(&data.application_question))
|
||||
.private_instance(data.private_instance)
|
||||
.default_theme(data.default_theme.clone())
|
||||
.default_post_listing_type(data.default_post_listing_type)
|
||||
|
|
@ -204,3 +172,411 @@ impl PerformCrud for EditSite {
|
|||
Ok(res)
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_update_payload(local_site: &LocalSite, edit_site: &EditSite) -> LemmyResult<()> {
|
||||
// Check that the slur regex compiles, and return the regex if valid...
|
||||
// Prioritize using new slur regex from the request; if not provided, use the existing regex.
|
||||
let slur_regex = build_and_check_regex(
|
||||
&edit_site
|
||||
.slur_filter_regex
|
||||
.as_deref()
|
||||
.or(local_site.slur_filter_regex.as_deref()),
|
||||
)?;
|
||||
|
||||
if let Some(name) = &edit_site.name {
|
||||
// The name doesn't need to be updated, but if provided it cannot be blanked out...
|
||||
site_name_length_check(name)?;
|
||||
check_slurs_opt(&edit_site.name, &slur_regex)?;
|
||||
}
|
||||
|
||||
if let Some(desc) = &edit_site.description {
|
||||
site_description_length_check(desc)?;
|
||||
check_slurs_opt(&edit_site.description, &slur_regex)?;
|
||||
}
|
||||
|
||||
site_default_post_listing_type_check(&edit_site.default_post_listing_type)?;
|
||||
|
||||
check_site_visibility_valid(
|
||||
local_site.private_instance,
|
||||
local_site.federation_enabled,
|
||||
&edit_site.private_instance,
|
||||
&edit_site.federation_enabled,
|
||||
)?;
|
||||
|
||||
// Ensure that the sidebar has fewer than the max num characters...
|
||||
is_valid_body_field(&edit_site.sidebar, false)?;
|
||||
|
||||
application_question_check(
|
||||
&local_site.application_question,
|
||||
&edit_site.application_question,
|
||||
edit_site
|
||||
.registration_mode
|
||||
.unwrap_or(local_site.registration_mode),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::site::update::validate_update_payload;
|
||||
use lemmy_api_common::site::EditSite;
|
||||
use lemmy_db_schema::{source::local_site::LocalSite, ListingType, RegistrationMode};
|
||||
|
||||
#[test]
|
||||
fn test_validate_invalid_update_payload() {
|
||||
let invalid_payloads = [
|
||||
(
|
||||
"EditSite name matches LocalSite slur filter",
|
||||
"slurs",
|
||||
&generate_local_site(
|
||||
Some(String::from("(foo|bar)")),
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
Some(String::from("foo site_name")),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"EditSite name matches new slur filter",
|
||||
"slurs",
|
||||
&generate_local_site(
|
||||
Some(String::from("(foo|bar)")),
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
Some(String::from("zeta site_name")),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
Some(String::from("(zeta|alpha)")),
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"EditSite listing type is Subscribed, which is invalid",
|
||||
"invalid_default_post_listing_type",
|
||||
&generate_local_site(
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
Some(String::from("site_name")),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
Some(ListingType::Subscribed),
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"EditSite is both private and federated",
|
||||
"cant_enable_private_instance_and_federation_together",
|
||||
&generate_local_site(
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
Some(String::from("site_name")),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
Some(true),
|
||||
Some(true),
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"LocalSite is private, but EditSite also makes it federated",
|
||||
"cant_enable_private_instance_and_federation_together",
|
||||
&generate_local_site(
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
Some(String::from("site_name")),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
Some(true),
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"EditSite requires application, but neither it nor LocalSite has an application question",
|
||||
"application_question_required",
|
||||
&generate_local_site(
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
Some(String::from("site_name")),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
Some(RegistrationMode::RequireApplication),
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
invalid_payloads.iter().enumerate().for_each(
|
||||
|(
|
||||
idx,
|
||||
&(reason, expected_err, local_site, edit_site),
|
||||
)| {
|
||||
match validate_update_payload(local_site, edit_site) {
|
||||
Ok(_) => {
|
||||
panic!(
|
||||
"Got Ok, but validation should have failed with error: {} for reason: {}. invalid_payloads.nth({})",
|
||||
expected_err, reason, idx
|
||||
)
|
||||
}
|
||||
Err(error) => {
|
||||
assert!(
|
||||
error.message.eq(&Some(String::from(expected_err))),
|
||||
"Got Err {:?}, but should have failed with message: {} for reason: {}. invalid_payloads.nth({})",
|
||||
error.message,
|
||||
expected_err,
|
||||
reason,
|
||||
idx
|
||||
)
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_validate_valid_update_payload() {
|
||||
let valid_payloads = [
|
||||
(
|
||||
"No changes between LocalSite and EditSite",
|
||||
&generate_local_site(
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"EditSite allows clearing and changing values",
|
||||
&generate_local_site(
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
Some(String::from("site_name")),
|
||||
Some(String::new()),
|
||||
Some(String::new()),
|
||||
Some(ListingType::All),
|
||||
Some(String::new()),
|
||||
Some(false),
|
||||
Some(true),
|
||||
Some(String::new()),
|
||||
Some(RegistrationMode::Open),
|
||||
),
|
||||
),
|
||||
(
|
||||
"EditSite name passes slur filter regex",
|
||||
&generate_local_site(
|
||||
Some(String::from("(foo|bar)")),
|
||||
true,
|
||||
false,
|
||||
None::<String>,
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
Some(String::from("foo site_name")),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
Some(String::new()),
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
None::<RegistrationMode>,
|
||||
),
|
||||
),
|
||||
(
|
||||
"LocalSite has application question and EditSite now requires applications,",
|
||||
&generate_local_site(
|
||||
None::<String>,
|
||||
true,
|
||||
false,
|
||||
Some(String::from("question")),
|
||||
RegistrationMode::Open,
|
||||
),
|
||||
&generate_edit_site(
|
||||
Some(String::from("site_name")),
|
||||
None::<String>,
|
||||
None::<String>,
|
||||
None::<ListingType>,
|
||||
None::<String>,
|
||||
None::<bool>,
|
||||
None::<bool>,
|
||||
None::<String>,
|
||||
Some(RegistrationMode::RequireApplication),
|
||||
),
|
||||
),
|
||||
];
|
||||
|
||||
valid_payloads
|
||||
.iter()
|
||||
.enumerate()
|
||||
.for_each(|(idx, &(reason, local_site, edit_site))| {
|
||||
assert!(
|
||||
validate_update_payload(local_site, edit_site).is_ok(),
|
||||
"Got Err, but should have got Ok for reason: {}. valid_payloads.nth({})",
|
||||
reason,
|
||||
idx
|
||||
);
|
||||
})
|
||||
}
|
||||
|
||||
fn generate_local_site(
|
||||
site_slur_filter_regex: Option<String>,
|
||||
site_is_private: bool,
|
||||
site_is_federated: bool,
|
||||
site_application_question: Option<String>,
|
||||
site_registration_mode: RegistrationMode,
|
||||
) -> LocalSite {
|
||||
LocalSite {
|
||||
id: Default::default(),
|
||||
site_id: Default::default(),
|
||||
site_setup: true,
|
||||
enable_downvotes: false,
|
||||
enable_nsfw: false,
|
||||
community_creation_admin_only: false,
|
||||
require_email_verification: false,
|
||||
application_question: site_application_question,
|
||||
private_instance: site_is_private,
|
||||
default_theme: String::new(),
|
||||
default_post_listing_type: ListingType::All,
|
||||
legal_information: None,
|
||||
hide_modlog_mod_names: false,
|
||||
application_email_admins: false,
|
||||
slur_filter_regex: site_slur_filter_regex,
|
||||
actor_name_max_length: 0,
|
||||
federation_enabled: site_is_federated,
|
||||
captcha_enabled: false,
|
||||
captcha_difficulty: String::new(),
|
||||
published: Default::default(),
|
||||
updated: None,
|
||||
registration_mode: site_registration_mode,
|
||||
reports_email_admins: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Allow the test helper function to have too many arguments.
|
||||
// It's either this or generate the entire struct each time for testing.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn generate_edit_site(
|
||||
site_name: Option<String>,
|
||||
site_description: Option<String>,
|
||||
site_sidebar: Option<String>,
|
||||
site_listing_type: Option<ListingType>,
|
||||
site_slur_filter_regex: Option<String>,
|
||||
site_is_private: Option<bool>,
|
||||
site_is_federated: Option<bool>,
|
||||
site_application_question: Option<String>,
|
||||
site_registration_mode: Option<RegistrationMode>,
|
||||
) -> EditSite {
|
||||
EditSite {
|
||||
name: site_name,
|
||||
sidebar: site_sidebar,
|
||||
description: site_description,
|
||||
icon: None,
|
||||
banner: None,
|
||||
enable_downvotes: None,
|
||||
enable_nsfw: None,
|
||||
community_creation_admin_only: None,
|
||||
require_email_verification: None,
|
||||
application_question: site_application_question,
|
||||
private_instance: site_is_private,
|
||||
default_theme: None,
|
||||
default_post_listing_type: site_listing_type,
|
||||
legal_information: None,
|
||||
application_email_admins: None,
|
||||
hide_modlog_mod_names: None,
|
||||
discussion_languages: None,
|
||||
slur_filter_regex: site_slur_filter_regex,
|
||||
actor_name_max_length: None,
|
||||
rate_limit_message: None,
|
||||
rate_limit_message_per_second: None,
|
||||
rate_limit_post: None,
|
||||
rate_limit_post_per_second: None,
|
||||
rate_limit_register: None,
|
||||
rate_limit_register_per_second: None,
|
||||
rate_limit_image: None,
|
||||
rate_limit_image_per_second: None,
|
||||
rate_limit_comment: None,
|
||||
rate_limit_comment_per_second: None,
|
||||
rate_limit_search: None,
|
||||
rate_limit_search_per_second: None,
|
||||
federation_enabled: site_is_federated,
|
||||
federation_debug: None,
|
||||
captcha_enabled: None,
|
||||
captcha_difficulty: None,
|
||||
allowed_instances: None,
|
||||
blocked_instances: None,
|
||||
taglines: None,
|
||||
registration_mode: site_registration_mode,
|
||||
reports_email_admins: None,
|
||||
auth: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ use lemmy_api_common::{
|
|||
use lemmy_db_schema::{
|
||||
aggregates::structs::PersonAggregates,
|
||||
source::{
|
||||
captcha_answer::{CaptchaAnswer, CheckCaptchaAnswer},
|
||||
local_user::{LocalUser, LocalUserInsertForm},
|
||||
person::{Person, PersonInsertForm},
|
||||
registration_application::{RegistrationApplication, RegistrationApplicationInsertForm},
|
||||
|
|
@ -71,6 +72,25 @@ impl PerformCrud for Register {
|
|||
return Err(LemmyError::from_message("passwords_dont_match"));
|
||||
}
|
||||
|
||||
if local_site.site_setup && local_site.captcha_enabled {
|
||||
if let Some(captcha_uuid) = &data.captcha_uuid {
|
||||
let uuid = uuid::Uuid::parse_str(captcha_uuid)?;
|
||||
let check = CaptchaAnswer::check_captcha(
|
||||
context.pool(),
|
||||
CheckCaptchaAnswer {
|
||||
uuid,
|
||||
answer: data.captcha_answer.clone().unwrap_or_default(),
|
||||
},
|
||||
)
|
||||
.await?;
|
||||
if !check {
|
||||
return Err(LemmyError::from_message("captcha_incorrect"));
|
||||
}
|
||||
} else {
|
||||
return Err(LemmyError::from_message("captcha_incorrect"));
|
||||
}
|
||||
}
|
||||
|
||||
let slur_regex = local_site_to_slur_regex(&local_site);
|
||||
check_slurs(&data.username, &slur_regex)?;
|
||||
check_slurs_opt(&data.answer, &slur_regex)?;
|
||||
|
|
|
|||
|
|
@ -43,12 +43,11 @@ pub(crate) async fn send_activity_in_community(
|
|||
|
||||
// send to user followers
|
||||
if !is_mod_action {
|
||||
inboxes.append(
|
||||
inboxes.extend(
|
||||
&mut PersonFollower::list_followers(context.pool(), actor.id)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|p| ApubPerson(p).shared_inbox_or_inbox())
|
||||
.collect(),
|
||||
.map(|p| ApubPerson(p).shared_inbox_or_inbox()),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,10 +11,7 @@ use lemmy_db_schema::{
|
|||
traits::Crud,
|
||||
utils::DbPool,
|
||||
};
|
||||
use lemmy_utils::{
|
||||
error::LemmyError,
|
||||
utils::mention::{scrape_text_for_mentions, MentionData},
|
||||
};
|
||||
use lemmy_utils::{error::LemmyError, utils::mention::scrape_text_for_mentions};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use url::Url;
|
||||
|
|
@ -67,10 +64,9 @@ pub async fn collect_non_local_mentions(
|
|||
let mentions = scrape_text_for_mentions(&comment.content)
|
||||
.into_iter()
|
||||
// Filter only the non-local ones
|
||||
.filter(|m| !m.is_local(&context.settings().hostname))
|
||||
.collect::<Vec<MentionData>>();
|
||||
.filter(|m| !m.is_local(&context.settings().hostname));
|
||||
|
||||
for mention in &mentions {
|
||||
for mention in mentions {
|
||||
let identifier = format!("{}@{}", mention.name, mention.domain);
|
||||
let person = webfinger_resolve_actor::<LemmyContext, ApubPerson>(&identifier, context).await;
|
||||
if let Ok(person) = person {
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ serde_json = { workspace = true, optional = true }
|
|||
activitypub_federation = { workspace = true, optional = true }
|
||||
lemmy_utils = { workspace = true, optional = true }
|
||||
bcrypt = { workspace = true, optional = true }
|
||||
diesel = { workspace = true, features = ["postgres","chrono", "serde_json"], optional = true }
|
||||
diesel = { workspace = true, features = ["postgres","chrono", "serde_json", "uuid"], optional = true }
|
||||
diesel-derive-newtype = { workspace = true, optional = true }
|
||||
diesel-derive-enum = { workspace = true, optional = true }
|
||||
diesel_migrations = { workspace = true, optional = true }
|
||||
|
|
@ -48,6 +48,7 @@ rustls = { workspace = true }
|
|||
futures-util = { workspace = true }
|
||||
tokio-postgres = { workspace = true }
|
||||
tokio-postgres-rustls = { workspace = true }
|
||||
uuid = { workspace = true, features = ["v4"] }
|
||||
|
||||
[dev-dependencies]
|
||||
serial_test = { workspace = true }
|
||||
|
|
|
|||
|
|
@ -19,8 +19,8 @@ index 255c6422..f2ccf5e2 100644
|
|||
|
||||
#[derive(diesel::sql_types::SqlType)]
|
||||
#[diesel(postgres_type(name = "sort_type_enum"))]
|
||||
@@ -67,13 +63,13 @@ diesel::table! {
|
||||
when_ -> Timestamp,
|
||||
@@ -76,13 +76,13 @@ diesel::table! {
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
118
crates/db_schema/src/impls/captcha_answer.rs
Normal file
118
crates/db_schema/src/impls/captcha_answer.rs
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
use crate::{
|
||||
schema::captcha_answer::dsl::{answer, captcha_answer, uuid},
|
||||
source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer},
|
||||
utils::{functions::lower, get_conn, DbPool},
|
||||
};
|
||||
use diesel::{
|
||||
delete,
|
||||
dsl::exists,
|
||||
insert_into,
|
||||
result::Error,
|
||||
select,
|
||||
ExpressionMethods,
|
||||
QueryDsl,
|
||||
};
|
||||
use diesel_async::RunQueryDsl;
|
||||
|
||||
impl CaptchaAnswer {
|
||||
pub async fn insert(pool: &DbPool, captcha: &CaptchaAnswerForm) -> Result<Self, Error> {
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
|
||||
insert_into(captcha_answer)
|
||||
.values(captcha)
|
||||
.get_result::<Self>(conn)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn check_captcha(pool: &DbPool, to_check: CheckCaptchaAnswer) -> Result<bool, Error> {
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
|
||||
// fetch requested captcha
|
||||
let captcha_exists = select(exists(
|
||||
captcha_answer
|
||||
.filter((uuid).eq(to_check.uuid))
|
||||
.filter(lower(answer).eq(to_check.answer.to_lowercase().clone())),
|
||||
))
|
||||
.get_result::<bool>(conn)
|
||||
.await?;
|
||||
|
||||
// delete checked captcha
|
||||
delete(captcha_answer.filter(uuid.eq(to_check.uuid)))
|
||||
.execute(conn)
|
||||
.await?;
|
||||
|
||||
Ok(captcha_exists)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::{
|
||||
source::captcha_answer::{CaptchaAnswer, CaptchaAnswerForm, CheckCaptchaAnswer},
|
||||
utils::build_db_pool_for_tests,
|
||||
};
|
||||
use serial_test::serial;
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn test_captcha_happy_path() {
|
||||
let pool = &build_db_pool_for_tests().await;
|
||||
|
||||
let inserted = CaptchaAnswer::insert(
|
||||
pool,
|
||||
&CaptchaAnswerForm {
|
||||
answer: "XYZ".to_string(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect("should not fail to insert captcha");
|
||||
|
||||
let result = CaptchaAnswer::check_captcha(
|
||||
pool,
|
||||
CheckCaptchaAnswer {
|
||||
uuid: inserted.uuid,
|
||||
answer: "xyz".to_string(),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(result.is_ok());
|
||||
assert!(result.unwrap());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
#[serial]
|
||||
async fn test_captcha_repeat_answer_fails() {
|
||||
let pool = &build_db_pool_for_tests().await;
|
||||
|
||||
let inserted = CaptchaAnswer::insert(
|
||||
pool,
|
||||
&CaptchaAnswerForm {
|
||||
answer: "XYZ".to_string(),
|
||||
},
|
||||
)
|
||||
.await
|
||||
.expect("should not fail to insert captcha");
|
||||
|
||||
let _result = CaptchaAnswer::check_captcha(
|
||||
pool,
|
||||
CheckCaptchaAnswer {
|
||||
uuid: inserted.uuid,
|
||||
answer: "xyz".to_string(),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
let result_repeat = CaptchaAnswer::check_captcha(
|
||||
pool,
|
||||
CheckCaptchaAnswer {
|
||||
uuid: inserted.uuid,
|
||||
answer: "xyz".to_string(),
|
||||
},
|
||||
)
|
||||
.await;
|
||||
|
||||
assert!(result_repeat.is_ok());
|
||||
assert!(!result_repeat.unwrap());
|
||||
}
|
||||
}
|
||||
|
|
@ -99,8 +99,7 @@ impl Comment {
|
|||
// left join comment c2 on c2.path <@ c.path and c2.path != c.path
|
||||
// group by c.id
|
||||
|
||||
let path_split = parent_path.0.split('.').collect::<Vec<&str>>();
|
||||
let parent_id = path_split.get(1);
|
||||
let parent_id = parent_path.0.split('.').nth(1);
|
||||
|
||||
if let Some(parent_id) = parent_id {
|
||||
let top_parent = format!("0.{}", parent_id);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
pub mod activity;
|
||||
pub mod actor_language;
|
||||
pub mod captcha_answer;
|
||||
pub mod comment;
|
||||
pub mod comment_reply;
|
||||
pub mod comment_report;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,11 @@
|
|||
use crate::{
|
||||
newtypes::LocalUserId,
|
||||
schema::password_reset_request::dsl::{password_reset_request, published, token_encrypted},
|
||||
schema::password_reset_request::dsl::{
|
||||
local_user_id,
|
||||
password_reset_request,
|
||||
published,
|
||||
token_encrypted,
|
||||
},
|
||||
source::password_reset_request::{PasswordResetRequest, PasswordResetRequestForm},
|
||||
traits::Crud,
|
||||
utils::{get_conn, DbPool},
|
||||
|
|
@ -74,6 +79,19 @@ impl PasswordResetRequest {
|
|||
.first::<Self>(conn)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn get_recent_password_resets_count(
|
||||
pool: &DbPool,
|
||||
user_id: LocalUserId,
|
||||
) -> Result<i64, Error> {
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
password_reset_request
|
||||
.filter(local_user_id.eq(user_id))
|
||||
.filter(published.gt(now - 1.days()))
|
||||
.count()
|
||||
.get_result(conn)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
fn bytes_to_hex(bytes: Vec<u8>) -> String {
|
||||
|
|
|
|||
|
|
@ -64,6 +64,15 @@ diesel::table! {
|
|||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
captcha_answer (id) {
|
||||
id -> Int4,
|
||||
uuid -> Uuid,
|
||||
answer -> Text,
|
||||
published -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
use diesel::sql_types::*;
|
||||
use diesel_ltree::sql_types::Ltree;
|
||||
|
|
@ -914,6 +923,7 @@ diesel::allow_tables_to_appear_in_same_query!(
|
|||
admin_purge_community,
|
||||
admin_purge_person,
|
||||
admin_purge_post,
|
||||
captcha_answer,
|
||||
comment,
|
||||
comment_aggregates,
|
||||
comment_like,
|
||||
|
|
|
|||
33
crates/db_schema/src/source/captcha_answer.rs
Normal file
33
crates/db_schema/src/source/captcha_answer.rs
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
#[cfg(feature = "full")]
|
||||
use crate::schema::captcha_answer;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_with::skip_serializing_none;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "full", derive(Queryable))]
|
||||
#[cfg_attr(feature = "full", diesel(table_name = captcha_answer))]
|
||||
pub struct CaptchaAnswer {
|
||||
pub id: i32,
|
||||
pub uuid: Uuid,
|
||||
pub answer: String,
|
||||
pub published: chrono::NaiveDateTime,
|
||||
}
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "full", derive(Queryable))]
|
||||
#[cfg_attr(feature = "full", diesel(table_name = captcha_answer))]
|
||||
pub struct CheckCaptchaAnswer {
|
||||
pub uuid: Uuid,
|
||||
pub answer: String,
|
||||
}
|
||||
|
||||
#[skip_serializing_none]
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)]
|
||||
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
|
||||
#[cfg_attr(feature = "full", diesel(table_name = captcha_answer))]
|
||||
pub struct CaptchaAnswerForm {
|
||||
pub answer: String,
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
#[cfg(feature = "full")]
|
||||
pub mod activity;
|
||||
pub mod actor_language;
|
||||
pub mod captcha_answer;
|
||||
pub mod comment;
|
||||
pub mod comment_reply;
|
||||
pub mod comment_report;
|
||||
|
|
|
|||
|
|
@ -126,6 +126,7 @@ pub struct CommunityQuery<'a> {
|
|||
local_user: Option<&'a LocalUser>,
|
||||
search_term: Option<String>,
|
||||
is_mod_or_admin: Option<bool>,
|
||||
show_nsfw: Option<bool>,
|
||||
page: Option<i64>,
|
||||
limit: Option<i64>,
|
||||
}
|
||||
|
|
@ -203,8 +204,8 @@ impl<'a> CommunityQuery<'a> {
|
|||
query = query.filter(community_block::person_id.is_null());
|
||||
query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true)));
|
||||
} else {
|
||||
// No person in request, only show nsfw communities if show_nsfw passed into request
|
||||
if !self.local_user.map(|l| l.show_nsfw).unwrap_or(false) {
|
||||
// No person in request, only show nsfw communities if show_nsfw is passed into request
|
||||
if !self.show_nsfw.unwrap_or(false) {
|
||||
query = query.filter(community::nsfw.eq(false));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use crate::error::{LemmyError, LemmyResult};
|
||||
use itertools::Itertools;
|
||||
use once_cell::sync::Lazy;
|
||||
use regex::Regex;
|
||||
use regex::{Regex, RegexBuilder};
|
||||
use totp_rs::{Secret, TOTP};
|
||||
use url::Url;
|
||||
|
||||
|
|
@ -17,9 +17,13 @@ static CLEAN_URL_PARAMS_REGEX: Lazy<Regex> = Lazy::new(|| {
|
|||
Regex::new(r"^utm_source|utm_medium|utm_campaign|utm_term|utm_content|gclid|gclsrc|dclid|fbclid$")
|
||||
.expect("compile regex")
|
||||
});
|
||||
|
||||
const BODY_MAX_LENGTH: usize = 10000;
|
||||
const POST_BODY_MAX_LENGTH: usize = 50000;
|
||||
const BIO_MAX_LENGTH: usize = 300;
|
||||
const SITE_NAME_MAX_LENGTH: usize = 20;
|
||||
const SITE_NAME_MIN_LENGTH: usize = 1;
|
||||
const SITE_DESCRIPTION_MAX_LENGTH: usize = 150;
|
||||
|
||||
fn has_newline(name: &str) -> bool {
|
||||
name.contains('\n')
|
||||
|
|
@ -97,14 +101,83 @@ pub fn is_valid_reason_field(reason: &str) -> LemmyResult<()> {
|
|||
}
|
||||
|
||||
pub fn is_valid_bio_field(bio: &str) -> LemmyResult<()> {
|
||||
let check = bio.chars().count() <= BIO_MAX_LENGTH;
|
||||
if !check {
|
||||
Err(LemmyError::from_message("bio_length_overflow"))
|
||||
max_length_check(bio, BIO_MAX_LENGTH, String::from("bio_length_overflow"))
|
||||
}
|
||||
|
||||
/// Checks the site name length, the limit as defined in the DB.
|
||||
pub fn site_name_length_check(name: &str) -> LemmyResult<()> {
|
||||
min_max_length_check(
|
||||
name,
|
||||
SITE_NAME_MIN_LENGTH,
|
||||
SITE_NAME_MAX_LENGTH,
|
||||
String::from("site_name_required"),
|
||||
String::from("site_name_length_overflow"),
|
||||
)
|
||||
}
|
||||
|
||||
/// Checks the site description length, the limit as defined in the DB.
|
||||
pub fn site_description_length_check(description: &str) -> LemmyResult<()> {
|
||||
max_length_check(
|
||||
description,
|
||||
SITE_DESCRIPTION_MAX_LENGTH,
|
||||
String::from("site_description_length_overflow"),
|
||||
)
|
||||
}
|
||||
|
||||
fn max_length_check(item: &str, max_length: usize, msg: String) -> LemmyResult<()> {
|
||||
if item.len() > max_length {
|
||||
Err(LemmyError::from_message(&msg))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn min_max_length_check(
|
||||
item: &str,
|
||||
min_length: usize,
|
||||
max_length: usize,
|
||||
min_msg: String,
|
||||
max_msg: String,
|
||||
) -> LemmyResult<()> {
|
||||
if item.len() > max_length {
|
||||
Err(LemmyError::from_message(&max_msg))
|
||||
} else if item.len() < min_length {
|
||||
Err(LemmyError::from_message(&min_msg))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Attempts to build a regex and check it for common errors before inserting into the DB.
|
||||
pub fn build_and_check_regex(regex_str_opt: &Option<&str>) -> LemmyResult<Option<Regex>> {
|
||||
regex_str_opt.map_or_else(
|
||||
|| Ok(None::<Regex>),
|
||||
|regex_str| {
|
||||
if regex_str.is_empty() {
|
||||
// If the proposed regex is empty, return as having no regex at all; this is the same
|
||||
// behavior that happens downstream before the write to the database.
|
||||
return Ok(None::<Regex>);
|
||||
}
|
||||
|
||||
RegexBuilder::new(regex_str)
|
||||
.case_insensitive(true)
|
||||
.build()
|
||||
.map_err(|e| LemmyError::from_error_message(e, "invalid_regex"))
|
||||
.and_then(|regex| {
|
||||
// NOTE: It is difficult to know, in the universe of user-crafted regex, which ones
|
||||
// may match against any string text. To keep it simple, we'll match the regex
|
||||
// against an innocuous string - a single number - which should help catch a regex
|
||||
// that accidentally matches against all strings.
|
||||
if regex.is_match("1") {
|
||||
return Err(LemmyError::from_message("permissive_regex"));
|
||||
}
|
||||
|
||||
Ok(Some(regex))
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn clean_url_params(url: &Url) -> Url {
|
||||
let mut url_out = url.clone();
|
||||
if url.query().is_some() {
|
||||
|
|
@ -186,13 +259,20 @@ pub fn check_site_visibility_valid(
|
|||
mod tests {
|
||||
use super::build_totp_2fa;
|
||||
use crate::utils::validation::{
|
||||
build_and_check_regex,
|
||||
check_site_visibility_valid,
|
||||
clean_url_params,
|
||||
generate_totp_2fa_secret,
|
||||
is_valid_actor_name,
|
||||
is_valid_bio_field,
|
||||
is_valid_display_name,
|
||||
is_valid_matrix_id,
|
||||
is_valid_post_title,
|
||||
site_description_length_check,
|
||||
site_name_length_check,
|
||||
BIO_MAX_LENGTH,
|
||||
SITE_DESCRIPTION_MAX_LENGTH,
|
||||
SITE_NAME_MAX_LENGTH,
|
||||
};
|
||||
use url::Url;
|
||||
|
||||
|
|
@ -261,6 +341,126 @@ mod tests {
|
|||
assert!(totp.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_site_name() {
|
||||
let valid_names = [
|
||||
(0..SITE_NAME_MAX_LENGTH).map(|_| 'A').collect::<String>(),
|
||||
String::from("A"),
|
||||
];
|
||||
let invalid_names = [
|
||||
(
|
||||
&(0..SITE_NAME_MAX_LENGTH + 1)
|
||||
.map(|_| 'A')
|
||||
.collect::<String>(),
|
||||
"site_name_length_overflow",
|
||||
),
|
||||
(&String::new(), "site_name_required"),
|
||||
];
|
||||
|
||||
valid_names.iter().for_each(|valid_name| {
|
||||
assert!(
|
||||
site_name_length_check(valid_name).is_ok(),
|
||||
"Expected {} of length {} to be Ok.",
|
||||
valid_name,
|
||||
valid_name.len()
|
||||
)
|
||||
});
|
||||
|
||||
invalid_names
|
||||
.iter()
|
||||
.for_each(|&(invalid_name, expected_err)| {
|
||||
let result = site_name_length_check(invalid_name);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(
|
||||
result
|
||||
.unwrap_err()
|
||||
.message
|
||||
.eq(&Some(String::from(expected_err))),
|
||||
"Testing {}, expected error {}",
|
||||
invalid_name,
|
||||
expected_err
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_bio() {
|
||||
assert!(is_valid_bio_field(&(0..BIO_MAX_LENGTH).map(|_| 'A').collect::<String>()).is_ok());
|
||||
|
||||
let invalid_result =
|
||||
is_valid_bio_field(&(0..BIO_MAX_LENGTH + 1).map(|_| 'A').collect::<String>());
|
||||
|
||||
assert!(
|
||||
invalid_result.is_err()
|
||||
&& invalid_result
|
||||
.unwrap_err()
|
||||
.message
|
||||
.eq(&Some(String::from("bio_length_overflow")))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_site_description() {
|
||||
assert!(site_description_length_check(
|
||||
&(0..SITE_DESCRIPTION_MAX_LENGTH)
|
||||
.map(|_| 'A')
|
||||
.collect::<String>()
|
||||
)
|
||||
.is_ok());
|
||||
|
||||
let invalid_result = site_description_length_check(
|
||||
&(0..SITE_DESCRIPTION_MAX_LENGTH + 1)
|
||||
.map(|_| 'A')
|
||||
.collect::<String>(),
|
||||
);
|
||||
|
||||
assert!(
|
||||
invalid_result.is_err()
|
||||
&& invalid_result
|
||||
.unwrap_err()
|
||||
.message
|
||||
.eq(&Some(String::from("site_description_length_overflow")))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_valid_slur_regex() {
|
||||
let valid_regexes = [&None, &Some(""), &Some("(foo|bar)")];
|
||||
|
||||
valid_regexes.iter().for_each(|regex| {
|
||||
let result = build_and_check_regex(regex);
|
||||
|
||||
assert!(result.is_ok(), "Testing regex: {:?}", regex);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_too_permissive_slur_regex() {
|
||||
let match_everything_regexes = [
|
||||
(&Some("["), "invalid_regex"),
|
||||
(&Some("(foo|bar|)"), "permissive_regex"),
|
||||
(&Some(".*"), "permissive_regex"),
|
||||
];
|
||||
|
||||
match_everything_regexes
|
||||
.iter()
|
||||
.for_each(|&(regex_str, expected_err)| {
|
||||
let result = build_and_check_regex(regex_str);
|
||||
|
||||
assert!(result.is_err());
|
||||
assert!(
|
||||
result
|
||||
.unwrap_err()
|
||||
.message
|
||||
.eq(&Some(String::from(expected_err))),
|
||||
"Testing regex {:?}, expected error {}",
|
||||
regex_str,
|
||||
expected_err
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_check_site_visibility_valid() {
|
||||
assert!(check_site_visibility_valid(true, true, &None, &None).is_err());
|
||||
|
|
|
|||
|
|
@ -1 +1 @@
|
|||
Subproject commit 7fc71d0860bbe5c6d620ec27112350ffe5b9229c
|
||||
Subproject commit b3cca4b7e26dd7d9a389f75c6e50a489d93791ea
|
||||
|
|
@ -4,7 +4,7 @@ update local_user set default_sort_type = 'TopDay' where default_sort_type in ('
|
|||
-- rename the old enum
|
||||
alter type sort_type_enum rename to sort_type_enum__;
|
||||
-- create the new enum
|
||||
CREATE TYPE sort_type_enum AS ENUM ('Active', 'Hot', 'New', 'Old', 'TopDay', 'TopWeek', 'TopMonth', 'TopYear', 'TopAll', 'MostComments', 'NewComments');
|
||||
CREATE TYPE sort_type_enum AS ENUM ('Active', 'Hot', 'New', 'Old', 'TopDay', 'TopWeek', 'TopMonth', 'TopYear', 'TopAll', 'MostComments', 'NewComments', 'TopHour', 'TopSixHour', 'TopTwelveHour');
|
||||
|
||||
-- alter all you enum columns
|
||||
alter table local_user
|
||||
|
|
|
|||
1
migrations/2023-06-21-153242_add_captcha/down.sql
Normal file
1
migrations/2023-06-21-153242_add_captcha/down.sql
Normal file
|
|
@ -0,0 +1 @@
|
|||
drop table captcha_answer;
|
||||
6
migrations/2023-06-21-153242_add_captcha/up.sql
Normal file
6
migrations/2023-06-21-153242_add_captcha/up.sql
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
create table captcha_answer (
|
||||
id serial primary key,
|
||||
uuid uuid not null unique default gen_random_uuid(),
|
||||
answer text not null,
|
||||
published timestamp not null default now()
|
||||
);
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
drop index idx_comment_aggregates_published;
|
||||
drop index idx_community_aggregates_published;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
-- Add indexes on published column (needed for hot_rank updates)
|
||||
|
||||
create index idx_community_aggregates_published on community_aggregates (published desc);
|
||||
create index idx_comment_aggregates_published on comment_aggregates (published desc);
|
||||
|
|
@ -14,7 +14,10 @@ cargo clippy --workspace --fix --allow-staged --allow-dirty --tests --all-target
|
|||
-D clippy::manual_string_new -D clippy::redundant_closure_for_method_calls \
|
||||
-D clippy::unused_self \
|
||||
-A clippy::uninlined_format_args \
|
||||
-D clippy::get_first
|
||||
-D clippy::get_first \
|
||||
-D clippy::explicit_into_iter_loop \
|
||||
-D clippy::explicit_iter_loop \
|
||||
-D clippy::needless_collect
|
||||
|
||||
cargo clippy --workspace --features console -- \
|
||||
-D clippy::unwrap_used \
|
||||
|
|
|
|||
|
|
@ -10,15 +10,15 @@ third_semver=$(echo $new_tag | cut -d "." -f 3)
|
|||
# IE, when the third semver is a number, not '2-rc'
|
||||
if [ ! -z "${third_semver##*[!0-9]*}" ]; then
|
||||
pushd ../docker
|
||||
sed -i "s/dessalines\/lemmy:.*/dessalines\/lemmy:$new_tag/" ../docker-compose.yml
|
||||
sed -i "s/dessalines\/lemmy-ui:.*/dessalines\/lemmy-ui:$new_tag/" ../docker-compose.yml
|
||||
sed -i "s/dessalines\/lemmy-ui:.*/dessalines\/lemmy-ui:$new_tag/" ../federation/docker-compose.yml
|
||||
git add ../docker-compose.yml
|
||||
git add ../federation/docker-compose.yml
|
||||
sed -i "s/dessalines\/lemmy:.*/dessalines\/lemmy:$new_tag/" docker-compose.yml
|
||||
sed -i "s/dessalines\/lemmy-ui:.*/dessalines\/lemmy-ui:$new_tag/" docker-compose.yml
|
||||
sed -i "s/dessalines\/lemmy-ui:.*/dessalines\/lemmy-ui:$new_tag/" federation/docker-compose.yml
|
||||
git add docker-compose.yml
|
||||
git add federation/docker-compose.yml
|
||||
popd
|
||||
|
||||
# Setting the version for Ansible
|
||||
pushd ../../../lemmy-ansible
|
||||
pushd ../../lemmy-ansible
|
||||
echo $new_tag > "VERSION"
|
||||
git add "VERSION"
|
||||
git commit -m"Updating VERSION"
|
||||
|
|
@ -34,10 +34,15 @@ old_tag=$(grep version Cargo.toml | head -1 | cut -d'"' -f 2)
|
|||
sed -i "s/{ version = \"=$old_tag\", path/{ version = \"=$new_tag\", path/g" Cargo.toml
|
||||
sed -i "s/version = \"$old_tag\"/version = \"$new_tag\"/g" Cargo.toml
|
||||
git add Cargo.toml
|
||||
cargo update
|
||||
cargo check
|
||||
git add Cargo.lock
|
||||
popd
|
||||
|
||||
# Update the submodules
|
||||
git submodule update --remote
|
||||
git add crates/utils/translations
|
||||
|
||||
# The commit
|
||||
git commit -m"Version $new_tag"
|
||||
git tag $new_tag
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ use lemmy_api_common::{
|
|||
ChangePassword,
|
||||
DeleteAccount,
|
||||
GetBannedPersons,
|
||||
GetCaptcha,
|
||||
GetPersonDetails,
|
||||
GetPersonMentions,
|
||||
GetReplies,
|
||||
|
|
@ -272,6 +273,12 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
|
|||
.wrap(rate_limit.register())
|
||||
.route(web::post().to(route_post_crud::<Register>)),
|
||||
)
|
||||
.service(
|
||||
// Handle captcha separately
|
||||
web::resource("/user/get_captcha")
|
||||
.wrap(rate_limit.post())
|
||||
.route(web::get().to(route_get::<GetCaptcha>)),
|
||||
)
|
||||
// User actions
|
||||
.service(
|
||||
web::scope("/user")
|
||||
|
|
|
|||
|
|
@ -1,28 +1,21 @@
|
|||
use chrono::NaiveDateTime;
|
||||
use clokwerk::{Scheduler, TimeUnits as CTimeUnits};
|
||||
use diesel::{
|
||||
dsl::{now, IntervalDsl},
|
||||
sql_types::{Integer, Timestamp},
|
||||
Connection,
|
||||
ExpressionMethods,
|
||||
NullableExpressionMethods,
|
||||
QueryDsl,
|
||||
QueryableByName,
|
||||
};
|
||||
// Import week days and WeekDay
|
||||
use diesel::{sql_query, PgConnection, RunQueryDsl};
|
||||
use lemmy_api_common::context::LemmyContext;
|
||||
use lemmy_db_schema::{
|
||||
schema::{
|
||||
activity,
|
||||
comment,
|
||||
comment_aggregates,
|
||||
community_aggregates,
|
||||
community_person_ban,
|
||||
instance,
|
||||
person,
|
||||
post,
|
||||
post_aggregates,
|
||||
},
|
||||
schema::{activity, captcha_answer, comment, community_person_ban, instance, person, post},
|
||||
source::instance::{Instance, InstanceForm},
|
||||
utils::{functions::hot_rank, naive_now, DELETED_REPLACEMENT_TEXT},
|
||||
utils::{naive_now, DELETED_REPLACEMENT_TEXT},
|
||||
};
|
||||
use lemmy_routes::nodeinfo::NodeInfo;
|
||||
use lemmy_utils::{error::LemmyError, REQWEST_TIMEOUT};
|
||||
|
|
@ -49,13 +42,20 @@ pub fn setup(
|
|||
update_banned_when_expired(&mut conn);
|
||||
});
|
||||
|
||||
// Update hot ranks every 5 minutes
|
||||
// Update hot ranks every 15 minutes
|
||||
let url = db_url.clone();
|
||||
scheduler.every(CTimeUnits::minutes(5)).run(move || {
|
||||
scheduler.every(CTimeUnits::minutes(15)).run(move || {
|
||||
let mut conn = PgConnection::establish(&url).expect("could not establish connection");
|
||||
update_hot_ranks(&mut conn, true);
|
||||
});
|
||||
|
||||
// Delete any captcha answers older than ten minutes, every ten minutes
|
||||
let url = db_url.clone();
|
||||
scheduler.every(CTimeUnits::minutes(10)).run(move || {
|
||||
let mut conn = PgConnection::establish(&url).expect("could not establish connection");
|
||||
delete_expired_captcha_answers(&mut conn);
|
||||
});
|
||||
|
||||
// Clear old activities every week
|
||||
let url = db_url.clone();
|
||||
scheduler.every(CTimeUnits::weeks(1)).run(move || {
|
||||
|
|
@ -100,64 +100,105 @@ fn startup_jobs(db_url: &str) {
|
|||
}
|
||||
|
||||
/// Update the hot_rank columns for the aggregates tables
|
||||
/// Runs in batches until all necessary rows are updated once
|
||||
fn update_hot_ranks(conn: &mut PgConnection, last_week_only: bool) {
|
||||
let mut post_update = diesel::update(post_aggregates::table).into_boxed();
|
||||
let mut comment_update = diesel::update(comment_aggregates::table).into_boxed();
|
||||
let mut community_update = diesel::update(community_aggregates::table).into_boxed();
|
||||
|
||||
// Only update for the last week of content
|
||||
if last_week_only {
|
||||
let process_start_time = if last_week_only {
|
||||
info!("Updating hot ranks for last week...");
|
||||
let last_week = now - diesel::dsl::IntervalDsl::weeks(1);
|
||||
|
||||
post_update = post_update.filter(post_aggregates::published.gt(last_week));
|
||||
comment_update = comment_update.filter(comment_aggregates::published.gt(last_week));
|
||||
community_update = community_update.filter(community_aggregates::published.gt(last_week));
|
||||
naive_now() - chrono::Duration::days(7)
|
||||
} else {
|
||||
info!("Updating hot ranks for all history...");
|
||||
}
|
||||
NaiveDateTime::from_timestamp_opt(0, 0).expect("0 timestamp creation")
|
||||
};
|
||||
|
||||
match post_update
|
||||
.set((
|
||||
post_aggregates::hot_rank.eq(hot_rank(post_aggregates::score, post_aggregates::published)),
|
||||
post_aggregates::hot_rank_active.eq(hot_rank(
|
||||
post_aggregates::score,
|
||||
post_aggregates::newest_comment_time_necro,
|
||||
)),
|
||||
process_hot_ranks_in_batches(
|
||||
conn,
|
||||
"post_aggregates",
|
||||
"SET hot_rank = hot_rank(a.score, a.published),
|
||||
hot_rank_active = hot_rank(a.score, a.newest_comment_time_necro)",
|
||||
process_start_time,
|
||||
);
|
||||
|
||||
process_hot_ranks_in_batches(
|
||||
conn,
|
||||
"comment_aggregates",
|
||||
"SET hot_rank = hot_rank(a.score, a.published)",
|
||||
process_start_time,
|
||||
);
|
||||
|
||||
process_hot_ranks_in_batches(
|
||||
conn,
|
||||
"community_aggregates",
|
||||
"SET hot_rank = hot_rank(a.subscribers, a.published)",
|
||||
process_start_time,
|
||||
);
|
||||
|
||||
info!("Finished hot ranks update!");
|
||||
}
|
||||
|
||||
#[derive(QueryableByName)]
|
||||
struct HotRanksUpdateResult {
|
||||
#[diesel(sql_type = Timestamp)]
|
||||
published: NaiveDateTime,
|
||||
}
|
||||
|
||||
/// Runs the hot rank update query in batches until all rows after `process_start_time` have been
|
||||
/// processed.
|
||||
/// In `set_clause`, "a" will refer to the current aggregates table.
|
||||
/// Locked rows are skipped in order to prevent deadlocks (they will likely get updated on the next
|
||||
/// run)
|
||||
fn process_hot_ranks_in_batches(
|
||||
conn: &mut PgConnection,
|
||||
table_name: &str,
|
||||
set_clause: &str,
|
||||
process_start_time: NaiveDateTime,
|
||||
) {
|
||||
let update_batch_size = 1000; // Bigger batches than this tend to cause seq scans
|
||||
let mut previous_batch_result = Some(process_start_time);
|
||||
while let Some(previous_batch_last_published) = previous_batch_result {
|
||||
// Raw `sql_query` is used as a performance optimization - Diesel does not support doing this
|
||||
// in a single query (neither as a CTE, nor using a subquery)
|
||||
let result = sql_query(format!(
|
||||
r#"WITH batch AS (SELECT a.id
|
||||
FROM {aggregates_table} a
|
||||
WHERE a.published > $1
|
||||
ORDER BY a.published
|
||||
LIMIT $2
|
||||
FOR UPDATE SKIP LOCKED)
|
||||
UPDATE {aggregates_table} a {set_clause}
|
||||
FROM batch WHERE a.id = batch.id RETURNING a.published;
|
||||
"#,
|
||||
aggregates_table = table_name,
|
||||
set_clause = set_clause
|
||||
))
|
||||
.execute(conn)
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
error!("Failed to update post_aggregates hot_ranks: {}", e)
|
||||
.bind::<Timestamp, _>(previous_batch_last_published)
|
||||
.bind::<Integer, _>(update_batch_size)
|
||||
.get_results::<HotRanksUpdateResult>(conn);
|
||||
|
||||
match result {
|
||||
Ok(updated_rows) => previous_batch_result = updated_rows.last().map(|row| row.published),
|
||||
Err(e) => {
|
||||
error!("Failed to update {} hot_ranks: {}", table_name, e);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
info!(
|
||||
"Finished process_hot_ranks_in_batches execution for {}",
|
||||
table_name
|
||||
);
|
||||
}
|
||||
|
||||
match comment_update
|
||||
.set(comment_aggregates::hot_rank.eq(hot_rank(
|
||||
comment_aggregates::score,
|
||||
comment_aggregates::published,
|
||||
)))
|
||||
.execute(conn)
|
||||
{
|
||||
Ok(_) => {}
|
||||
Err(e) => {
|
||||
error!("Failed to update comment_aggregates hot_ranks: {}", e)
|
||||
}
|
||||
}
|
||||
|
||||
match community_update
|
||||
.set(community_aggregates::hot_rank.eq(hot_rank(
|
||||
community_aggregates::subscribers,
|
||||
community_aggregates::published,
|
||||
)))
|
||||
.execute(conn)
|
||||
fn delete_expired_captcha_answers(conn: &mut PgConnection) {
|
||||
match diesel::delete(
|
||||
captcha_answer::table.filter(captcha_answer::published.lt(now - IntervalDsl::minutes(10))),
|
||||
)
|
||||
.execute(conn)
|
||||
{
|
||||
Ok(_) => {
|
||||
info!("Done.");
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to update community_aggregates hot_ranks: {}", e)
|
||||
error!("Failed to clear old captcha answers: {}", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue