From: self <self@awful.systems>
Date: Tue, 25 Jul 2023 12:17:16 +0000 (-0700)
Subject: add enable_federated_downvotes site option
X-Git-Url: http://these/git/%7B%60/feeds/%7B%60%24%7BwebArchiveUrl%7D/save/static/git-logo.png?a=commitdiff_plain;p=lemmy.git

add enable_federated_downvotes site option
---

diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs
index 35b6d77e..a5b2afae 100644
--- a/crates/api_common/src/site.rs
+++ b/crates/api_common/src/site.rs
@@ -2,44 +2,20 @@ use crate::sensitive::Sensitive;
 use lemmy_db_schema::{
   newtypes::{CommentId, CommunityId, LanguageId, PersonId, PostId},
   source::{instance::Instance, language::Language, tagline::Tagline},
-  ListingType,
-  ModlogActionType,
-  RegistrationMode,
-  SearchType,
-  SortType,
+  ListingType, ModlogActionType, RegistrationMode, SearchType, SortType,
 };
 use lemmy_db_views::structs::{
-  CommentView,
-  CustomEmojiView,
-  LocalUserView,
-  PostView,
-  RegistrationApplicationView,
-  SiteView,
+  CommentView, CustomEmojiView, LocalUserView, PostView, RegistrationApplicationView, SiteView,
 };
 use lemmy_db_views_actor::structs::{
-  CommunityBlockView,
-  CommunityFollowerView,
-  CommunityModeratorView,
-  CommunityView,
-  PersonBlockView,
-  PersonView,
+  CommunityBlockView, CommunityFollowerView, CommunityModeratorView, CommunityView,
+  PersonBlockView, PersonView,
 };
 use lemmy_db_views_moderator::structs::{
-  AdminPurgeCommentView,
-  AdminPurgeCommunityView,
-  AdminPurgePersonView,
-  AdminPurgePostView,
-  ModAddCommunityView,
-  ModAddView,
-  ModBanFromCommunityView,
-  ModBanView,
-  ModFeaturePostView,
-  ModHideCommunityView,
-  ModLockPostView,
-  ModRemoveCommentView,
-  ModRemoveCommunityView,
-  ModRemovePostView,
-  ModTransferCommunityView,
+  AdminPurgeCommentView, AdminPurgeCommunityView, AdminPurgePersonView, AdminPurgePostView,
+  ModAddCommunityView, ModAddView, ModBanFromCommunityView, ModBanView, ModFeaturePostView,
+  ModHideCommunityView, ModLockPostView, ModRemoveCommentView, ModRemoveCommunityView,
+  ModRemovePostView, ModTransferCommunityView,
 };
 use serde::{Deserialize, Serialize};
 use serde_with::skip_serializing_none;
