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