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_person_in_community,
|
|
|
|
voting::{vote_comment, vote_post},
|
|
|
|
},
|
|
|
|
activity_lists::AnnouncableActivities,
|
2022-06-02 14:33:41 +00:00
|
|
|
local_instance,
|
2021-10-29 10:32:42 +00:00
|
|
|
objects::{community::ApubCommunity, person::ApubPerson},
|
|
|
|
protocol::activities::voting::vote::{Vote, VoteType},
|
2022-06-02 14:33:41 +00:00
|
|
|
ActorType,
|
2021-10-29 10:32:42 +00:00
|
|
|
PostOrComment,
|
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use activitypub_federation::{core::object_id::ObjectId, data::Data, traits::ActivityHandler};
|
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_db_schema::{
|
|
|
|
newtypes::CommunityId,
|
2022-10-27 09:24:07 +00:00
|
|
|
source::{community::Community, local_site::LocalSite, post::Post},
|
2021-11-03 17:33:51 +00:00
|
|
|
traits::Crud,
|
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2021-11-03 17:33:51 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
2022-06-02 14:33:41 +00:00
|
|
|
use url::Url;
|
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,
|
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()),
|
|
|
|
object: ObjectId::new(object.ap_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())?,
|
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();
|
2022-07-29 13:32:12 +00:00
|
|
|
let vote = Vote::new(object, actor, kind, context)?;
|
2021-08-02 20:33:40 +00:00
|
|
|
|
|
|
|
let activity = AnnouncableActivities::Vote(vote);
|
2022-06-08 15:45:39 +00:00
|
|
|
send_activity_in_community(activity, 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;
|
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)]
|
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-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-10-27 09:24:07 +00:00
|
|
|
let enable_downvotes = blocking(context.pool(), LocalSite::read)
|
|
|
|
.await?
|
|
|
|
.map(|l| l.enable_downvotes)
|
|
|
|
.unwrap_or(true);
|
|
|
|
if self.kind == VoteType::Dislike && !enable_downvotes {
|
2022-03-14 18:20:18 +00:00
|
|
|
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
|
2022-06-08 15:45:39 +00:00
|
|
|
.dereference(context, local_instance(context), request_counter)
|
2021-12-06 22:54:34 +00:00
|
|
|
.await?;
|
|
|
|
let object = self
|
|
|
|
.object
|
2022-06-08 15:45:39 +00:00
|
|
|
.dereference(context, local_instance(context), request_counter)
|
2021-12-06 22:54:34 +00:00
|
|
|
.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
|
2022-06-08 15:45:39 +00:00
|
|
|
.dereference(context, local_instance(context), request_counter)
|
2021-12-06 22:54:34 +00:00
|
|
|
.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())
|
|
|
|
}
|
|
|
|
}
|