1 use crate::structs::PersonMentionView;
2 use diesel::{dsl::*, result::Error, *};
4 aggregates::structs::CommentAggregates,
5 newtypes::{PersonId, PersonMentionId},
21 comment::{Comment, CommentSaved},
22 community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
23 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
24 person_block::PersonBlock,
25 person_mention::PersonMention,
28 traits::{MaybeOptional, ToSafe, ViewToVec},
29 utils::{functions::hot_rank, limit_and_offset, SortType},
32 type PersonMentionViewTuple = (
40 Option<CommunityPersonBan>,
41 Option<CommunityFollower>,
47 impl PersonMentionView {
50 person_mention_id: PersonMentionId,
51 my_person_id: Option<PersonId>,
52 ) -> Result<Self, Error> {
53 // The left join below will return None in this case
54 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
64 creator_banned_from_community,
69 ) = person_mention::table
70 .find(person_mention_id)
71 .inner_join(comment::table)
72 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
73 .inner_join(post::table.on(comment::post_id.eq(post::id)))
74 .inner_join(community::table.on(post::community_id.eq(community::id)))
75 .inner_join(person_alias_1::table)
76 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
78 community_person_ban::table.on(
80 .eq(community_person_ban::community_id)
81 .and(community_person_ban::person_id.eq(comment::creator_id))
83 community_person_ban::expires
85 .or(community_person_ban::expires.gt(now)),
90 community_follower::table.on(
92 .eq(community_follower::community_id)
93 .and(community_follower::person_id.eq(person_id_join)),
97 comment_saved::table.on(
99 .eq(comment_saved::comment_id)
100 .and(comment_saved::person_id.eq(person_id_join)),
104 person_block::table.on(
106 .eq(person_block::target_id)
107 .and(person_block::person_id.eq(person_id_join)),
111 comment_like::table.on(
113 .eq(comment_like::comment_id)
114 .and(comment_like::person_id.eq(person_id_join)),
118 person_mention::all_columns,
119 comment::all_columns,
120 Person::safe_columns_tuple(),
122 Community::safe_columns_tuple(),
123 PersonAlias1::safe_columns_tuple(),
124 comment_aggregates::all_columns,
125 community_person_ban::all_columns.nullable(),
126 community_follower::all_columns.nullable(),
127 comment_saved::all_columns.nullable(),
128 person_block::all_columns.nullable(),
129 comment_like::score.nullable(),
131 .first::<PersonMentionViewTuple>(conn)?;
133 Ok(PersonMentionView {
141 creator_banned_from_community: creator_banned_from_community.is_some(),
142 subscribed: subscribed.is_some(),
143 saved: saved.is_some(),
144 creator_blocked: creator_blocked.is_some(),
149 /// Gets the number of unread mentions
150 pub fn get_unread_mentions(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
153 person_mention::table
154 .filter(person_mention::recipient_id.eq(my_person_id))
155 .filter(person_mention::read.eq(false))
156 .select(count(person_mention::id))
161 pub struct PersonMentionQueryBuilder<'a> {
162 conn: &'a PgConnection,
163 my_person_id: Option<PersonId>,
164 recipient_id: Option<PersonId>,
165 sort: Option<SortType>,
166 unread_only: Option<bool>,
171 impl<'a> PersonMentionQueryBuilder<'a> {
172 pub fn create(conn: &'a PgConnection) -> Self {
173 PersonMentionQueryBuilder {
184 pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
185 self.sort = sort.get_optional();
189 pub fn unread_only<T: MaybeOptional<bool>>(mut self, unread_only: T) -> Self {
190 self.unread_only = unread_only.get_optional();
194 pub fn recipient_id<T: MaybeOptional<PersonId>>(mut self, recipient_id: T) -> Self {
195 self.recipient_id = recipient_id.get_optional();
199 pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
200 self.my_person_id = my_person_id.get_optional();
204 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
205 self.page = page.get_optional();
209 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
210 self.limit = limit.get_optional();
214 pub fn list(self) -> Result<Vec<PersonMentionView>, Error> {
217 // The left join below will return None in this case
218 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
220 let mut query = person_mention::table
221 .inner_join(comment::table)
222 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
223 .inner_join(post::table.on(comment::post_id.eq(post::id)))
224 .inner_join(community::table.on(post::community_id.eq(community::id)))
225 .inner_join(person_alias_1::table)
226 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
228 community_person_ban::table.on(
230 .eq(community_person_ban::community_id)
231 .and(community_person_ban::person_id.eq(comment::creator_id))
233 community_person_ban::expires
235 .or(community_person_ban::expires.gt(now)),
240 community_follower::table.on(
242 .eq(community_follower::community_id)
243 .and(community_follower::person_id.eq(person_id_join)),
247 comment_saved::table.on(
249 .eq(comment_saved::comment_id)
250 .and(comment_saved::person_id.eq(person_id_join)),
254 person_block::table.on(
256 .eq(person_block::target_id)
257 .and(person_block::person_id.eq(person_id_join)),
261 comment_like::table.on(
263 .eq(comment_like::comment_id)
264 .and(comment_like::person_id.eq(person_id_join)),
268 person_mention::all_columns,
269 comment::all_columns,
270 Person::safe_columns_tuple(),
272 Community::safe_columns_tuple(),
273 PersonAlias1::safe_columns_tuple(),
274 comment_aggregates::all_columns,
275 community_person_ban::all_columns.nullable(),
276 community_follower::all_columns.nullable(),
277 comment_saved::all_columns.nullable(),
278 person_block::all_columns.nullable(),
279 comment_like::score.nullable(),
283 if let Some(recipient_id) = self.recipient_id {
284 query = query.filter(person_mention::recipient_id.eq(recipient_id));
287 if self.unread_only.unwrap_or(false) {
288 query = query.filter(person_mention::read.eq(false));
291 query = match self.sort.unwrap_or(SortType::Hot) {
292 SortType::Hot | SortType::Active => query
293 .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
294 .then_order_by(comment_aggregates::published.desc()),
295 SortType::New | SortType::MostComments | SortType::NewComments => {
296 query.order_by(comment::published.desc())
298 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
299 SortType::TopYear => query
300 .filter(comment::published.gt(now - 1.years()))
301 .order_by(comment_aggregates::score.desc()),
302 SortType::TopMonth => query
303 .filter(comment::published.gt(now - 1.months()))
304 .order_by(comment_aggregates::score.desc()),
305 SortType::TopWeek => query
306 .filter(comment::published.gt(now - 1.weeks()))
307 .order_by(comment_aggregates::score.desc()),
308 SortType::TopDay => query
309 .filter(comment::published.gt(now - 1.days()))
310 .order_by(comment_aggregates::score.desc()),
313 let (limit, offset) = limit_and_offset(self.page, self.limit);
318 .load::<PersonMentionViewTuple>(self.conn)?;
320 Ok(PersonMentionView::from_tuple_to_vec(res))
324 impl ViewToVec for PersonMentionView {
325 type DbTuple = PersonMentionViewTuple;
326 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
330 person_mention: a.0.to_owned(),
331 comment: a.1.to_owned(),
332 creator: a.2.to_owned(),
333 post: a.3.to_owned(),
334 community: a.4.to_owned(),
335 recipient: a.5.to_owned(),
336 counts: a.6.to_owned(),
337 creator_banned_from_community: a.7.is_some(),
338 subscribed: a.8.is_some(),
339 saved: a.9.is_some(),
340 creator_blocked: a.10.is_some(),
343 .collect::<Vec<Self>>()