1 use activitystreams::{activity::kind::UpdateType, public, unparsed::Unparsed};
2 use serde::{Deserialize, Serialize};
5 use lemmy_api_common::blocking;
8 traits::{ActivityFields, ActivityHandler, ActorType, ApubObject},
10 use lemmy_db_schema::{
11 source::community::{Community, CommunityForm},
14 use lemmy_utils::LemmyError;
15 use lemmy_websocket::{send::send_community_ws_message, LemmyContext, UserOperationCrud};
20 announce::{AnnouncableActivities, GetCommunity},
27 verify_person_in_community,
29 fetcher::object_id::ObjectId,
30 objects::{community::ApubCommunity, person::ApubPerson},
31 protocol::objects::group::Group,
34 /// This activity is received from a remote community mod, and updates the description or other
35 /// fields of a local community.
36 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
37 #[serde(rename_all = "camelCase")]
38 pub struct UpdateCommunity {
39 actor: ObjectId<ApubPerson>,
41 // TODO: would be nice to use a separate struct here, which only contains the fields updated here
44 #[serde(rename = "type")]
51 impl UpdateCommunity {
53 community: &ApubCommunity,
55 context: &LemmyContext,
56 ) -> Result<(), LemmyError> {
57 let id = generate_activity_id(
59 &context.settings().get_protocol_and_hostname(),
61 let update = UpdateCommunity {
62 actor: ObjectId::new(actor.actor_id()),
64 object: community.to_apub(context).await?,
65 cc: vec![community.actor_id()],
66 kind: UpdateType::Update,
68 unparsed: Default::default(),
71 let activity = AnnouncableActivities::UpdateCommunity(Box::new(update));
72 send_to_community(activity, &id, actor, community, vec![], context).await
76 #[async_trait::async_trait(?Send)]
77 impl ActivityHandler for UpdateCommunity {
78 type DataType = LemmyContext;
81 context: &Data<LemmyContext>,
82 request_counter: &mut i32,
83 ) -> Result<(), LemmyError> {
84 verify_is_public(&self.to)?;
85 verify_activity(self, &context.settings())?;
86 let community = self.get_community(context, request_counter).await?;
87 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
88 verify_mod_action(&self.actor, &community, context, request_counter).await?;
94 context: &Data<LemmyContext>,
95 request_counter: &mut i32,
96 ) -> Result<(), LemmyError> {
97 let community = self.get_community(context, request_counter).await?;
99 let updated_community = Group::from_apub_to_form(
101 &community.actor_id.clone().into(),
105 let cf = CommunityForm {
106 name: updated_community.name,
107 title: updated_community.title,
108 description: updated_community.description,
109 nsfw: updated_community.nsfw,
110 // TODO: icon and banner would be hosted on the other instance, ideally we would copy it to ours
111 icon: updated_community.icon,
112 banner: updated_community.banner,
113 ..CommunityForm::default()
115 let updated_community = blocking(context.pool(), move |conn| {
116 Community::update(conn, community.id, &cf)
120 send_community_ws_message(
121 updated_community.id,
122 UserOperationCrud::EditCommunity,
132 #[async_trait::async_trait(?Send)]
133 impl GetCommunity for UpdateCommunity {
134 async fn get_community(
136 context: &LemmyContext,
137 request_counter: &mut i32,
138 ) -> Result<ApubCommunity, LemmyError> {
139 let cid = ObjectId::new(self.object.id.clone());
140 cid.dereference(context, request_counter).await