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-07-14 15:17:06 +00:00
|
|
|
insert_received_activity,
|
2023-08-02 16:52:41 +00:00
|
|
|
objects::community::ApubCommunity,
|
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,
|
|
|
|
},
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
2023-08-02 16:52:41 +00:00
|
|
|
fetch::object_id::ObjectId,
|
2023-03-21 15:03:05 +00:00
|
|
|
kinds::{activity::UndoType, public},
|
|
|
|
traits::ActivityHandler,
|
|
|
|
};
|
2023-08-02 16:52:41 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2023-02-18 14:50:28 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
2023-09-09 16:25:03 +00:00
|
|
|
activity::ActivitySendTargets,
|
2023-02-18 14:50:28 +00:00
|
|
|
community::Community,
|
2023-08-02 16:52:41 +00:00
|
|
|
person::Person,
|
2023-02-18 14:50:28 +00:00
|
|
|
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-09-26 01:39:18 +00:00
|
|
|
verify_mod_action(&self.actor, &community, 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-08-08 09:41:41 +00:00
|
|
|
let form = PostUpdateForm {
|
|
|
|
locked: Some(true),
|
|
|
|
..Default::default()
|
|
|
|
};
|
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-07-14 15:17:06 +00:00
|
|
|
insert_received_activity(&self.id, context).await?;
|
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-09-26 01:39:18 +00:00
|
|
|
verify_mod_action(&self.actor, &community, 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-08-08 09:41:41 +00:00
|
|
|
let form = PostUpdateForm {
|
|
|
|
locked: Some(false),
|
|
|
|
..Default::default()
|
|
|
|
};
|
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-08-02 16:52:41 +00:00
|
|
|
pub(crate) async fn send_lock_post(
|
|
|
|
post: Post,
|
|
|
|
actor: Person,
|
|
|
|
locked: bool,
|
|
|
|
context: Data<LemmyContext>,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let community: ApubCommunity = Community::read(&mut context.pool(), post.community_id)
|
|
|
|
.await?
|
|
|
|
.into();
|
|
|
|
let id = generate_activity_id(
|
|
|
|
LockType::Lock,
|
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?;
|
|
|
|
let community_id = community.actor_id.inner().clone();
|
|
|
|
let lock = LockPage {
|
|
|
|
actor: actor.actor_id.clone().into(),
|
|
|
|
to: vec![public()],
|
|
|
|
object: ObjectId::from(post.ap_id),
|
|
|
|
cc: vec![community_id.clone()],
|
|
|
|
kind: LockType::Lock,
|
|
|
|
id,
|
|
|
|
audience: Some(community_id.into()),
|
|
|
|
};
|
|
|
|
let activity = if locked {
|
|
|
|
AnnouncableActivities::LockPost(lock)
|
|
|
|
} else {
|
2023-02-18 14:50:28 +00:00
|
|
|
let id = generate_activity_id(
|
2023-08-02 16:52:41 +00:00
|
|
|
UndoType::Undo,
|
2023-02-18 14:50:28 +00:00
|
|
|
&context.settings().get_protocol_and_hostname(),
|
|
|
|
)?;
|
2023-08-02 16:52:41 +00:00
|
|
|
let undo = UndoLockPage {
|
|
|
|
actor: lock.actor.clone(),
|
2023-02-18 14:50:28 +00:00
|
|
|
to: vec![public()],
|
2023-08-02 16:52:41 +00:00
|
|
|
cc: lock.cc.clone(),
|
|
|
|
kind: UndoType::Undo,
|
2023-02-18 14:50:28 +00:00
|
|
|
id,
|
2023-08-02 16:52:41 +00:00
|
|
|
audience: lock.audience.clone(),
|
|
|
|
object: lock,
|
2023-02-18 14:50:28 +00:00
|
|
|
};
|
2023-08-02 16:52:41 +00:00
|
|
|
AnnouncableActivities::UndoLockPost(undo)
|
|
|
|
};
|
2023-09-09 16:25:03 +00:00
|
|
|
send_activity_in_community(
|
|
|
|
activity,
|
|
|
|
&actor.into(),
|
|
|
|
&community,
|
|
|
|
ActivitySendTargets::empty(),
|
|
|
|
true,
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
2023-08-02 16:52:41 +00:00
|
|
|
Ok(())
|
2023-02-18 14:50:28 +00:00
|
|
|
}
|