Merge like/dislike activity handlers into vote

This commit is contained in:
Felix Ableitner 2021-08-01 13:51:57 +02:00
parent 6b57d716e1
commit 869b1b6f8f
9 changed files with 127 additions and 275 deletions

View File

@ -19,12 +19,7 @@ use crate::{
}, },
verify_activity, verify_activity,
verify_community, verify_community,
voting::{ voting::{undo_vote::UndoVote, vote::Vote},
dislike::DislikePostOrComment,
like::LikePostOrComment,
undo_dislike::UndoDislikePostOrComment,
undo_like::UndoLikePostOrComment,
},
}, },
activity_queue::send_activity_new, activity_queue::send_activity_new,
extensions::context::lemmy_context, extensions::context::lemmy_context,
@ -46,10 +41,8 @@ use url::Url;
pub enum AnnouncableActivities { pub enum AnnouncableActivities {
CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdateComment(CreateOrUpdateComment),
CreateOrUpdatePost(Box<CreateOrUpdatePost>), CreateOrUpdatePost(Box<CreateOrUpdatePost>),
LikePostOrComment(LikePostOrComment), Vote(Vote),
DislikePostOrComment(DislikePostOrComment), UndoVote(UndoVote),
UndoLikePostOrComment(UndoLikePostOrComment),
UndoDislikePostOrComment(UndoDislikePostOrComment),
DeletePostCommentOrCommunity(DeletePostCommentOrCommunity), DeletePostCommentOrCommunity(DeletePostCommentOrCommunity),
UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity), UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity),
RemovePostCommentCommunityOrMod(RemovePostCommentCommunityOrMod), RemovePostCommentCommunityOrMod(RemovePostCommentCommunityOrMod),

View File

@ -31,7 +31,6 @@ pub mod send;
pub mod voting; pub mod voting;
#[derive(Clone, Debug, ToString, Deserialize, Serialize)] #[derive(Clone, Debug, ToString, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum CreateOrUpdateType { pub enum CreateOrUpdateType {
Create, Create,
Update, Update,

View File

@ -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
}
}

View File

@ -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
}
}

View File

