3 community::announce::AnnouncableActivities,
7 verify_person_in_community,
9 activity_queue::send_to_community_new,
10 extensions::context::lemmy_context,
11 fetcher::object_id::ObjectId,
12 objects::{community::Group, ToApub},
15 use activitystreams::{
16 activity::kind::UpdateType,
18 primitives::OneOrMany,
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},
28 use lemmy_utils::LemmyError;
29 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
30 use serde::{Deserialize, Serialize};
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>,
40 // TODO: would be nice to use a separate struct here, which only contains the fields updated here
42 cc: [ObjectId<Community>; 1],
43 #[serde(rename = "type")]
46 #[serde(rename = "@context")]
47 context: OneOrMany<AnyBase>,
52 impl UpdateCommunity {
54 community: &Community,
56 context: &LemmyContext,
57 ) -> Result<(), LemmyError> {
58 let id = generate_activity_id(UpdateType::Update)?;
59 let update = UpdateCommunity {
60 actor: ObjectId::new(actor.actor_id()),
61 to: [PublicUrl::Public],
62 object: community.to_apub(context.pool()).await?,
63 cc: [ObjectId::new(community.actor_id())],
64 kind: UpdateType::Update,
66 context: lemmy_context(),
67 unparsed: Default::default(),
70 let activity = AnnouncableActivities::UpdateCommunity(Box::new(update));
71 send_to_community_new(activity, &id, actor, community, vec![], context).await
75 #[async_trait::async_trait(?Send)]
76 impl ActivityHandler for UpdateCommunity {
79 context: &LemmyContext,
80 request_counter: &mut i32,
81 ) -> Result<(), LemmyError> {
82 verify_activity(self)?;
83 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
84 verify_mod_action(&self.actor, self.cc[0].clone(), context).await?;
90 context: &LemmyContext,
91 _request_counter: &mut i32,
92 ) -> Result<(), LemmyError> {
93 let cc = self.cc[0].clone().into();
94 let community = blocking(context.pool(), move |conn| {
95 Community::read_from_apub_id(conn, &cc)
99 let updated_community =
100 Group::from_apub_to_form(&self.object, &community.actor_id.clone().into()).await?;
101 let cf = CommunityForm {
102 name: updated_community.name,
103 title: updated_community.title,
104 description: updated_community.description,
105 nsfw: updated_community.nsfw,
106 // TODO: icon and banner would be hosted on the other instance, ideally we would copy it to ours
107 icon: updated_community.icon,
108 banner: updated_community.banner,
109 ..CommunityForm::default()
111 let updated_community = blocking(context.pool(), move |conn| {
112 Community::update(conn, community.id, &cf)
116 send_community_ws_message(
117 updated_community.id,
118 UserOperationCrud::EditCommunity,