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