-
-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathcollection_add.rs
More file actions
197 lines (184 loc) · 6.13 KB
/
collection_add.rs
File metadata and controls
197 lines (184 loc) · 6.13 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::{
activity_lists::AnnouncableActivities,
check_community_deleted_or_removed,
community::send_activity_in_community,
generate_activity_id,
protocol::community::{collection_add::CollectionAdd, collection_remove::CollectionRemove},
};
use activitypub_federation::{
config::Data,
fetch::object_id::ObjectId,
kinds::activity::AddType,
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,
newtypes::CommunityId,
source::{
activity::ActivitySendTargets,
community::{Community, CommunityActions, CommunityModeratorForm},
modlog::{Modlog, ModlogInsertForm},
person::Person,
post::{Post, PostUpdateForm},
},
};
use lemmy_db_schema_file::PersonId;
use lemmy_diesel_utils::traits::Crud;
use lemmy_utils::error::{LemmyError, LemmyResult};
use url::Url;
impl CollectionAdd {
async fn send_add_mod(
community: &ApubCommunity,
added_mod: &ApubPerson,
actor: &ApubPerson,
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
let id = generate_activity_id(AddType::Add, None, context)?;
let add = CollectionAdd {
actor: actor.id().clone().into(),
to: generate_to(community)?,
object: added_mod.id().clone(),
target: generate_moderators_url(&community.ap_id)?.into(),
cc: vec![community.id().clone()],
kind: AddType::Add,
id: id.clone(),
audience: Some(community.ap_id.clone().into()),
};
let activity = AnnouncableActivities::CollectionAdd(add);
let inboxes = ActivitySendTargets::to_inbox(added_mod.shared_inbox_or_inbox());
send_activity_in_community(activity, actor, community, inboxes, true, context).await
}
async fn send_add_featured_post(
community: &ApubCommunity,
featured_post: &ApubPost,
actor: &ApubPerson,
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
let id = generate_activity_id(AddType::Add, None, context)?;
let add = CollectionAdd {
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: AddType::Add,
id: id.clone(),
audience: Some(community.ap_id.clone().into()),
};
let activity = AnnouncableActivities::CollectionAdd(add);
send_activity_in_community(
activity,
actor,
community,
ActivitySendTargets::empty(),
true,
context,
)
.await
}
}
#[async_trait::async_trait]
impl Activity for CollectionAdd {
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.clone().into()).await?;
match collection_type {
CollectionType::Moderators => {
let new_mod = ObjectId::<ApubPerson>::from(self.object)
.dereference(context)
.await?;
// If we had to refetch the community while parsing the activity, then the new mod has
// already been added. Skip it here as it would result in a duplicate key error.
let new_mod_id = new_mod.id;
let moderated_communities =
CommunityActions::get_person_moderated_communities(&mut context.pool(), new_mod_id)
.await?;
if !moderated_communities.contains(&community.id) {
let form = CommunityModeratorForm::new(community.id, new_mod.id);
CommunityActions::join(&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, new_mod.id, false);
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(true),
..Default::default()
};
Post::update(&mut context.pool(), post.id, &form).await?;
}
}
Ok(())
}
}
pub(crate) async fn send_add_mod_to_community(
actor: Person,
community_id: CommunityId,
updated_mod_id: PersonId,
added: bool,
context: Data<LemmyContext>,
) -> LemmyResult<()> {
let actor: ApubPerson = actor.into();
let community: ApubCommunity = Community::read(&mut context.pool(), community_id)
.await?
.into();
let updated_mod: ApubPerson = Person::read(&mut context.pool(), updated_mod_id)
.await?
.into();
if added {
CollectionAdd::send_add_mod(&community, &updated_mod, &actor, &context).await
} else {
CollectionRemove::send_remove_mod(&community, &updated_mod, &actor, &context).await
}
}
pub(crate) async fn send_feature_post(
post: Post,
actor: Person,
featured: bool,
context: Data<LemmyContext>,
) -> LemmyResult<()> {
let actor: ApubPerson = actor.into();
let post: ApubPost = post.into();
let community = Community::read(&mut context.pool(), post.community_id)
.await?
.into();
if featured {
CollectionAdd::send_add_featured_post(&community, &post, &actor, &context).await
} else {
CollectionRemove::send_remove_featured_post(&community, &post, &actor, &context).await
}
}