3 community::announce::AnnouncableActivities,
6 verify_person_in_community,
7 voting::{vote_comment, vote_post},
9 activity_queue::send_to_community_new,
10 extensions::context::lemmy_context,
12 objects::get_or_fetch_and_insert_post_or_comment,
13 person::get_or_fetch_and_upsert_person,
18 use activitystreams::{base::AnyBase, primitives::OneOrMany, unparsed::Unparsed};
20 use lemmy_api_common::blocking;
21 use lemmy_apub_lib::{values::PublicUrl, ActivityFields, ActivityHandler};
22 use lemmy_db_queries::Crud;
23 use lemmy_db_schema::{
24 source::{community::Community, person::Person},
27 use lemmy_utils::LemmyError;
28 use lemmy_websocket::LemmyContext;
29 use serde::{Deserialize, Serialize};
30 use std::{convert::TryFrom, ops::Deref};
31 use strum_macros::ToString;
34 #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
40 impl TryFrom<i16> for VoteType {
41 type Error = LemmyError;
43 fn try_from(value: i16) -> Result<Self, Self::Error> {
45 1 => Ok(VoteType::Like),
46 -1 => Ok(VoteType::Dislike),
47 _ => Err(anyhow!("invalid vote value").into()),
52 impl From<&VoteType> for i16 {
53 fn from(value: &VoteType) -> i16 {
56 VoteType::Dislike => -1,
61 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
62 #[serde(rename_all = "camelCase")]
66 pub(in crate::activities::voting) object: Url,
68 #[serde(rename = "type")]
69 pub(in crate::activities::voting) kind: VoteType,
71 #[serde(rename = "@context")]
72 context: OneOrMany<AnyBase>,
78 pub(in crate::activities::voting) fn new(
79 object: &PostOrComment,
81 community: &Community,
83 ) -> Result<Vote, LemmyError> {
85 actor: actor.actor_id(),
86 to: PublicUrl::Public,
87 object: object.ap_id(),
88 cc: [community.actor_id()],
90 id: generate_activity_id(kind)?,
91 context: lemmy_context(),
92 unparsed: Default::default(),
97 object: &PostOrComment,
99 community_id: CommunityId,
101 context: &LemmyContext,
102 ) -> Result<(), LemmyError> {
103 let community = blocking(context.pool(), move |conn| {
104 Community::read(conn, community_id)
107 let vote = Vote::new(object, actor, &community, kind)?;
108 let vote_id = vote.id.clone();
110 let activity = AnnouncableActivities::Vote(vote);
111 send_to_community_new(activity, &vote_id, actor, &community, vec![], context).await
115 #[async_trait::async_trait(?Send)]
116 impl ActivityHandler for Vote {
119 context: &LemmyContext,
120 request_counter: &mut i32,
121 ) -> Result<(), LemmyError> {
122 verify_activity(self)?;
123 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
129 context: &LemmyContext,
130 request_counter: &mut i32,
131 ) -> Result<(), LemmyError> {
132 let actor = get_or_fetch_and_upsert_person(&self.actor, context, request_counter).await?;
134 get_or_fetch_and_insert_post_or_comment(&self.object, context, request_counter).await?;
136 PostOrComment::Post(p) => vote_post(&self.kind, actor, p.deref(), context).await,
137 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, c.deref(), context).await,