Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make vote timestamps optional (ref #5016) #5122

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions config/defaults.hjson
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
database: "string"
# Maximum number of active sql connections
pool_size: 30
# Set true to store the time when votes were cast. Requires more storage space but can be
# used to detect vote manipulation.
store_vote_timestamps: false
}
# Pictrs image server configuration.
pictrs: {
Expand Down
27 changes: 11 additions & 16 deletions crates/api/src/comment/like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ use lemmy_api_common::{
};
use lemmy_db_schema::{
newtypes::LocalUserId,
source::{
comment::{CommentLike, CommentLikeForm},
comment_reply::CommentReply,
local_site::LocalSite,
},
source::{comment::CommentLike, comment_reply::CommentReply, local_site::LocalSite},
traits::Likeable,
};
use lemmy_db_views::structs::{CommentView, LocalUserView};
Expand Down Expand Up @@ -65,24 +61,23 @@ pub async fn like_comment(
}
}

let like_form = CommentLikeForm {
comment_id: data.comment_id,
post_id: orig_comment.post.id,
person_id: local_user_view.person.id,
score: data.score,
};

// Remove any likes first
let person_id = local_user_view.person.id;

CommentLike::remove(&mut context.pool(), person_id, comment_id).await?;

// Only add the like if the score isnt 0
let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
let do_add = data.score != 0 && (data.score == 1 || data.score == -1);
if do_add {
CommentLike::like(&mut context.pool(), &like_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikeComment)?;
CommentLike::like(
&mut context.pool(),
person_id,
data.comment_id,
data.score,
context.settings(),
)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikeComment)?;
}

ActivityChannel::submit_activity(
Expand Down
22 changes: 11 additions & 11 deletions crates/api/src/post/like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use lemmy_db_schema::{
source::{
community::Community,
local_site::LocalSite,
post::{Post, PostLike, PostLikeForm},
post::{Post, PostLike},
},
traits::{Crud, Likeable},
};
Expand Down Expand Up @@ -54,23 +54,23 @@ pub async fn like_post(
)
.await?;

let like_form = PostLikeForm {
post_id: data.post_id,
person_id: local_user_view.person.id,
score: data.score,
};

// Remove any likes first
let person_id = local_user_view.person.id;

PostLike::remove(&mut context.pool(), person_id, post_id).await?;

// Only add the like if the score isnt 0
let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
let do_add = data.score != 0 && (data.score == 1 || data.score == -1);
if do_add {
PostLike::like(&mut context.pool(), &like_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikePost)?;
PostLike::like(
&mut context.pool(),
person_id,
post_id,
data.score,
context.settings(),
)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikePost)?;
}

