3 community::{announce::AnnouncableActivities, send_to_community},
6 verify_person_in_community,
7 voting::{vote_comment, vote_post},
9 context::lemmy_context,
10 fetcher::object_id::ObjectId,
13 use activitystreams::{base::AnyBase, primitives::OneOrMany, unparsed::Unparsed};
15 use lemmy_api_common::blocking;
18 traits::{ActivityFields, ActivityHandler, ActorType},
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, ActivityFields)]
61 #[serde(rename_all = "camelCase")]
63 actor: ObjectId<Person>,
65 pub(in crate::activities::voting) object: ObjectId<PostOrComment>,
66 cc: [ObjectId<Community>; 1],
67 #[serde(rename = "type")]
68 pub(in crate::activities::voting) kind: VoteType,
70 #[serde(rename = "@context")]
71 context: OneOrMany<AnyBase>,
77 pub(in crate::activities::voting) fn new(
78 object: &PostOrComment,
80 community: &Community,
82 context: &LemmyContext,
83 ) -> Result<Vote, LemmyError> {
85 actor: ObjectId::new(actor.actor_id()),
86 to: [PublicUrl::Public],
87 object: ObjectId::new(object.ap_id()),
88 cc: [ObjectId::new(community.actor_id())],
90 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
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, context)?;
108 let vote_id = vote.id.clone();
110 let activity = AnnouncableActivities::Vote(vote);
111 send_to_community(activity, &vote_id, actor, &community, vec![], context).await
115 #[async_trait::async_trait(?Send)]
116 impl ActivityHandler for Vote {
117 type DataType = LemmyContext;
120 context: &Data<LemmyContext>,
121 request_counter: &mut i32,
122 ) -> Result<(), LemmyError> {
123 verify_activity(self, &context.settings())?;
124 verify_person_in_community(&self.actor, &self.cc[0], context, request_counter).await?;
130 context: &Data<LemmyContext>,
131 request_counter: &mut i32,
132 ) -> Result<(), LemmyError> {
133 let actor = self.actor.dereference(context, request_counter).await?;
134 let object = self.object.dereference(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,