3 community::announce::AnnouncableActivities,
7 verify_person_in_community,
9 activity_queue::send_to_community_new,
10 extensions::context::lemmy_context,
11 objects::{community::Group, ToApub},
14 use activitystreams::{
15 activity::kind::UpdateType,
17 primitives::OneOrMany,
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},
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
29 use serde::{Deserialize, Serialize};
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 {
39 // TODO: would be nice to use a separate struct here, which only contains the fields updated here
42 #[serde(rename = "type")]
45 #[serde(rename = "@context")]
46 context: OneOrMany<AnyBase>,
51 impl UpdateCommunity {
53 community: &Community,
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,
65 context: lemmy_context(),
66 unparsed: Default::default(),
69 let activity = AnnouncableActivities::UpdateCommunity(Box::new(update));
70 send_to_community_new(activity, &id, actor, community, vec![], context).await
74 #[async_trait::async_trait(?Send)]
75 impl ActivityHandler for UpdateCommunity {
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?;
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)
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()
110 let updated_community = blocking(context.pool(), move |conn| {
111 Community::update(conn, community.id, &cf)
115 send_community_ws_message(
116 updated_community.id,
117 UserOperationCrud::EditCommunity,