2024-01-03 18:39:21 +00:00
|
|
|
use crate::structs::VoteView;
|
2024-11-11 10:34:10 +00:00
|
|
|
use diesel::{result::Error, ExpressionMethods, NullableExpressionMethods, QueryDsl};
|
2024-01-03 18:39:21 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
|
|
|
use lemmy_db_schema::{
|
2024-11-11 10:34:10 +00:00
|
|
|
aliases::creator_community_actions,
|
2024-01-03 18:39:21 +00:00
|
|
|
newtypes::{CommentId, PostId},
|
2024-11-11 10:34:10 +00:00
|
|
|
schema::{comment, comment_actions, community_actions, person, post, post_actions},
|
|
|
|
utils::{action_query, actions_alias, get_conn, limit_and_offset, DbPool},
|
2024-01-03 18:39:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
impl VoteView {
|
|
|
|
pub async fn list_for_post(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
post_id: PostId,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
let (limit, offset) = limit_and_offset(page, limit)?;
|
|
|
|
|
2024-11-11 10:34:10 +00:00
|
|
|
action_query(post_actions::like_score)
|
2024-01-03 18:39:21 +00:00
|
|
|
.inner_join(person::table)
|
2024-03-26 15:22:04 +00:00
|
|
|
.inner_join(post::table)
|
2024-11-11 10:34:10 +00:00
|
|
|
.left_join(actions_alias(
|
|
|
|
creator_community_actions,
|
|
|
|
post_actions::person_id,
|
|
|
|
post::community_id,
|
|
|
|
))
|
|
|
|
.filter(post_actions::post_id.eq(post_id))
|
2024-03-26 15:22:04 +00:00
|
|
|
.select((
|
|
|
|
person::all_columns,
|
2024-11-11 10:34:10 +00:00
|
|
|
creator_community_actions
|
|
|
|
.field(community_actions::received_ban)
|
|
|
|
.nullable()
|
|
|
|
.is_not_null(),
|
|
|
|
post_actions::like_score.assume_not_null(),
|
2024-03-26 15:22:04 +00:00
|
|
|
))
|
2024-11-11 10:34:10 +00:00
|
|
|
.order_by(post_actions::like_score)
|
2024-01-03 18:39:21 +00:00
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
.load::<Self>(conn)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn list_for_comment(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
comment_id: CommentId,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
) -> Result<Vec<Self>, Error> {
|
|
|
|
let conn = &mut get_conn(pool).await?;
|
|
|
|
let (limit, offset) = limit_and_offset(page, limit)?;
|
|
|
|
|
2024-11-11 10:34:10 +00:00
|
|
|
action_query(comment_actions::like_score)
|
2024-01-03 18:39:21 +00:00
|
|
|
.inner_join(person::table)
|
2024-11-11 10:34:10 +00:00
|
|
|
.inner_join(comment::table.inner_join(post::table))
|
|
|
|
.left_join(actions_alias(
|
|
|
|
creator_community_actions,
|
|
|
|
comment_actions::person_id,
|
|
|
|
post::community_id,
|
|
|
|
))
|
|
|
|
.filter(comment_actions::comment_id.eq(comment_id))
|
2024-03-26 15:22:04 +00:00
|
|
|
.select((
|
|
|
|
person::all_columns,
|
2024-11-11 10:34:10 +00:00
|
|
|
creator_community_actions
|
|
|
|
.field(community_actions::received_ban)
|
|
|
|
.nullable()
|
|
|
|
.is_not_null(),
|
|
|
|
comment_actions::like_score.assume_not_null(),
|
2024-03-26 15:22:04 +00:00
|
|
|
))
|
2024-11-11 10:34:10 +00:00
|
|
|
.order_by(comment_actions::like_score)
|
2024-01-03 18:39:21 +00:00
|
|
|
.limit(limit)
|
|
|
|
.offset(offset)
|
|
|
|
.load::<Self>(conn)
|
|
|
|
.await
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
use crate::structs::VoteView;
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
|
|
|
comment::{Comment, CommentInsertForm, CommentLike, CommentLikeForm},
|
2024-03-26 15:22:04 +00:00
|
|
|
community::{Community, CommunityInsertForm, CommunityPersonBan, CommunityPersonBanForm},
|
2024-01-03 18:39:21 +00:00
|
|
|
instance::Instance,
|
|
|
|
person::{Person, PersonInsertForm},
|
|
|
|
post::{Post, PostInsertForm, PostLike, PostLikeForm},
|
|
|
|
},
|
2024-03-26 15:22:04 +00:00
|
|
|
traits::{Bannable, Crud, Likeable},
|
2024-01-03 18:39:21 +00:00
|
|
|
utils::build_db_pool_for_tests,
|
|
|
|
};
|
2024-10-02 12:50:21 +00:00
|
|
|
use lemmy_utils::error::LemmyResult;
|
2024-01-04 09:47:18 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2024-01-03 18:39:21 +00:00
|
|
|
use serial_test::serial;
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
#[serial]
|
2024-10-02 12:50:21 +00:00
|
|
|
async fn post_and_comment_vote_views() -> LemmyResult<()> {
|
2024-11-07 10:49:05 +00:00
|
|
|
let pool = &build_db_pool_for_tests();
|
2024-01-03 18:39:21 +00:00
|
|
|
let pool = &mut pool.into();
|
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
2024-06-06 12:29:18 +00:00
|
|
|
let new_person = PersonInsertForm::test_form(inserted_instance.id, "timmy_vv");
|
2024-01-03 18:39:21 +00:00
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_timmy = Person::create(pool, &new_person).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
2024-06-06 12:29:18 +00:00
|
|
|
let new_person_2 = PersonInsertForm::test_form(inserted_instance.id, "sara_vv");
|
2024-01-03 18:39:21 +00:00
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_sara = Person::create(pool, &new_person_2).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
2024-09-20 12:15:25 +00:00
|
|
|
let new_community = CommunityInsertForm::new(
|
|
|
|
inserted_instance.id,
|
|
|
|
"test community vv".to_string(),
|
|
|
|
"nada".to_owned(),
|
|
|
|
"pubkey".to_string(),
|
|
|
|
);
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_community = Community::create(pool, &new_community).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
2024-09-20 12:15:25 +00:00
|
|
|
let new_post = PostInsertForm::new(
|
|
|
|
"A test post vv".into(),
|
|
|
|
inserted_timmy.id,
|
|
|
|
inserted_community.id,
|
|
|
|
);
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_post = Post::create(pool, &new_post).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
2024-09-20 12:15:25 +00:00
|
|
|
let comment_form = CommentInsertForm::new(
|
|
|
|
inserted_timmy.id,
|
|
|
|
inserted_post.id,
|
|
|
|
"A test comment vv".into(),
|
|
|
|
);
|
2024-10-02 12:50:21 +00:00
|
|
|
let inserted_comment = Comment::create(pool, &comment_form, None).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
|
|
|
// Timmy upvotes his own post
|
2024-11-15 10:21:08 +00:00
|
|
|
let timmy_post_vote_form = PostLikeForm::new(inserted_post.id, inserted_timmy.id, 1);
|
2024-10-02 12:50:21 +00:00
|
|
|
PostLike::like(pool, &timmy_post_vote_form).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
|
|
|
// Sara downvotes timmy's post
|
2024-11-15 10:21:08 +00:00
|
|
|
let sara_post_vote_form = PostLikeForm::new(inserted_post.id, inserted_sara.id, -1);
|
2024-10-02 12:50:21 +00:00
|
|
|
PostLike::like(pool, &sara_post_vote_form).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
|
|
|
let expected_post_vote_views = [
|
|
|
|
VoteView {
|
|
|
|
creator: inserted_sara.clone(),
|
2024-03-26 15:22:04 +00:00
|
|
|
creator_banned_from_community: false,
|
2024-01-03 18:39:21 +00:00
|
|
|
score: -1,
|
|
|
|
},
|
|
|
|
VoteView {
|
|
|
|
creator: inserted_timmy.clone(),
|
2024-03-26 15:22:04 +00:00
|
|
|
creator_banned_from_community: false,
|
2024-01-03 18:39:21 +00:00
|
|
|
score: 1,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
let read_post_vote_views = VoteView::list_for_post(pool, inserted_post.id, None, None).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
assert_eq!(read_post_vote_views, expected_post_vote_views);
|
|
|
|
|
|
|
|
// Timothy votes down his own comment
|
|
|
|
let timmy_comment_vote_form = CommentLikeForm {
|
|
|
|
comment_id: inserted_comment.id,
|
|
|
|
person_id: inserted_timmy.id,
|
|
|
|
score: -1,
|
|
|
|
};
|
2024-10-02 12:50:21 +00:00
|
|
|
CommentLike::like(pool, &timmy_comment_vote_form).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
|
|
|
// Sara upvotes timmy's comment
|
|
|
|
let sara_comment_vote_form = CommentLikeForm {
|
|
|
|
comment_id: inserted_comment.id,
|
|
|
|
person_id: inserted_sara.id,
|
|
|
|
score: 1,
|
|
|
|
};
|
2024-10-02 12:50:21 +00:00
|
|
|
CommentLike::like(pool, &sara_comment_vote_form).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
|
|
|
|
let expected_comment_vote_views = [
|
|
|
|
VoteView {
|
|
|
|
creator: inserted_timmy.clone(),
|
2024-03-26 15:22:04 +00:00
|
|
|
creator_banned_from_community: false,
|
2024-01-03 18:39:21 +00:00
|
|
|
score: -1,
|
|
|
|
},
|
|
|
|
VoteView {
|
|
|
|
creator: inserted_sara.clone(),
|
2024-03-26 15:22:04 +00:00
|
|
|
creator_banned_from_community: false,
|
2024-01-03 18:39:21 +00:00
|
|
|
score: 1,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
let read_comment_vote_views =
|
|
|
|
VoteView::list_for_comment(pool, inserted_comment.id, None, None).await?;
|
2024-01-03 18:39:21 +00:00
|
|
|
assert_eq!(read_comment_vote_views, expected_comment_vote_views);
|
|
|
|
|
2024-03-26 15:22:04 +00:00
|
|
|
// Ban timmy from that community
|
|
|
|
let ban_timmy_form = CommunityPersonBanForm {
|
|
|
|
community_id: inserted_community.id,
|
|
|
|
person_id: inserted_timmy.id,
|
|
|
|
expires: None,
|
|
|
|
};
|
2024-10-02 12:50:21 +00:00
|
|
|
CommunityPersonBan::ban(pool, &ban_timmy_form).await?;
|
2024-03-26 15:22:04 +00:00
|
|
|
|
|
|
|
// Make sure creator_banned_from_community is true
|
|
|
|
let read_comment_vote_views_after_ban =
|
2024-10-02 12:50:21 +00:00
|
|
|
VoteView::list_for_comment(pool, inserted_comment.id, None, None).await?;
|
2024-03-26 15:22:04 +00:00
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
assert!(read_comment_vote_views_after_ban
|
|
|
|
.first()
|
|
|
|
.is_some_and(|c| c.creator_banned_from_community));
|
2024-03-26 15:22:04 +00:00
|
|
|
|
|
|
|
let read_post_vote_views_after_ban =
|
2024-10-02 12:50:21 +00:00
|
|
|
VoteView::list_for_post(pool, inserted_post.id, None, None).await?;
|
2024-03-26 15:22:04 +00:00
|
|
|
|
2024-10-02 12:50:21 +00:00
|
|
|
assert!(read_post_vote_views_after_ban
|
|
|
|
.get(1)
|
|
|
|
.is_some_and(|p| p.creator_banned_from_community));
|
2024-03-26 15:22:04 +00:00
|
|
|
|
2024-01-03 18:39:21 +00:00
|
|
|
// Cleanup
|
2024-10-02 12:50:21 +00:00
|
|
|
Instance::delete(pool, inserted_instance.id).await?;
|
|
|
|
|
|
|
|
Ok(())
|
2024-01-03 18:39:21 +00:00
|
|
|
}
|
|
|
|
}
|