mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-13 06:08:20 +00:00
Finishing up inbox.
This commit is contained in:
parent
05f218d53c
commit
41cfdca1cf
|
@ -14,6 +14,7 @@ pub async fn list_inbox(
|
||||||
local_user_view: LocalUserView,
|
local_user_view: LocalUserView,
|
||||||
) -> LemmyResult<Json<ListInboxResponse>> {
|
) -> LemmyResult<Json<ListInboxResponse>> {
|
||||||
let unread_only = data.unread_only;
|
let unread_only = data.unread_only;
|
||||||
|
let type_ = data.type_;
|
||||||
let person_id = local_user_view.person.id;
|
let person_id = local_user_view.person.id;
|
||||||
let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts);
|
let show_bot_accounts = Some(local_user_view.local_user.show_bot_accounts);
|
||||||
|
|
||||||
|
@ -26,13 +27,13 @@ pub async fn list_inbox(
|
||||||
let page_back = data.page_back;
|
let page_back = data.page_back;
|
||||||
|
|
||||||
let inbox = InboxCombinedQuery {
|
let inbox = InboxCombinedQuery {
|
||||||
my_person_id: person_id,
|
type_,
|
||||||
unread_only,
|
unread_only,
|
||||||
show_bot_accounts,
|
show_bot_accounts,
|
||||||
page_after,
|
page_after,
|
||||||
page_back,
|
page_back,
|
||||||
}
|
}
|
||||||
.list(&mut context.pool())
|
.list(&mut context.pool(), person_id)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
Ok(Json(ListInboxResponse { inbox }))
|
Ok(Json(ListInboxResponse { inbox }))
|
||||||
|
|
|
@ -10,6 +10,7 @@ use lemmy_db_schema::{
|
||||||
sensitive::SensitiveString,
|
sensitive::SensitiveString,
|
||||||
source::{login_token::LoginToken, site::Site},
|
source::{login_token::LoginToken, site::Site},
|
||||||
CommentSortType,
|
CommentSortType,
|
||||||
|
InboxDataType,
|
||||||
ListingType,
|
ListingType,
|
||||||
PostListingMode,
|
PostListingMode,
|
||||||
PostSortType,
|
PostSortType,
|
||||||
|
@ -380,6 +381,8 @@ pub struct BlockPersonResponse {
|
||||||
#[cfg_attr(feature = "full", ts(export))]
|
#[cfg_attr(feature = "full", ts(export))]
|
||||||
/// Get your inbox (replies, comment mentions, post mentions, and messages)
|
/// Get your inbox (replies, comment mentions, post mentions, and messages)
|
||||||
pub struct ListInbox {
|
pub struct ListInbox {
|
||||||
|
#[cfg_attr(feature = "full", ts(optional))]
|
||||||
|
pub type_: Option<InboxDataType>,
|
||||||
#[cfg_attr(feature = "full", ts(optional))]
|
#[cfg_attr(feature = "full", ts(optional))]
|
||||||
pub unread_only: Option<bool>,
|
pub unread_only: Option<bool>,
|
||||||
#[cfg_attr(feature = "full", ts(optional))]
|
#[cfg_attr(feature = "full", ts(optional))]
|
||||||
|
|
|
@ -219,6 +219,18 @@ pub enum ModlogActionType {
|
||||||
AdminAllowInstance,
|
AdminAllowInstance,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
#[cfg_attr(feature = "full", derive(TS))]
|
||||||
|
#[cfg_attr(feature = "full", ts(export))]
|
||||||
|
/// A list of possible types for the inbox.
|
||||||
|
pub enum InboxDataType {
|
||||||
|
All,
|
||||||
|
CommentReply,
|
||||||
|
CommentMention,
|
||||||
|
PostMention,
|
||||||
|
PrivateMessage,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(
|
#[derive(
|
||||||
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash,
|
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash,
|
||||||
)]
|
)]
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
use crate::structs::CommentReplyView;
|
use crate::structs::CommentReplyView;
|
||||||
use diesel::{
|
use diesel::{
|
||||||
dsl::{exists, not},
|
dsl::exists,
|
||||||
pg::Pg,
|
|
||||||
result::Error,
|
result::Error,
|
||||||
BoolExpressionMethods,
|
BoolExpressionMethods,
|
||||||
ExpressionMethods,
|
ExpressionMethods,
|
||||||
|
@ -25,26 +24,18 @@ use lemmy_db_schema::{
|
||||||
person_actions,
|
person_actions,
|
||||||
post,
|
post,
|
||||||
},
|
},
|
||||||
source::{community::CommunityFollower, local_user::LocalUser},
|
source::community::CommunityFollower,
|
||||||
utils::{
|
utils::{actions, actions_alias, get_conn, DbPool},
|
||||||
actions,
|
|
||||||
actions_alias,
|
|
||||||
get_conn,
|
|
||||||
limit_and_offset,
|
|
||||||
DbConn,
|
|
||||||
DbPool,
|
|
||||||
ListFn,
|
|
||||||
Queries,
|
|
||||||
ReadFn,
|
|
||||||
},
|
|
||||||
CommentSortType,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO get rid of all this
|
impl CommentReplyView {
|
||||||
fn queries<'a>() -> Queries<
|
pub async fn read(
|
||||||
impl ReadFn<'a, CommentReplyView, (CommentReplyId, Option<PersonId>)>,
|
pool: &mut DbPool<'_>,
|
||||||
impl ListFn<'a, CommentReplyView, CommentReplyQuery>,
|
comment_reply_id: CommentReplyId,
|
||||||
> {
|
my_person_id: Option<PersonId>,
|
||||||
|
) -> Result<Self, Error> {
|
||||||
|
let conn = &mut get_conn(pool).await?;
|
||||||
|
|
||||||
let creator_is_admin = exists(
|
let creator_is_admin = exists(
|
||||||
local_user::table.filter(
|
local_user::table.filter(
|
||||||
comment::creator_id
|
comment::creator_id
|
||||||
|
@ -53,9 +44,8 @@ fn queries<'a>() -> Queries<
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
let all_joins = move |query: comment_reply::BoxedQuery<'a, Pg>,
|
comment_reply::table
|
||||||
my_person_id: Option<PersonId>| {
|
.find(comment_reply_id)
|
||||||
query
|
|
||||||
.inner_join(comment::table)
|
.inner_join(comment::table)
|
||||||
.inner_join(person::table.on(comment::creator_id.eq(person::id)))
|
.inner_join(person::table.on(comment::creator_id.eq(person::id)))
|
||||||
.inner_join(post::table.on(comment::post_id.eq(post::id)))
|
.inner_join(post::table.on(comment::post_id.eq(post::id)))
|
||||||
|
@ -101,280 +91,7 @@ fn queries<'a>() -> Queries<
|
||||||
person_actions::blocked.nullable().is_not_null(),
|
person_actions::blocked.nullable().is_not_null(),
|
||||||
comment_actions::like_score.nullable(),
|
comment_actions::like_score.nullable(),
|
||||||
))
|
))
|
||||||
};
|
.first(conn)
|
||||||
|
|
||||||
let read =
|
|
||||||
move |mut conn: DbConn<'a>,
|
|
||||||
(comment_reply_id, my_person_id): (CommentReplyId, Option<PersonId>)| async move {
|
|
||||||
all_joins(
|
|
||||||
comment_reply::table.find(comment_reply_id).into_boxed(),
|
|
||||||
my_person_id,
|
|
||||||
)
|
|
||||||
.first(&mut conn)
|
|
||||||
.await
|
|
||||||
};
|
|
||||||
|
|
||||||
let list = move |mut conn: DbConn<'a>, options: CommentReplyQuery| async move {
|
|
||||||
// These filters need to be kept in sync with the filters in
|
|
||||||
// CommentReplyView::get_unread_replies()
|
|
||||||
let mut query = all_joins(comment_reply::table.into_boxed(), options.my_person_id);
|
|
||||||
|
|
||||||
if let Some(recipient_id) = options.recipient_id {
|
|
||||||
query = query.filter(comment_reply::recipient_id.eq(recipient_id));
|
|
||||||
}
|
|
||||||
|
|
||||||
if options.unread_only {
|
|
||||||
query = query.filter(comment_reply::read.eq(false));
|
|
||||||
}
|
|
||||||
|
|
||||||
if !options.show_bot_accounts {
|
|
||||||
query = query.filter(not(person::bot_account));
|
|
||||||
};
|
|
||||||
|
|
||||||
// Don't show replies from blocked persons
|
|
||||||
query = query.filter(person_actions::blocked.is_null());
|
|
||||||
|
|
||||||
query = match options.sort.unwrap_or(CommentSortType::New) {
|
|
||||||
CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()),
|
|
||||||
CommentSortType::Controversial => {
|
|
||||||
query.then_order_by(comment_aggregates::controversy_rank.desc())
|
|
||||||
}
|
|
||||||
CommentSortType::New => query.then_order_by(comment_reply::published.desc()),
|
|
||||||
CommentSortType::Old => query.then_order_by(comment_reply::published.asc()),
|
|
||||||
CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
|
|
||||||
};
|
|
||||||
|
|
||||||
let (limit, offset) = limit_and_offset(options.page, options.limit)?;
|
|
||||||
|
|
||||||
query
|
|
||||||
.limit(limit)
|
|
||||||
.offset(offset)
|
|
||||||
.load::<CommentReplyView>(&mut conn)
|
|
||||||
.await
|
|
||||||
};
|
|
||||||
|
|
||||||
Queries::new(read, list)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CommentReplyView {
|
|
||||||
pub async fn read(
|
|
||||||
pool: &mut DbPool<'_>,
|
|
||||||
comment_reply_id: CommentReplyId,
|
|
||||||
my_person_id: Option<PersonId>,
|
|
||||||
) -> Result<Self, Error> {
|
|
||||||
queries().read(pool, (comment_reply_id, my_person_id)).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the number of unread replies
|
|
||||||
pub async fn get_unread_count(
|
|
||||||
pool: &mut DbPool<'_>,
|
|
||||||
local_user: &LocalUser,
|
|
||||||
) -> Result<i64, Error> {
|
|
||||||
use diesel::dsl::count;
|
|
||||||
|
|
||||||
let conn = &mut get_conn(pool).await?;
|
|
||||||
|
|
||||||
let mut query = comment_reply::table
|
|
||||||
.inner_join(comment::table)
|
|
||||||
.left_join(actions(
|
|
||||||
person_actions::table,
|
|
||||||
Some(local_user.person_id),
|
|
||||||
comment::creator_id,
|
|
||||||
))
|
|
||||||
.inner_join(person::table.on(comment::creator_id.eq(person::id)))
|
|
||||||
.into_boxed();
|
|
||||||
|
|
||||||
// These filters need to be kept in sync with the filters in queries().list()
|
|
||||||
if !local_user.show_bot_accounts {
|
|
||||||
query = query.filter(not(person::bot_account));
|
|
||||||
}
|
|
||||||
|
|
||||||
query
|
|
||||||
// Don't count replies from blocked users
|
|
||||||
.filter(person_actions::blocked.is_null())
|
|
||||||
.filter(comment_reply::recipient_id.eq(local_user.person_id))
|
|
||||||
.filter(comment_reply::read.eq(false))
|
|
||||||
.filter(comment::deleted.eq(false))
|
|
||||||
.filter(comment::removed.eq(false))
|
|
||||||
.select(count(comment_reply::id))
|
|
||||||
.first::<i64>(conn)
|
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default, Clone)]
|
|
||||||
pub struct CommentReplyQuery {
|
|
||||||
pub my_person_id: Option<PersonId>,
|
|
||||||
pub recipient_id: Option<PersonId>,
|
|
||||||
pub sort: Option<CommentSortType>,
|
|
||||||
pub unread_only: bool,
|
|
||||||
pub show_bot_accounts: bool,
|
|
||||||
pub page: Option<i64>,
|
|
||||||
pub limit: Option<i64>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl CommentReplyQuery {
|
|
||||||
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommentReplyView>, Error> {
|
|
||||||
queries().list(pool, self).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
|
|
||||||
use crate::{comment_reply_view::CommentReplyQuery, structs::CommentReplyView};
|
|
||||||
use lemmy_db_schema::{
|
|
||||||
source::{
|
|
||||||
comment::{Comment, CommentInsertForm},
|
|
||||||
comment_reply::{CommentReply, CommentReplyInsertForm, CommentReplyUpdateForm},
|
|
||||||
community::{Community, CommunityInsertForm},
|
|
||||||
instance::Instance,
|
|
||||||
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
|
|
||||||
person::{Person, PersonInsertForm, PersonUpdateForm},
|
|
||||||
person_block::{PersonBlock, PersonBlockForm},
|
|
||||||
post::{Post, PostInsertForm},
|
|
||||||
},
|
|
||||||
traits::{Blockable, Crud},
|
|
||||||
utils::build_db_pool_for_tests,
|
|
||||||
};
|
|
||||||
use lemmy_db_views::structs::LocalUserView;
|
|
||||||
use lemmy_utils::error::LemmyResult;
|
|
||||||
use pretty_assertions::assert_eq;
|
|
||||||
use serial_test::serial;
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
#[serial]
|
|
||||||
async fn test_crud() -> LemmyResult<()> {
|
|
||||||
let pool = &build_db_pool_for_tests();
|
|
||||||
let pool = &mut pool.into();
|
|
||||||
|
|
||||||
let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
|
|
||||||
|
|
||||||
let terry_form = PersonInsertForm::test_form(inserted_instance.id, "terrylake");
|
|
||||||
let inserted_terry = Person::create(pool, &terry_form).await?;
|
|
||||||
|
|
||||||
let recipient_form = PersonInsertForm {
|
|
||||||
local: Some(true),
|
|
||||||
..PersonInsertForm::test_form(inserted_instance.id, "terrylakes recipient")
|
|
||||||
};
|
|
||||||
|
|
||||||
let inserted_recipient = Person::create(pool, &recipient_form).await?;
|
|
||||||
let recipient_id = inserted_recipient.id;
|
|
||||||
|
|
||||||
let recipient_local_user =
|
|
||||||
LocalUser::create(pool, &LocalUserInsertForm::test_form(recipient_id), vec![]).await?;
|
|
||||||
|
|
||||||
let new_community = CommunityInsertForm::new(
|
|
||||||
inserted_instance.id,
|
|
||||||
"test community lake".to_string(),
|
|
||||||
"nada".to_owned(),
|
|
||||||
"pubkey".to_string(),
|
|
||||||
);
|
|
||||||
let inserted_community = Community::create(pool, &new_community).await?;
|
|
||||||
|
|
||||||
let new_post = PostInsertForm::new(
|
|
||||||
"A test post".into(),
|
|
||||||
inserted_terry.id,
|
|
||||||
inserted_community.id,
|
|
||||||
);
|
|
||||||
let inserted_post = Post::create(pool, &new_post).await?;
|
|
||||||
|
|
||||||
let comment_form =
|
|
||||||
CommentInsertForm::new(inserted_terry.id, inserted_post.id, "A test comment".into());
|
|
||||||
let inserted_comment = Comment::create(pool, &comment_form, None).await?;
|
|
||||||
|
|
||||||
let comment_reply_form = CommentReplyInsertForm {
|
|
||||||
recipient_id: inserted_recipient.id,
|
|
||||||
comment_id: inserted_comment.id,
|
|
||||||
read: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let inserted_reply = CommentReply::create(pool, &comment_reply_form).await?;
|
|
||||||
|
|
||||||
let expected_reply = CommentReply {
|
|
||||||
id: inserted_reply.id,
|
|
||||||
recipient_id: inserted_reply.recipient_id,
|
|
||||||
comment_id: inserted_reply.comment_id,
|
|
||||||
read: false,
|
|
||||||
published: inserted_reply.published,
|
|
||||||
};
|
|
||||||
|
|
||||||
let read_reply = CommentReply::read(pool, inserted_reply.id).await?;
|
|
||||||
|
|
||||||
let comment_reply_update_form = CommentReplyUpdateForm { read: Some(false) };
|
|
||||||
let updated_reply =
|
|
||||||
CommentReply::update(pool, inserted_reply.id, &comment_reply_update_form).await?;
|
|
||||||
|
|
||||||
// Test to make sure counts and blocks work correctly
|
|
||||||
let unread_replies = CommentReplyView::get_unread_count(pool, &recipient_local_user).await?;
|
|
||||||
|
|
||||||
let query = CommentReplyQuery {
|
|
||||||
recipient_id: Some(recipient_id),
|
|
||||||
my_person_id: Some(recipient_id),
|
|
||||||
sort: None,
|
|
||||||
unread_only: false,
|
|
||||||
show_bot_accounts: true,
|
|
||||||
page: None,
|
|
||||||
limit: None,
|
|
||||||
};
|
|
||||||
let replies = query.clone().list(pool).await?;
|
|
||||||
assert_eq!(1, unread_replies);
|
|
||||||
assert_eq!(1, replies.len());
|
|
||||||
|
|
||||||
// Block the person, and make sure these counts are now empty
|
|
||||||
let block_form = PersonBlockForm {
|
|
||||||
person_id: recipient_id,
|
|
||||||
target_id: inserted_terry.id,
|
|
||||||
};
|
|
||||||
PersonBlock::block(pool, &block_form).await?;
|
|
||||||
|
|
||||||
let unread_replies_after_block =
|
|
||||||
CommentReplyView::get_unread_count(pool, &recipient_local_user).await?;
|
|
||||||
let replies_after_block = query.clone().list(pool).await?;
|
|
||||||
assert_eq!(0, unread_replies_after_block);
|
|
||||||
assert_eq!(0, replies_after_block.len());
|
|
||||||
|
|
||||||
// Unblock user so we can reuse the same person
|
|
||||||
PersonBlock::unblock(pool, &block_form).await?;
|
|
||||||
|
|
||||||
// Turn Terry into a bot account
|
|
||||||
let person_update_form = PersonUpdateForm {
|
|
||||||
bot_account: Some(true),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
Person::update(pool, inserted_terry.id, &person_update_form).await?;
|
|
||||||
|
|
||||||
let recipient_local_user_update_form = LocalUserUpdateForm {
|
|
||||||
show_bot_accounts: Some(false),
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
LocalUser::update(
|
|
||||||
pool,
|
|
||||||
recipient_local_user.id,
|
|
||||||
&recipient_local_user_update_form,
|
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
let recipient_local_user_view = LocalUserView::read(pool, recipient_local_user.id).await?;
|
|
||||||
|
|
||||||
let unread_replies_after_hide_bots =
|
|
||||||
CommentReplyView::get_unread_count(pool, &recipient_local_user_view.local_user).await?;
|
|
||||||
|
|
||||||
let mut query_without_bots = query.clone();
|
|
||||||
query_without_bots.show_bot_accounts = false;
|
|
||||||
let replies_after_hide_bots = query_without_bots.list(pool).await?;
|
|
||||||
assert_eq!(0, unread_replies_after_hide_bots);
|
|
||||||
assert_eq!(0, replies_after_hide_bots.len());
|
|
||||||
|
|
||||||
Comment::delete(pool, inserted_comment.id).await?;
|
|
||||||
Post::delete(pool, inserted_post.id).await?;
|
|
||||||
Community::delete(pool, inserted_community.id).await?;
|
|
||||||
Person::delete(pool, inserted_terry.id).await?;
|
|
||||||
Person::delete(pool, inserted_recipient.id).await?;
|
|
||||||
Instance::delete(pool, inserted_instance.id).await?;
|
|
||||||
|
|
||||||
assert_eq!(expected_reply, read_reply);
|
|
||||||
assert_eq!(expected_reply, inserted_reply);
|
|
||||||
assert_eq!(expected_reply, updated_reply);
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -1,6 +1,6 @@
|
||||||
use crate::structs::PersonCommentMentionView;
|
use crate::structs::PersonCommentMentionView;
|
||||||
use diesel::{
|
use diesel::{
|
||||||
dsl::{exists, not},
|
dsl::exists,
|
||||||
result::Error,
|
result::Error,
|
||||||
BoolExpressionMethods,
|
BoolExpressionMethods,
|
||||||
ExpressionMethods,
|
ExpressionMethods,
|
||||||
|
@ -24,7 +24,7 @@ use lemmy_db_schema::{
|
||||||
person_comment_mention,
|
person_comment_mention,
|
||||||
post,
|
post,
|
||||||
},
|
},
|
||||||
source::{community::CommunityFollower, local_user::LocalUser},
|
source::community::CommunityFollower,
|
||||||
utils::{actions, actions_alias, get_conn, DbPool},
|
utils::{actions, actions_alias, get_conn, DbPool},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -94,40 +94,4 @@ impl PersonCommentMentionView {
|
||||||
.first(conn)
|
.first(conn)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets the number of unread mentions
|
|
||||||
// TODO get rid of this
|
|
||||||
pub async fn get_unread_count(
|
|
||||||
pool: &mut DbPool<'_>,
|
|
||||||
local_user: &LocalUser,
|
|
||||||
) -> Result<i64, Error> {
|
|
||||||
use diesel::dsl::count;
|
|
||||||
let conn = &mut get_conn(pool).await?;
|
|
||||||
|
|
||||||
let mut query = person_comment_mention::table
|
|
||||||
.inner_join(comment::table)
|
|
||||||
.left_join(actions(
|
|
||||||
person_actions::table,
|
|
||||||
Some(local_user.person_id),
|
|
||||||
comment::creator_id,
|
|
||||||
))
|
|
||||||
.inner_join(person::table.on(comment::creator_id.eq(person::id)))
|
|
||||||
.into_boxed();
|
|
||||||
|
|
||||||
// These filters need to be kept in sync with the filters in queries().list()
|
|
||||||
if !local_user.show_bot_accounts {
|
|
||||||
query = query.filter(not(person::bot_account));
|
|
||||||
}
|
|
||||||
|
|
||||||
query
|
|
||||||
// Don't count replies from blocked users
|
|
||||||
.filter(person_actions::blocked.is_null())
|
|
||||||
.filter(person_comment_mention::recipient_id.eq(local_user.person_id))
|
|
||||||
.filter(person_comment_mention::read.eq(false))
|
|
||||||
.filter(comment::deleted.eq(false))
|
|
||||||
.filter(comment::removed.eq(false))
|
|
||||||
.select(count(person_comment_mention::id))
|
|
||||||
.first::<i64>(conn)
|
|
||||||
.await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,28 +1,22 @@
|
||||||
use crate::structs::PrivateMessageView;
|
use crate::structs::PrivateMessageView;
|
||||||
use diesel::{
|
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl};
|
||||||
debug_query,
|
|
||||||
pg::Pg,
|
|
||||||
result::Error,
|
|
||||||
BoolExpressionMethods,
|
|
||||||
ExpressionMethods,
|
|
||||||
JoinOnDsl,
|
|
||||||
QueryDsl,
|
|
||||||
};
|
|
||||||
use diesel_async::RunQueryDsl;
|
use diesel_async::RunQueryDsl;
|
||||||
use lemmy_db_schema::{
|
use lemmy_db_schema::{
|
||||||
aliases,
|
aliases,
|
||||||
newtypes::{PersonId, PrivateMessageId},
|
newtypes::PrivateMessageId,
|
||||||
schema::{instance_actions, person, person_actions, private_message},
|
schema::{instance_actions, person, person_actions, private_message},
|
||||||
utils::{actions, get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
|
utils::{actions, get_conn, DbPool},
|
||||||
};
|
};
|
||||||
use tracing::debug;
|
|
||||||
|
|
||||||
fn queries<'a>() -> Queries<
|
impl PrivateMessageView {
|
||||||
impl ReadFn<'a, PrivateMessageView, PrivateMessageId>,
|
pub async fn read(
|
||||||
impl ListFn<'a, PrivateMessageView, (PrivateMessageQuery, PersonId)>,
|
pool: &mut DbPool<'_>,
|
||||||
> {
|
private_message_id: PrivateMessageId,
|
||||||
let all_joins = |query: private_message::BoxedQuery<'a, Pg>| {
|
) -> Result<Self, Error> {
|
||||||
query
|
let conn = &mut get_conn(pool).await?;
|
||||||
|
|
||||||
|
private_message::table
|
||||||
|
.find(private_message_id)
|
||||||
.inner_join(person::table.on(private_message::creator_id.eq(person::id)))
|
.inner_join(person::table.on(private_message::creator_id.eq(person::id)))
|
||||||
.inner_join(
|
.inner_join(
|
||||||
aliases::person1.on(private_message::recipient_id.eq(aliases::person1.field(person::id))),
|
aliases::person1.on(private_message::recipient_id.eq(aliases::person1.field(person::id))),
|
||||||
|
@ -37,361 +31,12 @@ fn queries<'a>() -> Queries<
|
||||||
Some(aliases::person1.field(person::id)),
|
Some(aliases::person1.field(person::id)),
|
||||||
person::instance_id,
|
person::instance_id,
|
||||||
))
|
))
|
||||||
};
|
.select((
|
||||||
|
|
||||||
let selection = (
|
|
||||||
private_message::all_columns,
|
private_message::all_columns,
|
||||||
person::all_columns,
|
person::all_columns,
|
||||||
aliases::person1.fields(person::all_columns),
|
aliases::person1.fields(person::all_columns),
|
||||||
);
|
|
||||||
|
|
||||||
let read = move |mut conn: DbConn<'a>, private_message_id: PrivateMessageId| async move {
|
|
||||||
all_joins(private_message::table.find(private_message_id).into_boxed())
|
|
||||||
.order_by(private_message::published.desc())
|
|
||||||
.select(selection)
|
|
||||||
.first(&mut conn)
|
|
||||||
.await
|
|
||||||
};
|
|
||||||
|
|
||||||
let list = move |mut conn: DbConn<'a>,
|
|
||||||
(options, recipient_id): (PrivateMessageQuery, PersonId)| async move {
|
|
||||||
let mut query = all_joins(private_message::table.into_boxed())
|
|
||||||
.select(selection)
|
|
||||||
// Dont show replies from blocked users
|
|
||||||
.filter(person_actions::blocked.is_null())
|
|
||||||
// Dont show replies from blocked instances
|
|
||||||
.filter(instance_actions::blocked.is_null());
|
|
||||||
|
|
||||||
// If its unread, I only want the ones to me
|
|
||||||
if options.unread_only {
|
|
||||||
query = query.filter(private_message::read.eq(false));
|
|
||||||
if let Some(i) = options.creator_id {
|
|
||||||
query = query.filter(private_message::creator_id.eq(i))
|
|
||||||
}
|
|
||||||
query = query.filter(private_message::recipient_id.eq(recipient_id));
|
|
||||||
}
|
|
||||||
// Otherwise, I want the ALL view to show both sent and received
|
|
||||||
else {
|
|
||||||
query = query.filter(
|
|
||||||
private_message::recipient_id
|
|
||||||
.eq(recipient_id)
|
|
||||||
.or(private_message::creator_id.eq(recipient_id)),
|
|
||||||
);
|
|
||||||
if let Some(i) = options.creator_id {
|
|
||||||
query = query.filter(
|
|
||||||
private_message::creator_id
|
|
||||||
.eq(i)
|
|
||||||
.or(private_message::recipient_id.eq(i)),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let (limit, offset) = limit_and_offset(options.page, options.limit)?;
|
|
||||||
|
|
||||||
query = query
|
|
||||||
.filter(private_message::deleted.eq(false))
|
|
||||||
.limit(limit)
|
|
||||||
.offset(offset)
|
|
||||||
.order_by(private_message::published.desc());
|
|
||||||
|
|
||||||
debug!(
|
|
||||||
"Private Message View Query: {:?}",
|
|
||||||
debug_query::<Pg, _>(&query)
|
|
||||||
);
|
|
||||||
|
|
||||||
query.load::<PrivateMessageView>(&mut conn).await
|
|
||||||
};
|
|
||||||
|
|
||||||
Queries::new(read, list)
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PrivateMessageView {
|
|
||||||
pub async fn read(
|
|
||||||
pool: &mut DbPool<'_>,
|
|
||||||
private_message_id: PrivateMessageId,
|
|
||||||
) -> Result<Self, Error> {
|
|
||||||
queries().read(pool, private_message_id).await
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Gets the number of unread messages
|
|
||||||
pub async fn get_unread_count(
|
|
||||||
pool: &mut DbPool<'_>,
|
|
||||||
my_person_id: PersonId,
|
|
||||||
) -> Result<i64, Error> {
|
|
||||||
use diesel::dsl::count;
|
|
||||||
let conn = &mut get_conn(pool).await?;
|
|
||||||
private_message::table
|
|
||||||
// Necessary to get the senders instance_id
|
|
||||||
.inner_join(person::table.on(private_message::creator_id.eq(person::id)))
|
|
||||||
.left_join(actions(
|
|
||||||
person_actions::table,
|
|
||||||
Some(my_person_id),
|
|
||||||
private_message::creator_id,
|
|
||||||
))
|
))
|
||||||
.left_join(actions(
|
.first(conn)
|
||||||
instance_actions::table,
|
|
||||||
Some(my_person_id),
|
|
||||||
person::instance_id,
|
|
||||||
))
|
|
||||||
// Dont count replies from blocked users
|
|
||||||
.filter(person_actions::blocked.is_null())
|
|
||||||
// Dont count replies from blocked instances
|
|
||||||
.filter(instance_actions::blocked.is_null())
|
|
||||||
.filter(private_message::read.eq(false))
|
|
||||||
.filter(private_message::recipient_id.eq(my_person_id))
|
|
||||||
.filter(private_message::deleted.eq(false))
|
|
||||||
.select(count(private_message::id))
|
|
||||||
.first::<i64>(conn)
|
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
|
||||||
pub struct PrivateMessageQuery {
|
|
||||||
pub unread_only: bool,
|
|
||||||
pub page: Option<i64>,
|
|
||||||
pub limit: Option<i64>,
|
|
||||||
pub creator_id: Option<PersonId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PrivateMessageQuery {
|
|
||||||
pub async fn list(
|
|
||||||
self,
|
|
||||||
pool: &mut DbPool<'_>,
|
|
||||||
recipient_id: PersonId,
|
|
||||||
) -> Result<Vec<PrivateMessageView>, Error> {
|
|
||||||
queries().list(pool, (self, recipient_id)).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
#[expect(clippy::indexing_slicing)]
|
|
||||||
mod tests {
|
|
||||||
|
|
||||||
use crate::{private_message_view::PrivateMessageQuery, structs::PrivateMessageView};
|
|
||||||
use lemmy_db_schema::{
|
|
||||||
assert_length,
|
|
||||||
newtypes::InstanceId,
|
|
||||||
source::{
|
|
||||||
instance::Instance,
|
|
||||||
instance_block::{InstanceBlock, InstanceBlockForm},
|
|
||||||
person::{Person, PersonInsertForm},
|
|
||||||
person_block::{PersonBlock, PersonBlockForm},
|
|
||||||
private_message::{PrivateMessage, PrivateMessageInsertForm},
|
|
||||||
},
|
|
||||||
traits::{Blockable, Crud},
|
|
||||||
utils::{build_db_pool_for_tests, DbPool},
|
|
||||||
};
|
|
||||||
use lemmy_utils::error::LemmyResult;
|
|
||||||
use pretty_assertions::assert_eq;
|
|
||||||
use serial_test::serial;
|
|
||||||
|
|
||||||
struct Data {
|
|
||||||
instance: Instance,
|
|
||||||
timmy: Person,
|
|
||||||
jess: Person,
|
|
||||||
sara: Person,
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn init_data(pool: &mut DbPool<'_>) -> LemmyResult<Data> {
|
|
||||||
let message_content = String::new();
|
|
||||||
|
|
||||||
let instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?;
|
|
||||||
|
|
||||||
let timmy_form = PersonInsertForm::test_form(instance.id, "timmy_rav");
|
|
||||||
|
|
||||||
let timmy = Person::create(pool, &timmy_form).await?;
|
|
||||||
|
|
||||||
let sara_form = PersonInsertForm::test_form(instance.id, "sara_rav");
|
|
||||||
|
|
||||||
let sara = Person::create(pool, &sara_form).await?;
|
|
||||||
|
|
||||||
let jess_form = PersonInsertForm::test_form(instance.id, "jess_rav");
|
|
||||||
|
|
||||||
let jess = Person::create(pool, &jess_form).await?;
|
|
||||||
|
|
||||||
let sara_timmy_message_form =
|
|
||||||
PrivateMessageInsertForm::new(sara.id, timmy.id, message_content.clone());
|
|
||||||
PrivateMessage::create(pool, &sara_timmy_message_form).await?;
|
|
||||||
|
|
||||||
let sara_jess_message_form =
|
|
||||||
PrivateMessageInsertForm::new(sara.id, jess.id, message_content.clone());
|
|
||||||
PrivateMessage::create(pool, &sara_jess_message_form).await?;
|
|
||||||
|
|
||||||
let timmy_sara_message_form =
|
|
||||||
PrivateMessageInsertForm::new(timmy.id, sara.id, message_content.clone());
|
|
||||||
PrivateMessage::create(pool, &timmy_sara_message_form).await?;
|
|
||||||
|
|
||||||
let jess_timmy_message_form =
|
|
||||||
PrivateMessageInsertForm::new(jess.id, timmy.id, message_content.clone());
|
|
||||||
PrivateMessage::create(pool, &jess_timmy_message_form).await?;
|
|
||||||
|
|
||||||
Ok(Data {
|
|
||||||
instance,
|
|
||||||
timmy,
|
|
||||||
jess,
|
|
||||||
sara,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn cleanup(instance_id: InstanceId, pool: &mut DbPool<'_>) -> LemmyResult<()> {
|
|
||||||
// This also deletes all persons and private messages thanks to sql `on delete cascade`
|
|
||||||
Instance::delete(pool, instance_id).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
#[serial]
|
|
||||||
async fn read_private_messages() -> LemmyResult<()> {
|
|
||||||
let pool = &build_db_pool_for_tests();
|
|
||||||
let pool = &mut pool.into();
|
|
||||||
let Data {
|
|
||||||
timmy,
|
|
||||||
jess,
|
|
||||||
sara,
|
|
||||||
instance,
|
|
||||||
} = init_data(pool).await?;
|
|
||||||
|
|
||||||
let timmy_messages = PrivateMessageQuery {
|
|
||||||
unread_only: false,
|
|
||||||
creator_id: None,
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.list(pool, timmy.id)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
assert_length!(3, &timmy_messages);
|
|
||||||
assert_eq!(timmy_messages[0].creator.id, jess.id);
|
|
||||||
assert_eq!(timmy_messages[0].recipient.id, timmy.id);
|
|
||||||
assert_eq!(timmy_messages[1].creator.id, timmy.id);
|
|
||||||
assert_eq!(timmy_messages[1].recipient.id, sara.id);
|
|
||||||
assert_eq!(timmy_messages[2].creator.id, sara.id);
|
|
||||||
assert_eq!(timmy_messages[2].recipient.id, timmy.id);
|
|
||||||
|
|
||||||
let timmy_unread_messages = PrivateMessageQuery {
|
|
||||||
unread_only: true,
|
|
||||||
creator_id: None,
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.list(pool, timmy.id)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
assert_length!(2, &timmy_unread_messages);
|
|
||||||
assert_eq!(timmy_unread_messages[0].creator.id, jess.id);
|
|
||||||
assert_eq!(timmy_unread_messages[0].recipient.id, timmy.id);
|
|
||||||
assert_eq!(timmy_unread_messages[1].creator.id, sara.id);
|
|
||||||
assert_eq!(timmy_unread_messages[1].recipient.id, timmy.id);
|
|
||||||
|
|
||||||
let timmy_sara_messages = PrivateMessageQuery {
|
|
||||||
unread_only: false,
|
|
||||||
creator_id: Some(sara.id),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.list(pool, timmy.id)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
assert_length!(2, &timmy_sara_messages);
|
|
||||||
assert_eq!(timmy_sara_messages[0].creator.id, timmy.id);
|
|
||||||
assert_eq!(timmy_sara_messages[0].recipient.id, sara.id);
|
|
||||||
assert_eq!(timmy_sara_messages[1].creator.id, sara.id);
|
|
||||||
assert_eq!(timmy_sara_messages[1].recipient.id, timmy.id);
|
|
||||||
|
|
||||||
let timmy_sara_unread_messages = PrivateMessageQuery {
|
|
||||||
unread_only: true,
|
|
||||||
creator_id: Some(sara.id),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.list(pool, timmy.id)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
assert_length!(1, &timmy_sara_unread_messages);
|
|
||||||
assert_eq!(timmy_sara_unread_messages[0].creator.id, sara.id);
|
|
||||||
assert_eq!(timmy_sara_unread_messages[0].recipient.id, timmy.id);
|
|
||||||
|
|
||||||
cleanup(instance.id, pool).await
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
#[serial]
|
|
||||||
async fn ensure_person_block() -> LemmyResult<()> {
|
|
||||||
let pool = &build_db_pool_for_tests();
|
|
||||||
let pool = &mut pool.into();
|
|
||||||
let Data {
|
|
||||||
timmy,
|
|
||||||
sara,
|
|
||||||
instance,
|
|
||||||
jess: _,
|
|
||||||
} = init_data(pool).await?;
|
|
||||||
|
|
||||||
// Make sure blocks are working
|
|
||||||
let timmy_blocks_sara_form = PersonBlockForm {
|
|
||||||
person_id: timmy.id,
|
|
||||||
target_id: sara.id,
|
|
||||||
};
|
|
||||||
|
|
||||||
let inserted_block = PersonBlock::block(pool, &timmy_blocks_sara_form).await?;
|
|
||||||
|
|
||||||
let expected_block = PersonBlock {
|
|
||||||
person_id: timmy.id,
|
|
||||||
target_id: sara.id,
|
|
||||||
published: inserted_block.published,
|
|
||||||
};
|
|
||||||
assert_eq!(expected_block, inserted_block);
|
|
||||||
|
|
||||||
let timmy_messages = PrivateMessageQuery {
|
|
||||||
unread_only: true,
|
|
||||||
creator_id: None,
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.list(pool, timmy.id)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
assert_length!(1, &timmy_messages);
|
|
||||||
|
|
||||||
let timmy_unread_messages = PrivateMessageView::get_unread_count(pool, timmy.id).await?;
|
|
||||||
assert_eq!(timmy_unread_messages, 1);
|
|
||||||
|
|
||||||
cleanup(instance.id, pool).await
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
#[serial]
|
|
||||||
async fn ensure_instance_block() -> LemmyResult<()> {
|
|
||||||
let pool = &build_db_pool_for_tests();
|
|
||||||
let pool = &mut pool.into();
|
|
||||||
let Data {
|
|
||||||
timmy,
|
|
||||||
jess: _,
|
|
||||||
sara,
|
|
||||||
instance,
|
|
||||||
} = init_data(pool).await?;
|
|
||||||
// Make sure instance_blocks are working
|
|
||||||
let timmy_blocks_instance_form = InstanceBlockForm {
|
|
||||||
person_id: timmy.id,
|
|
||||||
instance_id: sara.instance_id,
|
|
||||||
};
|
|
||||||
|
|
||||||
let inserted_instance_block = InstanceBlock::block(pool, &timmy_blocks_instance_form).await?;
|
|
||||||
|
|
||||||
let expected_instance_block = InstanceBlock {
|
|
||||||
person_id: timmy.id,
|
|
||||||
instance_id: sara.instance_id,
|
|
||||||
published: inserted_instance_block.published,
|
|
||||||
};
|
|
||||||
assert_eq!(expected_instance_block, inserted_instance_block);
|
|
||||||
|
|
||||||
let timmy_messages = PrivateMessageQuery {
|
|
||||||
unread_only: true,
|
|
||||||
creator_id: None,
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
.list(pool, timmy.id)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
assert_length!(0, &timmy_messages);
|
|
||||||
|
|
||||||
let timmy_unread_messages = PrivateMessageView::get_unread_count(pool, timmy.id).await?;
|
|
||||||
assert_eq!(timmy_unread_messages, 0);
|
|
||||||
cleanup(instance.id, pool).await
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -357,18 +357,14 @@ async fn get_feed_inbox(context: &LemmyContext, jwt: &str) -> LemmyResult<Channe
|
||||||
let local_user = local_user_view_from_jwt(jwt, context).await?;
|
let local_user = local_user_view_from_jwt(jwt, context).await?;
|
||||||
let my_person_id = local_user.person.id;
|
let my_person_id = local_user.person.id;
|
||||||
let show_bot_accounts = Some(local_user.local_user.show_bot_accounts);
|
let show_bot_accounts = Some(local_user.local_user.show_bot_accounts);
|
||||||
let unread_only = Some(false);
|
|
||||||
|
|
||||||
check_private_instance(&Some(local_user.clone()), &site_view.local_site)?;
|
check_private_instance(&Some(local_user.clone()), &site_view.local_site)?;
|
||||||
|
|
||||||
let inbox = InboxCombinedQuery {
|
let inbox = InboxCombinedQuery {
|
||||||
my_person_id,
|
|
||||||
unread_only,
|
|
||||||
show_bot_accounts,
|
show_bot_accounts,
|
||||||
page_after: None,
|
..Default::default()
|
||||||
page_back: None,
|
|
||||||
}
|
}
|
||||||
.list(&mut context.pool())
|
.list(&mut context.pool(), my_person_id)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
|
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
|
||||||
|
|
Loading…
Reference in a new issue