Skip to content

Commit

Permalink
Remove backwards compatibility code for feature/lock post
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Mar 21, 2023
1 parent 849069b commit 4ab7d27
Show file tree
Hide file tree
Showing 10 changed files with 15 additions and 76 deletions.
1 change: 0 additions & 1 deletion crates/api_common/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ fn html_to_site_metadata(html_bytes: &[u8]) -> Result<SiteMetadata, LemmyError>
let first_line = html
.trim_start()
.lines()
.into_iter()
.next()
.ok_or_else(|| LemmyError::from_message("No lines in html"))?
.to_lowercase();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
],
"commentsEnabled": true,
"sensitive": false,
"stickied": false,
"language": {
"identifier": "ko",
"name": "한국어"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
],
"commentsEnabled": true,
"sensitive": false,
"stickied": false,
"published": "2021-10-29T15:10:51.557399+00:00",
"updated": "2021-10-29T15:11:35.976374+00:00"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"attachment": [],
"commentsEnabled": true,
"sensitive": false,
"stickied": true,
"published": "2023-02-06T06:42:41.939437+00:00",
"language": {
"identifier": "de",
Expand All @@ -39,7 +38,6 @@
"attachment": [],
"commentsEnabled": true,
"sensitive": false,
"stickied": true,
"published": "2023-02-06T06:42:37.119567+00:00",
"language": {
"identifier": "de",
Expand Down
1 change: 0 additions & 1 deletion crates/apub/assets/lemmy/objects/page.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
},
"sensitive": false,
"commentsEnabled": true,
"stickied": true,
"language": {
"identifier": "fr",
"name": "Français"
Expand Down
14 changes: 1 addition & 13 deletions crates/apub/src/activities/community/collection_add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ use crate::{
local_instance,
objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::{
activities::{
community::{collection_add::CollectionAdd, collection_remove::CollectionRemove},
create_or_update::page::CreateOrUpdatePage,
CreateOrUpdateType,
},
activities::community::{collection_add::CollectionAdd, collection_remove::CollectionRemove},
InCommunity,
},
ActorType,
Expand Down Expand Up @@ -234,14 +230,6 @@ impl SendActivity for FeaturePost {
) -> Result<(), LemmyError> {
let local_user_view =
get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
// Deprecated, for backwards compatibility with 0.17
CreateOrUpdatePage::send(
&response.post_view.post,
local_user_view.person.id,
CreateOrUpdateType::Update,
context,
)
.await?;
let community = Community::read(context.pool(), response.post_view.community.id)
.await?
.into();
Expand Down
14 changes: 1 addition & 13 deletions crates/apub/src/activities/community/lock_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ use crate::{
activity_lists::AnnouncableActivities,
local_instance,
protocol::{
activities::{
community::lock_page::{LockPage, LockType, UndoLockPage},
create_or_update::page::CreateOrUpdatePage,
CreateOrUpdateType,
},
activities::community::lock_page::{LockPage, LockType, UndoLockPage},
InCommunity,
},
SendActivity,
Expand Down Expand Up @@ -144,14 +140,6 @@ impl SendActivity for LockPost {
) -> Result<(), LemmyError> {
let local_user_view =
get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
// For backwards compat with 0.17
CreateOrUpdatePage::send(
&response.post_view.post,
local_user_view.person.id,
CreateOrUpdateType::Update,
context,
)
.await?;
let id = generate_activity_id(
LockType::Lock,
&context.settings().get_protocol_and_hostname(),
Expand Down
13 changes: 5 additions & 8 deletions crates/apub/src/activities/create_or_update/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,13 @@ impl ActivityHandler for CreateOrUpdatePage {
CreateOrUpdateType::Create => {
verify_domains_match(self.actor.inner(), self.object.id.inner())?;
verify_urls_match(self.actor.inner(), self.object.creator()?.inner())?;
// Check that the post isnt locked or stickied, as that isnt possible for newly created posts.
// Check that the post isnt locked, as that isnt possible for newly created posts.
// 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,
// locked 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.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",
));
let is_locked = self.object.comments_enabled == Some(false);
if community.local && is_locked {
return Err(LemmyError::from_message("New post cannot be locked"));
}
}
CreateOrUpdateType::Update => {
Expand Down
19 changes: 4 additions & 15 deletions crates/apub/src/objects/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use lemmy_db_schema::{
source::{
community::Community,
local_site::LocalSite,
moderator::{ModFeaturePost, ModFeaturePostForm, ModLockPost, ModLockPostForm},
moderator::{ModLockPost, ModLockPostForm},
person::Person,
post::{Post, PostInsertForm, PostUpdateForm},
},
Expand Down Expand Up @@ -124,7 +124,6 @@ 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),
Expand Down Expand Up @@ -241,7 +240,7 @@ impl ApubObject for ApubPost {
ap_id: Some(page.id.clone().into()),
local: Some(false),
language_id,
featured_community: page.stickied,
featured_community: None,
featured_local: None,
}
} else {
Expand All @@ -252,7 +251,6 @@ 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.stickied)
.updated(page.updated.map(|u| u.naive_local()))
.build()
};
Expand All @@ -263,16 +261,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.stickied) {
let form = ModFeaturePostForm {
mod_person_id: creator.id,
post_id: post.id,
featured: post.featured_community,
is_featured_community: true,
};
ModFeaturePost::create(context.pool(), &form).await?;
}
// write mod log entry for lock
if Page::is_locked_changed(&old_post, &page.comments_enabled) {
let form = ModLockPostForm {
mod_person_id: creator.id,
Expand Down Expand Up @@ -323,7 +312,7 @@ mod tests {
assert!(post.body.is_some());
assert_eq!(post.body.as_ref().unwrap().len(), 45);
assert!(!post.locked);
assert!(post.featured_community);
assert!(!post.featured_community);
assert_eq!(request_counter, 0);

Post::delete(context.pool(), post.id).await.unwrap();
Expand Down
25 changes: 4 additions & 21 deletions crates/apub/src/protocol/objects/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@ pub struct Page {
pub(crate) image: Option<ImageObject>,
pub(crate) comments_enabled: Option<bool>,
pub(crate) sensitive: Option<bool>,
/// Deprecated, for compatibility with Lemmy 0.17
pub(crate) stickied: Option<bool>,
pub(crate) published: Option<DateTime<FixedOffset>>,
pub(crate) updated: Option<DateTime<FixedOffset>>,
pub(crate) language: Option<LanguageTag>,
Expand Down Expand Up @@ -132,31 +130,16 @@ pub(crate) struct AttributedToPeertube {
}

impl Page {
/// Only mods can change the post's stickied/locked status. So if either of these is changed from
/// the current value, it is a mod action and needs to be verified as such.
/// Only mods can change the post's locked status. So if it is changed from the default value,
/// it is a mod action and needs to be verified as such.
///
/// Both stickied and locked need to be false on a newly created post (verified in [[CreatePost]].
/// Locked needs to be false on a newly created post (verified in [[CreatePost]].
pub(crate) async fn is_mod_action(&self, context: &LemmyContext) -> Result<bool, LemmyError> {
let old_post = ObjectId::<ApubPost>::new(self.id.clone())
.dereference_local(context)
.await;

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

pub(crate) fn is_featured_changed<E>(
old_post: &Result<ApubPost, E>,
new_featured_community: &Option<bool>,
) -> bool {
if let Some(new_featured_community) = new_featured_community {
if let Ok(old_post) = old_post {
return new_featured_community != &old_post.featured_community;
}
}

false
Ok(Page::is_locked_changed(&old_post, &self.comments_enabled))
}

pub(crate) fn is_locked_changed<E>(
Expand Down

0 comments on commit 4ab7d27

Please sign in to comment.