2 activities::community::send_activity_in_community,
3 activity_lists::AnnouncableActivities,
4 fetcher::post_or_comment::PostOrComment,
5 objects::{comment::ApubComment, person::ApubPerson, post::ApubPost},
6 protocol::activities::voting::{
8 vote::{Vote, VoteType},
12 use activitypub_federation::core::object_id::ObjectId;
13 use lemmy_api_common::{
14 comment::{CommentResponse, CreateCommentLike},
15 context::LemmyContext,
16 post::{CreatePostLike, PostResponse},
18 utils::get_local_user_view_from_jwt,
20 send::{send_comment_ws_message_simple, send_post_ws_message},
24 use lemmy_db_schema::{
25 newtypes::CommunityId,
27 comment::{CommentLike, CommentLikeForm},
30 post::{PostLike, PostLikeForm},
32 traits::{Crud, Likeable},
34 use lemmy_utils::error::LemmyError;
39 #[async_trait::async_trait(?Send)]
40 impl SendActivity for CreatePostLike {
41 type Response = PostResponse;
43 async fn send_activity(
45 response: &Self::Response,
46 context: &LemmyContext,
47 ) -> Result<(), LemmyError> {
48 let object_id = ObjectId::new(response.post_view.post.ap_id.clone());
49 let community_id = response.post_view.community.id;
61 #[async_trait::async_trait(?Send)]
62 impl SendActivity for CreateCommentLike {
63 type Response = CommentResponse;
65 async fn send_activity(
67 response: &Self::Response,
68 context: &LemmyContext,
69 ) -> Result<(), LemmyError> {
70 let object_id = ObjectId::new(response.comment_view.comment.ap_id.clone());
71 let community_id = response.comment_view.community.id;
83 async fn send_activity(
84 object_id: ObjectId<PostOrComment>,
85 community_id: CommunityId,
87 jwt: &Sensitive<String>,
88 context: &LemmyContext,
89 ) -> Result<(), LemmyError> {
90 let community = Community::read(context.pool(), community_id).await?.into();
91 let local_user_view = get_local_user_view_from_jwt(jwt, context.pool(), context.secret()).await?;
92 let actor = Person::read(context.pool(), local_user_view.person.id)
96 // score of 1 means upvote, -1 downvote, 0 undo a previous vote
98 let vote = Vote::new(object_id, &actor, &community, score.try_into()?, context)?;
99 let activity = AnnouncableActivities::Vote(vote);
100 send_activity_in_community(activity, &actor, &community, vec![], false, context).await
102 // Lemmy API doesnt distinguish between Undo/Like and Undo/Dislike, so we hardcode it here.
103 let vote = Vote::new(object_id, &actor, &community, VoteType::Like, context)?;
104 let undo_vote = UndoVote::new(vote, &actor, &community, context)?;
105 let activity = AnnouncableActivities::UndoVote(undo_vote);
106 send_activity_in_community(activity, &actor, &community, vec![], false, context).await
110 #[tracing::instrument(skip_all)]
111 async fn vote_comment(
112 vote_type: &VoteType,
114 comment: &ApubComment,
115 context: &LemmyContext,
116 ) -> Result<(), LemmyError> {
117 let comment_id = comment.id;
118 let like_form = CommentLikeForm {
120 post_id: comment.post_id,
122 score: vote_type.into(),
124 let person_id = actor.id;
125 CommentLike::remove(context.pool(), person_id, comment_id).await?;
126 CommentLike::like(context.pool(), &like_form).await?;
128 send_comment_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
132 #[tracing::instrument(skip_all)]
134 vote_type: &VoteType,
137 context: &LemmyContext,
138 ) -> Result<(), LemmyError> {
139 let post_id = post.id;
140 let like_form = PostLikeForm {
143 score: vote_type.into(),
145 let person_id = actor.id;
146 PostLike::remove(context.pool(), person_id, post_id).await?;
147 PostLike::like(context.pool(), &like_form).await?;
149 send_post_ws_message(post.id, UserOperation::CreatePostLike, None, None, context).await?;
153 #[tracing::instrument(skip_all)]
154 async fn undo_vote_comment(
156 comment: &ApubComment,
157 context: &LemmyContext,
158 ) -> Result<(), LemmyError> {
159 let comment_id = comment.id;
160 let person_id = actor.id;
161 CommentLike::remove(context.pool(), person_id, comment_id).await?;
163 send_comment_ws_message_simple(comment_id, UserOperation::CreateCommentLike, context).await?;
167 #[tracing::instrument(skip_all)]
168 async fn undo_vote_post(
171 context: &LemmyContext,
172 ) -> Result<(), LemmyError> {
173 let post_id = post.id;
174 let person_id = actor.id;
175 PostLike::remove(context.pool(), person_id, post_id).await?;
177 send_post_ws_message(post_id, UserOperation::CreatePostLike, None, None, context).await?;