]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/update.rs
Rewrite delete activities (#1699)
[lemmy.git] / crates / apub / src / activities / community / update.rs
1 use crate::{
2   activities::{verify_activity, verify_mod_action, verify_person_in_community},
3   objects::community::Group,
4 };
5 use activitystreams::activity::kind::UpdateType;
6 use lemmy_api_common::blocking;
7 use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
8 use lemmy_db_queries::{ApubObject, Crud};
9 use lemmy_db_schema::source::community::{Community, CommunityForm};
10 use lemmy_utils::LemmyError;
11 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
12 use url::Url;
13
14 /// This activity is received from a remote community mod, and updates the description or other
15 /// fields of a local community.
16 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
17 #[serde(rename_all = "camelCase")]
18 pub struct UpdateCommunity {
19   to: PublicUrl,
20   object: Group,
21   cc: [Url; 1],
22   #[serde(rename = "type")]
23   kind: UpdateType,
24   #[serde(flatten)]
25   common: ActivityCommonFields,
26 }
27
28 #[async_trait::async_trait(?Send)]
29 impl ActivityHandler for UpdateCommunity {
30   async fn verify(
31     &self,
32     context: &LemmyContext,
33     request_counter: &mut i32,
34   ) -> Result<(), LemmyError> {
35     verify_activity(self.common())?;
36     verify_person_in_community(&self.common.actor, &self.cc[0], context, request_counter).await?;
37     verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await?;
38     Ok(())
39   }
40
41   async fn receive(
42     self,
43     context: &LemmyContext,
44     _request_counter: &mut i32,
45   ) -> Result<(), LemmyError> {
46     let cc = self.cc[0].clone().into();
47     let community = blocking(context.pool(), move |conn| {
48       Community::read_from_apub_id(conn, &cc)
49     })
50     .await??;
51
52     let updated_community =
53       Group::from_apub_to_form(&self.object, &community.actor_id.clone().into()).await?;
54     let cf = CommunityForm {
55       name: updated_community.name,
56       title: updated_community.title,
57       description: updated_community.description,
58       nsfw: updated_community.nsfw,
59       // TODO: icon and banner would be hosted on the other instance, ideally we would copy it to ours
60       icon: updated_community.icon,
61       banner: updated_community.banner,
62       ..CommunityForm::default()
63     };
64     let updated_community = blocking(context.pool(), move |conn| {
65       Community::update(conn, community.id, &cf)
66     })
67     .await??;
68
69     send_community_ws_message(
70       updated_community.id,
71       UserOperationCrud::EditCommunity,
72       None,
73       None,
74       context,
75     )
76     .await?;
77     Ok(())
78   }
79
80   fn common(&self) -> &ActivityCommonFields {
81     &self.common
82   }
83 }