-
-
Notifications
You must be signed in to change notification settings - Fork 948
Expand file tree
/
Copy pathvote.rs
More file actions
95 lines (84 loc) · 3 KB
/
vote.rs
File metadata and controls
95 lines (84 loc) · 3 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
use crate::{
check_community_deleted_or_removed,
generate_activity_id,
protocol::voting::vote::{Vote, VoteType},
voting::{undo_vote_comment, undo_vote_post, vote_comment, vote_post},
};
use activitypub_federation::{
config::Data,
fetch::object_id::ObjectId,
traits::{Activity, Object},
};
use lemmy_api_utils::{context::LemmyContext, utils::check_bot_account};
use lemmy_apub_objects::{
objects::{PostOrComment, community::ApubCommunity, person::ApubPerson},
utils::{functions::verify_person_in_community, protocol::InCommunity},
};
use lemmy_db_schema_file::enums::FederationMode;
use lemmy_db_views_site::SiteView;
use lemmy_utils::error::{LemmyError, LemmyResult};
use url::Url;
impl Vote {
pub(in crate::voting) fn new(
object_id: ObjectId<PostOrComment>,
actor: &ApubPerson,
community: &ApubCommunity,
kind: VoteType,
context: &Data<LemmyContext>,
) -> LemmyResult<Vote> {
Ok(Vote {
actor: actor.id().clone().into(),
object: object_id,
kind: kind.clone(),
id: generate_activity_id(kind, None, context)?,
audience: Some(community.ap_id.clone().into()),
})
}
}
#[async_trait::async_trait]
impl Activity for Vote {
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<LemmyContext>) -> LemmyResult<()> {
let community = self.community(context).await?;
check_community_deleted_or_removed(&community)?;
verify_person_in_community(&self.actor, &community, context).await?;
Ok(())
}
async fn receive(self, context: &Data<LemmyContext>) -> LemmyResult<()> {
let actor = self.actor.dereference(context).await?;
let object = self.object.dereference(context).await?;
check_bot_account(&actor.0)?;
// Check for enabled federation votes
let local_site = SiteView::read_local(&mut context.pool())
.await
.map(|s| s.local_site)
.unwrap_or_default();
let (downvote_setting, upvote_setting) = match object {
PostOrComment::Left(_) => (local_site.post_downvotes, local_site.post_upvotes),
PostOrComment::Right(_) => (local_site.comment_downvotes, local_site.comment_upvotes),
};
// Don't allow dislikes for either disabled, or local only votes
let downvote_fail = self.kind == VoteType::Dislike && downvote_setting != FederationMode::All;
let upvote_fail = self.kind == VoteType::Like && upvote_setting != FederationMode::All;
if downvote_fail || upvote_fail {
// If this is a rejection, undo the vote
match object {
PostOrComment::Left(p) => undo_vote_post(actor, &p, context).await,
PostOrComment::Right(c) => undo_vote_comment(actor, &c, context).await,
}
} else {
// Otherwise apply the vote normally
match object {
PostOrComment::Left(p) => vote_post(&self.kind, actor, &p, context).await,
PostOrComment::Right(c) => vote_comment(&self.kind, actor, &c, context).await,
}
}
}
}