]> Untitled Git - lemmy.git/commitdiff
Write mod log for federated sticky/lock post actions (#2203)
authorNutomic <me@nutomic.com>
Mon, 11 Apr 2022 23:03:31 +0000 (23:03 +0000)
committerGitHub <noreply@github.com>
Mon, 11 Apr 2022 23:03:31 +0000 (23:03 +0000)
crates/apub/src/objects/post.rs
crates/apub/src/protocol/objects/page.rs
docker/dev/docker_update.sh
docker/dev/docker_update_volume_mount.sh
docker/federation/start-local-instances.bash

index 3d0725b7258884e16fa2b2410c852eff6a801926..1e59c339edd3cb84a976082cd74d7a37bb831a11 100644 (file)
@@ -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::<ApubPost>::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())
   }
 }
index 987a7d75019fd29b696bf464bd3c4377b1bb9585..8b66073081aa09859f7d37045197a95398753eb3 100644 (file)
@@ -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<E>(
+    old_post: &Result<ApubPost, E>,
+    new_stickied: &Option<bool>,
+  ) -> 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<E>(
+    old_post: &Result<ApubPost, E>,
+    new_comments_enabled: &Option<bool>,
+  ) -> 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,
index 9623d4dce76961614e4c98f813fff7e93b27b6cb..bfbff069e4950e81cd3a0c840f5c6d449f3b9026 100755 (executable)
@@ -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
index 7fbd212a5db6fca5fa1decf019aa86fdcc2b11d4..b31f3cb771d18575c3d3b864ecbc4803f65ab733 100755 (executable)
@@ -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
index ad261c6a19e923b1b9b2861567d5381e66c943d0..1c38c2daedede7c563d59696966c007bfd8ceb69 100755 (executable)
@@ -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