From 869b1b6f8fe6f2f8e491189079bbed4ecb44bd80 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Sun, 1 Aug 2021 13:51:57 +0200 Subject: [PATCH] Merge like/dislike activity handlers into vote --- .../apub/src/activities/community/announce.rs | 13 +-- crates/apub/src/activities/mod.rs | 1 - crates/apub/src/activities/voting/dislike.rs | 54 ----------- crates/apub/src/activities/voting/like.rs | 54 ----------- crates/apub/src/activities/voting/mod.rs | 92 ++++--------------- .../apub/src/activities/voting/undo_like.rs | 55 ----------- .../voting/{undo_dislike.rs => undo_vote.rs} | 38 +++++--- crates/apub/src/activities/voting/vote.rs | 76 +++++++++++++++ crates/apub/src/http/inbox_enums.rs | 19 +--- 9 files changed, 127 insertions(+), 275 deletions(-) delete mode 100644 crates/apub/src/activities/voting/dislike.rs delete mode 100644 crates/apub/src/activities/voting/like.rs delete mode 100644 crates/apub/src/activities/voting/undo_like.rs rename crates/apub/src/activities/voting/{undo_dislike.rs => undo_vote.rs} (57%) create mode 100644 crates/apub/src/activities/voting/vote.rs diff --git a/crates/apub/src/activities/community/announce.rs b/crates/apub/src/activities/community/announce.rs index b72cd1a5c..9cde8a43c 100644 --- a/crates/apub/src/activities/community/announce.rs +++ b/crates/apub/src/activities/community/announce.rs @@ -19,12 +19,7 @@ use crate::{ }, verify_activity, verify_community, - voting::{ - dislike::DislikePostOrComment, - like::LikePostOrComment, - undo_dislike::UndoDislikePostOrComment, - undo_like::UndoLikePostOrComment, - }, + voting::{undo_vote::UndoVote, vote::Vote}, }, activity_queue::send_activity_new, extensions::context::lemmy_context, @@ -46,10 +41,8 @@ use url::Url; pub enum AnnouncableActivities { CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdatePost(Box), - LikePostOrComment(LikePostOrComment), - DislikePostOrComment(DislikePostOrComment), - UndoLikePostOrComment(UndoLikePostOrComment), - UndoDislikePostOrComment(UndoDislikePostOrComment), + Vote(Vote), + UndoVote(UndoVote), DeletePostCommentOrCommunity(DeletePostCommentOrCommunity), UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity), RemovePostCommentCommunityOrMod(RemovePostCommentCommunityOrMod), diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index f4c535ee6..afbad6c8b 100644 --- a/crates/apub/src/activities/mod.rs +++ b/crates/apub/src/activities/mod.rs @@ -31,7 +31,6 @@ pub mod send; pub mod voting; #[derive(Clone, Debug, ToString, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] pub enum CreateOrUpdateType { Create, Update, diff --git a/crates/apub/src/activities/voting/dislike.rs b/crates/apub/src/activities/voting/dislike.rs deleted file mode 100644 index 54b673008..000000000 --- a/crates/apub/src/activities/voting/dislike.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::activities::{ - verify_activity, - verify_person_in_community, - voting::receive_like_or_dislike, -}; -use activitystreams::activity::kind::DislikeType; -use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler}; -use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; -use url::Url; - -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -#[serde(rename_all = "camelCase")] -pub struct DislikePostOrComment { - to: PublicUrl, - pub(in crate::activities) object: Url, - cc: [Url; 1], - #[serde(rename = "type")] - kind: DislikeType, - #[serde(flatten)] - common: ActivityCommonFields, -} - -#[async_trait::async_trait(?Send)] -impl ActivityHandler for DislikePostOrComment { - async fn verify( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - verify_activity(self.common())?; - verify_person_in_community(&self.common.actor, &self.cc[0], context, request_counter).await?; - Ok(()) - } - - async fn receive( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - receive_like_or_dislike( - -1, - &self.common.actor, - &self.object, - context, - request_counter, - ) - .await - } - - fn common(&self) -> &ActivityCommonFields { - &self.common - } -} diff --git a/crates/apub/src/activities/voting/like.rs b/crates/apub/src/activities/voting/like.rs deleted file mode 100644 index 90f29c427..000000000 --- a/crates/apub/src/activities/voting/like.rs +++ /dev/null @@ -1,54 +0,0 @@ -use crate::activities::{ - verify_activity, - verify_person_in_community, - voting::receive_like_or_dislike, -}; -use activitystreams::activity::kind::LikeType; -use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler}; -use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; -use url::Url; - -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -#[serde(rename_all = "camelCase")] -pub struct LikePostOrComment { - to: PublicUrl, - pub(in crate::activities::voting) object: Url, - cc: [Url; 1], - #[serde(rename = "type")] - kind: LikeType, - #[serde(flatten)] - common: ActivityCommonFields, -} - -#[async_trait::async_trait(?Send)] -impl ActivityHandler for LikePostOrComment { - async fn verify( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - verify_activity(self.common())?; - verify_person_in_community(&self.common.actor, &self.cc[0], context, request_counter).await?; - Ok(()) - } - - async fn receive( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - receive_like_or_dislike( - 1, - &self.common.actor, - &self.object, - context, - request_counter, - ) - .await - } - - fn common(&self) -> &ActivityCommonFields { - &self.common - } -} diff --git a/crates/apub/src/activities/voting/mod.rs b/crates/apub/src/activities/voting/mod.rs index 739c43051..3d4a6d10f 100644 --- a/crates/apub/src/activities/voting/mod.rs +++ b/crates/apub/src/activities/voting/mod.rs @@ -1,62 +1,33 @@ -use crate::{ - activities::{ - comment::send_websocket_message as send_comment_message, - post::send_websocket_message as send_post_message, - }, - fetcher::{ - objects::get_or_fetch_and_insert_post_or_comment, - person::get_or_fetch_and_upsert_person, - }, - PostOrComment, +use crate::activities::{ + comment::send_websocket_message as send_comment_message, + post::send_websocket_message as send_post_message, + voting::vote::VoteType, }; use lemmy_api_common::blocking; use lemmy_db_queries::Likeable; use lemmy_db_schema::source::{ comment::{Comment, CommentLike, CommentLikeForm}, + person::Person, post::{Post, PostLike, PostLikeForm}, }; use lemmy_utils::LemmyError; use lemmy_websocket::{LemmyContext, UserOperation}; -use std::ops::Deref; -use url::Url; -pub mod dislike; -pub mod like; -pub mod undo_dislike; -pub mod undo_like; +pub mod undo_vote; +pub mod vote; -pub(in crate::activities::voting) async fn receive_like_or_dislike( - score: i16, - actor: &Url, - object: &Url, - context: &LemmyContext, - request_counter: &mut i32, -) -> Result<(), LemmyError> { - match get_or_fetch_and_insert_post_or_comment(object, context, request_counter).await? { - PostOrComment::Post(p) => { - like_or_dislike_post(score, actor, p.deref(), context, request_counter).await - } - PostOrComment::Comment(c) => { - like_or_dislike_comment(score, actor, c.deref(), context, request_counter).await - } - } -} - -async fn like_or_dislike_comment( - score: i16, - actor: &Url, +async fn vote_comment( + vote_type: &VoteType, + actor: Person, comment: &Comment, context: &LemmyContext, - request_counter: &mut i32, ) -> Result<(), LemmyError> { - let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?; - let comment_id = comment.id; let like_form = CommentLikeForm { comment_id, post_id: comment.post_id, person_id: actor.id, - score, + score: vote_type.score(), }; let person_id = actor.id; blocking(context.pool(), move |conn| { @@ -74,20 +45,17 @@ async fn like_or_dislike_comment( .await } -async fn like_or_dislike_post( - score: i16, - actor: &Url, +async fn vote_post( + vote_type: &VoteType, + actor: Person, post: &Post, context: &LemmyContext, - request_counter: &mut i32, ) -> Result<(), LemmyError> { - let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?; - let post_id = post.id; let like_form = PostLikeForm { post_id: post.id, person_id: actor.id, - score, + score: vote_type.score(), }; let person_id = actor.id; blocking(context.pool(), move |conn| { @@ -99,30 +67,11 @@ async fn like_or_dislike_post( send_post_message(post.id, UserOperation::CreatePostLike, context).await } -pub(in crate::activities::voting) async fn receive_undo_like_or_dislike( - actor: &Url, - object: &Url, - context: &LemmyContext, - request_counter: &mut i32, -) -> Result<(), LemmyError> { - match get_or_fetch_and_insert_post_or_comment(object, context, request_counter).await? { - PostOrComment::Post(p) => { - undo_like_or_dislike_post(actor, p.deref(), context, request_counter).await - } - PostOrComment::Comment(c) => { - undo_like_or_dislike_comment(actor, c.deref(), context, request_counter).await - } - } -} - -async fn undo_like_or_dislike_comment( - actor: &Url, +async fn undo_vote_comment( + actor: Person, comment: &Comment, context: &LemmyContext, - request_counter: &mut i32, ) -> Result<(), LemmyError> { - let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?; - let comment_id = comment.id; let person_id = actor.id; blocking(context.pool(), move |conn| { @@ -139,14 +88,11 @@ async fn undo_like_or_dislike_comment( .await } -async fn undo_like_or_dislike_post( - actor: &Url, +async fn undo_vote_post( + actor: Person, post: &Post, context: &LemmyContext, - request_counter: &mut i32, ) -> Result<(), LemmyError> { - let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?; - let post_id = post.id; let person_id = actor.id; blocking(context.pool(), move |conn| { diff --git a/crates/apub/src/activities/voting/undo_like.rs b/crates/apub/src/activities/voting/undo_like.rs deleted file mode 100644 index ab15da70b..000000000 --- a/crates/apub/src/activities/voting/undo_like.rs +++ /dev/null @@ -1,55 +0,0 @@ -use crate::activities::{ - verify_activity, - verify_person_in_community, - voting::{like::LikePostOrComment, receive_undo_like_or_dislike}, -}; -use activitystreams::activity::kind::UndoType; -use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler}; -use lemmy_utils::LemmyError; -use lemmy_websocket::LemmyContext; -use url::Url; - -#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] -#[serde(rename_all = "camelCase")] -pub struct UndoLikePostOrComment { - to: PublicUrl, - object: LikePostOrComment, - cc: [Url; 1], - #[serde(rename = "type")] - kind: UndoType, - #[serde(flatten)] - common: ActivityCommonFields, -} - -#[async_trait::async_trait(?Send)] -impl ActivityHandler for UndoLikePostOrComment { - async fn verify( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - verify_activity(self.common())?; - verify_person_in_community(&self.common.actor, &self.cc[0], context, request_counter).await?; - verify_urls_match(&self.common.actor, &self.object.common().actor)?; - self.object.verify(context, request_counter).await?; - Ok(()) - } - - async fn receive( - &self, - context: &LemmyContext, - request_counter: &mut i32, - ) -> Result<(), LemmyError> { - receive_undo_like_or_dislike( - &self.common.actor, - &self.object.object, - context, - request_counter, - ) - .await - } - - fn common(&self) -> &ActivityCommonFields { - &self.common - } -} diff --git a/crates/apub/src/activities/voting/undo_dislike.rs b/crates/apub/src/activities/voting/undo_vote.rs similarity index 57% rename from crates/apub/src/activities/voting/undo_dislike.rs rename to crates/apub/src/activities/voting/undo_vote.rs index 13e3e1f76..50299645b 100644 --- a/crates/apub/src/activities/voting/undo_dislike.rs +++ b/crates/apub/src/activities/voting/undo_vote.rs @@ -1,19 +1,27 @@ -use crate::activities::{ - verify_activity, - verify_person_in_community, - voting::{dislike::DislikePostOrComment, receive_undo_like_or_dislike}, +use crate::{ + activities::{ + verify_activity, + verify_person_in_community, + voting::{undo_vote_comment, undo_vote_post, vote::Vote}, + }, + fetcher::{ + objects::get_or_fetch_and_insert_post_or_comment, + person::get_or_fetch_and_upsert_person, + }, + PostOrComment, }; use activitystreams::activity::kind::UndoType; use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler}; use lemmy_utils::LemmyError; use lemmy_websocket::LemmyContext; +use std::ops::Deref; use url::Url; #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[serde(rename_all = "camelCase")] -pub struct UndoDislikePostOrComment { +pub struct UndoVote { to: PublicUrl, - object: DislikePostOrComment, + object: Vote, cc: [Url; 1], #[serde(rename = "type")] kind: UndoType, @@ -22,7 +30,7 @@ pub struct UndoDislikePostOrComment { } #[async_trait::async_trait(?Send)] -impl ActivityHandler for UndoDislikePostOrComment { +impl ActivityHandler for UndoVote { async fn verify( &self, context: &LemmyContext, @@ -40,13 +48,15 @@ impl ActivityHandler for UndoDislikePostOrComment { context: &LemmyContext, request_counter: &mut i32, ) -> Result<(), LemmyError> { - receive_undo_like_or_dislike( - &self.common.actor, - &self.object.object, - context, - request_counter, - ) - .await + let actor = + get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?; + let object = + get_or_fetch_and_insert_post_or_comment(&self.object.object, context, request_counter) + .await?; + match object { + PostOrComment::Post(p) => undo_vote_post(actor, p.deref(), context).await, + PostOrComment::Comment(c) => undo_vote_comment(actor, c.deref(), context).await, + } } fn common(&self) -> &ActivityCommonFields { diff --git a/crates/apub/src/activities/voting/vote.rs b/crates/apub/src/activities/voting/vote.rs new file mode 100644 index 000000000..073c23dc0 --- /dev/null +++ b/crates/apub/src/activities/voting/vote.rs @@ -0,0 +1,76 @@ +use crate::{ + activities::{ + verify_activity, + verify_person_in_community, + voting::{vote_comment, vote_post}, + }, + fetcher::{ + objects::get_or_fetch_and_insert_post_or_comment, + person::get_or_fetch_and_upsert_person, + }, + PostOrComment, +}; +use lemmy_apub_lib::{values::PublicUrl, ActivityCommonFields, ActivityHandler}; +use lemmy_utils::LemmyError; +use lemmy_websocket::LemmyContext; +use serde::{Deserialize, Serialize}; +use std::ops::Deref; +use url::Url; + +#[derive(Clone, Debug, Deserialize, Serialize)] +pub enum VoteType { + Like, + Dislike, +} + +impl VoteType { + pub(crate) fn score(&self) -> i16 { + match self { + VoteType::Like => 1, + VoteType::Dislike => -1, + } + } +} +#[derive(Clone, Debug, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Vote { + to: PublicUrl, + pub(in crate::activities::voting) object: Url, + cc: [Url; 1], + #[serde(rename = "type")] + kind: VoteType, + #[serde(flatten)] + common: ActivityCommonFields, +} + +#[async_trait::async_trait(?Send)] +impl ActivityHandler for Vote { + async fn verify( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { + verify_activity(self.common())?; + verify_person_in_community(&self.common.actor, &self.cc[0], context, request_counter).await?; + Ok(()) + } + + async fn receive( + &self, + context: &LemmyContext, + request_counter: &mut i32, + ) -> Result<(), LemmyError> { + let actor = + get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?; + let object = + get_or_fetch_and_insert_post_or_comment(&self.object, context, request_counter).await?; + match object { + PostOrComment::Post(p) => vote_post(&self.kind, actor, p.deref(), context).await, + PostOrComment::Comment(c) => vote_comment(&self.kind, actor, c.deref(), context).await, + } + } + + fn common(&self) -> &ActivityCommonFields { + &self.common + } +} diff --git a/crates/apub/src/http/inbox_enums.rs b/crates/apub/src/http/inbox_enums.rs index f8ee0cb21..034048777 100644 --- a/crates/apub/src/http/inbox_enums.rs +++ b/crates/apub/src/http/inbox_enums.rs @@ -19,12 +19,7 @@ use crate::activities::{ remove::RemovePostCommentCommunityOrMod, undo_remove::UndoRemovePostCommentOrCommunity, }, - voting::{ - dislike::DislikePostOrComment, - like::LikePostOrComment, - undo_dislike::UndoDislikePostOrComment, - undo_like::UndoLikePostOrComment, - }, + voting::{undo_vote::UndoVote, vote::Vote}, }; use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler}; use lemmy_utils::LemmyError; @@ -48,10 +43,8 @@ pub enum GroupInboxActivities { UndoFollowCommunity(UndoFollowCommunity), CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdatePost(Box), - LikePostOrComment(LikePostOrComment), - DislikePostOrComment(DislikePostOrComment), - UndoLikePostOrComment(UndoLikePostOrComment), - UndoDislikePostOrComment(UndoDislikePostOrComment), + Vote(Vote), + UndoVote(UndoVote), DeletePostCommentOrCommunity(DeletePostCommentOrCommunity), UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity), RemovePostCommentOrCommunity(RemovePostCommentCommunityOrMod), @@ -70,10 +63,8 @@ pub enum SharedInboxActivities { UndoFollowCommunity(UndoFollowCommunity), CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdatePost(Box), - LikePostOrComment(LikePostOrComment), - DislikePostOrComment(DislikePostOrComment), - UndoDislikePostOrComment(UndoDislikePostOrComment), - UndoLikePostOrComment(UndoLikePostOrComment), + Vote(Vote), + UndoVote(UndoVote), DeletePostCommentOrCommunity(DeletePostCommentOrCommunity), UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity), RemovePostCommentOrCommunity(RemovePostCommentCommunityOrMod),