4 announce::{AnnouncableActivities, GetCommunity},
11 verify_person_in_community,
13 context::lemmy_context,
14 fetcher::object_id::ObjectId,
16 community::{ApubCommunity, Group},
20 use activitystreams::{
21 activity::kind::UpdateType,
23 primitives::OneOrMany,
27 use lemmy_api_common::blocking;
30 traits::{ActivityFields, ActivityHandler, ActorType, ApubObject},
32 use lemmy_db_schema::{
33 source::community::{Community, CommunityForm},
36 use lemmy_utils::LemmyError;
37 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
38 use serde::{Deserialize, Serialize};
41 /// This activity is received from a remote community mod, and updates the description or other
42 /// fields of a local community.
43 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
44 #[serde(rename_all = "camelCase")]
45 pub struct UpdateCommunity {
46 actor: ObjectId<ApubPerson>,
48 // TODO: would be nice to use a separate struct here, which only contains the fields updated here
51 #[serde(rename = "type")]
54 #[serde(rename = "@context")]
55 context: OneOrMany<AnyBase>,
60 impl UpdateCommunity {
62 community: &ApubCommunity,
64 context: &LemmyContext,
65 ) -> Result<(), LemmyError> {
66 let id = generate_activity_id(
68 &context.settings().get_protocol_and_hostname(),
70 let update = UpdateCommunity {
71 actor: ObjectId::new(actor.actor_id()),
73 object: community.to_apub(context).await?,
74 cc: vec![community.actor_id()],
75 kind: UpdateType::Update,
77 context: lemmy_context(),
78 unparsed: Default::default(),
81 let activity = AnnouncableActivities::UpdateCommunity(Box::new(update));
82 send_to_community(activity, &id, actor, community, vec![], context).await
86 #[async_trait::async_trait(?Send)]
87 impl ActivityHandler for UpdateCommunity {
88 type DataType = LemmyContext;
91 context: &Data<LemmyContext>,
92 request_counter: &mut i32,
93 ) -> Result<(), LemmyError> {
94 verify_is_public(&self.to)?;
95 verify_activity(self, &context.settings())?;
96 let community = self.get_community(context, request_counter).await?;
97 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
98 verify_mod_action(&self.actor, &community, context, request_counter).await?;
104 context: &Data<LemmyContext>,
105 request_counter: &mut i32,
106 ) -> Result<(), LemmyError> {
107 let community = self.get_community(context, request_counter).await?;
109 let updated_community = Group::from_apub_to_form(
111 &community.actor_id.clone().into(),
115 let cf = CommunityForm {
116 name: updated_community.name,
117 title: updated_community.title,
118 description: updated_community.description,
119 nsfw: updated_community.nsfw,
120 // TODO: icon and banner would be hosted on the other instance, ideally we would copy it to ours
121 icon: updated_community.icon,
122 banner: updated_community.banner,
123 ..CommunityForm::default()
125 let updated_community = blocking(context.pool(), move |conn| {
126 Community::update(conn, community.id, &cf)
130 send_community_ws_message(
131 updated_community.id,
132 UserOperationCrud::EditCommunity,
142 #[async_trait::async_trait(?Send)]
143 impl GetCommunity for UpdateCommunity {
144 async fn get_community(
146 context: &LemmyContext,
147 request_counter: &mut i32,
148 ) -> Result<ApubCommunity, LemmyError> {
149 let cid = ObjectId::new(self.object.id.clone());
150 cid.dereference(context, request_counter).await