2 activities::community::send_activity_in_community,
3 activity_lists::AnnouncableActivities,
4 fetcher::post_or_comment::PostOrComment,
5 objects::{comment::ApubComment, community::ApubCommunity, person::ApubPerson, post::ApubPost},
6 protocol::activities::voting::{
8 vote::{Vote, VoteType},
11 use activitypub_federation::{config::Data, fetch::object_id::ObjectId};
12 use lemmy_api_common::context::LemmyContext;
13 use lemmy_db_schema::{
16 comment::{CommentLike, CommentLikeForm},
19 post::{PostLike, PostLikeForm},
23 use lemmy_utils::error::LemmyError;
28 pub(crate) async fn send_like_activity(
33 context: Data<LemmyContext>,
34 ) -> Result<(), LemmyError> {
35 let object_id: ObjectId<PostOrComment> = object_id.try_into()?;
36 let actor: ApubPerson = actor.into();
37 let community: ApubCommunity = community.into();
39 // score of 1 means upvote, -1 downvote, 0 undo a previous vote
41 let vote = Vote::new(object_id, &actor, &community, score.try_into()?, &context)?;
42 let activity = AnnouncableActivities::Vote(vote);
43 send_activity_in_community(activity, &actor, &community, vec![], false, &context).await
45 // Lemmy API doesnt distinguish between Undo/Like and Undo/Dislike, so we hardcode it here.
46 let vote = Vote::new(object_id, &actor, &community, VoteType::Like, &context)?;
47 let undo_vote = UndoVote::new(vote, &actor, &community, &context)?;
48 let activity = AnnouncableActivities::UndoVote(undo_vote);
49 send_activity_in_community(activity, &actor, &community, vec![], false, &context).await
53 #[tracing::instrument(skip_all)]
54 async fn vote_comment(
57 comment: &ApubComment,
58 context: &Data<LemmyContext>,
59 ) -> Result<(), LemmyError> {
60 let comment_id = comment.id;
61 let like_form = CommentLikeForm {
63 post_id: comment.post_id,
65 score: vote_type.into(),
67 let person_id = actor.id;
68 CommentLike::remove(&mut context.pool(), person_id, comment_id).await?;
69 CommentLike::like(&mut context.pool(), &like_form).await?;
73 #[tracing::instrument(skip_all)]
78 context: &Data<LemmyContext>,
79 ) -> Result<(), LemmyError> {
80 let post_id = post.id;
81 let like_form = PostLikeForm {
84 score: vote_type.into(),
86 let person_id = actor.id;
87 PostLike::remove(&mut context.pool(), person_id, post_id).await?;
88 PostLike::like(&mut context.pool(), &like_form).await?;
92 #[tracing::instrument(skip_all)]
93 async fn undo_vote_comment(
95 comment: &ApubComment,
96 context: &Data<LemmyContext>,
97 ) -> Result<(), LemmyError> {
98 let comment_id = comment.id;
99 let person_id = actor.id;
100 CommentLike::remove(&mut context.pool(), person_id, comment_id).await?;
104 #[tracing::instrument(skip_all)]
105 async fn undo_vote_post(
108 context: &Data<LemmyContext>,
109 ) -> Result<(), LemmyError> {
110 let post_id = post.id;
111 let person_id = actor.id;
112 PostLike::remove(&mut context.pool(), person_id, post_id).await?;