-
-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathcollection_remove.rs
More file actions
147 lines (136 loc) · 4.52 KB
/
collection_remove.rs
File metadata and controls
147 lines (136 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use crate::{
activity_lists::AnnouncableActivities,
check_community_deleted_or_removed,
community::send_activity_in_community,
generate_activity_id,
protocol::community::collection_remove::CollectionRemove,
};
use activitypub_federation::{
config::Data,
fetch::object_id::ObjectId,
kinds::activity::RemoveType,
traits::{Activity, Actor, Object},
};
use lemmy_api_utils::{
context::LemmyContext,
notify::notify_mod_action,
utils::{generate_featured_url, generate_moderators_url},
};
use lemmy_apub_objects::{
objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
utils::{
functions::{generate_to, verify_mod_action, verify_visibility},
protocol::InCommunity,
},
};
use lemmy_db_schema::{
impls::community::CollectionType,
source::{
activity::ActivitySendTargets,
community::{Community, CommunityActions, CommunityModeratorForm},
modlog::{Modlog, ModlogInsertForm},
post::{Post, PostUpdateForm},
},
};
use lemmy_diesel_utils::traits::Crud;
use lemmy_utils::error::{LemmyError, LemmyResult};
use url::Url;
impl CollectionRemove {
pub(super) async fn send_remove_mod(
community: &ApubCommunity,
removed_mod: &ApubPerson,
actor: &ApubPerson,
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
let id = generate_activity_id(RemoveType::Remove, None, context)?;
let remove = CollectionRemove {
actor: actor.id().clone().into(),
to: generate_to(community)?,
object: removed_mod.id().clone(),
target: generate_moderators_url(&community.ap_id)?.into(),
id: id.clone(),
cc: vec![community.id().clone()],
kind: RemoveType::Remove,
audience: Some(community.ap_id.clone().into()),
};
let activity = AnnouncableActivities::CollectionRemove(remove);
let inboxes = ActivitySendTargets::to_inbox(removed_mod.shared_inbox_or_inbox());
send_activity_in_community(activity, actor, community, inboxes, true, context).await
}
pub(super) async fn send_remove_featured_post(
community: &ApubCommunity,
featured_post: &ApubPost,
actor: &ApubPerson,
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
let id = generate_activity_id(RemoveType::Remove, None, context)?;
let remove = CollectionRemove {
actor: actor.id().clone().into(),
to: generate_to(community)?,
object: featured_post.ap_id.clone().into(),
target: generate_featured_url(&community.ap_id)?.into(),
cc: vec![community.id().clone()],
kind: RemoveType::Remove,
id: id.clone(),
audience: Some(community.ap_id.clone().into()),
};
let activity = AnnouncableActivities::CollectionRemove(remove);
send_activity_in_community(
activity,
actor,
community,
ActivitySendTargets::empty(),
true,
context,
)
.await
}
}
#[async_trait::async_trait]
impl Activity for CollectionRemove {
type DataType = LemmyContext;
type Error = LemmyError;
fn id(&self) -> &Url {
&self.id
}
fn actor(&self) -> &Url {
self.actor.inner()
}
async fn verify(&self, context: &Data<Self::DataType>) -> LemmyResult<()> {
let community = self.community(context).await?;
verify_visibility(&self.to, &self.cc, &community)?;
verify_mod_action(&self.actor, &community, context).await?;
check_community_deleted_or_removed(&community)?;
Ok(())
}
async fn receive(self, context: &Data<Self::DataType>) -> LemmyResult<()> {
let (community, collection_type) =
Community::get_by_collection_url(&mut context.pool(), &self.target.into()).await?;
match collection_type {
CollectionType::Moderators => {
let remove_mod = ObjectId::<ApubPerson>::from(self.object)
.dereference(context)
.await?;
let form = CommunityModeratorForm::new(community.id, remove_mod.id);
CommunityActions::leave(&mut context.pool(), &form).await?;
// write mod log
let actor = self.actor.dereference(context).await?;
let form =
ModlogInsertForm::mod_add_to_community(actor.id, community.id, remove_mod.id, true);
let action = Modlog::create(&mut context.pool(), &[form]).await?;
notify_mod_action(action, context);
}
CollectionType::Featured => {
let post = ObjectId::<ApubPost>::from(self.object)
.dereference(context)
.await?;
let form = PostUpdateForm {
featured_community: Some(false),
..Default::default()
};
Post::update(&mut context.pool(), post.id, &form).await?;
}
}
Ok(())
}
}