Write mod log for federated sticky/lock post actions (#2203)

This commit is contained in:
Nutomic 2022-04-11 23:03:31 +00:00 committed by GitHub
parent a7540bd59f
commit fed73a72c1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 63 additions and 9 deletions

View 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())
}
}

View 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,

View file

@ -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

View file

@ -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

View file

@ -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