mirror of
https://github.com/LemmyNet/lemmy.git
synced 2024-12-23 03:11:32 +00:00
Finishing up inbox.
This commit is contained in:
parent
05f218d53c
commit
41cfdca1cf
8 changed files with 618 additions and 1262 deletions
|
@ -14,6 +14,7 @@ pub async fn list_inbox(
|
|||
local_user_view: LocalUserView,
|
||||
) -> LemmyResult<Json<ListInboxResponse>> {
|
||||
let unread_only = data.unread_only;
|
||||
let type_ = data.type_;
|
||||
let person_id = local_user_view.person.id;
|
||||
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 inbox = InboxCombinedQuery {
|
||||
my_person_id: person_id,
|
||||
type_,
|
||||
unread_only,
|
||||
show_bot_accounts,
|
||||
page_after,
|
||||
page_back,
|
||||
}
|
||||
.list(&mut context.pool())
|
||||
.list(&mut context.pool(), person_id)
|
||||
.await?;
|
||||
|
||||
Ok(Json(ListInboxResponse { inbox }))
|
||||
|
|
|
@ -10,6 +10,7 @@ use lemmy_db_schema::{
|
|||
sensitive::SensitiveString,
|
||||
source::{login_token::LoginToken, site::Site},
|
||||
CommentSortType,
|
||||
InboxDataType,
|
||||
ListingType,
|
||||
PostListingMode,
|
||||
PostSortType,
|
||||
|
@ -380,6 +381,8 @@ pub struct BlockPersonResponse {
|
|||
#[cfg_attr(feature = "full", ts(export))]
|
||||
/// Get your inbox (replies, comment mentions, post mentions, and messages)
|
||||
pub struct ListInbox {
|
||||
#[cfg_attr(feature = "full", ts(optional))]
|
||||
pub type_: Option<InboxDataType>,
|
||||
#[cfg_attr(feature = "full", ts(optional))]
|
||||
pub unread_only: Option<bool>,
|
||||
#[cfg_attr(feature = "full", ts(optional))]
|
||||
|
|
|
@ -219,6 +219,18 @@ pub enum ModlogActionType {
|
|||
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(
|
||||
EnumString, Display, Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash,
|
||||
)]
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use crate::structs::CommentReplyView;
|
||||
use diesel::{
|
||||
dsl::{exists, not},
|
||||
pg::Pg,
|
||||
dsl::exists,
|
||||
result::Error,
|
||||
BoolExpressionMethods,
|
||||
ExpressionMethods,
|
||||
|
@ -25,37 +24,28 @@ use lemmy_db_schema::{
|
|||
person_actions,
|
||||
post,
|
||||
},
|
||||
source::{community::CommunityFollower, local_user::LocalUser},
|
||||
utils::{
|
||||
actions,
|
||||
actions_alias,
|
||||
get_conn,
|
||||
limit_and_offset,
|
||||
DbConn,
|
||||
DbPool,
|
||||
ListFn,
|
||||
Queries,
|
||||
ReadFn,
|
||||
},
|
||||
CommentSortType,
|
||||
source::community::CommunityFollower,
|
||||
utils::{actions, actions_alias, get_conn, DbPool},
|
||||
};
|
||||
|
||||
// TODO get rid of all this
|
||||
fn queries<'a>() -> Queries<
|
||||
impl ReadFn<'a, CommentReplyView, (CommentReplyId, Option<PersonId>)>,
|
||||
impl ListFn<'a, CommentReplyView, CommentReplyQuery>,
|
||||
> {
|
||||
let creator_is_admin = exists(
|
||||
local_user::table.filter(
|
||||
comment::creator_id
|
||||
.eq(local_user::person_id)
|
||||
.and(local_user::admin.eq(true)),
|
||||
),
|
||||
);
|
||||
impl CommentReplyView {
|
||||
pub async fn read(
|
||||
pool: &mut DbPool<'_>,
|
||||
comment_reply_id: CommentReplyId,
|
||||
my_person_id: Option<PersonId>,
|
||||
) -> Result<Self, Error> {
|
||||
let conn = &mut get_conn(pool).await?;
|
||||
|
||||
let all_joins = move |query: comment_reply::BoxedQuery<'a, Pg>,
|
||||
my_person_id: Option<PersonId>| {
|
||||
query
|
||||
let creator_is_admin = exists(
|
||||
local_user::table.filter(
|
||||
comment::creator_id
|
||||
.eq(local_user::person_id)
|
||||
.and(local_user::admin.eq(true)),
|
||||
),
|
||||
);
|
||||
|
||||
comment_reply::table
|
||||
.find(comment_reply_id)
|
||||
.inner_join(comment::table)
|
||||
.inner_join(person::table.on(comment::creator_id.eq(person::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(),
|
||||
comment_actions::like_score.nullable(),
|
||||
))
|
||||
};
|
||||
|
||||
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)
|
||||
.first(conn)
|
||||
.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 diesel::{
|
||||
dsl::{exists, not},
|
||||
dsl::exists,
|
||||
result::Error,
|
||||
BoolExpressionMethods,
|
||||
ExpressionMethods,
|
||||
|
@ -24,7 +24,7 @@ use lemmy_db_schema::{
|
|||
person_comment_mention,
|
||||
post,
|
||||
},
|
||||
source::{community::CommunityFollower, local_user::LocalUser},
|
||||
source::community::CommunityFollower,
|
||||
utils::{actions, actions_alias, get_conn, DbPool},
|
||||
};
|
||||
|
||||
|
@ -94,40 +94,4 @@ impl PersonCommentMentionView {
|
|||
.first(conn)
|
||||
.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 diesel::{
|
||||
debug_query,
|
||||
pg::Pg,
|
||||
result::Error,
|
||||
BoolExpressionMethods,
|
||||
ExpressionMethods,
|
||||
JoinOnDsl,
|
||||
QueryDsl,
|
||||
};
|
||||
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, QueryDsl};
|
||||
use diesel_async::RunQueryDsl;
|
||||
use lemmy_db_schema::{
|
||||
aliases,
|
||||
newtypes::{PersonId, PrivateMessageId},
|
||||
newtypes::PrivateMessageId,
|
||||
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 ReadFn<'a, PrivateMessageView, PrivateMessageId>,
|
||||
impl ListFn<'a, PrivateMessageView, (PrivateMessageQuery, PersonId)>,
|
||||
> {
|
||||
let all_joins = |query: private_message::BoxedQuery<'a, Pg>| {
|
||||
query
|
||||
impl PrivateMessageView {
|
||||
pub async fn read(
|
||||
pool: &mut DbPool<'_>,
|
||||
private_message_id: PrivateMessageId,
|
||||
) -> Result<Self, Error> {
|
||||
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(
|
||||
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)),
|
||||
person::instance_id,
|
||||
))
|
||||
};
|
||||
|
||||
let selection = (
|
||||
private_message::all_columns,
|
||||
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,
|
||||
.select((
|
||||
private_message::all_columns,
|
||||
person::all_columns,
|
||||
aliases::person1.fields(person::all_columns),
|
||||
))
|
||||
.left_join(actions(
|
||||
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)
|
||||
.first(conn)
|
||||
.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 my_person_id = local_user.person.id;
|
||||
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)?;
|
||||
|
||||
let inbox = InboxCombinedQuery {
|
||||
my_person_id,
|
||||
unread_only,
|
||||
show_bot_accounts,
|
||||
page_after: None,
|
||||
page_back: None,
|
||||
..Default::default()
|
||||
}
|
||||
.list(&mut context.pool())
|
||||
.list(&mut context.pool(), my_person_id)
|
||||
.await?;
|
||||
|
||||
let protocol_and_hostname = context.settings().get_protocol_and_hostname();
|
||||
|
|
Loading…
Reference in a new issue