]> Untitled Git - lemmy.git/blob - crates/apub/src/activities/community/update.rs
Move object and collection structs to protocol folder
[lemmy.git] / crates / apub / src / activities / community / update.rs
1 use activitystreams::{activity::kind::UpdateType, public, unparsed::Unparsed};
2 use serde::{Deserialize, Serialize};
3 use url::Url;
4
5 use lemmy_api_common::blocking;
6 use lemmy_apub_lib::{
7   data::Data,
8   traits::{ActivityFields, ActivityHandler, ActorType, ApubObject},
9 };
10 use lemmy_db_schema::{
11   source::community::{Community, CommunityForm},
12   traits::Crud,
13 };
14 use lemmy_utils::LemmyError;
15 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
16
17 use crate::{
18   activities::{
19     community::{
20       announce::{AnnouncableActivities, GetCommunity},
21       send_to_community,
22     },
23     generate_activity_id,
24     verify_activity,
25     verify_is_public,
26     verify_mod_action,
27     verify_person_in_community,
28   },
29   fetcher::object_id::ObjectId,
30   objects::{community::ApubCommunity, person::ApubPerson},
31   protocol::objects::group::Group,
32 };
33
34 /// This activity is received from a remote community mod, and updates the description or other
35 /// fields of a local community.
36 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
37 #[serde(rename_all = "camelCase")]
38 pub struct UpdateCommunity {
39   actor: ObjectId<ApubPerson>,
40   to: Vec<Url>,
41   // TODO: would be nice to use a separate struct here, which only contains the fields updated here
42   object: Group,
43   cc: Vec<Url>,
44   #[serde(rename = "type")]
45   kind: UpdateType,
46   id: Url,
47   #[serde(flatten)]
48   unparsed: Unparsed,
49 }
50
51 impl UpdateCommunity {
52   pub async fn send(
53     community: &ApubCommunity,
54     actor: &ApubPerson,
55     context: &LemmyContext,
56   ) -> Result<(), LemmyError> {
57     let id = generate_activity_id(
58       UpdateType::Update,
59       &context.settings().get_protocol_and_hostname(),
60     )?;
61     let update = UpdateCommunity {
62       actor: ObjectId::new(actor.actor_id()),
63       to: vec![public()],
64       object: community.to_apub(context).await?,
65       cc: vec![community.actor_id()],
66       kind: UpdateType::Update,
67       id: id.clone(),
68       unparsed: Default::default(),
69     };
70
71     let activity = AnnouncableActivities::UpdateCommunity(Box::new(update));
72     send_to_community(activity, &id, actor, community, vec![], context).await
73   }
74 }
75
76 #[async_trait::async_trait(?Send)]
77 impl ActivityHandler for UpdateCommunity {
78   type DataType = LemmyContext;
79   async fn verify(
80     &self,
81     context: &Data<LemmyContext>,
82     request_counter: &mut i32,
83   ) -> Result<(), LemmyError> {
84     verify_is_public(&self.to)?;
85     verify_activity(self, &context.settings())?;
86     let community = self.get_community(context, request_counter).await?;
87     verify_person_in_community(&self.actor, &community, context, request_counter).await?;
88     verify_mod_action(&self.actor, &community, context, request_counter).await?;
89     Ok(())
90   }
91
92   async fn receive(
93     self,
94     context: &Data<LemmyContext>,
95     request_counter: &mut i32,
96   ) -> Result<(), LemmyError> {
97     let community = self.get_community(context, request_counter).await?;
98
99     let updated_community = Group::from_apub_to_form(
100       &self.object,
101       &community.actor_id.clone().into(),
102       &context.settings(),
103     )
104     .await?;
105     let cf = CommunityForm {
106       name: updated_community.name,
107       title: updated_community.title,
108       description: updated_community.description,
109       nsfw: updated_community.nsfw,
110       // TODO: icon and banner would be hosted on the other instance, ideally we would copy it to ours
111       icon: updated_community.icon,
112       banner: updated_community.banner,
113       ..CommunityForm::default()
114     };
115     let updated_community = blocking(context.pool(), move |conn| {
116       Community::update(conn, community.id, &cf)
117     })
118     .await??;
119
120     send_community_ws_message(
121       updated_community.id,
122       UserOperationCrud::EditCommunity,
123       None,
124       None,
125       context,
126     )
127     .await?;
128     Ok(())
129   }
130 }
131
132 #[async_trait::async_trait(?Send)]
133 impl GetCommunity for UpdateCommunity {
134   async fn get_community(
135     &self,
136     context: &LemmyContext,
137     request_counter: &mut i32,
138   ) -> Result<ApubCommunity, LemmyError> {
139     let cid = ObjectId::new(self.object.id.clone());
140     cid.dereference(context, request_counter).await
141   }
142 }