Skip to content

Commit

Permalink
Comment fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
makotech222 committed Nov 25, 2022
1 parent 6f9ec1b commit 8df0f7a
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 40 deletions.
5 changes: 3 additions & 2 deletions crates/api/src/post/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(),
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion crates/api_common/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<String>,
}

Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/activities/create_or_update/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 6 additions & 7 deletions crates/apub/src/objects/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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
Expand All @@ -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()
};
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions crates/apub/src/protocol/objects/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ pub struct Page {
pub(crate) image: Option<ImageObject>,
pub(crate) comments_enabled: Option<bool>,
pub(crate) sensitive: Option<bool>,
pub(crate) stickied: Option<bool>,
pub(crate) published: Option<DateTime<FixedOffset>>,
pub(crate) updated: Option<DateTime<FixedOffset>>,
pub(crate) language: Option<LanguageTag>,
pub(crate) featured_community: Option<bool>,
pub(crate) featured_local: Option<bool>,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
Expand Down Expand Up @@ -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)
}
Expand Down
28 changes: 13 additions & 15 deletions crates/db_schema/src/impls/post.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
7 changes: 7 additions & 0 deletions crates/db_schema/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
7 changes: 4 additions & 3 deletions migrations/2022-11-20-032430_sticky_local/down.sql
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
27 changes: 21 additions & 6 deletions migrations/2022-11-20-032430_sticky_local/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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();
WHEN (old.featured_local IS DISTINCT FROM new.featured_local)
EXECUTE FUNCTION public.post_aggregates_featured_local();
2 changes: 1 addition & 1 deletion src/api_routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) {
web::post().to(route_post::<MarkPostAsRead>),
)
.route("/lock", web::post().to(route_post::<LockPost>))
.route("/feature_post", web::post().to(route_post::<FeaturePost>))
.route("/feature", web::post().to(route_post::<FeaturePost>))
.route("/list", web::get().to(route_get_crud::<GetPosts>))
.route("/like", web::post().to(route_post::<CreatePostLike>))
.route("/save", web::put().to(route_post::<SavePost>))
Expand Down

0 comments on commit 8df0f7a

Please sign in to comment.