1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{
3 aggregates::comment_aggregates::CommentAggregates,
11 use lemmy_db_schema::{
27 comment::{Comment, CommentSaved},
28 community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
29 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
30 person_block::PersonBlock,
31 person_mention::PersonMention,
39 #[derive(Debug, PartialEq, Serialize, Clone)]
40 pub struct PersonMentionView {
41 pub person_mention: PersonMention,
43 pub creator: PersonSafe,
45 pub community: CommunitySafe,
46 pub recipient: PersonSafeAlias1,
47 pub counts: CommentAggregates,
48 pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
49 pub subscribed: bool, // Left join to CommunityFollower
50 pub saved: bool, // Left join to CommentSaved
51 pub creator_blocked: bool, // Left join to PersonBlock
52 pub my_vote: Option<i16>, // Left join to CommentLike
55 type PersonMentionViewTuple = (
63 Option<CommunityPersonBan>,
64 Option<CommunityFollower>,
70 impl PersonMentionView {
73 person_mention_id: PersonMentionId,
74 my_person_id: Option<PersonId>,
75 ) -> Result<Self, Error> {
76 // The left join below will return None in this case
77 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
87 creator_banned_from_community,
92 ) = person_mention::table
93 .find(person_mention_id)
94 .inner_join(comment::table)
95 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
96 .inner_join(post::table.on(comment::post_id.eq(post::id)))
97 .inner_join(community::table.on(post::community_id.eq(community::id)))
98 .inner_join(person_alias_1::table)
99 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
101 community_person_ban::table.on(
103 .eq(community_person_ban::community_id)
104 .and(community_person_ban::person_id.eq(comment::creator_id)),
108 community_follower::table.on(
110 .eq(community_follower::community_id)
111 .and(community_follower::person_id.eq(person_id_join)),
115 comment_saved::table.on(
117 .eq(comment_saved::comment_id)
118 .and(comment_saved::person_id.eq(person_id_join)),
122 person_block::table.on(
124 .eq(person_block::target_id)
125 .and(person_block::person_id.eq(person_id_join)),
129 comment_like::table.on(
131 .eq(comment_like::comment_id)
132 .and(comment_like::person_id.eq(person_id_join)),
136 person_mention::all_columns,
137 comment::all_columns,
138 Person::safe_columns_tuple(),
140 Community::safe_columns_tuple(),
141 PersonAlias1::safe_columns_tuple(),
142 comment_aggregates::all_columns,
143 community_person_ban::all_columns.nullable(),
144 community_follower::all_columns.nullable(),
145 comment_saved::all_columns.nullable(),
146 person_block::all_columns.nullable(),
147 comment_like::score.nullable(),
149 .first::<PersonMentionViewTuple>(conn)?;
151 Ok(PersonMentionView {
159 creator_banned_from_community: creator_banned_from_community.is_some(),
160 subscribed: subscribed.is_some(),
161 saved: saved.is_some(),
162 creator_blocked: creator_blocked.is_some(),
167 /// Gets the number of unread mentions
168 pub fn get_unread_mentions(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
171 person_mention::table
172 .filter(person_mention::recipient_id.eq(my_person_id))
173 .filter(person_mention::read.eq(false))
174 .select(count(person_mention::id))
179 pub struct PersonMentionQueryBuilder<'a> {
180 conn: &'a PgConnection,
181 my_person_id: Option<PersonId>,
182 recipient_id: Option<PersonId>,
183 sort: Option<SortType>,
184 unread_only: Option<bool>,
189 impl<'a> PersonMentionQueryBuilder<'a> {
190 pub fn create(conn: &'a PgConnection) -> Self {
191 PersonMentionQueryBuilder {
202 pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
203 self.sort = sort.get_optional();
207 pub fn unread_only<T: MaybeOptional<bool>>(mut self, unread_only: T) -> Self {
208 self.unread_only = unread_only.get_optional();
212 pub fn recipient_id<T: MaybeOptional<PersonId>>(mut self, recipient_id: T) -> Self {
213 self.recipient_id = recipient_id.get_optional();
217 pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
218 self.my_person_id = my_person_id.get_optional();
222 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
223 self.page = page.get_optional();
227 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
228 self.limit = limit.get_optional();
232 pub fn list(self) -> Result<Vec<PersonMentionView>, Error> {
235 // The left join below will return None in this case
236 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
238 let mut query = person_mention::table
239 .inner_join(comment::table)
240 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
241 .inner_join(post::table.on(comment::post_id.eq(post::id)))
242 .inner_join(community::table.on(post::community_id.eq(community::id)))
243 .inner_join(person_alias_1::table)
244 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
246 community_person_ban::table.on(
248 .eq(community_person_ban::community_id)
249 .and(community_person_ban::person_id.eq(comment::creator_id)),
253 community_follower::table.on(
255 .eq(community_follower::community_id)
256 .and(community_follower::person_id.eq(person_id_join)),
260 comment_saved::table.on(
262 .eq(comment_saved::comment_id)
263 .and(comment_saved::person_id.eq(person_id_join)),
267 person_block::table.on(
269 .eq(person_block::target_id)
270 .and(person_block::person_id.eq(person_id_join)),
274 comment_like::table.on(
276 .eq(comment_like::comment_id)
277 .and(comment_like::person_id.eq(person_id_join)),
281 person_mention::all_columns,
282 comment::all_columns,
283 Person::safe_columns_tuple(),
285 Community::safe_columns_tuple(),
286 PersonAlias1::safe_columns_tuple(),
287 comment_aggregates::all_columns,
288 community_person_ban::all_columns.nullable(),
289 community_follower::all_columns.nullable(),
290 comment_saved::all_columns.nullable(),
291 person_block::all_columns.nullable(),
292 comment_like::score.nullable(),
296 if let Some(recipient_id) = self.recipient_id {
297 query = query.filter(person_mention::recipient_id.eq(recipient_id));
300 if self.unread_only.unwrap_or(false) {
301 query = query.filter(person_mention::read.eq(false));
304 query = match self.sort.unwrap_or(SortType::Hot) {
305 SortType::Hot | SortType::Active => query
306 .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
307 .then_order_by(comment_aggregates::published.desc()),
308 SortType::New | SortType::MostComments | SortType::NewComments => {
309 query.order_by(comment::published.desc())
311 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
312 SortType::TopYear => query
313 .filter(comment::published.gt(now - 1.years()))
314 .order_by(comment_aggregates::score.desc()),
315 SortType::TopMonth => query
316 .filter(comment::published.gt(now - 1.months()))
317 .order_by(comment_aggregates::score.desc()),
318 SortType::TopWeek => query
319 .filter(comment::published.gt(now - 1.weeks()))
320 .order_by(comment_aggregates::score.desc()),
321 SortType::TopDay => query
322 .filter(comment::published.gt(now - 1.days()))
323 .order_by(comment_aggregates::score.desc()),
326 let (limit, offset) = limit_and_offset(self.page, self.limit);
331 .load::<PersonMentionViewTuple>(self.conn)?;
333 Ok(PersonMentionView::from_tuple_to_vec(res))
337 impl ViewToVec for PersonMentionView {
338 type DbTuple = PersonMentionViewTuple;
339 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
343 person_mention: a.0.to_owned(),
344 comment: a.1.to_owned(),
345 creator: a.2.to_owned(),
346 post: a.3.to_owned(),
347 community: a.4.to_owned(),
348 recipient: a.5.to_owned(),
349 counts: a.6.to_owned(),
350 creator_banned_from_community: a.7.is_some(),
351 subscribed: a.8.is_some(),
352 saved: a.9.is_some(),
353 creator_blocked: a.10.is_some(),
356 .collect::<Vec<Self>>()