1 use crate::structs::PersonMentionView;
9 NullableExpressionMethods,
12 use diesel_async::RunQueryDsl;
13 use lemmy_db_schema::{
14 aggregates::structs::CommentAggregates,
16 newtypes::{PersonId, PersonMentionId},
32 community::{Community, CommunityFollower},
34 person_mention::PersonMention,
38 utils::{get_conn, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
43 type PersonMentionViewTuple = (
58 fn queries<'a>() -> Queries<
59 impl ReadFn<'a, PersonMentionView, (PersonMentionId, Option<PersonId>)>,
60 impl ListFn<'a, PersonMentionView, PersonMentionQuery>,
62 let all_joins = |query: person_mention::BoxedQuery<'a, Pg>, my_person_id: Option<PersonId>| {
63 // The left join below will return None in this case
64 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
67 .inner_join(comment::table)
68 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
69 .inner_join(post::table.on(comment::post_id.eq(post::id)))
70 .inner_join(community::table.on(post::community_id.eq(community::id)))
71 .inner_join(aliases::person1)
72 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
74 community_follower::table.on(
76 .eq(community_follower::community_id)
77 .and(community_follower::person_id.eq(person_id_join)),
81 comment_saved::table.on(
83 .eq(comment_saved::comment_id)
84 .and(comment_saved::person_id.eq(person_id_join)),
88 person_block::table.on(
90 .eq(person_block::target_id)
91 .and(person_block::person_id.eq(person_id_join)),
95 comment_like::table.on(
97 .eq(comment_like::comment_id)
98 .and(comment_like::person_id.eq(person_id_join)),
104 person_mention::all_columns,
105 comment::all_columns,
108 community::all_columns,
109 aliases::person1.fields(person::all_columns),
110 comment_aggregates::all_columns,
111 community_person_ban::id.nullable().is_not_null(),
112 CommunityFollower::select_subscribed_type(),
113 comment_saved::id.nullable().is_not_null(),
114 person_block::id.nullable().is_not_null(),
115 comment_like::score.nullable(),
119 move |mut conn: DbConn<'a>,
120 (person_mention_id, my_person_id): (PersonMentionId, Option<PersonId>)| async move {
122 person_mention::table.find(person_mention_id).into_boxed(),
126 community_person_ban::table.on(
128 .eq(community_person_ban::community_id)
129 .and(community_person_ban::person_id.eq(comment::creator_id)),
133 .first::<PersonMentionViewTuple>(&mut conn)
137 let list = move |mut conn: DbConn<'a>, options: PersonMentionQuery| async move {
138 let mut query = all_joins(person_mention::table.into_boxed(), options.my_person_id)
140 community_person_ban::table.on(
142 .eq(community_person_ban::community_id)
143 .and(community_person_ban::person_id.eq(comment::creator_id))
145 community_person_ban::expires
147 .or(community_person_ban::expires.gt(now)),
153 if let Some(recipient_id) = options.recipient_id {
154 query = query.filter(person_mention::recipient_id.eq(recipient_id));
157 if options.unread_only.unwrap_or(false) {
158 query = query.filter(person_mention::read.eq(false));
161 if !options.show_bot_accounts.unwrap_or(true) {
162 query = query.filter(person::bot_account.eq(false));
165 query = match options.sort.unwrap_or(CommentSortType::Hot) {
166 CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()),
167 CommentSortType::Controversial => {
168 query.then_order_by(comment_aggregates::controversy_rank.desc())
170 CommentSortType::New => query.then_order_by(comment::published.desc()),
171 CommentSortType::Old => query.then_order_by(comment::published.asc()),
172 CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
175 let (limit, offset) = limit_and_offset(options.page, options.limit)?;
180 .load::<PersonMentionViewTuple>(&mut conn)
184 Queries::new(read, list)
187 impl PersonMentionView {
189 pool: &mut DbPool<'_>,
190 person_mention_id: PersonMentionId,
191 my_person_id: Option<PersonId>,
192 ) -> Result<Self, Error> {
194 .read(pool, (person_mention_id, my_person_id))
198 /// Gets the number of unread mentions
199 pub async fn get_unread_mentions(
200 pool: &mut DbPool<'_>,
201 my_person_id: PersonId,
202 ) -> Result<i64, Error> {
203 use diesel::dsl::count;
204 let conn = &mut get_conn(pool).await?;
206 person_mention::table
207 .inner_join(comment::table)
208 .filter(person_mention::recipient_id.eq(my_person_id))
209 .filter(person_mention::read.eq(false))
210 .filter(comment::deleted.eq(false))
211 .filter(comment::removed.eq(false))
212 .select(count(person_mention::id))
219 pub struct PersonMentionQuery {
220 pub my_person_id: Option<PersonId>,
221 pub recipient_id: Option<PersonId>,
222 pub sort: Option<CommentSortType>,
223 pub unread_only: Option<bool>,
224 pub show_bot_accounts: Option<bool>,
225 pub page: Option<i64>,
226 pub limit: Option<i64>,
229 impl PersonMentionQuery {
230 pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<PersonMentionView>, Error> {
231 queries().list(pool, self).await
235 impl JoinView for PersonMentionView {
236 type JoinTuple = PersonMentionViewTuple;
237 fn from_tuple(a: Self::JoinTuple) -> Self {
246 creator_banned_from_community: a.7,
249 creator_blocked: a.10,