1 use crate::structs::PersonMentionView;
8 NullableExpressionMethods,
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
13 aggregates::structs::CommentAggregates,
14 newtypes::{PersonId, PersonMentionId},
29 comment::{Comment, CommentSaved},
30 community::{Community, CommunityFollower, CommunityPersonBan},
32 person_block::PersonBlock,
33 person_mention::PersonMention,
37 utils::{get_conn, limit_and_offset, DbPool},
40 use typed_builder::TypedBuilder;
42 type PersonMentionViewTuple = (
50 Option<CommunityPersonBan>,
51 Option<CommunityFollower>,
57 impl PersonMentionView {
59 pool: &mut DbPool<'_>,
60 person_mention_id: PersonMentionId,
61 my_person_id: Option<PersonId>,
62 ) -> Result<Self, Error> {
63 let conn = &mut get_conn(pool).await?;
64 let person_alias_1 = diesel::alias!(person as person1);
66 // The left join below will return None in this case
67 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
77 creator_banned_from_community,
82 ) = person_mention::table
83 .find(person_mention_id)
84 .inner_join(comment::table)
85 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
86 .inner_join(post::table.on(comment::post_id.eq(post::id)))
87 .inner_join(community::table.on(post::community_id.eq(community::id)))
88 .inner_join(person_alias_1)
89 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
91 community_person_ban::table.on(
93 .eq(community_person_ban::community_id)
94 .and(community_person_ban::person_id.eq(comment::creator_id)),
98 community_follower::table.on(
100 .eq(community_follower::community_id)
101 .and(community_follower::person_id.eq(person_id_join)),
105 comment_saved::table.on(
107 .eq(comment_saved::comment_id)
108 .and(comment_saved::person_id.eq(person_id_join)),
112 person_block::table.on(
114 .eq(person_block::target_id)
115 .and(person_block::person_id.eq(person_id_join)),
119 comment_like::table.on(
121 .eq(comment_like::comment_id)
122 .and(comment_like::person_id.eq(person_id_join)),
126 person_mention::all_columns,
127 comment::all_columns,
130 community::all_columns,
131 person_alias_1.fields(person::all_columns),
132 comment_aggregates::all_columns,
133 community_person_ban::all_columns.nullable(),
134 community_follower::all_columns.nullable(),
135 comment_saved::all_columns.nullable(),
136 person_block::all_columns.nullable(),
137 comment_like::score.nullable(),
139 .first::<PersonMentionViewTuple>(conn)
142 Ok(PersonMentionView {
150 creator_banned_from_community: creator_banned_from_community.is_some(),
151 subscribed: CommunityFollower::to_subscribed_type(&follower),
152 saved: saved.is_some(),
153 creator_blocked: creator_blocked.is_some(),
158 /// Gets the number of unread mentions
159 pub async fn get_unread_mentions(
160 pool: &mut DbPool<'_>,
161 my_person_id: PersonId,
162 ) -> Result<i64, Error> {
163 use diesel::dsl::count;
164 let conn = &mut get_conn(pool).await?;
166 person_mention::table
167 .inner_join(comment::table)
168 .filter(person_mention::recipient_id.eq(my_person_id))
169 .filter(person_mention::read.eq(false))
170 .filter(comment::deleted.eq(false))
171 .filter(comment::removed.eq(false))
172 .select(count(person_mention::id))
178 #[derive(TypedBuilder)]
179 #[builder(field_defaults(default))]
180 pub struct PersonMentionQuery<'a, 'b: 'a> {
182 pool: &'a mut DbPool<'b>,
183 my_person_id: Option<PersonId>,
184 recipient_id: Option<PersonId>,
185 sort: Option<CommentSortType>,
186 unread_only: Option<bool>,
187 show_bot_accounts: Option<bool>,
192 impl<'a, 'b: 'a> PersonMentionQuery<'a, 'b> {
193 pub async fn list(self) -> Result<Vec<PersonMentionView>, Error> {
194 let conn = &mut get_conn(self.pool).await?;
196 let person_alias_1 = diesel::alias!(person as person1);
198 // The left join below will return None in this case
199 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
201 let mut query = person_mention::table
202 .inner_join(comment::table)
203 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
204 .inner_join(post::table.on(comment::post_id.eq(post::id)))
205 .inner_join(community::table.on(post::community_id.eq(community::id)))
206 .inner_join(person_alias_1)
207 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
209 community_person_ban::table.on(
211 .eq(community_person_ban::community_id)
212 .and(community_person_ban::person_id.eq(comment::creator_id))
214 community_person_ban::expires
216 .or(community_person_ban::expires.gt(now)),
221 community_follower::table.on(
223 .eq(community_follower::community_id)
224 .and(community_follower::person_id.eq(person_id_join)),
228 comment_saved::table.on(
230 .eq(comment_saved::comment_id)
231 .and(comment_saved::person_id.eq(person_id_join)),
235 person_block::table.on(
237 .eq(person_block::target_id)
238 .and(person_block::person_id.eq(person_id_join)),
242 comment_like::table.on(
244 .eq(comment_like::comment_id)
245 .and(comment_like::person_id.eq(person_id_join)),
249 person_mention::all_columns,
250 comment::all_columns,
253 community::all_columns,
254 person_alias_1.fields(person::all_columns),
255 comment_aggregates::all_columns,
256 community_person_ban::all_columns.nullable(),
257 community_follower::all_columns.nullable(),
258 comment_saved::all_columns.nullable(),
259 person_block::all_columns.nullable(),
260 comment_like::score.nullable(),
264 if let Some(recipient_id) = self.recipient_id {
265 query = query.filter(person_mention::recipient_id.eq(recipient_id));
268 if self.unread_only.unwrap_or(false) {
269 query = query.filter(person_mention::read.eq(false));
272 if !self.show_bot_accounts.unwrap_or(true) {
273 query = query.filter(person::bot_account.eq(false));
276 query = match self.sort.unwrap_or(CommentSortType::Hot) {
277 CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()),
278 CommentSortType::New => query.then_order_by(comment::published.desc()),
279 CommentSortType::Old => query.then_order_by(comment::published.asc()),
280 CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
283 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
288 .load::<PersonMentionViewTuple>(conn)
291 Ok(res.into_iter().map(PersonMentionView::from_tuple).collect())
295 impl JoinView for PersonMentionView {
296 type JoinTuple = PersonMentionViewTuple;
297 fn from_tuple(a: Self::JoinTuple) -> Self {
306 creator_banned_from_community: a.7.is_some(),
307 subscribed: CommunityFollower::to_subscribed_type(&a.8),
308 saved: a.9.is_some(),
309 creator_blocked: a.10.is_some(),