From 6454a4d43d3ca667b29d4cd169fead754f924f0d Mon Sep 17 00:00:00 2001 From: Nutomic Date: Mon, 16 Sep 2024 17:18:16 +0200 Subject: [PATCH] Remove enable nsfw (#5017) * Remove `local_site.enable_nsfw` in favor of `site.content_warning` (fixes #4627) * cleanup usage of SiteView::read_local * test * uppercase --- .../src/local_user/generate_totp_secret.rs | 14 +++---- crates/api/src/local_user/login.rs | 4 +- crates/api/src/local_user/reset_password.rs | 4 +- crates/api/src/local_user/save_settings.rs | 4 +- crates/api/src/local_user/verify_email.rs | 4 +- crates/api/src/site/federated_instances.rs | 6 +-- crates/api/src/site/leave_admin.rs | 4 +- crates/api_common/src/request.rs | 8 ++-- crates/api_common/src/utils.rs | 7 ---- crates/api_crud/src/community/create.rs | 4 +- crates/api_crud/src/community/list.rs | 6 +-- crates/api_crud/src/post/create.rs | 1 - crates/api_crud/src/post/read.rs | 4 +- crates/api_crud/src/post/update.rs | 1 - crates/api_crud/src/site/create.rs | 5 +-- crates/api_crud/src/site/read.rs | 4 +- crates/api_crud/src/site/update.rs | 9 +---- crates/api_crud/src/user/create.rs | 4 +- crates/apub/src/activities/block/mod.rs | 9 +---- crates/apub/src/api/list_posts.rs | 4 +- crates/apub/src/api/read_person.rs | 4 +- crates/apub/src/api/search.rs | 6 +-- .../apub/src/collections/community_outbox.rs | 14 ++----- crates/apub/src/http/site.rs | 10 ++--- crates/apub/src/objects/post.rs | 6 +-- crates/db_schema/src/impls/site.rs | 17 ++++++++- crates/db_schema/src/schema.rs | 1 - crates/db_schema/src/source/local_site.rs | 4 -- crates/db_views/src/site_view.rs | 38 ++++++++++--------- crates/routes/src/feeds.rs | 20 +++------- crates/routes/src/nodeinfo.rs | 10 ++--- .../down.sql | 17 +++++++++ .../2024-09-12-130204_drop-enable-nsfw/up.sql | 20 ++++++++++ src/code_migrations.rs | 2 +- src/lib.rs | 4 +- 35 files changed, 126 insertions(+), 153 deletions(-) create mode 100644 migrations/2024-09-12-130204_drop-enable-nsfw/down.sql create mode 100644 migrations/2024-09-12-130204_drop-enable-nsfw/up.sql diff --git a/crates/api/src/local_user/generate_totp_secret.rs b/crates/api/src/local_user/generate_totp_secret.rs index f2bfe7f9c..03ba69759 100644 --- a/crates/api/src/local_user/generate_totp_secret.rs +++ b/crates/api/src/local_user/generate_totp_secret.rs @@ -2,8 +2,11 @@ use crate::{build_totp_2fa, generate_totp_2fa_secret}; use activitypub_federation::config::Data; use actix_web::web::Json; use lemmy_api_common::{context::LemmyContext, person::GenerateTotpSecretResponse}; -use lemmy_db_schema::source::local_user::{LocalUser, LocalUserUpdateForm}; -use lemmy_db_views::structs::{LocalUserView, SiteView}; +use lemmy_db_schema::source::{ + local_user::{LocalUser, LocalUserUpdateForm}, + site::Site, +}; +use lemmy_db_views::structs::LocalUserView; use lemmy_utils::error::{LemmyErrorType, LemmyResult}; /// Generate a new secret for two-factor-authentication. Afterwards you need to call [toggle_totp] @@ -13,17 +16,14 @@ pub async fn generate_totp_secret( local_user_view: LocalUserView, context: Data, ) -> LemmyResult> { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site = Site::read_local(&mut context.pool()).await?; if local_user_view.local_user.totp_2fa_enabled { return Err(LemmyErrorType::TotpAlreadyEnabled)?; } let secret = generate_totp_2fa_secret(); - let secret_url = - build_totp_2fa(&site_view.site.name, &local_user_view.person.name, &secret)?.get_url(); + let secret_url = build_totp_2fa(&site.name, &local_user_view.person.name, &secret)?.get_url(); let local_user_form = LocalUserUpdateForm { totp_2fa_secret: Some(Some(secret)), diff --git a/crates/api/src/local_user/login.rs b/crates/api/src/local_user/login.rs index 19f84f703..e6ae38510 100644 --- a/crates/api/src/local_user/login.rs +++ b/crates/api/src/local_user/login.rs @@ -24,9 +24,7 @@ pub async fn login( req: HttpRequest, context: Data, ) -> LemmyResult> { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; // Fetch that username / email let username_or_email = data.username_or_email.clone(); diff --git a/crates/api/src/local_user/reset_password.rs b/crates/api/src/local_user/reset_password.rs index b6b113c07..1c47e6c4e 100644 --- a/crates/api/src/local_user/reset_password.rs +++ b/crates/api/src/local_user/reset_password.rs @@ -20,9 +20,7 @@ pub async fn reset_password( .await? .ok_or(LemmyErrorType::IncorrectLogin)?; - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; check_email_verified(&local_user_view, &site_view)?; // Email the pure token to the user. diff --git a/crates/api/src/local_user/save_settings.rs b/crates/api/src/local_user/save_settings.rs index 13bdd3646..d5b6a7ec1 100644 --- a/crates/api/src/local_user/save_settings.rs +++ b/crates/api/src/local_user/save_settings.rs @@ -36,9 +36,7 @@ pub async fn save_user_settings( context: Data, local_user_view: LocalUserView, ) -> LemmyResult> { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let slur_regex = local_site_to_slur_regex(&site_view.local_site); let url_blocklist = get_url_blocklist(&context).await?; diff --git a/crates/api/src/local_user/verify_email.rs b/crates/api/src/local_user/verify_email.rs index 5b895ec7e..bbd442723 100644 --- a/crates/api/src/local_user/verify_email.rs +++ b/crates/api/src/local_user/verify_email.rs @@ -16,9 +16,7 @@ pub async fn verify_email( data: Json, context: Data, ) -> LemmyResult> { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let token = data.token.clone(); let verification = EmailVerification::read_for_token(&mut context.pool(), &token) .await? diff --git a/crates/api/src/site/federated_instances.rs b/crates/api/src/site/federated_instances.rs index 66b0ff2c1..5943cfd9a 100644 --- a/crates/api/src/site/federated_instances.rs +++ b/crates/api/src/site/federated_instances.rs @@ -5,15 +5,13 @@ use lemmy_api_common::{ utils::build_federated_instances, }; use lemmy_db_views::structs::SiteView; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn get_federated_instances( context: Data, ) -> LemmyResult> { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let federated_instances = build_federated_instances(&site_view.local_site, &mut context.pool()).await?; diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index e7a5464f3..52b8a32ef 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -55,9 +55,7 @@ pub async fn leave_admin( ModAdd::create(&mut context.pool(), &form).await?; // Reread site and admins - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let admins = PersonView::admins(&mut context.pool()).await?; let all_languages = Language::read_all(&mut context.pool()).await?; diff --git a/crates/api_common/src/request.rs b/crates/api_common/src/request.rs index de6ba4f39..90a626e4f 100644 --- a/crates/api_common/src/request.rs +++ b/crates/api_common/src/request.rs @@ -3,7 +3,7 @@ use crate::{ lemmy_db_schema::traits::Crud, post::{LinkMetadata, OpenGraphData}, send_activity::{ActivityChannel, SendActivityData}, - utils::{local_site_opt_to_sensitive, proxy_image_link}, + utils::proxy_image_link, }; use activitypub_federation::config::Data; use chrono::{DateTime, Utc}; @@ -13,8 +13,8 @@ use lemmy_db_schema::{ newtypes::DbUrl, source::{ images::{ImageDetailsForm, LocalImage, LocalImageForm}, - local_site::LocalSite, post::{Post, PostUpdateForm}, + site::Site, }, }; use lemmy_utils::{ @@ -130,7 +130,6 @@ pub async fn generate_post_link_metadata( post: Post, custom_thumbnail: Option, send_activity: impl FnOnce(Post) -> Option + Send + 'static, - local_site: Option, context: Data, ) -> LemmyResult<()> { let metadata = match &post.url { @@ -144,7 +143,8 @@ pub async fn generate_post_link_metadata( .is_some_and(|content_type| content_type.starts_with("image")); // Decide if we are allowed to generate local thumbnail - let allow_sensitive = local_site_opt_to_sensitive(&local_site); + let site = Site::read_local(&mut context.pool()).await?; + let allow_sensitive = site.content_warning.is_some(); let allow_generate_thumbnail = allow_sensitive || !post.nsfw; let image_url = if is_image_post { diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 64cd41135..ebcc237e5 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -538,13 +538,6 @@ pub fn local_site_opt_to_slur_regex(local_site: &Option) -> Option) -> bool { - local_site - .as_ref() - .map(|site| site.enable_nsfw) - .unwrap_or(false) -} - pub async fn get_url_blocklist(context: &LemmyContext) -> LemmyResult { static URL_BLOCKLIST: LazyLock> = LazyLock::new(|| { Cache::builder() diff --git a/crates/api_crud/src/community/create.rs b/crates/api_crud/src/community/create.rs index 4289b7d24..6d84b3e64 100644 --- a/crates/api_crud/src/community/create.rs +++ b/crates/api_crud/src/community/create.rs @@ -47,9 +47,7 @@ pub async fn create_community( context: Data, local_user_view: LocalUserView, ) -> LemmyResult> { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let local_site = site_view.local_site; if local_site.community_creation_admin_only && is_admin(&local_user_view).is_err() { diff --git a/crates/api_crud/src/community/list.rs b/crates/api_crud/src/community/list.rs index 587b5cdfa..9c13ae89f 100644 --- a/crates/api_crud/src/community/list.rs +++ b/crates/api_crud/src/community/list.rs @@ -6,7 +6,7 @@ use lemmy_api_common::{ }; use lemmy_db_views::structs::{LocalUserView, SiteView}; use lemmy_db_views_actor::community_view::CommunityQuery; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn list_communities( @@ -14,9 +14,7 @@ pub async fn list_communities( context: Data, local_user_view: Option, ) -> LemmyResult> { - let local_site = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let local_site = SiteView::read_local(&mut context.pool()).await?; let is_admin = local_user_view .as_ref() .map(|luv| is_admin(luv).is_ok()) diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index a0f0b7525..5fcde58f3 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -149,7 +149,6 @@ pub async fn create_post( inserted_post.clone(), custom_thumbnail.map(Into::into), |post| Some(SendActivityData::CreatePost(post)), - Some(local_site), context.reset_request_count(), ) .await?; diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index ebf8940a2..4ecf24e0c 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -21,9 +21,7 @@ pub async fn get_post( context: Data, local_user_view: Option, ) -> LemmyResult> { - let local_site = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let local_site = SiteView::read_local(&mut context.pool()).await?; check_private_instance(&local_user_view, &local_site.local_site)?; diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index 835398596..d5053515c 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -129,7 +129,6 @@ pub async fn update_post( updated_post.clone(), custom_thumbnail.flatten().map(Into::into), |post| Some(SendActivityData::UpdatePost(post)), - Some(local_site), context.reset_request_count(), ) .await?; diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 9dcd1595a..6566a7a9f 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -92,7 +92,6 @@ pub async fn create_site( site_setup: Some(true), enable_downvotes: data.enable_downvotes, registration_mode: data.registration_mode, - enable_nsfw: data.enable_nsfw, community_creation_admin_only: data.community_creation_admin_only, require_email_verification: data.require_email_verification, application_question: diesel_string_update(data.application_question.as_deref()), @@ -133,9 +132,7 @@ pub async fn create_site( LocalSiteRateLimit::update(&mut context.pool(), &local_site_rate_limit_form).await?; - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let new_taglines = data.taglines.clone(); let taglines = Tagline::replace(&mut context.pool(), local_site.id, new_taglines).await?; diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index d2a13ac5e..94a28a4ad 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -37,9 +37,7 @@ pub async fn get_site( // This data is independent from the user account so we can cache it across requests let mut site_response = CACHE .try_get_with::<_, LemmyError>((), async { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let admins = PersonView::admins(&mut context.pool()).await?; let all_languages = Language::read_all(&mut context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index f6377038d..f68b00c04 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -52,9 +52,7 @@ pub async fn update_site( context: Data, local_user_view: LocalUserView, ) -> LemmyResult> { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let local_site = site_view.local_site; let site = site_view.site; @@ -103,7 +101,6 @@ pub async fn update_site( let local_site_form = LocalSiteUpdateForm { enable_downvotes: data.enable_downvotes, registration_mode: data.registration_mode, - enable_nsfw: data.enable_nsfw, community_creation_admin_only: data.community_creation_admin_only, require_email_verification: data.require_email_verification, application_question: diesel_string_update(data.application_question.as_deref()), @@ -191,9 +188,7 @@ pub async fn update_site( let new_taglines = data.taglines.clone(); let taglines = Tagline::replace(&mut context.pool(), local_site.id, new_taglines).await?; - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let rate_limit_config = local_site_rate_limit_to_rate_limit_config(&site_view.local_site_rate_limit); diff --git a/crates/api_crud/src/user/create.rs b/crates/api_crud/src/user/create.rs index 64bef8760..b717d8816 100644 --- a/crates/api_crud/src/user/create.rs +++ b/crates/api_crud/src/user/create.rs @@ -45,9 +45,7 @@ pub async fn register( req: HttpRequest, context: Data, ) -> LemmyResult> { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let local_site = site_view.local_site; let require_registration_application = local_site.registration_mode == RegistrationMode::RequireApplication; diff --git a/crates/apub/src/activities/block/mod.rs b/crates/apub/src/activities/block/mod.rs index d42b62369..d1ba4c574 100644 --- a/crates/apub/src/activities/block/mod.rs +++ b/crates/apub/src/activities/block/mod.rs @@ -22,7 +22,6 @@ use lemmy_db_schema::{ traits::Crud, utils::DbPool, }; -use lemmy_db_views::structs::SiteView; use lemmy_utils::{ error::{LemmyError, LemmyResult}, LemmyErrorType, @@ -142,13 +141,7 @@ pub(crate) async fn send_ban_from_site( expires: Option, context: Data, ) -> LemmyResult<()> { - let site = SiteOrCommunity::Site( - SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)? - .site - .into(), - ); + let site = SiteOrCommunity::Site(Site::read_local(&mut context.pool()).await?.into()); let expires = check_expire_time(expires)?; // if the action affects a local user, federate to other instances diff --git a/crates/apub/src/api/list_posts.rs b/crates/apub/src/api/list_posts.rs index 7ceafed8d..cb2a37a3c 100644 --- a/crates/apub/src/api/list_posts.rs +++ b/crates/apub/src/api/list_posts.rs @@ -23,9 +23,7 @@ pub async fn list_posts( context: Data, local_user_view: Option, ) -> LemmyResult> { - let local_site = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let local_site = SiteView::read_local(&mut context.pool()).await?; check_private_instance(&local_user_view, &local_site.local_site)?; diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index d61f6d9c5..0a2a76465 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -26,9 +26,7 @@ pub async fn read_person( Err(LemmyErrorType::NoIdGiven)? } - let local_site = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let local_site = SiteView::read_local(&mut context.pool()).await?; check_private_instance(&local_user_view, &local_site.local_site)?; diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index 905886913..d50363d4a 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -13,7 +13,7 @@ use lemmy_db_views::{ structs::{LocalUserView, SiteView}, }; use lemmy_db_views_actor::{community_view::CommunityQuery, person_view::PersonQuery}; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_utils::error::LemmyResult; #[tracing::instrument(skip(context))] pub async fn search( @@ -21,9 +21,7 @@ pub async fn search( context: Data, local_user_view: Option, ) -> LemmyResult> { - let local_site = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let local_site = SiteView::read_local(&mut context.pool()).await?; check_private_instance(&local_user_view, &local_site.local_site)?; diff --git a/crates/apub/src/collections/community_outbox.rs b/crates/apub/src/collections/community_outbox.rs index 8aca82d38..38a66c62b 100644 --- a/crates/apub/src/collections/community_outbox.rs +++ b/crates/apub/src/collections/community_outbox.rs @@ -18,12 +18,9 @@ use activitypub_federation::{ }; use futures::future::join_all; use lemmy_api_common::{context::LemmyContext, utils::generate_outbox_url}; -use lemmy_db_schema::{utils::FETCH_LIMIT_MAX, SortType}; -use lemmy_db_views::{post_view::PostQuery, structs::SiteView}; -use lemmy_utils::{ - error::{LemmyError, LemmyResult}, - LemmyErrorType, -}; +use lemmy_db_schema::{source::site::Site, utils::FETCH_LIMIT_MAX, SortType}; +use lemmy_db_views::post_view::PostQuery; +use lemmy_utils::error::{LemmyError, LemmyResult}; use url::Url; #[derive(Clone, Debug)] @@ -38,10 +35,7 @@ impl Collection for ApubCommunityOutbox { #[tracing::instrument(skip_all)] async fn read_local(owner: &Self::Owner, data: &Data) -> LemmyResult { - let site = SiteView::read_local(&mut data.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)? - .site; + let site = Site::read_local(&mut data.pool()).await?; let post_views = PostQuery { community_id: Some(owner.id), diff --git a/crates/apub/src/http/site.rs b/crates/apub/src/http/site.rs index 54d3c0e32..95175a006 100644 --- a/crates/apub/src/http/site.rs +++ b/crates/apub/src/http/site.rs @@ -6,16 +6,12 @@ use crate::{ use activitypub_federation::{config::Data, traits::Object}; use actix_web::HttpResponse; use lemmy_api_common::context::LemmyContext; -use lemmy_db_views::structs::SiteView; -use lemmy_utils::{error::LemmyResult, LemmyErrorType}; +use lemmy_db_schema::source::site::Site; +use lemmy_utils::error::LemmyResult; use url::Url; pub(crate) async fn get_apub_site_http(context: Data) -> LemmyResult { - let site: ApubSite = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)? - .site - .into(); + let site: ApubSite = Site::read_local(&mut context.pool()).await?.into(); let apub = site.into_json(&context).await?; create_apub_response(&apub) diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 44e842413..28f5338f2 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -270,9 +270,9 @@ impl Object for ApubPost { // Generates a post thumbnail in background task, because some sites can be very slow to // respond. - spawn_try_task(async move { - generate_post_link_metadata(post_, None, |_| None, local_site, context_).await - }); + spawn_try_task( + async move { generate_post_link_metadata(post_, None, |_| None, context_).await }, + ); Ok(post.into()) } diff --git a/crates/db_schema/src/impls/site.rs b/crates/db_schema/src/impls/site.rs index 9dbd2401d..f2b634354 100644 --- a/crates/db_schema/src/impls/site.rs +++ b/crates/db_schema/src/impls/site.rs @@ -1,6 +1,6 @@ use crate::{ newtypes::{DbUrl, InstanceId, SiteId}, - schema::site, + schema::{local_site, site}, source::{ actor_language::SiteLanguage, site::{Site, SiteInsertForm, SiteUpdateForm}, @@ -10,6 +10,7 @@ use crate::{ }; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, OptionalExtension, QueryDsl}; use diesel_async::RunQueryDsl; +use lemmy_utils::{error::LemmyResult, LemmyErrorType}; use url::Url; #[async_trait] @@ -102,4 +103,18 @@ impl Site { url.set_query(None); url } + + pub async fn read_local(pool: &mut DbPool<'_>) -> LemmyResult { + let conn = &mut get_conn(pool).await?; + + Ok( + site::table + .inner_join(local_site::table) + .select(site::all_columns) + .first(conn) + .await + .optional()? + .ok_or(LemmyErrorType::LocalSiteNotSetup)?, + ) + } } diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index c78acfe37..aa143c4c9 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -370,7 +370,6 @@ diesel::table! { site_id -> Int4, site_setup -> Bool, enable_downvotes -> Bool, - enable_nsfw -> Bool, community_creation_admin_only -> Bool, require_email_verification -> Bool, application_question -> Nullable, diff --git a/crates/db_schema/src/source/local_site.rs b/crates/db_schema/src/source/local_site.rs index 05583c065..21af1f6ca 100644 --- a/crates/db_schema/src/source/local_site.rs +++ b/crates/db_schema/src/source/local_site.rs @@ -29,8 +29,6 @@ pub struct LocalSite { pub site_setup: bool, /// Whether downvotes are enabled. pub enable_downvotes: bool, - /// Whether NSFW is enabled. - pub enable_nsfw: bool, /// Whether only admins can create communities. pub community_creation_admin_only: bool, /// Whether emails are required. @@ -81,7 +79,6 @@ pub struct LocalSiteInsertForm { pub site_id: SiteId, pub site_setup: Option, pub enable_downvotes: Option, - pub enable_nsfw: Option, pub community_creation_admin_only: Option, pub require_email_verification: Option, pub application_question: Option, @@ -109,7 +106,6 @@ pub struct LocalSiteInsertForm { pub struct LocalSiteUpdateForm { pub site_setup: Option, pub enable_downvotes: Option, - pub enable_nsfw: Option, pub community_creation_admin_only: Option, pub require_email_verification: Option, pub application_question: Option>, diff --git a/crates/db_views/src/site_view.rs b/crates/db_views/src/site_view.rs index 8f0722318..6014ad964 100644 --- a/crates/db_views/src/site_view.rs +++ b/crates/db_views/src/site_view.rs @@ -1,28 +1,32 @@ use crate::structs::SiteView; -use diesel::{result::Error, ExpressionMethods, JoinOnDsl, OptionalExtension, QueryDsl}; +use diesel::{ExpressionMethods, JoinOnDsl, OptionalExtension, QueryDsl}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ schema::{local_site, local_site_rate_limit, site, site_aggregates}, utils::{get_conn, DbPool}, }; +use lemmy_utils::{error::LemmyResult, LemmyErrorType}; impl SiteView { - pub async fn read_local(pool: &mut DbPool<'_>) -> Result, Error> { + pub async fn read_local(pool: &mut DbPool<'_>) -> LemmyResult { let conn = &mut get_conn(pool).await?; - site::table - .inner_join(local_site::table) - .inner_join( - local_site_rate_limit::table.on(local_site::id.eq(local_site_rate_limit::local_site_id)), - ) - .inner_join(site_aggregates::table) - .select(( - site::all_columns, - local_site::all_columns, - local_site_rate_limit::all_columns, - site_aggregates::all_columns, - )) - .first(conn) - .await - .optional() + Ok( + site::table + .inner_join(local_site::table) + .inner_join( + local_site_rate_limit::table.on(local_site::id.eq(local_site_rate_limit::local_site_id)), + ) + .inner_join(site_aggregates::table) + .select(( + site::all_columns, + local_site::all_columns, + local_site_rate_limit::all_columns, + site_aggregates::all_columns, + )) + .first(conn) + .await + .optional()? + .ok_or(LemmyErrorType::LocalSiteNotSetup)?, + ) } } diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index f7e7d4059..02114f382 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -151,9 +151,7 @@ async fn get_feed_data( limit: i64, page: i64, ) -> LemmyResult { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; check_private_instance(&None, &site_view.local_site)?; @@ -258,9 +256,7 @@ async fn get_feed_user( page: &i64, user_name: &str, ) -> LemmyResult { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let person = Person::read_from_name(&mut context.pool(), user_name, false) .await? .ok_or(LemmyErrorType::CouldntFindPerson)?; @@ -298,9 +294,7 @@ async fn get_feed_community( page: &i64, community_name: &str, ) -> LemmyResult { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let community = Community::read_from_name(&mut context.pool(), community_name, false) .await? .ok_or(LemmyErrorType::CouldntFindCommunity)?; @@ -345,9 +339,7 @@ async fn get_feed_front( page: &i64, jwt: &str, ) -> LemmyResult { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let local_user = local_user_view_from_jwt(jwt, context).await?; check_private_instance(&Some(local_user.clone()), &site_view.local_site)?; @@ -382,9 +374,7 @@ async fn get_feed_front( #[tracing::instrument(skip_all)] async fn get_feed_inbox(context: &LemmyContext, jwt: &str) -> LemmyResult { - let site_view = SiteView::read_local(&mut context.pool()) - .await? - .ok_or(LemmyErrorType::LocalSiteNotSetup)?; + let site_view = SiteView::read_local(&mut context.pool()).await?; let local_user = local_user_view_from_jwt(jwt, context).await?; let person_id = local_user.local_user.person_id; let show_bot_accounts = local_user.local_user.show_bot_accounts; diff --git a/crates/routes/src/nodeinfo.rs b/crates/routes/src/nodeinfo.rs index bcb835309..e5b183a0b 100644 --- a/crates/routes/src/nodeinfo.rs +++ b/crates/routes/src/nodeinfo.rs @@ -1,11 +1,10 @@ -use actix_web::{error::ErrorBadRequest, web, Error, HttpResponse, Result}; -use anyhow::anyhow; +use actix_web::{web, Error, HttpResponse, Result}; use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::RegistrationMode; use lemmy_db_views::structs::SiteView; use lemmy_utils::{ cache_header::{cache_1hour, cache_3days}, - error::{LemmyError, LemmyResult}, + error::LemmyResult, VERSION, }; use serde::{Deserialize, Serialize}; @@ -44,10 +43,7 @@ async fn node_info_well_known(context: web::Data) -> LemmyResult) -> Result { - let site_view = SiteView::read_local(&mut context.pool()) - .await - .map_err(|_| ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))? - .ok_or(ErrorBadRequest(LemmyError::from(anyhow!("not_found"))))?; + let site_view = SiteView::read_local(&mut context.pool()).await?; // Since there are 3 registration options, // we need to set open_registrations as true if RegistrationMode is not Closed. diff --git a/migrations/2024-09-12-130204_drop-enable-nsfw/down.sql b/migrations/2024-09-12-130204_drop-enable-nsfw/down.sql new file mode 100644 index 000000000..7cbad1998 --- /dev/null +++ b/migrations/2024-09-12-130204_drop-enable-nsfw/down.sql @@ -0,0 +1,17 @@ +ALTER TABLE local_site + ADD COLUMN enable_nsfw boolean NOT NULL DEFAULT FALSE; + +UPDATE + local_site +SET + enable_nsfw = CASE WHEN site.content_warning IS NULL THEN + FALSE + ELSE + TRUE + END +FROM + site +WHERE + -- only local site has private key + site.private_key IS NOT NULL; + diff --git a/migrations/2024-09-12-130204_drop-enable-nsfw/up.sql b/migrations/2024-09-12-130204_drop-enable-nsfw/up.sql new file mode 100644 index 000000000..61d30bd51 --- /dev/null +++ b/migrations/2024-09-12-130204_drop-enable-nsfw/up.sql @@ -0,0 +1,20 @@ +-- if site has enable_nsfw, set a default content warning +UPDATE + site +SET + content_warning = CASE WHEN local_site.enable_nsfw THEN + 'NSFW' + ELSE + NULL + END +FROM + local_site + -- only local site has private key +WHERE + private_key IS NOT NULL + -- dont overwrite existing content warning + AND content_warning IS NOT NULL; + +ALTER TABLE local_site + DROP enable_nsfw; + diff --git a/src/code_migrations.rs b/src/code_migrations.rs index ae6155bb6..12a688b80 100644 --- a/src/code_migrations.rs +++ b/src/code_migrations.rs @@ -345,7 +345,7 @@ async fn instance_actor_2022_01_28( settings: &Settings, ) -> LemmyResult<()> { info!("Running instance_actor_2021_09_29"); - if let Ok(Some(site_view)) = SiteView::read_local(pool).await { + if let Ok(site_view) = SiteView::read_local(pool).await { let site = site_view.site; // if site already has public key, we dont need to do anything here if !site.public_key.is_empty() { diff --git a/src/lib.rs b/src/lib.rs index da14e5cd8..3662f6bc8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -127,9 +127,7 @@ pub async fn start_lemmy_server(args: CmdArgs) -> LemmyResult<()> { .expect("Couldn't initialize secrets."); // Make sure the local site is set up. - let site_view = SiteView::read_local(&mut (&pool).into()) - .await? - .expect("local site not set up"); + let site_view = SiteView::read_local(&mut (&pool).into()).await?; let local_site = site_view.local_site; let federation_enabled = local_site.federation_enabled;