2024-01-03 18:39:21 +00:00
|
|
|
use crate::structs::VoteView;
|
2024-03-26 15:22:04 +00:00
|
|
|
use diesel::{
|
|
|
|
result::Error,
|
|
|
|
BoolExpressionMethods,
|
|
|
|
ExpressionMethods,
|
|
|
|
JoinOnDsl,
|
|
|
|
NullableExpressionMethods,
|
|
|
|
QueryDsl,
|
|
|
|
};
|
2024-01-03 18:39:21 +00:00
|
|
|
use diesel_async::RunQueryDsl;
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
newtypes::{CommentId, PostId},
|
2024-03-26 15:22:04 +00:00
|
|
|
schema::{comment_like, community_person_ban, person, post, post_like},
|
2024-01-03 18:39:21 +00:00
|
|
|
utils::{get_conn, limit_and_offset, DbPool},
|
|
|
|
};
|
|
|
|
|
|
|
|
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)?;
|
|
|
|
|
|
|
|
post_like::table
|
|
|
|
.inner_join(person::table)
|
2024-03-26 15:22:04 +00:00
|
|
|
.inner_join(post::table)
|
|
|
|
// Join to community_person_ban to get creator_banned_from_community
|
|
|
|
.left_join(
|
|
|
|
community_person_ban::table.on(
|
|
|
|
post::community_id
|
|
|
|
.eq(community_person_ban::community_id)
|
|
|
|
.and(community_person_ban::person_id.eq(post_like::person_id)),
|
|
|
|
),
|
|
|
|
)
|
2024-01-03 18:39:21 +00:00
|
|
|
.filter(post_like::post_id.eq(post_id))
|
2024-03-26 15:22:04 +00:00
|
|
|
.select((
|
|
|
|
person::all_columns,
|
|
|
|
community_person_ban::community_id.nullable().is_not_null(),
|
|
|
|
post_like::score,
|
|
|
|
))
|
2024-01-03 18:39:21 +00:00
|
|
|
.order_by(post_like::score)
|
|
|
|
.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)?;
|
|
|
|
|
|
|
|
comment_like::table
|
|
|
|
.inner_join(person::table)
|
2024-03-26 15:22:04 +00:00
|
|
|
.inner_join(post::table)
|
|
|
|
// Join to community_person_ban to get creator_banned_from_community
|
|
|
|
.left_join(
|
|
|
|
community_person_ban::table.on(
|
|
|
|
post::community_id
|
|
|
|
.eq(community_person_ban::community_id)
|
|
|
|
.and(community_person_ban::person_id.eq(comment_like::person_id)),
|
|
|
|
),
|
|
|
|
)
|
2024-01-03 18:39:21 +00:00
|
|
|
.filter(comment_like::comment_id.eq(comment_id))
|
2024-03-26 15:22:04 +00:00
|
|
|
.select((
|
|
|
|
person::all_columns,
|
|
|
|
community_person_ban::community_id.nullable().is_not_null(),
|
|
|
|
comment_like::score,
|
|
|
|
))
|
2024-01-03 18:39:21 +00:00
|
|
|
.order_by(comment_like::score)
|
|
|
|
.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-01-03 18:39:21 +00:00
|
|
|
let pool = &build_db_pool_for_tests().await;
|
|
|
|
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
|
|
|
|
let timmy_post_vote_form = PostLikeForm {
|
|
|
|
post_id: inserted_post.id,
|
|
|
|
person_id: inserted_timmy.id,
|
|
|
|
score: 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
|
|
|
|
let sara_post_vote_form = PostLikeForm {
|
|
|
|
post_id: inserted_post.id,
|
|
|
|
person_id: inserted_sara.id,
|
|
|
|
score: -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 {
|
|
|
|
post_id: inserted_post.id,
|
|
|
|
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 {
|
|
|
|
post_id: inserted_post.id,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|