From fed73a72c1ea8a79483cb292101daab19c8b46d8 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Mon, 11 Apr 2022 23:03:31 +0000 Subject: [PATCH] Write mod log for federated sticky/lock post actions (#2203) --- crates/apub/src/objects/post.rs | 35 +++++++++++++++++--- crates/apub/src/protocol/objects/page.rs | 33 +++++++++++++++--- docker/dev/docker_update.sh | 1 + docker/dev/docker_update_volume_mount.sh | 1 + docker/federation/start-local-instances.bash | 2 ++ 5 files changed, 63 insertions(+), 9 deletions(-) diff --git a/crates/apub/src/objects/post.rs b/crates/apub/src/objects/post.rs index 3d0725b7..1e59c339 100644 --- a/crates/apub/src/objects/post.rs +++ b/crates/apub/src/objects/post.rs @@ -24,6 +24,7 @@ use lemmy_db_schema::{ self, source::{ community::Community, + moderator::{ModLockPost, ModLockPostForm, ModStickyPost, ModStickyPostForm}, person::Person, post::{Post, PostForm}, }, @@ -161,8 +162,6 @@ impl ApubObject for ApubPost { .await?; let community = page.extract_community(context, request_counter).await?; - // TODO: write mod log if stickied or locked changed - let url = if let Some(attachment) = page.attachment.first() { Some(attachment.href.clone()) } else { @@ -177,9 +176,9 @@ impl ApubObject for ApubPost { let (embed_title, embed_description, embed_html) = metadata_res .map(|u| (u.title, u.description, u.html)) .unwrap_or((None, None, None)); - let body_slurs_removed = read_from_string_or_source_opt(&page.content, &page.source) .map(|s| remove_slurs(&s, &context.settings().slur_regex())); + let form = PostForm { name: page.name.clone(), url: url.map(Into::into), @@ -197,10 +196,38 @@ impl ApubObject for ApubPost { embed_description, embed_html, thumbnail_url: pictrs_thumbnail.map(|u| u.into()), - ap_id: Some(page.id.into()), + ap_id: Some(page.id.clone().into()), local: Some(false), }; + + // read existing, local post if any (for generating mod log) + let old_post = ObjectId::::new(page.id.clone()) + .dereference_local(context) + .await; + let post = blocking(context.pool(), move |conn| Post::upsert(conn, &form)).await??; + + // write mod log entries for sticky/lock + if Page::is_stickied_changed(&old_post, &page.stickied) { + let form = ModStickyPostForm { + mod_person_id: creator.id, + post_id: post.id, + stickied: Some(post.stickied), + }; + blocking(context.pool(), move |conn| { + ModStickyPost::create(conn, &form) + }) + .await??; + } + if Page::is_locked_changed(&old_post, &page.comments_enabled) { + let form = ModLockPostForm { + mod_person_id: creator.id, + post_id: post.id, + locked: Some(post.locked), + }; + blocking(context.pool(), move |conn| ModLockPost::create(conn, &form)).await??; + } + Ok(post.into()) } } diff --git a/crates/apub/src/protocol/objects/page.rs b/crates/apub/src/protocol/objects/page.rs index 987a7d75..8b660730 100644 --- a/crates/apub/src/protocol/objects/page.rs +++ b/crates/apub/src/protocol/objects/page.rs @@ -75,14 +75,37 @@ impl Page { .dereference_local(context) .await; - let is_mod_action = if let Ok(old_post) = old_post { - self.stickied != Some(old_post.stickied) || self.comments_enabled != Some(!old_post.locked) - } else { - false - }; + let is_mod_action = Page::is_stickied_changed(&old_post, &self.stickied) + || Page::is_locked_changed(&old_post, &self.comments_enabled); Ok(is_mod_action) } + pub(crate) fn is_stickied_changed( + old_post: &Result, + new_stickied: &Option, + ) -> bool { + if let Some(new_stickied) = new_stickied { + if let Ok(old_post) = old_post { + return new_stickied != &old_post.stickied; + } + } + + false + } + + pub(crate) fn is_locked_changed( + old_post: &Result, + new_comments_enabled: &Option, + ) -> bool { + if let Some(new_comments_enabled) = new_comments_enabled { + if let Ok(old_post) = old_post { + return new_comments_enabled != &!old_post.locked; + } + } + + false + } + pub(crate) async fn extract_community( &self, context: &LemmyContext, diff --git a/docker/dev/docker_update.sh b/docker/dev/docker_update.sh index 9623d4dc..bfbff069 100755 --- a/docker/dev/docker_update.sh +++ b/docker/dev/docker_update.sh @@ -7,6 +7,7 @@ set -e mkdir -p volumes/pictrs sudo chown -R 991:991 volumes/pictrs +sudo docker-compose down sudo docker build ../../ --file ../dev/Dockerfile -t lemmy-dev:latest sudo docker-compose pull --ignore-pull-failures || true sudo docker-compose up -d diff --git a/docker/dev/docker_update_volume_mount.sh b/docker/dev/docker_update_volume_mount.sh index 7fbd212a..b31f3cb7 100755 --- a/docker/dev/docker_update_volume_mount.sh +++ b/docker/dev/docker_update_volume_mount.sh @@ -7,6 +7,7 @@ set -e mkdir -p volumes/pictrs sudo chown -R 991:991 volumes/pictrs +sudo docker-compose down sudo docker build ../../ --file ../dev/volume_mount.dockerfile -t lemmy-dev:latest sudo docker-compose pull --ignore-pull-failures || true sudo docker-compose up diff --git a/docker/federation/start-local-instances.bash b/docker/federation/start-local-instances.bash index ad261c6a..1c38c2da 100755 --- a/docker/federation/start-local-instances.bash +++ b/docker/federation/start-local-instances.bash @@ -1,6 +1,8 @@ #!/bin/bash set -e +sudo docker-compose down + sudo docker build ../../ --file ../dev/volume_mount.dockerfile -t lemmy-federation:latest for Item in alpha beta gamma delta epsilon ; do -- 2.44.1