-
-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathlock.rs
More file actions
201 lines (180 loc) · 5.91 KB
/
lock.rs
File metadata and controls
201 lines (180 loc) · 5.91 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
198
199
200
201
use crate::{
MOD_ACTION_DEFAULT_REASON,
activity_lists::AnnouncableActivities,
check_community_deleted_or_removed,
community::send_activity_in_community,
generate_activity_id,
post_or_comment_community,
protocol::community::lock::{LockPageOrNote, LockType, UndoLockPageOrNote},
};
use activitypub_federation::{
config::Data,
fetch::object_id::ObjectId,
kinds::activity::UndoType,
traits::Activity,
};
use lemmy_api_utils::{context::LemmyContext, notify::notify_mod_action};
use lemmy_apub_objects::{
objects::{PostOrComment, community::ApubCommunity},
utils::{
functions::{generate_to, verify_mod_action, verify_visibility},
protocol::InCommunity,
},
};
use lemmy_db_schema::source::{
activity::ActivitySendTargets,
comment::Comment,
modlog::{Modlog, ModlogInsertForm},
person::Person,
post::{Post, PostUpdateForm},
};
use lemmy_diesel_utils::traits::Crud;
use lemmy_utils::error::{LemmyError, LemmyResult};
use url::Url;
#[async_trait::async_trait]
impl Activity for LockPageOrNote {
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>) -> Result<(), Self::Error> {
let community = self.community(context).await?;
verify_visibility(&self.to, &self.cc, &community)?;
check_community_deleted_or_removed(&community)?;
verify_mod_action(&self.actor, &community, context).await?;
Ok(())
}
async fn receive(self, context: &Data<Self::DataType>) -> Result<(), Self::Error> {
let reason = self
.summary
.unwrap_or_else(|| MOD_ACTION_DEFAULT_REASON.to_string());
let actor = self.actor.dereference(context).await?;
match self.object.dereference(context).await? {
PostOrComment::Left(post) => {
let form = PostUpdateForm {
locked: Some(true),
..Default::default()
};
Post::update(&mut context.pool(), post.id, &form).await?;
let form = ModlogInsertForm::mod_lock_post(actor.id, &post, true, &reason);
let action = Modlog::create(&mut context.pool(), &[form]).await?;
notify_mod_action(action, context);
}
PostOrComment::Right(comment) => {
Comment::update_locked_for_comment_and_children(&mut context.pool(), &comment.path, true)
.await?;
let community_id = Post::read(&mut context.pool(), comment.post_id)
.await?
.community_id;
let form =
ModlogInsertForm::mod_lock_comment(actor.id, &comment, community_id, true, &reason);
let action = Modlog::create(&mut context.pool(), &[form]).await?;
notify_mod_action(action, context);
}
}
Ok(())
}
}
#[async_trait::async_trait]
impl Activity for UndoLockPageOrNote {
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>) -> Result<(), Self::Error> {
let community = self.object.community(context).await?;
verify_visibility(&self.to, &self.cc, &community)?;
check_community_deleted_or_removed(&community)?;
verify_mod_action(&self.actor, &community, context).await?;
Ok(())
}
async fn receive(self, context: &Data<Self::DataType>) -> Result<(), Self::Error> {
let reason = self
.summary
.unwrap_or_else(|| MOD_ACTION_DEFAULT_REASON.to_string());
let actor = self.actor.dereference(context).await?;
match self.object.object.dereference(context).await? {
PostOrComment::Left(post) => {
let form = PostUpdateForm {
locked: Some(false),
..Default::default()
};
Post::update(&mut context.pool(), post.id, &form).await?;
let form = ModlogInsertForm::mod_lock_post(actor.id, &post, false, &reason);
let action = Modlog::create(&mut context.pool(), &[form]).await?;
notify_mod_action(action, context);
}
PostOrComment::Right(comment) => {
Comment::update_locked_for_comment_and_children(&mut context.pool(), &comment.path, false)
.await?;
let community_id = Post::read(&mut context.pool(), comment.post_id)
.await?
.community_id;
let form =
ModlogInsertForm::mod_lock_comment(actor.id, &comment, community_id, false, &reason);
let action = Modlog::create(&mut context.pool(), &[form]).await?;
notify_mod_action(action, context);
}
}
Ok(())
}
}
pub(crate) async fn send_lock(
object: PostOrComment,
actor: Person,
locked: bool,
reason: String,
context: Data<LemmyContext>,
) -> LemmyResult<()> {
let community: ApubCommunity = post_or_comment_community(&object, &context).await?.into();
let id = generate_activity_id(LockType::Lock, None, &context)?;
let community_id = community.ap_id.inner().clone();
let ap_id = match object {
PostOrComment::Left(p) => p.ap_id.clone(),
PostOrComment::Right(c) => c.ap_id.clone(),
};
let lock = LockPageOrNote {
actor: actor.ap_id.clone().into(),
to: generate_to(&community)?,
object: ObjectId::from(ap_id),
cc: vec![community_id.clone()],
kind: LockType::Lock,
id,
summary: Some(reason.clone()),
audience: Some(community.ap_id.clone().into()),
};
let activity = if locked {
AnnouncableActivities::Lock(lock)
} else {
let id = generate_activity_id(UndoType::Undo, None, &context)?;
let undo = UndoLockPageOrNote {
actor: lock.actor.clone(),
to: generate_to(&community)?,
cc: lock.cc.clone(),
kind: UndoType::Undo,
id,
object: lock,
summary: Some(reason),
audience: Some(community.ap_id.clone().into()),
};
AnnouncableActivities::UndoLock(undo)
};
send_activity_in_community(
activity,
&actor.into(),
&community,
ActivitySendTargets::empty(),
true,
&context,
)
.await?;
Ok(())
}