1 use diesel::{dsl::*, result::Error, *};
3 aggregates::comment_aggregates::CommentAggregates,
6 newtypes::{PersonId, PersonMentionId},
22 comment::{Comment, CommentSaved},
23 community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
24 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
25 person_block::PersonBlock,
26 person_mention::PersonMention,
29 traits::{MaybeOptional, ToSafe, ViewToVec},
32 use serde::{Deserialize, Serialize};
34 #[derive(Debug, PartialEq, Serialize, Deserialize, Clone)]
35 pub struct PersonMentionView {
36 pub person_mention: PersonMention,
38 pub creator: PersonSafe,
40 pub community: CommunitySafe,
41 pub recipient: PersonSafeAlias1,
42 pub counts: CommentAggregates,
43 pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
44 pub subscribed: bool, // Left join to CommunityFollower
45 pub saved: bool, // Left join to CommentSaved
46 pub creator_blocked: bool, // Left join to PersonBlock
47 pub my_vote: Option<i16>, // Left join to CommentLike
50 type PersonMentionViewTuple = (
58 Option<CommunityPersonBan>,
59 Option<CommunityFollower>,
65 impl PersonMentionView {
68 person_mention_id: PersonMentionId,
69 my_person_id: Option<PersonId>,
70 ) -> Result<Self, Error> {
71 // The left join below will return None in this case
72 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
82 creator_banned_from_community,
87 ) = person_mention::table
88 .find(person_mention_id)
89 .inner_join(comment::table)
90 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
91 .inner_join(post::table.on(comment::post_id.eq(post::id)))
92 .inner_join(community::table.on(post::community_id.eq(community::id)))
93 .inner_join(person_alias_1::table)
94 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
96 community_person_ban::table.on(
98 .eq(community_person_ban::community_id)
99 .and(community_person_ban::person_id.eq(comment::creator_id))
101 community_person_ban::expires
103 .or(community_person_ban::expires.gt(now)),
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))
251 community_person_ban::expires
253 .or(community_person_ban::expires.gt(now)),
258 community_follower::table.on(
260 .eq(community_follower::community_id)
261 .and(community_follower::person_id.eq(person_id_join)),
265 comment_saved::table.on(
267 .eq(comment_saved::comment_id)
268 .and(comment_saved::person_id.eq(person_id_join)),
272 person_block::table.on(
274 .eq(person_block::target_id)
275 .and(person_block::person_id.eq(person_id_join)),
279 comment_like::table.on(
281 .eq(comment_like::comment_id)
282 .and(comment_like::person_id.eq(person_id_join)),
286 person_mention::all_columns,
287 comment::all_columns,
288 Person::safe_columns_tuple(),
290 Community::safe_columns_tuple(),
291 PersonAlias1::safe_columns_tuple(),
292 comment_aggregates::all_columns,
293 community_person_ban::all_columns.nullable(),
294 community_follower::all_columns.nullable(),
295 comment_saved::all_columns.nullable(),
296 person_block::all_columns.nullable(),
297 comment_like::score.nullable(),
301 if let Some(recipient_id) = self.recipient_id {
302 query = query.filter(person_mention::recipient_id.eq(recipient_id));
305 if self.unread_only.unwrap_or(false) {
306 query = query.filter(person_mention::read.eq(false));
309 query = match self.sort.unwrap_or(SortType::Hot) {
310 SortType::Hot | SortType::Active => query
311 .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
312 .then_order_by(comment_aggregates::published.desc()),
313 SortType::New | SortType::MostComments | SortType::NewComments => {
314 query.order_by(comment::published.desc())
316 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
317 SortType::TopYear => query
318 .filter(comment::published.gt(now - 1.years()))
319 .order_by(comment_aggregates::score.desc()),
320 SortType::TopMonth => query
321 .filter(comment::published.gt(now - 1.months()))
322 .order_by(comment_aggregates::score.desc()),
323 SortType::TopWeek => query
324 .filter(comment::published.gt(now - 1.weeks()))
325 .order_by(comment_aggregates::score.desc()),
326 SortType::TopDay => query
327 .filter(comment::published.gt(now - 1.days()))
328 .order_by(comment_aggregates::score.desc()),
331 let (limit, offset) = limit_and_offset(self.page, self.limit);
336 .load::<PersonMentionViewTuple>(self.conn)?;
338 Ok(PersonMentionView::from_tuple_to_vec(res))
342 impl ViewToVec for PersonMentionView {
343 type DbTuple = PersonMentionViewTuple;
344 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
348 person_mention: a.0.to_owned(),
349 comment: a.1.to_owned(),
350 creator: a.2.to_owned(),
351 post: a.3.to_owned(),
352 community: a.4.to_owned(),
353 recipient: a.5.to_owned(),
354 counts: a.6.to_owned(),
355 creator_banned_from_community: a.7.is_some(),
356 subscribed: a.8.is_some(),
357 saved: a.9.is_some(),
358 creator_blocked: a.10.is_some(),
361 .collect::<Vec<Self>>()