3 community::send_activity_in_community,
7 verify_person_in_community,
9 activity_lists::AnnouncableActivities,
10 insert_received_activity,
11 objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
13 activities::community::{collection_add::CollectionAdd, collection_remove::CollectionRemove},
17 use activitypub_federation::{
19 fetch::object_id::ObjectId,
20 kinds::{activity::AddType, public},
21 traits::{ActivityHandler, Actor},
23 use lemmy_api_common::{
24 context::LemmyContext,
25 utils::{generate_featured_url, generate_moderators_url},
27 use lemmy_db_schema::{
28 impls::community::CollectionType,
29 newtypes::{CommunityId, PersonId},
31 community::{Community, CommunityModerator, CommunityModeratorForm},
32 moderator::{ModAddCommunity, ModAddCommunityForm},
34 post::{Post, PostUpdateForm},
36 traits::{Crud, Joinable},
38 use lemmy_utils::error::LemmyError;
42 #[tracing::instrument(skip_all)]
43 pub async fn send_add_mod(
44 community: &ApubCommunity,
45 added_mod: &ApubPerson,
47 context: &Data<LemmyContext>,
48 ) -> Result<(), LemmyError> {
49 let id = generate_activity_id(
51 &context.settings().get_protocol_and_hostname(),
53 let add = CollectionAdd {
54 actor: actor.id().into(),
56 object: added_mod.id(),
57 target: generate_moderators_url(&community.actor_id)?.into(),
58 cc: vec![community.id()],
61 audience: Some(community.id().into()),
64 let activity = AnnouncableActivities::CollectionAdd(add);
65 let inboxes = vec![added_mod.shared_inbox_or_inbox()];
66 send_activity_in_community(activity, actor, community, inboxes, true, context).await
69 pub async fn send_add_featured_post(
70 community: &ApubCommunity,
71 featured_post: &ApubPost,
73 context: &Data<LemmyContext>,
74 ) -> Result<(), LemmyError> {
75 let id = generate_activity_id(
77 &context.settings().get_protocol_and_hostname(),
79 let add = CollectionAdd {
80 actor: actor.id().into(),
82 object: featured_post.ap_id.clone().into(),
83 target: generate_featured_url(&community.actor_id)?.into(),
84 cc: vec![community.id()],
87 audience: Some(community.id().into()),
89 let activity = AnnouncableActivities::CollectionAdd(add);
90 send_activity_in_community(activity, actor, community, vec![], true, context).await
94 #[async_trait::async_trait]
95 impl ActivityHandler for CollectionAdd {
96 type DataType = LemmyContext;
97 type Error = LemmyError;
99 fn id(&self) -> &Url {
103 fn actor(&self) -> &Url {
107 #[tracing::instrument(skip_all)]
108 async fn verify(&self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
109 insert_received_activity(&self.id, context).await?;
110 verify_is_public(&self.to, &self.cc)?;
111 let community = self.community(context).await?;
112 verify_person_in_community(&self.actor, &community, context).await?;
113 verify_mod_action(&self.actor, &self.object, community.id, context).await?;
117 #[tracing::instrument(skip_all)]
118 async fn receive(self, context: &Data<Self::DataType>) -> Result<(), LemmyError> {
119 let (community, collection_type) =
120 Community::get_by_collection_url(&mut context.pool(), &self.target.into()).await?;
121 match collection_type {
122 CollectionType::Moderators => {
123 let new_mod = ObjectId::<ApubPerson>::from(self.object)
124 .dereference(context)
127 // If we had to refetch the community while parsing the activity, then the new mod has already
128 // been added. Skip it here as it would result in a duplicate key error.
129 let new_mod_id = new_mod.id;
130 let moderated_communities =
131 CommunityModerator::get_person_moderated_communities(&mut context.pool(), new_mod_id)
133 if !moderated_communities.contains(&community.id) {
134 let form = CommunityModeratorForm {
135 community_id: community.id,
136 person_id: new_mod.id,
138 CommunityModerator::join(&mut context.pool(), &form).await?;
141 let actor = self.actor.dereference(context).await?;
142 let form = ModAddCommunityForm {
143 mod_person_id: actor.id,
144 other_person_id: new_mod.id,
145 community_id: community.id,
146 removed: Some(false),
148 ModAddCommunity::create(&mut context.pool(), &form).await?;
150 // TODO: send websocket notification about added mod
152 CollectionType::Featured => {
153 let post = ObjectId::<ApubPost>::from(self.object)
154 .dereference(context)
156 let form = PostUpdateForm::builder()
157 .featured_community(Some(true))
159 Post::update(&mut context.pool(), post.id, &form).await?;
166 pub(crate) async fn send_add_mod_to_community(
168 community_id: CommunityId,
169 updated_mod_id: PersonId,
171 context: Data<LemmyContext>,
172 ) -> Result<(), LemmyError> {
173 let actor: ApubPerson = actor.into();
174 let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
177 let updated_mod: ApubPerson = Person::read(&mut context.pool(), updated_mod_id)
181 CollectionAdd::send_add_mod(&community, &updated_mod, &actor, &context).await
183 CollectionRemove::send_remove_mod(&community, &updated_mod, &actor, &context).await
187 pub(crate) async fn send_feature_post(
191 context: Data<LemmyContext>,
192 ) -> Result<(), LemmyError> {
193 let actor: ApubPerson = actor.into();
194 let post: ApubPost = post.into();
195 let community = Community::read(&mut context.pool(), post.community_id)
199 CollectionAdd::send_add_featured_post(&community, &post, &actor, &context).await
201 CollectionRemove::send_remove_featured_post(&community, &post, &actor, &context).await