From 8df0f7a0e133129a20d7f5b46c1c112a0a1d297e Mon Sep 17 00:00:00 2001 From: Anon Date: Fri, 25 Nov 2022 13:52:55 -0600 Subject: [PATCH] Comment fixes --- crates/api/src/post/feature.rs | 5 ++-- crates/api_common/src/post.rs | 3 +- .../src/activities/create_or_update/post.rs | 4 +-- crates/apub/src/objects/post.rs | 13 ++++----- crates/apub/src/protocol/objects/page.rs | 5 ++-- crates/db_schema/src/impls/post.rs | 28 +++++++++---------- crates/db_schema/src/lib.rs | 7 +++++ .../2022-11-20-032430_sticky_local/down.sql | 7 +++-- .../2022-11-20-032430_sticky_local/up.sql | 27 ++++++++++++++---- src/api_routes.rs | 2 +- 10 files changed, 61 insertions(+), 40 deletions(-) diff --git a/crates/api/src/post/feature.rs b/crates/api/src/post/feature.rs index fd4589dbaa..23902e36b3 100644 --- a/crates/api/src/post/feature.rs +++ b/crates/api/src/post/feature.rs @@ -20,6 +20,7 @@ use lemmy_db_schema::{ post::{Post, PostUpdateForm}, }, traits::Crud, + PostFeatureType, }; use lemmy_utils::{error::LemmyError, ConnectionId}; use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperation}; @@ -49,7 +50,7 @@ impl Perform for FeaturePost { .await?; check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?; - if data.is_community { + if data.feature_type == PostFeatureType::Community { // Verify that only the mods can feature in community is_mod_or_admin( context.pool(), @@ -65,7 +66,7 @@ impl Perform for FeaturePost { let post_id = data.post_id; let featured = data.featured; let new_post: PostUpdateForm; - if data.is_community { + if data.feature_type == PostFeatureType::Community { new_post = PostUpdateForm::builder() .featured_community(Some(featured)) .build(); diff --git a/crates/api_common/src/post.rs b/crates/api_common/src/post.rs index 710e023a81..82a1d66499 100644 --- a/crates/api_common/src/post.rs +++ b/crates/api_common/src/post.rs @@ -2,6 +2,7 @@ use crate::sensitive::Sensitive; use lemmy_db_schema::{ newtypes::{CommentId, CommunityId, DbUrl, LanguageId, PostId, PostReportId}, ListingType, + PostFeatureType, SortType, }; use lemmy_db_views::structs::{PostReportView, PostView}; @@ -109,7 +110,7 @@ pub struct LockPost { pub struct FeaturePost { pub post_id: PostId, pub featured: bool, - pub is_community: bool, + pub feature_type: PostFeatureType, pub auth: Sensitive, } diff --git a/crates/apub/src/activities/create_or_update/post.rs b/crates/apub/src/activities/create_or_update/post.rs index 9d2dd5aefe..0477a011bb 100644 --- a/crates/apub/src/activities/create_or_update/post.rs +++ b/crates/apub/src/activities/create_or_update/post.rs @@ -101,8 +101,8 @@ impl ActivityHandler for CreateOrUpdatePost { // However, when fetching a remote post we generate a new create activity with the current // locked/stickied value, so this check may fail. So only check if its a local community, // because then we will definitely receive all create and update activities separately. - let is_featured_or_locked = self.object.featured_community == Some(true) - || self.object.comments_enabled == Some(false); + let is_featured_or_locked = + self.object.stickied == Some(true) || self.object.comments_enabled == Some(false); if community.local && is_featured_or_locked { return Err(LemmyError::from_message( "New post cannot be stickied or locked", diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 77b7343460..5d5e54c744 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -113,11 +113,10 @@ impl ApubObject for ApubPost { image: self.thumbnail_url.clone().map(ImageObject::new), comments_enabled: Some(!self.locked), sensitive: Some(self.nsfw), + stickied: Some(self.featured_community), language, published: Some(convert_datetime(self.published)), updated: self.updated.map(convert_datetime), - featured_community: Some(self.featured_community), - featured_local: Some(self.featured_local), }; Ok(page) } @@ -213,8 +212,8 @@ impl ApubObject for ApubPost { ap_id: Some(page.id.clone().into()), local: Some(false), language_id, - featured_community: page.featured_community, - featured_local: page.featured_local, + featured_community: page.stickied, + featured_local: Some(false), } } else { // if is mod action, only update locked/stickied fields, nothing else @@ -224,8 +223,8 @@ impl ApubObject for ApubPost { .community_id(community.id) .ap_id(Some(page.id.clone().into())) .locked(page.comments_enabled.map(|e| !e)) - .featured_community(page.featured_community) - .featured_local(page.featured_local) + .featured_community(page.stickied) + .featured_local(Some(false)) .updated(page.updated.map(|u| u.naive_local())) .build() }; @@ -238,7 +237,7 @@ impl ApubObject for ApubPost { let post = Post::create(context.pool(), &form).await?; // write mod log entries for feature/lock - if Page::is_featured_changed(&old_post, &page.featured_community) { + if Page::is_featured_changed(&old_post, &page.stickied) { let form = ModFeaturePostForm { mod_person_id: creator.id, post_id: post.id, diff --git a/crates/apub/src/protocol/objects/page.rs b/crates/apub/src/protocol/objects/page.rs index 13ee6801ab..2064527c1c 100644 --- a/crates/apub/src/protocol/objects/page.rs +++ b/crates/apub/src/protocol/objects/page.rs @@ -63,11 +63,10 @@ pub struct Page { pub(crate) image: Option, pub(crate) comments_enabled: Option, pub(crate) sensitive: Option, + pub(crate) stickied: Option, pub(crate) published: Option>, pub(crate) updated: Option>, pub(crate) language: Option, - pub(crate) featured_community: Option, - pub(crate) featured_local: Option, } #[derive(Clone, Debug, Deserialize, Serialize)] @@ -139,7 +138,7 @@ impl Page { .dereference_local(context) .await; - let featured_changed = Page::is_featured_changed(&old_post, &self.featured_community); + let featured_changed = Page::is_featured_changed(&old_post, &self.stickied); let locked_changed = Page::is_locked_changed(&old_post, &self.comments_enabled); Ok(featured_changed || locked_changed) } diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index 922fe59240..2a52644021 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -1,21 +1,19 @@ use crate::{ newtypes::{CommunityId, DbUrl, PersonId, PostId}, - schema::post::{ - dsl::{ - ap_id, - body, - community_id, - creator_id, - deleted, - name, - post, - published, - removed, - thumbnail_url, - updated, - url, - }, + schema::post::dsl::{ + ap_id, + body, + community_id, + creator_id, + deleted, featured_community, + name, + post, + published, + removed, + thumbnail_url, + updated, + url, }, source::post::{ Post, diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index e0fb2aea86..819ecb5120 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -96,3 +96,10 @@ pub enum ModlogActionType { AdminPurgePost, AdminPurgeComment, } + +#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq)] +pub enum PostFeatureType { + #[default] + Site, + Community, +} diff --git a/migrations/2022-11-20-032430_sticky_local/down.sql b/migrations/2022-11-20-032430_sticky_local/down.sql index 7cd9a4f2d3..a72abd83a8 100644 --- a/migrations/2022-11-20-032430_sticky_local/down.sql +++ b/migrations/2022-11-20-032430_sticky_local/down.sql @@ -1,7 +1,8 @@ -DROP TRIGGER IF EXISTS post_aggregates_featured ON post; -drop function - post_aggregates_featured; +DROP TRIGGER IF EXISTS post_aggregates_featured_local ON post; +DROP TRIGGER IF EXISTS post_aggregates_featured_community ON post; +drop function post_aggregates_featured_community; +drop function post_aggregates_featured_local; alter table post ADD stickied boolean NOT NULL DEFAULT false; diff --git a/migrations/2022-11-20-032430_sticky_local/up.sql b/migrations/2022-11-20-032430_sticky_local/up.sql index ce10ef37bc..3cee7ab83b 100644 --- a/migrations/2022-11-20-032430_sticky_local/up.sql +++ b/migrations/2022-11-20-032430_sticky_local/up.sql @@ -22,21 +22,36 @@ rename column stickied TO featured; alter table mod_sticky_post Rename To mod_feature_post; -create function post_aggregates_featured() +create function post_aggregates_featured_community() returns trigger language plpgsql as $$ begin update post_aggregates pa - set featured_community = NEW.featured_community, - featured_local = NEW.featured_local + set featured_community = NEW.featured_community where pa.post_id = NEW.id; + return null; +end $$; +create function post_aggregates_featured_local() +returns trigger language plpgsql +as $$ +begin + update post_aggregates pa + set featured_local = NEW.featured_local + where pa.post_id = NEW.id; return null; end $$; -CREATE TRIGGER post_aggregates_featured +CREATE TRIGGER post_aggregates_featured_community + AFTER UPDATE + ON public.post + FOR EACH ROW + WHEN (old.featured_community IS DISTINCT FROM new.featured_community) + EXECUTE FUNCTION public.post_aggregates_featured_community(); + +CREATE TRIGGER post_aggregates_featured_local AFTER UPDATE ON public.post FOR EACH ROW - WHEN (old.featured_community IS DISTINCT FROM new.featured_community or old.featured_local IS DISTINCT FROM new.featured_local) - EXECUTE FUNCTION public.post_aggregates_featured(); \ No newline at end of file + WHEN (old.featured_local IS DISTINCT FROM new.featured_local) + EXECUTE FUNCTION public.post_aggregates_featured_local(); \ No newline at end of file diff --git a/src/api_routes.rs b/src/api_routes.rs index 327c70d168..d623161551 100644 --- a/src/api_routes.rs +++ b/src/api_routes.rs @@ -181,7 +181,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { web::post().to(route_post::), ) .route("/lock", web::post().to(route_post::)) - .route("/feature_post", web::post().to(route_post::)) + .route("/feature", web::post().to(route_post::)) .route("/list", web::get().to(route_get_crud::)) .route("/like", web::post().to(route_post::)) .route("/save", web::put().to(route_post::))