@@ -150,6 +126,7 @@ pub struct CreateSite {
   pub icon: Option<String>,
   pub banner: Option<String>,
   pub enable_downvotes: Option<bool>,
+  pub enable_federated_downvotes: Option<bool>,
   pub enable_nsfw: Option<bool>,
   pub community_creation_admin_only: Option<bool>,
   pub require_email_verification: Option<bool>,
@@ -202,6 +179,8 @@ pub struct EditSite {
   pub banner: Option<String>,
   /// Whether to enable downvotes.
   pub enable_downvotes: Option<bool>,
+  /// Whether to allow downvotes from federated sites.
+  pub enable_federated_downvotes: Option<bool>,
   /// Whether to enable NSFW.
   pub enable_nsfw: Option<bool>,
   /// Limits community creation to admins only.
diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs
index 38d6111c..34e153c5 100644
--- a/crates/api_crud/src/site/create.rs
+++ b/crates/api_crud/src/site/create.rs
@@ -5,12 +5,8 @@ use lemmy_api_common::{
   context::LemmyContext,
   site::{CreateSite, SiteResponse},
   utils::{
-    generate_site_inbox_url,
-    is_admin,
-    local_site_rate_limit_to_rate_limit_config,
-    local_user_view_from_jwt,
-    sanitize_html,
-    sanitize_html_opt,
+    generate_site_inbox_url, is_admin, local_site_rate_limit_to_rate_limit_config,
+    local_user_view_from_jwt, sanitize_html, sanitize_html_opt,
   },
 };
 use lemmy_db_schema::{
@@ -30,11 +26,8 @@ use lemmy_utils::{
   utils::{
     slurs::{check_slurs, check_slurs_opt},
     validation::{
-      build_and_check_regex,
-      check_site_visibility_valid,
-      is_valid_body_field,
-      site_description_length_check,
-      site_name_length_check,
+      build_and_check_regex, check_site_visibility_valid, is_valid_body_field,
+      site_description_length_check, site_name_length_check,
     },
   },
 };
@@ -77,7 +70,6 @@ pub async fn create_site(
   let site_id = local_site.site_id;
 
   Site::update(&mut context.pool(), site_id, &site_form).await?;
-
   let application_question = sanitize_html_opt(&data.application_question);
   let default_theme = sanitize_html_opt(&data.default_theme);
   let legal_information = sanitize_html_opt(&data.legal_information);
@@ -86,6 +78,7 @@ pub async fn create_site(
     // Set the site setup to true
     site_setup: Some(true),
     enable_downvotes: data.enable_downvotes,
+    enable_federated_downvotes: data.enable_federated_downvotes,
     registration_mode: data.registration_mode,
     enable_nsfw: data.enable_nsfw,
     community_creation_admin_only: data.community_creation_admin_only,
@@ -513,6 +506,7 @@ mod tests {
       site_id: Default::default(),
       site_setup,
       enable_downvotes: false,
+      enable_federated_downvotes: false,
       enable_nsfw: false,
       community_creation_admin_only: false,
       require_email_verification: false,
@@ -556,6 +550,7 @@ mod tests {
       icon: None,
       banner: None,
       enable_downvotes: None,
+      enable_federated_downvotes: None,
       enable_nsfw: None,
       community_creation_admin_only: None,
       require_email_verification: None,
diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs
index 6148d546..83f74642 100644
--- a/crates/api_crud/src/site/update.rs
+++ b/crates/api_crud/src/site/update.rs
@@ -4,9 +4,7 @@ use lemmy_api_common::{
   context::LemmyContext,
   site::{EditSite, SiteResponse},
   utils::{
-    is_admin,
-    local_site_rate_limit_to_rate_limit_config,
-    local_user_view_from_jwt,
+    is_admin, local_site_rate_limit_to_rate_limit_config, local_user_view_from_jwt,
     sanitize_html_opt,
   },
 };
@@ -31,11 +29,8 @@ use lemmy_utils::{
   utils::{
     slurs::check_slurs_opt,
     validation::{
-      build_and_check_regex,
-      check_site_visibility_valid,
-      is_valid_body_field,
-      site_description_length_check,
-      site_name_length_check,
+      build_and_check_regex, check_site_visibility_valid, is_valid_body_field,
+      site_description_length_check, site_name_length_check,
     },
   },
 };
@@ -85,6 +80,7 @@ pub async fn update_site(
 
   let local_site_form = LocalSiteUpdateForm {
     enable_downvotes: data.enable_downvotes,
+    enable_federated_downvotes: data.enable_federated_downvotes,
     registration_mode: data.registration_mode,
     enable_nsfw: data.enable_nsfw,
     community_creation_admin_only: data.community_creation_admin_only,
@@ -511,6 +507,7 @@ mod tests {
       site_id: Default::default(),
       site_setup: true,
       enable_downvotes: false,
+      enable_federated_downvotes: false,
       enable_nsfw: false,
       community_creation_admin_only: false,
       require_email_verification: false,
@@ -554,6 +551,7 @@ mod tests {
       icon: None,
       banner: None,
       enable_downvotes: None,
+      enable_federated_downvotes: None,
       enable_nsfw: None,
       community_creation_admin_only: None,
       require_email_verification: None,
diff --git a/crates/apub/src/activities/voting/vote.rs b/crates/apub/src/activities/voting/vote.rs
index ef457298..81bed456 100644
--- a/crates/apub/src/activities/voting/vote.rs
+++ b/crates/apub/src/activities/voting/vote.rs
@@ -1,7 +1,6 @@
 use crate::{
   activities::{
-    generate_activity_id,
-    verify_person_in_community,
+    generate_activity_id, verify_person_in_community,
     voting::{vote_comment, vote_post},
   },
   insert_received_activity,
@@ -63,7 +62,11 @@ impl ActivityHandler for Vote {
       .await
       .map(|l| l.enable_downvotes)
       .unwrap_or(true);
-    if self.kind == VoteType::Dislike && !enable_downvotes {
+    let enable_federated_downvotes = LocalSite::read(&mut context.pool())
+      .await
+      .map(|l| l.enable_federated_downvotes)
+      .unwrap_or(true);
+    if self.kind == VoteType::Dislike && (!enable_downvotes || !enable_federated_downvotes) {
       return Err(anyhow!("Downvotes disabled").into());
     }
     Ok(())
diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs
index 7106b1c9..5aa54c9d 100644
--- a/crates/db_schema/src/schema.rs
+++ b/crates/db_schema/src/schema.rs
@@ -1,17 +1,17 @@
 // @generated automatically by Diesel CLI.
 
 pub mod sql_types {
-    #[derive(diesel::sql_types::SqlType)]
-    #[diesel(postgres_type(name = "listing_type_enum"))]
-    pub struct ListingTypeEnum;
+  #[derive(diesel::sql_types::SqlType)]
+  #[diesel(postgres_type(name = "listing_type_enum"))]
+  pub struct ListingTypeEnum;
 
-    #[derive(diesel::sql_types::SqlType)]
-    #[diesel(postgres_type(name = "registration_mode_enum"))]
-    pub struct RegistrationModeEnum;
+  #[derive(diesel::sql_types::SqlType)]
+  #[diesel(postgres_type(name = "registration_mode_enum"))]
+  pub struct RegistrationModeEnum;
 
-    #[derive(diesel::sql_types::SqlType)]
-    #[diesel(postgres_type(name = "sort_type_enum"))]
-    pub struct SortTypeEnum;
+  #[derive(diesel::sql_types::SqlType)]
+  #[diesel(postgres_type(name = "sort_type_enum"))]
+  pub struct SortTypeEnum;
 }
 
 diesel::table! {
@@ -324,6 +324,7 @@ diesel::table! {
         site_id -> Int4,
         site_setup -> Bool,
         enable_downvotes -> Bool,
+        enable_federated_downvotes -> Bool,
         enable_nsfw -> Bool,
         community_creation_admin_only -> Bool,
         require_email_verification -> Bool,
@@ -934,68 +935,68 @@ diesel::joinable!(site_language -> site (site_id));
 diesel::joinable!(tagline -> local_site (local_site_id));
 
 diesel::allow_tables_to_appear_in_same_query!(
-    admin_purge_comment,
-    admin_purge_community,
-    admin_purge_person,
-    admin_purge_post,
-    captcha_answer,
-    comment,
-    comment_aggregates,
-    comment_like,
-    comment_reply,
-    comment_report,
-    comment_saved,
-    community,
-    community_aggregates,
-    community_block,
-    community_follower,
-    community_language,
-    community_moderator,
-    community_person_ban,
-    custom_emoji,
-    custom_emoji_keyword,
-    email_verification,
-    federation_allowlist,
-    federation_blocklist,
-    instance,
-    language,
-    local_site,
-    local_site_rate_limit,
-    local_user,
-    local_user_language,
-    mod_add,
-    mod_add_community,
-    mod_ban,
-    mod_ban_from_community,
-    mod_feature_post,
-    mod_hide_community,
-    mod_lock_post,
-    mod_remove_comment,
-    mod_remove_community,
-    mod_remove_post,
-    mod_transfer_community,
-    password_reset_request,
-    person,
-    person_aggregates,
-    person_ban,
-    person_block,
-    person_follower,
-    person_mention,
-    person_post_aggregates,
-    post,
-    post_aggregates,
-    post_like,
-    post_read,
-    post_report,
-    post_saved,
-    private_message,
-    private_message_report,
-    received_activity,
-    registration_application,
-    secret,
-    sent_activity,
-    site,
-    site_aggregates,
-    site_language,
-    tagline,
+  admin_purge_comment,
+  admin_purge_community,
+  admin_purge_person,
+  admin_purge_post,
+  captcha_answer,
+  comment,
+  comment_aggregates,
+  comment_like,
+  comment_reply,
+  comment_report,
+  comment_saved,
+  community,
+  community_aggregates,
+  community_block,
+  community_follower,
+  community_language,
+  community_moderator,
+  community_person_ban,
+  custom_emoji,
+  custom_emoji_keyword,
+  email_verification,
+  federation_allowlist,
+  federation_blocklist,
+  instance,
+  language,
+  local_site,
+  local_site_rate_limit,
+  local_user,
+  local_user_language,
+  mod_add,
+  mod_add_community,
+  mod_ban,
+  mod_ban_from_community,
+  mod_feature_post,
+  mod_hide_community,
+  mod_lock_post,
+  mod_remove_comment,
+  mod_remove_community,
+  mod_remove_post,
+  mod_transfer_community,
+  password_reset_request,
+  person,
+  person_aggregates,
+  person_ban,
+  person_block,
+  person_follower,
+  person_mention,
+  person_post_aggregates,
+  post,
+  post_aggregates,
+  post_like,
+  post_read,
+  post_report,
+  post_saved,
+  private_message,
+  private_message_report,
+  received_activity,
+  registration_application,
+  secret,
+  sent_activity,
+  site,
+  site_aggregates,
+  site_language,
+  tagline,
 );
diff --git a/crates/db_schema/src/source/local_site.rs b/crates/db_schema/src/source/local_site.rs
index a57bf503..dfd7b606 100644
--- a/crates/db_schema/src/source/local_site.rs
+++ b/crates/db_schema/src/source/local_site.rs
@@ -2,8 +2,7 @@
 use crate::schema::local_site;
 use crate::{
   newtypes::{LocalSiteId, SiteId},
-  ListingType,
-  RegistrationMode,
+  ListingType, RegistrationMode,
 };
 use serde::{Deserialize, Serialize};
 use serde_with::skip_serializing_none;
@@ -25,6 +24,8 @@ pub struct LocalSite {
   pub site_setup: bool,
   /// Whether downvotes are enabled.
   pub enable_downvotes: bool,
+  /// Whether downvotes are allowed from federated sites.
+  pub enable_federated_downvotes: bool,
   /// Whether NSFW is enabled.
   pub enable_nsfw: bool,
   /// Whether only admins can create communities.
@@ -70,6 +71,7 @@ pub struct LocalSiteInsertForm {
   pub site_id: SiteId,
   pub site_setup: Option<bool>,
   pub enable_downvotes: Option<bool>,
+  pub enable_federated_downvotes: Option<bool>,
   pub enable_nsfw: Option<bool>,
   pub community_creation_admin_only: Option<bool>,
   pub require_email_verification: Option<bool>,
@@ -95,6 +97,7 @@ pub struct LocalSiteInsertForm {
 pub struct LocalSiteUpdateForm {
   pub site_setup: Option<bool>,
   pub enable_downvotes: Option<bool>,
+  pub enable_federated_downvotes: Option<bool>,
   pub enable_nsfw: Option<bool>,
   pub community_creation_admin_only: Option<bool>,
   pub require_email_verification: Option<bool>,
diff --git a/migrations/2023-07-25-113818_add_enable_federated_downvotes/down.sql b/migrations/2023-07-25-113818_add_enable_federated_downvotes/down.sql
new file mode 100644
index 00000000..8a668684
--- /dev/null
+++ b/migrations/2023-07-25-113818_add_enable_federated_downvotes/down.sql
@@ -0,0 +1 @@
+alter table local_site drop column enable_federated_downvotes;
diff --git a/migrations/2023-07-25-113818_add_enable_federated_downvotes/up.sql b/migrations/2023-07-25-113818_add_enable_federated_downvotes/up.sql
new file mode 100644
index 00000000..8a9d2d76
--- /dev/null
+++ b/migrations/2023-07-25-113818_add_enable_federated_downvotes/up.sql
@@ -0,0 +1,2 @@
+alter table local_site
+      add column enable_federated_downvotes boolean default true not null;