2021-10-29 10:32:42 +00:00
|
|
|
use crate::{
|
|
|
|
activities::{
|
2023-09-23 01:39:03 +00:00
|
|
|
generate_activity_id, verify_person_in_community,
|
2021-10-29 10:32:42 +00:00
|
|
|
voting::{vote_comment, vote_post},
|
|
|
|
},
|
2023-07-14 15:17:06 +00:00
|
|
|
insert_received_activity,
|
2021-10-29 10:32:42 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
2022-12-01 20:52:49 +00:00
|
|
|
protocol::{
|
|
|
|
activities::voting::vote::{Vote, VoteType},
|
|
|
|
InCommunity,
|
|
|
|
},
|
2021-10-29 10:32:42 +00:00
|
|
|
PostOrComment,
|
|
|
|
};
|
2023-03-21 15:03:05 +00:00
|
|
|
use activitypub_federation::{
|
|
|
|
config::Data,
|
|
|
|
fetch::object_id::ObjectId,
|
|
|
|
traits::{ActivityHandler, Actor},
|
|
|
|
};
|
2022-03-14 18:20:18 +00:00
|
|
|
use anyhow::anyhow;
|
2022-11-28 14:29:33 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
|
|
|
use lemmy_db_schema::source::local_site::LocalSite;
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
|
|
|
use url::Url;
|
2021-08-02 20:33:40 +00:00
|
|
|
|
|
|
|
impl Vote {
|
2021-08-19 21:24:33 +00:00
|
|
|
pub(in crate::activities::voting) fn new(
|
2022-11-28 14:29:33 +00:00
|
|
|
object_id: ObjectId<PostOrComment>,
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ApubPerson,
|
2022-12-01 20:52:49 +00:00
|
|
|
community: &ApubCommunity,
|
2021-08-19 21:24:33 +00:00
|
|
|
kind: VoteType,
|
2023-03-21 15:03:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2021-08-19 21:24:33 +00:00
|
|
|
) -> Result<Vote, LemmyError> {
|
|
|
|
Ok(Vote {
|
2023-03-21 15:03:05 +00:00
|
|
|
actor: actor.id().into(),
|
2022-11-28 14:29:33 +00:00
|
|
|
object: object_id,
|
2021-08-19 21:24:33 +00:00
|
|
|
kind: kind.clone(),
|
2021-09-22 15:57:09 +00:00
|
|
|
id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
|
2023-03-21 15:03:05 +00:00
|
|
|
audience: Some(community.id().into()),
|
2021-08-19 21:24:33 +00:00
|
|
|
})
|
|
|
|
}
|
2021-08-02 20:33:40 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 15:03:05 +00:00
|
|
|
#[async_trait::async_trait]
|
2021-08-02 20:33:40 +00:00
|
|
|
impl ActivityHandler for Vote {
|
2021-10-06 20:20:05 +00:00
|
|
|
type DataType = LemmyContext;
|
2022-06-02 14:33:41 +00:00
|
|
|
type Error = LemmyError;
|
|
|
|
|
|
|
|
fn id(&self) -> &Url {
|
|
|
|
&self.id
|
|
|
|
}
|
|
|
|
|
|
|
|
fn actor(&self) -> &Url {
|
|
|
|
self.actor.inner()
|
|
|
|
}
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn verify(&self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
|
2023-07-14 15:17:06 +00:00
|
|
|
insert_received_activity(&self.id, context).await?;
|
2023-03-21 15:03:05 +00:00
|
|
|
let community = self.community(context).await?;
|
|
|
|
verify_person_in_community(&self.actor, &community, context).await?;
|
2023-07-11 13:09:59 +00:00
|
|
|
let enable_downvotes = LocalSite::read(&mut context.pool())
|
2022-11-09 10:05:00 +00:00
|
|
|
.await
|
2022-10-27 09:24:07 +00:00
|
|
|
.map(|l| l.enable_downvotes)
|
|
|
|
.unwrap_or(true);
|
|
|
|
if self.kind == VoteType::Dislike && !enable_downvotes {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(anyhow!("Downvotes disabled").into())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
2022-03-14 18:20:18 +00:00
|
|
|
}
|
2021-08-02 20:33:40 +00:00
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2023-03-21 15:03:05 +00:00
|
|
|
async fn receive(self, context: &Data<LemmyContext>) -> Result<(), LemmyError> {
|
|
|
|
let actor = self.actor.dereference(context).await?;
|
|
|
|
let object = self.object.dereference(context).await?;
|
2021-08-02 20:33:40 +00:00
|
|
|
match object {
|
2021-11-06 13:25:34 +00:00
|
|
|
PostOrComment::Post(p) => vote_post(&self.kind, actor, &p, context).await,
|
2021-10-12 16:46:26 +00:00
|
|
|
PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
|
2021-08-02 20:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|