mark_post_as_read(person_id, post_id, &mut context.pool()).await?;
Expand Down
21 changes: 10 additions & 11 deletions crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use lemmy_db_schema::{
impls::actor_language::default_post_language,
source::{
actor_language::CommunityLanguage,
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
comment::{Comment, CommentInsertForm, CommentLike},
comment_reply::{CommentReply, CommentReplyUpdateForm},
local_site::LocalSite,
person_mention::{PersonMention, PersonMentionUpdateForm},
Expand Down Expand Up @@ -130,16 +130,15 @@ pub async fn create_comment(
.await?;

// You like your own comment by default
let like_form = CommentLikeForm {
comment_id: inserted_comment.id,
post_id: post.id,
person_id: local_user_view.person.id,
score: 1,
};

CommentLike::like(&mut context.pool(), &like_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikeComment)?;
CommentLike::like(
&mut context.pool(),
local_user_view.person.id,
inserted_comment.id,
1,
context.settings(),
)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikeComment)?;

ActivityChannel::submit_activity(
SendActivityData::CreateComment(inserted_comment.clone()),
Expand Down
18 changes: 9 additions & 9 deletions crates/api_crud/src/post/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use lemmy_db_schema::{
actor_language::CommunityLanguage,
community::Community,
local_site::LocalSite,
post::{Post, PostInsertForm, PostLike, PostLikeForm},
post::{Post, PostInsertForm, PostLike},
},
traits::{Crud, Likeable},
utils::diesel_url_create,
Expand Down Expand Up @@ -159,15 +159,15 @@ pub async fn create_post(
// They like their own post by default
let person_id = local_user_view.person.id;
let post_id = inserted_post.id;
let like_form = PostLikeForm {
post_id,
PostLike::like(
&mut context.pool(),
person_id,
score: 1,
};

PostLike::like(&mut context.pool(), &like_form)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikePost)?;
post_id,
1,
context.settings(),
)
.await
.with_lemmy_type(LemmyErrorType::CouldntLikePost)?;

mark_post_as_read(person_id, post_id, &mut context.pool()).await?;

Expand Down
17 changes: 9 additions & 8 deletions crates/apub/src/activities/create_or_update/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use lemmy_db_schema::{
newtypes::PersonId,
source::{
activity::ActivitySendTargets,
comment::{Comment, CommentLike, CommentLikeForm},
comment::{Comment, CommentLike},
community::Community,
person::Person,
post::Post,
Expand Down Expand Up @@ -151,13 +151,14 @@ impl ActivityHandler for CreateOrUpdateNote {
let comment = ApubComment::from_json(self.object, context).await?;

// author likes their own comment by default
let like_form = CommentLikeForm {
comment_id: comment.id,
post_id: comment.post_id,
person_id: comment.creator_id,
score: 1,
};
CommentLike::like(&mut context.pool(), &like_form).await?;
CommentLike::like(
&mut context.pool(),
comment.creator_id,
comment.id,
1,
context.settings(),
)
.await?;

// Calculate initial hot_rank
CommentAggregates::update_hot_rank(&mut context.pool(), comment.id).await?;
Expand Down
16 changes: 9 additions & 7 deletions crates/apub/src/activities/create_or_update/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use lemmy_db_schema::{
activity::ActivitySendTargets,
community::Community,
person::Person,
post::{Post, PostLike, PostLikeForm},
post::{Post, PostLike},
},
traits::{Crud, Likeable},
};
Expand Down Expand Up @@ -118,12 +118,14 @@ impl ActivityHandler for CreateOrUpdatePage {
let post = ApubPost::from_json(self.object, context).await?;

// author likes their own post by default
let like_form = PostLikeForm {
post_id: post.id,
person_id: post.creator_id,
score: 1,
};
PostLike::like(&mut context.pool(), &like_form).await?;
PostLike::like(
&mut context.pool(),
post.creator_id,
post.id,
1,
context.settings(),
)
.await?;

// Calculate initial hot_rank for post
PostAggregates::update_ranks(&mut context.pool(), post.id).await?;
Expand Down
41 changes: 20 additions & 21 deletions crates/apub/src/activities/voting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ use lemmy_db_schema::{
newtypes::DbUrl,
source::{
activity::ActivitySendTargets,
comment::{CommentLike, CommentLikeForm},
comment::CommentLike,
community::Community,
person::Person,
post::{PostLike, PostLikeForm},
post::PostLike,
},
traits::Likeable,
};
Expand Down Expand Up @@ -59,16 +59,15 @@ async fn vote_comment(
comment: &ApubComment,
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
let comment_id = comment.id;
let like_form = CommentLikeForm {
comment_id,
post_id: comment.post_id,
person_id: actor.id,
score: vote_type.into(),
};
let person_id = actor.id;
CommentLike::remove(&mut context.pool(), person_id, comment_id).await?;
CommentLike::like(&mut context.pool(), &like_form).await?;
CommentLike::remove(&mut context.pool(), actor.id, comment.id).await?;
CommentLike::like(
&mut context.pool(),
actor.id,
comment.id,
vote_type.into(),
context.settings(),
)
.await?;
Ok(())
}

Expand All @@ -79,15 +78,15 @@ async fn vote_post(
post: &ApubPost,
context: &Data<LemmyContext>,
) -> LemmyResult<()> {
let post_id = post.id;
let like_form = PostLikeForm {
post_id: post.id,
person_id: actor.id,
score: vote_type.into(),
};
let person_id = actor.id;
PostLike::remove(&mut context.pool(), person_id, post_id).await?;
PostLike::like(&mut context.pool(), &like_form).await?;
PostLike::remove(&mut context.pool(), actor.id, post.id).await?;
PostLike::like(
&mut context.pool(),
actor.id,
post.id,
vote_type.into(),
context.settings(),
)
.await?;
Ok(())
}

Expand Down
29 changes: 12 additions & 17 deletions crates/db_schema/src/aggregates/comment_aggregates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mod tests {
use crate::{
aggregates::comment_aggregates::CommentAggregates,
source::{
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
comment::{Comment, CommentInsertForm, CommentLike},
community::{Community, CommunityInsertForm},
instance::Instance,
person::{Person, PersonInsertForm},
Expand All @@ -45,6 +45,7 @@ mod tests {
utils::build_db_pool_for_tests,
};
use diesel::result::Error;
use lemmy_utils::settings::structs::Settings;
use pretty_assertions::assert_eq;
use serial_test::serial;

Expand Down Expand Up @@ -94,14 +95,8 @@ mod tests {
let _inserted_child_comment =
Comment::create(pool, &child_comment_form, Some(&inserted_comment.path)).await?;

let comment_like = CommentLikeForm {
comment_id: inserted_comment.id,
post_id: inserted_post.id,
person_id: inserted_person.id,
score: 1,
};

CommentLike::like(pool, &comment_like).await?;
let settings = Settings::default();
CommentLike::like(pool, inserted_person.id, inserted_comment.id, 1, &settings).await?;

let comment_aggs_before_delete = CommentAggregates::read(pool, inserted_comment.id).await?;

Expand All @@ -110,14 +105,14 @@ mod tests {
assert_eq!(0, comment_aggs_before_delete.downvotes);

// Add a post dislike from the other person
let comment_dislike = CommentLikeForm {
comment_id: inserted_comment.id,
post_id: inserted_post.id,
person_id: another_inserted_person.id,
score: -1,
};

CommentLike::like(pool, &comment_dislike).await?;
CommentLike::like(
pool,
another_inserted_person.id,
inserted_comment.id,
-1,
&settings,
)
.await?;

let comment_aggs_after_dislike = CommentAggregates::read(pool, inserted_comment.id).await?;

Expand Down
Loading