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, CommunitySafe},
31 person::{Person, PersonSafe},
32 person_block::PersonBlock,
33 person_mention::PersonMention,
36 traits::{ToSafe, ViewToVec},
37 utils::{functions::hot_rank, get_conn, limit_and_offset, DbPool},
40 use typed_builder::TypedBuilder;
42 type PersonMentionViewTuple = (
50 Option<CommunityPersonBan>,
51 Option<CommunityFollower>,
57 impl PersonMentionView {
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))
96 community_person_ban::expires
98 .or(community_person_ban::expires.gt(now)),
103 community_follower::table.on(
105 .eq(community_follower::community_id)
106 .and(community_follower::person_id.eq(person_id_join)),
110 comment_saved::table.on(
112 .eq(comment_saved::comment_id)
113 .and(comment_saved::person_id.eq(person_id_join)),
117 person_block::table.on(
119 .eq(person_block::target_id)
120 .and(person_block::person_id.eq(person_id_join)),
124 comment_like::table.on(
126 .eq(comment_like::comment_id)
127 .and(comment_like::person_id.eq(person_id_join)),
131 person_mention::all_columns,
132 comment::all_columns,
133 Person::safe_columns_tuple(),
135 Community::safe_columns_tuple(),
136 person_alias_1.fields(Person::safe_columns_tuple()),
137 comment_aggregates::all_columns,
138 community_person_ban::all_columns.nullable(),
139 community_follower::all_columns.nullable(),
140 comment_saved::all_columns.nullable(),
141 person_block::all_columns.nullable(),
142 comment_like::score.nullable(),
144 .first::<PersonMentionViewTuple>(conn)
147 Ok(PersonMentionView {
155 creator_banned_from_community: creator_banned_from_community.is_some(),
156 subscribed: CommunityFollower::to_subscribed_type(&follower),
157 saved: saved.is_some(),
158 creator_blocked: creator_blocked.is_some(),
163 /// Gets the number of unread mentions
164 pub async fn get_unread_mentions(pool: &DbPool, my_person_id: PersonId) -> Result<i64, Error> {
165 use diesel::dsl::count;
166 let conn = &mut get_conn(pool).await?;
168 person_mention::table
169 .inner_join(comment::table)
170 .filter(person_mention::recipient_id.eq(my_person_id))
171 .filter(person_mention::read.eq(false))
172 .filter(comment::deleted.eq(false))
173 .filter(comment::removed.eq(false))
174 .select(count(person_mention::id))
180 #[derive(TypedBuilder)]
181 #[builder(field_defaults(default))]
182 pub struct PersonMentionQuery<'a> {
185 my_person_id: Option<PersonId>,
186 recipient_id: Option<PersonId>,
187 sort: Option<CommentSortType>,
188 unread_only: Option<bool>,
189 show_bot_accounts: Option<bool>,
194 impl<'a> PersonMentionQuery<'a> {
195 pub async fn list(self) -> Result<Vec<PersonMentionView>, Error> {
196 let conn = &mut get_conn(self.pool).await?;
198 let person_alias_1 = diesel::alias!(person as person1);
200 // The left join below will return None in this case
201 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
203 let mut query = person_mention::table
204 .inner_join(comment::table)
205 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
206 .inner_join(post::table.on(comment::post_id.eq(post::id)))
207 .inner_join(community::table.on(post::community_id.eq(community::id)))
208 .inner_join(person_alias_1)
209 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
211 community_person_ban::table.on(
213 .eq(community_person_ban::community_id)
214 .and(community_person_ban::person_id.eq(comment::creator_id))
216 community_person_ban::expires
218 .or(community_person_ban::expires.gt(now)),
223 community_follower::table.on(
225 .eq(community_follower::community_id)
226 .and(community_follower::person_id.eq(person_id_join)),
230 comment_saved::table.on(
232 .eq(comment_saved::comment_id)
233 .and(comment_saved::person_id.eq(person_id_join)),
237 person_block::table.on(
239 .eq(person_block::target_id)
240 .and(person_block::person_id.eq(person_id_join)),
244 comment_like::table.on(
246 .eq(comment_like::comment_id)
247 .and(comment_like::person_id.eq(person_id_join)),
251 person_mention::all_columns,
252 comment::all_columns,
253 Person::safe_columns_tuple(),
255 Community::safe_columns_tuple(),
256 person_alias_1.fields(Person::safe_columns_tuple()),
257 comment_aggregates::all_columns,
258 community_person_ban::all_columns.nullable(),
259 community_follower::all_columns.nullable(),
260 comment_saved::all_columns.nullable(),
261 person_block::all_columns.nullable(),
262 comment_like::score.nullable(),
266 if let Some(recipient_id) = self.recipient_id {
267 query = query.filter(person_mention::recipient_id.eq(recipient_id));
270 if self.unread_only.unwrap_or(false) {
271 query = query.filter(person_mention::read.eq(false));
274 if !self.show_bot_accounts.unwrap_or(true) {
275 query = query.filter(person::bot_account.eq(false));
278 query = match self.sort.unwrap_or(CommentSortType::Hot) {
279 CommentSortType::Hot => query
280 .then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
281 .then_order_by(comment_aggregates::published.desc()),
282 CommentSortType::New => query.then_order_by(comment::published.desc()),
283 CommentSortType::Old => query.then_order_by(comment::published.asc()),
284 CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
287 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
292 .load::<PersonMentionViewTuple>(conn)
295 Ok(PersonMentionView::from_tuple_to_vec(res))
299 impl ViewToVec for PersonMentionView {
300 type DbTuple = PersonMentionViewTuple;
301 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
312 creator_banned_from_community: a.7.is_some(),
313 subscribed: CommunityFollower::to_subscribed_type(&a.8),
314 saved: a.9.is_some(),
315 creator_blocked: a.10.is_some(),
318 .collect::<Vec<Self>>()