2021-10-29 10:32:42 +00:00
|
|
|
use crate::{
|
|
|
|
activities::{
|
2021-11-15 21:37:19 +00:00
|
|
|
community::{announce::GetCommunity, send_activity_in_community},
|
2021-10-29 10:32:42 +00:00
|
|
|
generate_activity_id,
|
|
|
|
verify_activity,
|
|
|
|
verify_is_public,
|
|
|
|
verify_person_in_community,
|
|
|
|
voting::{vote_comment, vote_post},
|
|
|
|
},
|
|
|
|
activity_lists::AnnouncableActivities,
|
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
|
|
|
protocol::activities::voting::vote::{Vote, VoteType},
|
|
|
|
PostOrComment,
|
|
|
|
};
|
2021-11-19 17:47:06 +00:00
|
|
|
use activitystreams_kinds::public;
|
2022-03-14 18:20:18 +00:00
|
|
|
use anyhow::anyhow;
|
2022-05-03 17:44:13 +00:00
|
|
|
use lemmy_api_common::utils::blocking;
|
2021-11-03 17:33:51 +00:00
|
|
|
use lemmy_apub_lib::{
|
|
|
|
data::Data,
|
2021-11-05 00:24:10 +00:00
|
|
|
object_id::ObjectId,
|
2021-11-03 17:33:51 +00:00
|
|
|
traits::{ActivityHandler, ActorType},
|
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
newtypes::CommunityId,
|
2022-03-14 18:20:18 +00:00
|
|
|
source::{community::Community, post::Post, site::Site},
|
2021-11-03 17:33:51 +00:00
|
|
|
traits::Crud,
|
|
|
|
};
|
|
|
|
use lemmy_utils::LemmyError;
|
|
|
|
use lemmy_websocket::LemmyContext;
|
2021-08-02 20:33:40 +00:00
|
|
|
|
2022-03-24 16:33:42 +00:00
|
|
|
/// Vote has as:Public value in cc field, unlike other activities. This indicates to other software
|
|
|
|
/// (like GNU social, or presumably Mastodon), that the like actor should not be disclosed.
|
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(
|
|
|
|
object: &PostOrComment,
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ApubPerson,
|
|
|
|
community: &ApubCommunity,
|
2021-08-19 21:24:33 +00:00
|
|
|
kind: VoteType,
|
2021-09-22 15:57:09 +00:00
|
|
|
context: &LemmyContext,
|
2021-08-19 21:24:33 +00:00
|
|
|
) -> Result<Vote, LemmyError> {
|
|
|
|
Ok(Vote {
|
2021-09-25 15:44:52 +00:00
|
|
|
actor: ObjectId::new(actor.actor_id()),
|
2022-03-24 16:33:42 +00:00
|
|
|
to: vec![community.actor_id()],
|
2021-09-25 15:44:52 +00:00
|
|
|
object: ObjectId::new(object.ap_id()),
|
2022-03-24 16:33:42 +00:00
|
|
|
cc: vec![public()],
|
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())?,
|
2021-08-19 21:24:33 +00:00
|
|
|
unparsed: Default::default(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-08-02 20:33:40 +00:00
|
|
|
pub async fn send(
|
|
|
|
object: &PostOrComment,
|
2021-10-18 21:36:44 +00:00
|
|
|
actor: &ApubPerson,
|
2021-08-02 20:33:40 +00:00
|
|
|
community_id: CommunityId,
|
|
|
|
kind: VoteType,
|
|
|
|
context: &LemmyContext,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let community = blocking(context.pool(), move |conn| {
|
|
|
|
Community::read(conn, community_id)
|
|
|
|
})
|
2021-10-18 21:36:44 +00:00
|
|
|
.await??
|
|
|
|
.into();
|
2021-09-22 15:57:09 +00:00
|
|
|
let vote = Vote::new(object, actor, &community, kind, context)?;
|
2021-08-19 21:24:33 +00:00
|
|
|
let vote_id = vote.id.clone();
|
2021-08-02 20:33:40 +00:00
|
|
|
|
|
|
|
let activity = AnnouncableActivities::Vote(vote);
|
2021-11-15 21:37:19 +00:00
|
|
|
send_activity_in_community(activity, &vote_id, actor, &community, vec![], context).await
|
2021-08-02 20:33:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl ActivityHandler for Vote {
|
2021-10-06 20:20:05 +00:00
|
|
|
type DataType = LemmyContext;
|
2021-12-06 14:54:47 +00:00
|
|
|
|
|
|
|
#[tracing::instrument(skip_all)]
|
2021-08-02 20:33:40 +00:00
|
|
|
async fn verify(
|
|
|
|
&self,
|
2021-10-06 20:20:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2021-08-02 20:33:40 +00:00
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-11-06 17:44:34 +00:00
|
|
|
verify_is_public(&self.to, &self.cc)?;
|
2021-11-03 17:33:51 +00:00
|
|
|
verify_activity(&self.id, self.actor.inner(), &context.settings())?;
|
2021-10-28 11:46:48 +00:00
|
|
|
let community = self.get_community(context, request_counter).await?;
|
|
|
|
verify_person_in_community(&self.actor, &community, context, request_counter).await?;
|
2022-03-14 18:20:18 +00:00
|
|
|
let site = blocking(context.pool(), Site::read_local_site).await??;
|
|
|
|
if self.kind == VoteType::Dislike && !site.enable_downvotes {
|
|
|
|
return Err(anyhow!("Downvotes disabled").into());
|
|
|
|
}
|
2021-08-02 20:33:40 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-08-02 20:33:40 +00:00
|
|
|
async fn receive(
|
2021-08-12 12:48:09 +00:00
|
|
|
self,
|
2021-10-06 20:20:05 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2021-08-02 20:33:40 +00:00
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
2021-12-06 22:54:34 +00:00
|
|
|
let actor = self
|
|
|
|
.actor
|
|
|
|
.dereference(context, context.client(), request_counter)
|
|
|
|
.await?;
|
|
|
|
let object = self
|
|
|
|
.object
|
|
|
|
.dereference(context, context.client(), request_counter)
|
|
|
|
.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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-28 11:46:48 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl GetCommunity for Vote {
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip_all)]
|
2021-10-28 11:46:48 +00:00
|
|
|
async fn get_community(
|
|
|
|
&self,
|
|
|
|
context: &LemmyContext,
|
|
|
|
request_counter: &mut i32,
|
|
|
|
) -> Result<ApubCommunity, LemmyError> {
|
2021-12-06 22:54:34 +00:00
|
|
|
let object = self
|
|
|
|
.object
|
|
|
|
.dereference(context, context.client(), request_counter)
|
|
|
|
.await?;
|
2021-10-28 11:46:48 +00:00
|
|
|
let cid = match object {
|
|
|
|
PostOrComment::Post(p) => p.community_id,
|
|
|
|
PostOrComment::Comment(c) => {
|
|
|
|
blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
|
|
|
|
.await??
|
|
|
|
.community_id
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;
|
|
|
|
Ok(community.into())
|
|
|
|
}
|
|
|
|
}
|