Simplifying personmentionview.

This commit is contained in:
Dessalines 2023-04-16 16:00:17 -04:00
parent 6ea5d53d25
commit ada10fbdf0
6 changed files with 55 additions and 225 deletions

View file

@ -22,7 +22,6 @@ impl Perform for GetPersonMentions {
let local_user_view =
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
let sort = data.sort;
let page = data.page;
let limit = data.limit;
let unread_only = data.unread_only;
@ -32,8 +31,6 @@ impl Perform for GetPersonMentions {
let mentions = PersonMentionQuery::builder()
.pool(context.pool())
.recipient_id(person_id)
.my_person_id(person_id)
.sort(sort)
.unread_only(unread_only)
.show_bot_accounts(show_bot_accounts)
.page(page)

View file

@ -44,9 +44,7 @@ impl Perform for MarkPersonMentionAsRead {
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_comment"))?;
let person_mention_id = read_person_mention.id;
let person_id = local_user_view.person.id;
let person_mention_view =
PersonMentionView::read(context.pool(), person_mention_id, Some(person_id)).await?;
let person_mention_view = PersonMentionView::read(context.pool(), person_mention_id).await?;
Ok(PersonMentionResponse {
person_mention_view,

View file

@ -191,7 +191,6 @@ pub struct GetReplies {
#[derive(Debug, Serialize, Deserialize, Clone, Default)]
pub struct GetPersonMentions {
pub sort: Option<CommentSortType>,
pub page: Option<i64>,
pub limit: Option<i64>,
pub unread_only: Option<bool>,

View file

@ -1,6 +1,5 @@
use crate::structs::PersonMentionView;
use diesel::{
dsl::now,
result::Error,
BoolExpressionMethods,
ExpressionMethods,
@ -10,136 +9,39 @@ use diesel::{
};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
aggregates::structs::CommentAggregates,
newtypes::{PersonId, PersonMentionId},
schema::{
comment,
comment_aggregates,
comment_like,
comment_saved,
community,
community_follower,
community_person_ban,
person,
person_block,
person_mention,
post,
},
source::{
comment::{Comment, CommentSaved},
community::{Community, CommunityFollower, CommunityPersonBan},
person::Person,
person_block::PersonBlock,
person_mention::PersonMention,
post::Post,
},
schema::{comment, person, person_mention, post},
source::{comment::Comment, person::Person, person_mention::PersonMention, post::Post},
traits::JoinView,
utils::{functions::hot_rank, get_conn, limit_and_offset, DbPool},
CommentSortType,
utils::{get_conn, limit_and_offset, DbPool},
};
use typed_builder::TypedBuilder;
type PersonMentionViewTuple = (
PersonMention,
Comment,
Person,
Post,
Community,
Person,
CommentAggregates,
Option<CommunityPersonBan>,
Option<CommunityFollower>,
Option<CommentSaved>,
Option<PersonBlock>,
Option<i16>,
);
type PersonMentionViewTuple = (PersonMention, Option<Comment>, Option<Post>, Person, Person);
impl PersonMentionView {
pub async fn read(
pool: &DbPool,
person_mention_id: PersonMentionId,
my_person_id: Option<PersonId>,
) -> Result<Self, Error> {
pub async fn read(pool: &DbPool, person_mention_id: PersonMentionId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?;
let person_alias_1 = diesel::alias!(person as person1);
// The left join below will return None in this case
let person_id_join = my_person_id.unwrap_or(PersonId(-1));
let (
person_mention,
comment,
creator,
post,
community,
recipient,
counts,
creator_banned_from_community,
follower,
saved,
creator_blocked,
my_vote,
) = person_mention::table
let (person_mention, comment, post, creator, recipient) = person_mention::table
.find(person_mention_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)))
.inner_join(community::table.on(post::community_id.eq(community::id)))
.inner_join(person_alias_1)
.inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
.left_join(
community_person_ban::table.on(
community::id
.eq(community_person_ban::community_id)
.and(community_person_ban::person_id.eq(comment::creator_id))
.and(
community_person_ban::expires
.is_null()
.or(community_person_ban::expires.gt(now)),
),
),
)
.left_join(
community_follower::table.on(
post::community_id
.eq(community_follower::community_id)
.and(community_follower::person_id.eq(person_id_join)),
),
)
.left_join(
comment_saved::table.on(
comment::id
.eq(comment_saved::comment_id)
.and(comment_saved::person_id.eq(person_id_join)),
),
)
.left_join(
person_block::table.on(
.left_join(comment::table)
.left_join(post::table)
.inner_join(
person::table.on(
comment::creator_id
.eq(person_block::target_id)
.and(person_block::person_id.eq(person_id_join)),
),
)
.left_join(
comment_like::table.on(
comment::id
.eq(comment_like::comment_id)
.and(comment_like::person_id.eq(person_id_join)),
.eq(person::id)
.or(post::creator_id.eq(person::id)),
),
)
.inner_join(person_alias_1)
.select((
person_mention::all_columns,
comment::all_columns,
comment::all_columns.nullable(),
post::all_columns.nullable(),
person::all_columns,
post::all_columns,
community::all_columns,
person_alias_1.fields(person::all_columns),
comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(),
comment_saved::all_columns.nullable(),
person_block::all_columns.nullable(),
comment_like::score.nullable(),
))
.first::<PersonMentionViewTuple>(conn)
.await?;
@ -147,16 +49,9 @@ impl PersonMentionView {
Ok(PersonMentionView {
person_mention,
comment,
creator,
post,
community,
creator,
recipient,
counts,
creator_banned_from_community: creator_banned_from_community.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&follower),
saved: saved.is_some(),
creator_blocked: creator_blocked.is_some(),
my_vote,
})
}
@ -166,11 +61,15 @@ impl PersonMentionView {
let conn = &mut get_conn(pool).await?;
person_mention::table
.inner_join(comment::table)
.left_join(comment::table)
.left_join(post::table)
.filter(person_mention::recipient_id.eq(my_person_id))
.filter(person_mention::read.eq(false))
// TODO check to make sure these filters work. You might need to move them up to the joins
.filter(comment::deleted.eq(false))
.filter(comment::removed.eq(false))
.filter(post::deleted.eq(false))
.filter(post::removed.eq(false))
.select(count(person_mention::id))
.first::<i64>(conn)
.await
@ -182,9 +81,7 @@ impl PersonMentionView {
pub struct PersonMentionQuery<'a> {
#[builder(!default)]
pool: &'a DbPool,
my_person_id: Option<PersonId>,
recipient_id: Option<PersonId>,
sort: Option<CommentSortType>,
unread_only: Option<bool>,
show_bot_accounts: Option<bool>,
page: Option<i64>,
@ -197,69 +94,23 @@ impl<'a> PersonMentionQuery<'a> {
let person_alias_1 = diesel::alias!(person as person1);
// The left join below will return None in this case
let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
let mut query = person_mention::table
.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)))
.inner_join(community::table.on(post::community_id.eq(community::id)))
.inner_join(person_alias_1)
.inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
.left_join(
community_person_ban::table.on(
community::id
.eq(community_person_ban::community_id)
.and(community_person_ban::person_id.eq(comment::creator_id))
.and(
community_person_ban::expires
.is_null()
.or(community_person_ban::expires.gt(now)),
),
),
)
.left_join(
community_follower::table.on(
post::community_id
.eq(community_follower::community_id)
.and(community_follower::person_id.eq(person_id_join)),
),
)
.left_join(
comment_saved::table.on(
comment::id
.eq(comment_saved::comment_id)
.and(comment_saved::person_id.eq(person_id_join)),
),
)
.left_join(
person_block::table.on(
.left_join(comment::table)
.left_join(post::table)
.inner_join(
person::table.on(
comment::creator_id
.eq(person_block::target_id)
.and(person_block::person_id.eq(person_id_join)),
),
)
.left_join(
comment_like::table.on(
comment::id
.eq(comment_like::comment_id)
.and(comment_like::person_id.eq(person_id_join)),
.eq(person::id)
.or(post::creator_id.eq(person::id)),
),
)
.inner_join(person_alias_1)
.select((
person_mention::all_columns,
comment::all_columns,
comment::all_columns.nullable(),
post::all_columns.nullable(),
person::all_columns,
post::all_columns,
community::all_columns,
person_alias_1.fields(person::all_columns),
comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(),
comment_saved::all_columns.nullable(),
person_block::all_columns.nullable(),
comment_like::score.nullable(),
))
.into_boxed();
@ -275,14 +126,7 @@ impl<'a> PersonMentionQuery<'a> {
query = query.filter(person::bot_account.eq(false));
};
query = match self.sort.unwrap_or(CommentSortType::Hot) {
CommentSortType::Hot => query
.then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
.then_order_by(comment_aggregates::published.desc()),
CommentSortType::New => query.then_order_by(comment::published.desc()),
CommentSortType::Old => query.then_order_by(comment::published.asc()),
CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
};
query = query.order_by(person_mention::published.desc());
let (limit, offset) = limit_and_offset(self.page, self.limit)?;
@ -302,16 +146,9 @@ impl JoinView for PersonMentionView {
Self {
person_mention: a.0,
comment: a.1,
creator: a.2,
post: a.3,
community: a.4,
recipient: a.5,
counts: a.6,
creator_banned_from_community: a.7.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&a.8),
saved: a.9.is_some(),
creator_blocked: a.10.is_some(),
my_vote: a.11,
post: a.2,
creator: a.3,
recipient: a.4,
}
}
}

View file

@ -53,17 +53,10 @@ pub struct PersonBlockView {
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
pub struct PersonMentionView {
pub person_mention: PersonMention,
pub comment: Comment,
pub comment: Option<Comment>,
pub post: Option<Post>,
pub creator: Person,
pub post: Post,
pub community: Community,
pub recipient: Person,
pub counts: CommentAggregates,
pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
pub subscribed: SubscribedType, // Left join to CommunityFollower
pub saved: bool, // Left join to CommentSaved
pub creator_blocked: bool, // Left join to PersonBlock
pub my_vote: Option<i16>, // Left join to CommentLike
}
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]

View file

@ -315,9 +315,7 @@ async fn get_feed_inbox(
let mentions = PersonMentionQuery::builder()
.pool(pool)
.recipient_id(Some(person_id))
.my_person_id(Some(person_id))
.show_bot_accounts(Some(show_bot_accounts))
.sort(Some(sort))
.limit(Some(RSS_FETCH_LIMIT))
.build()
.list()
@ -348,10 +346,7 @@ fn create_reply_and_mention_items(
let mut reply_items: Vec<Item> = replies
.iter()
.map(|r| {
let reply_url = format!(
"{}/post/{}/comment/{}",
protocol_and_hostname, r.post.id, r.comment.id
);
let reply_url = format!("{}/comment/{}", protocol_and_hostname, r.comment.id);
build_item(
&r.creator.name,
&r.comment.published,
@ -365,15 +360,26 @@ fn create_reply_and_mention_items(
let mut mention_items: Vec<Item> = mentions
.iter()
.map(|m| {
let mention_url = format!(
"{}/post/{}/comment/{}",
protocol_and_hostname, m.post.id, m.comment.id
);
let (mention_url, published, content) = if let Some(comment) = &m.comment {
(
format!("{}/comment/{}", protocol_and_hostname, comment.id),
comment.published,
comment.content.to_owned(),
)
} else {
let post = m.post.as_ref().expect("must have a post");
(
format!("{}/post/{}", protocol_and_hostname, post.id),
post.published,
post.body.as_ref().expect("has a post body").to_owned(),
)
};
build_item(
&m.creator.name,
&m.comment.published,
&published,
&mention_url,
&m.comment.content,
&content,
protocol_and_hostname,
)
})