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,
19 use lemmy_api_common::blocking;
20 use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler};
21 use lemmy_db_queries::Crud;
22 use lemmy_db_schema::{
23 source::{community::Community, person::Person},
26 use lemmy_utils::LemmyError;
27 use lemmy_websocket::LemmyContext;
28 use serde::{Deserialize, Serialize};
29 use std::{convert::TryFrom, ops::Deref};
30 use strum_macros::ToString;
33 #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
39 impl TryFrom<i16> for VoteType {
40 type Error = LemmyError;
42 fn try_from(value: i16) -> Result<Self, Self::Error> {
44 1 => Ok(VoteType::Like),
45 -1 => Ok(VoteType::Dislike),
46 _ => Err(anyhow!("invalid vote value").into()),
51 impl From<&VoteType> for i16 {
52 fn from(value: &VoteType) -> i16 {
55 VoteType::Dislike => -1,
60 #[derive(Clone, Debug, Deserialize, Serialize)]
61 #[serde(rename_all = "camelCase")]
63 pub(in crate::activities::voting) to: PublicUrl,
64 pub(in crate::activities::voting) object: Url,
65 pub(in crate::activities::voting) cc: [Url; 1],
66 #[serde(rename = "type")]
67 pub(in crate::activities::voting) kind: VoteType,
69 pub(in crate::activities::voting) common: ActivityCommonFields,
74 object: &PostOrComment,
76 community_id: CommunityId,
78 context: &LemmyContext,
79 ) -> Result<(), LemmyError> {
80 let community = blocking(context.pool(), move |conn| {
81 Community::read(conn, community_id)
84 let id = generate_activity_id(kind.clone())?;
87 to: PublicUrl::Public,
88 object: object.ap_id(),
89 cc: [community.actor_id()],
91 common: ActivityCommonFields {
92 context: lemmy_context(),
94 actor: actor.actor_id(),
95 unparsed: Default::default(),
98 let activity = AnnouncableActivities::Vote(vote);
99 send_to_community_new(activity, &id, actor, &community, vec![], context).await
103 #[async_trait::async_trait(?Send)]
104 impl ActivityHandler for Vote {
107 context: &LemmyContext,
108 request_counter: &mut i32,
109 ) -> Result<(), LemmyError> {
110 verify_activity(self.common())?;
111 verify_person_in_community(&self.common.actor, &self.cc[0], context, request_counter).await?;
117 context: &LemmyContext,
118 request_counter: &mut i32,
119 ) -> Result<(), LemmyError> {
121 get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
123 get_or_fetch_and_insert_post_or_comment(&self.object, context, request_counter).await?;
125 PostOrComment::Post(p) => vote_post(&self.kind, actor, p.deref(), context).await,
126 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, c.deref(), context).await,
130 fn common(&self) -> &ActivityCommonFields {