1 use crate::activities::{
3 verify_add_remove_moderator_target,
5 verify_person_in_community,
7 use activitystreams::{activity::kind::AddType, base::AnyBase};
8 use lemmy_api_common::blocking;
10 fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
13 use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler, PublicUrl};
14 use lemmy_db_queries::{source::community::CommunityModerator_, Joinable};
15 use lemmy_db_schema::source::community::{CommunityModerator, CommunityModeratorForm};
16 use lemmy_utils::LemmyError;
17 use lemmy_websocket::LemmyContext;
20 #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
21 #[serde(rename_all = "camelCase")]
27 #[serde(rename = "type")]
30 common: ActivityCommonFields,
33 #[async_trait::async_trait(?Send)]
34 impl ActivityHandler for AddMod {
37 context: &LemmyContext,
38 request_counter: &mut i32,
39 ) -> Result<(), LemmyError> {
40 verify_activity(self.common())?;
41 verify_person_in_community(&self.common.actor, &self.cc, context, request_counter).await?;
42 verify_mod_action(&self.common.actor, self.cc[0].clone(), context).await?;
43 verify_add_remove_moderator_target(&self.target, self.cc[0].clone())?;
49 context: &LemmyContext,
50 request_counter: &mut i32,
51 ) -> Result<(), LemmyError> {
53 get_or_fetch_and_upsert_community(&self.cc[0], context, request_counter).await?;
54 let new_mod = get_or_fetch_and_upsert_person(&self.object, context, request_counter).await?;
56 // If we had to refetch the community while parsing the activity, then the new mod has already
57 // been added. Skip it here as it would result in a duplicate key error.
58 let new_mod_id = new_mod.id;
59 let moderated_communities = blocking(context.pool(), move |conn| {
60 CommunityModerator::get_person_moderated_communities(conn, new_mod_id)
63 if !moderated_communities.contains(&community.id) {
64 let form = CommunityModeratorForm {
65 community_id: community.id,
66 person_id: new_mod.id,
68 blocking(context.pool(), move |conn| {
69 CommunityModerator::join(conn, &form)
74 let anybase = AnyBase::from_arbitrary_json(serde_json::to_string(self)?)?;
76 .send_announce(anybase, Some(self.object.clone()), context)
79 // TODO: send websocket notification about added mod
83 fn common(&self) -> &ActivityCommonFields {