4 announce::{AnnouncableActivities, GetCommunity},
10 verify_person_in_community,
11 voting::{vote_comment, vote_post},
13 context::lemmy_context,
14 fetcher::object_id::ObjectId,
15 objects::{community::ApubCommunity, person::ApubPerson},
18 use activitystreams::{base::AnyBase, primitives::OneOrMany, public, unparsed::Unparsed};
20 use lemmy_api_common::blocking;
23 traits::{ActivityFields, ActivityHandler, ActorType},
25 use lemmy_db_schema::{
26 newtypes::CommunityId,
27 source::{community::Community, post::Post},
30 use lemmy_utils::LemmyError;
31 use lemmy_websocket::LemmyContext;
32 use serde::{Deserialize, Serialize};
33 use std::{convert::TryFrom, ops::Deref};
34 use strum_macros::ToString;
37 #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
43 impl TryFrom<i16> for VoteType {
44 type Error = LemmyError;
46 fn try_from(value: i16) -> Result<Self, Self::Error> {
48 1 => Ok(VoteType::Like),
49 -1 => Ok(VoteType::Dislike),
50 _ => Err(anyhow!("invalid vote value").into()),
55 impl From<&VoteType> for i16 {
56 fn from(value: &VoteType) -> i16 {
59 VoteType::Dislike => -1,
64 #[derive(Clone, Debug, Deserialize, Serialize, ActivityFields)]
65 #[serde(rename_all = "camelCase")]
67 actor: ObjectId<ApubPerson>,
69 pub(in crate::activities::voting) object: ObjectId<PostOrComment>,
71 #[serde(rename = "type")]
72 pub(in crate::activities::voting) kind: VoteType,
74 #[serde(rename = "@context")]
75 context: OneOrMany<AnyBase>,
81 pub(in crate::activities::voting) fn new(
82 object: &PostOrComment,
84 community: &ApubCommunity,
86 context: &LemmyContext,
87 ) -> Result<Vote, LemmyError> {
89 actor: ObjectId::new(actor.actor_id()),
91 object: ObjectId::new(object.ap_id()),
92 cc: vec![community.actor_id()],
94 id: generate_activity_id(kind, &context.settings().get_protocol_and_hostname())?,
95 context: lemmy_context(),
96 unparsed: Default::default(),
101 object: &PostOrComment,
103 community_id: CommunityId,
105 context: &LemmyContext,
106 ) -> Result<(), LemmyError> {
107 let community = blocking(context.pool(), move |conn| {
108 Community::read(conn, community_id)
112 let vote = Vote::new(object, actor, &community, kind, context)?;
113 let vote_id = vote.id.clone();
115 let activity = AnnouncableActivities::Vote(vote);
116 send_to_community(activity, &vote_id, actor, &community, vec![], context).await
120 #[async_trait::async_trait(?Send)]
121 impl ActivityHandler for Vote {
122 type DataType = LemmyContext;
125 context: &Data<LemmyContext>,
126 request_counter: &mut i32,
127 ) -> Result<(), LemmyError> {
128 verify_is_public(&self.to)?;
129 verify_activity(self, &context.settings())?;
130 let community = self.get_community(context, request_counter).await?;
131 verify_person_in_community(&self.actor, &community, context, request_counter).await?;
137 context: &Data<LemmyContext>,
138 request_counter: &mut i32,
139 ) -> Result<(), LemmyError> {
140 let actor = self.actor.dereference(context, request_counter).await?;
141 let object = self.object.dereference(context, request_counter).await?;
143 PostOrComment::Post(p) => vote_post(&self.kind, actor, p.deref(), context).await,
144 PostOrComment::Comment(c) => vote_comment(&self.kind, actor, &c, context).await,
149 #[async_trait::async_trait(?Send)]
150 impl GetCommunity for Vote {
151 async fn get_community(
153 context: &LemmyContext,
154 request_counter: &mut i32,
155 ) -> Result<ApubCommunity, LemmyError> {
156 let object = self.object.dereference(context, request_counter).await?;
157 let cid = match object {
158 PostOrComment::Post(p) => p.community_id,
159 PostOrComment::Comment(c) => {
160 blocking(context.pool(), move |conn| Post::read(conn, c.post_id))
165 let community = blocking(context.pool(), move |conn| Community::read(conn, cid)).await??;