@ -1,62 +1,33 @@
use crate::{ use crate::activities::{
activities::{
comment::send_websocket_message as send_comment_message, comment::send_websocket_message as send_comment_message,
post::send_websocket_message as send_post_message, post::send_websocket_message as send_post_message,
}, voting::vote::VoteType,
fetcher::{
objects::get_or_fetch_and_insert_post_or_comment,
person::get_or_fetch_and_upsert_person,
},
PostOrComment,
}; };
use lemmy_api_common::blocking; use lemmy_api_common::blocking;
use lemmy_db_queries::Likeable; use lemmy_db_queries::Likeable;
use lemmy_db_schema::source::{ use lemmy_db_schema::source::{
comment::{Comment, CommentLike, CommentLikeForm}, comment::{Comment, CommentLike, CommentLikeForm},
person::Person,
post::{Post, PostLike, PostLikeForm}, post::{Post, PostLike, PostLikeForm},
}; };
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::{LemmyContext, UserOperation}; use lemmy_websocket::{LemmyContext, UserOperation};
use std::ops::Deref;
use url::Url;
pub mod dislike; pub mod undo_vote;
pub mod like; pub mod vote;
pub mod undo_dislike;
pub mod undo_like;
pub(in crate::activities::voting) async fn receive_like_or_dislike( async fn vote_comment(
score: i16, vote_type: &VoteType,
actor: &Url, actor: Person,
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,
comment: &Comment, comment: &Comment,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?;
let comment_id = comment.id; let comment_id = comment.id;
let like_form = CommentLikeForm { let like_form = CommentLikeForm {
comment_id, comment_id,
post_id: comment.post_id, post_id: comment.post_id,
person_id: actor.id, person_id: actor.id,
score, score: vote_type.score(),
}; };
let person_id = actor.id; let person_id = actor.id;
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
@ -74,20 +45,17 @@ async fn like_or_dislike_comment(
.await .await
} }
async fn like_or_dislike_post( async fn vote_post(
score: i16, vote_type: &VoteType,
actor: &Url, actor: Person,
post: &Post, post: &Post,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?;
let post_id = post.id; let post_id = post.id;
let like_form = PostLikeForm { let like_form = PostLikeForm {
post_id: post.id, post_id: post.id,
person_id: actor.id, person_id: actor.id,
score, score: vote_type.score(),
}; };
let person_id = actor.id; let person_id = actor.id;
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
@ -99,30 +67,11 @@ async fn like_or_dislike_post(
send_post_message(post.id, UserOperation::CreatePostLike, context).await send_post_message(post.id, UserOperation::CreatePostLike, context).await
} }
pub(in crate::activities::voting) async fn receive_undo_like_or_dislike( async fn undo_vote_comment(
actor: &Url, actor: Person,
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,
comment: &Comment, comment: &Comment,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?;
let comment_id = comment.id; let comment_id = comment.id;
let person_id = actor.id; let person_id = actor.id;
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {
@ -139,14 +88,11 @@ async fn undo_like_or_dislike_comment(
.await .await
} }
async fn undo_like_or_dislike_post( async fn undo_vote_post(
actor: &Url, actor: Person,
post: &Post, post: &Post,
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
let actor = get_or_fetch_and_upsert_person(actor, context, request_counter).await?;
let post_id = post.id; let post_id = post.id;
let person_id = actor.id; let person_id = actor.id;
blocking(context.pool(), move |conn| { blocking(context.pool(), move |conn| {

View File

@ -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
}
}

View File

@ -1,19 +1,27 @@
use crate::activities::{ use crate::{
activities::{
verify_activity, verify_activity,
verify_person_in_community, verify_person_in_community,
voting::{dislike::DislikePostOrComment, receive_undo_like_or_dislike}, 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 activitystreams::activity::kind::UndoType;
use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler}; use lemmy_apub_lib::{values::PublicUrl, verify_urls_match, ActivityCommonFields, ActivityHandler};
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use std::ops::Deref;
use url::Url; use url::Url;
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)] #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct UndoDislikePostOrComment { pub struct UndoVote {
to: PublicUrl, to: PublicUrl,
object: DislikePostOrComment, object: Vote,
cc: [Url; 1], cc: [Url; 1],
#[serde(rename = "type")] #[serde(rename = "type")]
kind: UndoType, kind: UndoType,
@ -22,7 +30,7 @@ pub struct UndoDislikePostOrComment {
} }
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
impl ActivityHandler for UndoDislikePostOrComment { impl ActivityHandler for UndoVote {
async fn verify( async fn verify(
&self, &self,
context: &LemmyContext, context: &LemmyContext,
@ -40,13 +48,15 @@ impl ActivityHandler for UndoDislikePostOrComment {
context: &LemmyContext, context: &LemmyContext,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
receive_undo_like_or_dislike( let actor =
&self.common.actor, get_or_fetch_and_upsert_person(&self.common.actor, context, request_counter).await?;
&self.object.object, let object =
context, get_or_fetch_and_insert_post_or_comment(&self.object.object, context, request_counter)
request_counter, .await?;
) match object {
.await 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 { fn common(&self) -> &ActivityCommonFields {

View File

@ -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
}
}

View File

@ -19,12 +19,7 @@ use crate::activities::{
remove::RemovePostCommentCommunityOrMod, remove::RemovePostCommentCommunityOrMod,
undo_remove::UndoRemovePostCommentOrCommunity, undo_remove::UndoRemovePostCommentOrCommunity,
}, },
voting::{ voting::{undo_vote::UndoVote, vote::Vote},
dislike::DislikePostOrComment,
like::LikePostOrComment,
undo_dislike::UndoDislikePostOrComment,
undo_like::UndoLikePostOrComment,
},
}; };
use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler}; use lemmy_apub_lib::{ActivityCommonFields, ActivityHandler};
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
@ -48,10 +43,8 @@ pub enum GroupInboxActivities {
UndoFollowCommunity(UndoFollowCommunity), UndoFollowCommunity(UndoFollowCommunity),
CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdateComment(CreateOrUpdateComment),
CreateOrUpdatePost(Box<CreateOrUpdatePost>), CreateOrUpdatePost(Box<CreateOrUpdatePost>),
LikePostOrComment(LikePostOrComment), Vote(Vote),
DislikePostOrComment(DislikePostOrComment), UndoVote(UndoVote),
UndoLikePostOrComment(UndoLikePostOrComment),
UndoDislikePostOrComment(UndoDislikePostOrComment),
DeletePostCommentOrCommunity(DeletePostCommentOrCommunity), DeletePostCommentOrCommunity(DeletePostCommentOrCommunity),
UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity), UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity),
RemovePostCommentOrCommunity(RemovePostCommentCommunityOrMod), RemovePostCommentOrCommunity(RemovePostCommentCommunityOrMod),
@ -70,10 +63,8 @@ pub enum SharedInboxActivities {
UndoFollowCommunity(UndoFollowCommunity), UndoFollowCommunity(UndoFollowCommunity),
CreateOrUpdateComment(CreateOrUpdateComment), CreateOrUpdateComment(CreateOrUpdateComment),
CreateOrUpdatePost(Box<CreateOrUpdatePost>), CreateOrUpdatePost(Box<CreateOrUpdatePost>),
LikePostOrComment(LikePostOrComment), Vote(Vote),
DislikePostOrComment(DislikePostOrComment), UndoVote(UndoVote),
UndoDislikePostOrComment(UndoDislikePostOrComment),
UndoLikePostOrComment(UndoLikePostOrComment),
DeletePostCommentOrCommunity(DeletePostCommentOrCommunity), DeletePostCommentOrCommunity(DeletePostCommentOrCommunity),
UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity), UndoDeletePostCommentOrCommunity(UndoDeletePostCommentOrCommunity),
RemovePostCommentOrCommunity(RemovePostCommentCommunityOrMod), RemovePostCommentOrCommunity(RemovePostCommentCommunityOrMod),