3 community::send_activity_in_community,
7 verify_person_in_community,
9 activity_lists::AnnouncableActivities,
11 objects::{community::ApubCommunity, person::ApubPerson},
12 protocol::{activities::community::update::UpdateCommunity, InCommunity},
15 use activitypub_federation::{
17 kinds::{activity::UpdateType, public},
18 traits::{ActivityHandler, Actor, Object},
20 use lemmy_api_common::{
21 community::{CommunityResponse, EditCommunity, HideCommunity},
22 context::LemmyContext,
23 utils::get_local_user_view_from_jwt,
24 websocket::UserOperationCrud,
26 use lemmy_db_schema::{source::community::Community, traits::Crud};
27 use lemmy_utils::error::LemmyError;
30 #[async_trait::async_trait]
31 impl SendActivity for EditCommunity {
32 type Response = CommunityResponse;
34 async fn send_activity(
36 _response: &Self::Response,
37 context: &Data<LemmyContext>,
38 ) -> Result<(), LemmyError> {
40 get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
41 let community = Community::read(context.pool(), request.community_id).await?;
42 UpdateCommunity::send(community.into(), &local_user_view.person.into(), context).await
46 impl UpdateCommunity {
47 #[tracing::instrument(skip_all)]
49 community: ApubCommunity,
51 context: &Data<LemmyContext>,
52 ) -> Result<(), LemmyError> {
53 let id = generate_activity_id(
55 &context.settings().get_protocol_and_hostname(),
57 let update = UpdateCommunity {
58 actor: actor.id().into(),
60 object: Box::new(community.clone().into_json(context).await?),
61 cc: vec![community.id()],
62 kind: UpdateType::Update,
64 audience: Some(community.id().into()),
67 let activity = AnnouncableActivities::UpdateCommunity(update);
68 send_activity_in_community(activity, actor, &community, vec![], true, context).await
72 #[async_trait::async_trait]
73 impl ActivityHandler for UpdateCommunity {
74 type DataType = LemmyContext;
75 type Error = LemmyError;
77 fn id(&self) -> &Url {
81 fn actor(&self) -> &Url {
85 #[tracing::instrument(skip_all)]
86 async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
87 verify_is_public(&self.to, &self.cc)?;
88 let community = self.community(context).await?;
89 verify_person_in_community(&self.actor, &community, context).await?;
90 verify_mod_action(&self.actor, self.object.id.inner(), community.id, context).await?;
91 ApubCommunity::verify(&self.object, &community.actor_id.clone().into(), context).await?;
95 #[tracing::instrument(skip_all)]
96 async fn receive(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
97 insert_activity(&self.id, &self, false, false, context).await?;
98 let community = self.community(context).await?;
100 let community_update_form = self.object.into_update_form();
102 let updated_community =
103 Community::update(context.pool(), community.id, &community_update_form).await?;
106 .send_community_ws_message(
107 &UserOperationCrud::EditCommunity,
108 updated_community.id,
117 #[async_trait::async_trait]
118 impl SendActivity for HideCommunity {
119 type Response = CommunityResponse;
121 async fn send_activity(
123 _response: &Self::Response,
124 context: &Data<LemmyContext>,
125 ) -> Result<(), LemmyError> {
126 let local_user_view =
127 get_local_user_view_from_jwt(&request.auth, context.pool(), context.secret()).await?;
128 let community = Community::read(context.pool(), request.community_id).await?;
129 UpdateCommunity::send(community.into(), &local_user_view.person.into(), context).await