2023-02-18 14:50:28 +00:00
|
|
|
use crate::{
|
|
|
|
activities::{
|
|
|
|
check_community_deleted_or_removed,
|
|
|
|
community::send_activity_in_community,
|
|
|
|
generate_activity_id,
|
|
|
|
verify_is_public,
|
|
|
|
verify_mod_action,
|
|
|
|
verify_person_in_community,
|
|
|
|
},
|
|
|
|
activity_lists::AnnouncableActivities,
|
2023-03-21 15:03:05 +00:00
|
|
|
insert_activity,
|
2023-02-18 14:50:28 +00:00
|
|
|
protocol::{
|
2023-06-07 19:18:17 +00:00
|
|
|
activities::community::lock_page::{LockPage, LockType, UndoLockPage},
|
2023-02-18 14:50:28 +00:00
|
|
|
InCommunity,
|
|
|
|
},
|
|
|
|
SendActivity,
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
|
|
|
kinds::{activity::UndoType, public},
|
|
|
|
traits::ActivityHandler,
|
|
|
|
};
|
2023-02-18 14:50:28 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
context::LemmyContext,
|
|
|
|
post::{LockPost, PostResponse},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::local_user_view_from_jwt,
|
2023-02-18 14:50:28 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
community::Community,
|
|
|
|
post::{Post, PostUpdateForm},
|
|
|
|
},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
|
|
|
use lemmy_utils::error::LemmyError;
|
|
|
|
use url::Url;
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2023-02-18 14:50:28 +00:00
|
|
|
impl ActivityHandler for LockPage {
|
|
|
|
type DataType = LemmyContext;
|
|
|
|
type Error = LemmyError;
|
|
|
|
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
self.actor.inner()
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
2023-02-18 14:50:28 +00:00
|
|
|
verify_is_public(&self.to, &self.cc)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let community = self.community(context).await?;
|
|
|
|
verify_person_in_community(&self.actor, &community, context).await?;
|
2023-02-18 14:50:28 +00:00
|
|
|
check_community_deleted_or_removed(&community)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
verify_mod_action(&self.actor, self.object.inner(), community.id, context).await?;
|
2023-02-18 14:50:28 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn receive(self, context: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
2023-02-18 14:50:28 +00:00
|
|
|
let form = PostUpdateForm::builder().locked(Some(true)).build();
|
2023-03-21 15:03:05 +00:00
|
|
|
let post = self.object.dereference(context).await?;
|
2023-07-11 13:09:59 +00:00
|
|
|
Post::update(&mut context.pool(), post.id, &form).await?;
|
2023-02-18 14:50:28 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2023-02-18 14:50:28 +00:00
|
|
|
impl ActivityHandler for UndoLockPage {
|
|
|
|
type DataType = LemmyContext;
|
|
|
|
type Error = LemmyError;
|
|
|
|
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
self.actor.inner()
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
2023-02-18 14:50:28 +00:00
|
|
|
verify_is_public(&self.to, &self.cc)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let community = self.community(context).await?;
|
|
|
|
verify_person_in_community(&self.actor, &community, context).await?;
|
2023-02-18 14:50:28 +00:00
|
|
|
check_community_deleted_or_removed(&community)?;
|
|
|
|
verify_mod_action(
|
|
|
|
&self.actor,
|
|
|
|
self.object.object.inner(),
|
|
|
|
community.id,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn receive(self, context: &Data<Self::DataType>) -> Result<(), Self::Error> {
|
|
|
|
insert_activity(&self.id, &self, false, false, context).await?;
|
2023-02-18 14:50:28 +00:00
|
|
|
let form = PostUpdateForm::builder().locked(Some(false)).build();
|
2023-03-21 15:03:05 +00:00
|
|
|
let post = self.object.object.dereference(context).await?;
|
2023-07-11 13:09:59 +00:00
|
|
|
Post::update(&mut context.pool(), post.id, &form).await?;
|
2023-02-18 14:50:28 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2023-02-18 14:50:28 +00:00
|
|
|
impl SendActivity for LockPost {
|
|
|
|
type Response = PostResponse;
|
|
|
|
|
|
|
|
async fn send_activity(
|
|
|
|
request: &Self,
|
|
|
|
response: &Self::Response,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2023-02-18 14:50:28 +00:00
|
|
|
) -> Result<(), LemmyError> {
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&request.auth, context).await?;
|
2023-02-18 14:50:28 +00:00
|
|
|
let id = generate_activity_id(
|
|
|
|
LockType::Lock,
|
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let community_id = response.post_view.community.actor_id.clone();
|
|
|
|
let actor = local_user_view.person.actor_id.clone().into();
|
2023-02-18 14:50:28 +00:00
|
|
|
let lock = LockPage {
|
|
|
|
actor,
|
|
|
|
to: vec![public()],
|
2023-03-21 15:03:05 +00:00
|
|
|
object: response.post_view.post.ap_id.clone().into(),
|
|
|
|
cc: vec![community_id.clone().into()],
|
2023-02-18 14:50:28 +00:00
|
|
|
kind: LockType::Lock,
|
|
|
|
id,
|
2023-03-21 15:03:05 +00:00
|
|
|
audience: Some(community_id.into()),
|
2023-02-18 14:50:28 +00:00
|
|
|
};
|
|
|
|
let activity = if request.locked {
|
|
|
|
AnnouncableActivities::LockPost(lock)
|
|
|
|
} else {
|
|
|
|
let id = generate_activity_id(
|
|
|
|
UndoType::Undo,
|
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?;
|
|
|
|
let undo = UndoLockPage {
|
|
|
|
actor: lock.actor.clone(),
|
|
|
|
to: vec![public()],
|
|
|
|
cc: lock.cc.clone(),
|
|
|
|
kind: UndoType::Undo,
|
|
|
|
id,
|
|
|
|
audience: lock.audience.clone(),
|
|
|
|
object: lock,
|
|
|
|
};
|
|
|
|
AnnouncableActivities::UndoLockPost(undo)
|
|
|
|
};
|
2023-07-11 13:09:59 +00:00
|
|
|
let community = Community::read(&mut context.pool(), response.post_view.community.id).await?;
|
2023-02-18 14:50:28 +00:00
|
|
|
send_activity_in_community(
|
|
|
|
activity,
|
|
|
|
&local_user_view.person.into(),
|
|
|
|
&community.into(),
|
|
|
|
vec![],
|
|
|
|
true,
|
|
|
|
context,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|