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},
33 type PersonMentionViewTuple = (
41 Option<CommunityPersonBan>,
42 Option<CommunityFollower>,
48 impl PersonMentionView {
51 person_mention_id: PersonMentionId,
52 my_person_id: Option<PersonId>,
53 ) -> Result<Self, Error> {
54 // The left join below will return None in this case
55 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
65 creator_banned_from_community,
70 ) = person_mention::table
71 .find(person_mention_id)
72 .inner_join(comment::table)
73 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
74 .inner_join(post::table.on(comment::post_id.eq(post::id)))
75 .inner_join(community::table.on(post::community_id.eq(community::id)))
76 .inner_join(person_alias_1::table)
77 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
79 community_person_ban::table.on(
81 .eq(community_person_ban::community_id)
82 .and(community_person_ban::person_id.eq(comment::creator_id))
84 community_person_ban::expires
86 .or(community_person_ban::expires.gt(now)),
91 community_follower::table.on(
93 .eq(community_follower::community_id)
94 .and(community_follower::person_id.eq(person_id_join)),
98 comment_saved::table.on(
100 .eq(comment_saved::comment_id)
101 .and(comment_saved::person_id.eq(person_id_join)),
105 person_block::table.on(
107 .eq(person_block::target_id)
108 .and(person_block::person_id.eq(person_id_join)),
112 comment_like::table.on(
114 .eq(comment_like::comment_id)
115 .and(comment_like::person_id.eq(person_id_join)),
119 person_mention::all_columns,
120 comment::all_columns,
121 Person::safe_columns_tuple(),
123 Community::safe_columns_tuple(),
124 PersonAlias1::safe_columns_tuple(),
125 comment_aggregates::all_columns,
126 community_person_ban::all_columns.nullable(),
127 community_follower::all_columns.nullable(),
128 comment_saved::all_columns.nullable(),
129 person_block::all_columns.nullable(),
130 comment_like::score.nullable(),
132 .first::<PersonMentionViewTuple>(conn)?;
134 Ok(PersonMentionView {
142 creator_banned_from_community: creator_banned_from_community.is_some(),
143 subscribed: CommunityFollower::to_subscribed_type(&follower),
144 saved: saved.is_some(),
145 creator_blocked: creator_blocked.is_some(),
150 /// Gets the number of unread mentions
151 pub fn get_unread_mentions(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
154 person_mention::table
155 .filter(person_mention::recipient_id.eq(my_person_id))
156 .filter(person_mention::read.eq(false))
157 .select(count(person_mention::id))
162 pub struct PersonMentionQueryBuilder<'a> {
163 conn: &'a PgConnection,
164 my_person_id: Option<PersonId>,
165 recipient_id: Option<PersonId>,
166 sort: Option<SortType>,
167 unread_only: Option<bool>,
172 impl<'a> PersonMentionQueryBuilder<'a> {
173 pub fn create(conn: &'a PgConnection) -> Self {
174 PersonMentionQueryBuilder {
185 pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
186 self.sort = sort.get_optional();
190 pub fn unread_only<T: MaybeOptional<bool>>(mut self, unread_only: T) -> Self {
191 self.unread_only = unread_only.get_optional();
195 pub fn recipient_id<T: MaybeOptional<PersonId>>(mut self, recipient_id: T) -> Self {
196 self.recipient_id = recipient_id.get_optional();
200 pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
201 self.my_person_id = my_person_id.get_optional();
205 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
206 self.page = page.get_optional();
210 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
211 self.limit = limit.get_optional();
215 pub fn list(self) -> Result<Vec<PersonMentionView>, Error> {
218 // The left join below will return None in this case
219 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
221 let mut query = person_mention::table
222 .inner_join(comment::table)
223 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
224 .inner_join(post::table.on(comment::post_id.eq(post::id)))
225 .inner_join(community::table.on(post::community_id.eq(community::id)))
226 .inner_join(person_alias_1::table)
227 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
229 community_person_ban::table.on(
231 .eq(community_person_ban::community_id)
232 .and(community_person_ban::person_id.eq(comment::creator_id))
234 community_person_ban::expires
236 .or(community_person_ban::expires.gt(now)),
241 community_follower::table.on(
243 .eq(community_follower::community_id)
244 .and(community_follower::person_id.eq(person_id_join)),
248 comment_saved::table.on(
250 .eq(comment_saved::comment_id)
251 .and(comment_saved::person_id.eq(person_id_join)),
255 person_block::table.on(
257 .eq(person_block::target_id)
258 .and(person_block::person_id.eq(person_id_join)),
262 comment_like::table.on(
264 .eq(comment_like::comment_id)
265 .and(comment_like::person_id.eq(person_id_join)),
269 person_mention::all_columns,
270 comment::all_columns,
271 Person::safe_columns_tuple(),
273 Community::safe_columns_tuple(),
274 PersonAlias1::safe_columns_tuple(),
275 comment_aggregates::all_columns,
276 community_person_ban::all_columns.nullable(),
277 community_follower::all_columns.nullable(),
278 comment_saved::all_columns.nullable(),
279 person_block::all_columns.nullable(),
280 comment_like::score.nullable(),
284 if let Some(recipient_id) = self.recipient_id {
285 query = query.filter(person_mention::recipient_id.eq(recipient_id));
288 if self.unread_only.unwrap_or(false) {
289 query = query.filter(person_mention::read.eq(false));
292 query = match self.sort.unwrap_or(SortType::Hot) {
293 SortType::Hot | SortType::Active => query
294 .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
295 .then_order_by(comment_aggregates::published.desc()),
296 SortType::New | SortType::MostComments | SortType::NewComments => {
297 query.order_by(comment::published.desc())
299 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
300 SortType::TopYear => query
301 .filter(comment::published.gt(now - 1.years()))
302 .order_by(comment_aggregates::score.desc()),
303 SortType::TopMonth => query
304 .filter(comment::published.gt(now - 1.months()))
305 .order_by(comment_aggregates::score.desc()),
306 SortType::TopWeek => query
307 .filter(comment::published.gt(now - 1.weeks()))
308 .order_by(comment_aggregates::score.desc()),
309 SortType::TopDay => query
310 .filter(comment::published.gt(now - 1.days()))
311 .order_by(comment_aggregates::score.desc()),
314 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
319 .load::<PersonMentionViewTuple>(self.conn)?;
321 Ok(PersonMentionView::from_tuple_to_vec(res))
325 impl ViewToVec for PersonMentionView {
326 type DbTuple = PersonMentionViewTuple;
327 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
331 person_mention: a.0.to_owned(),
332 comment: a.1.to_owned(),
333 creator: a.2.to_owned(),
334 post: a.3.to_owned(),
335 community: a.4.to_owned(),
336 recipient: a.5.to_owned(),
337 counts: a.6.to_owned(),
338 creator_banned_from_community: a.7.is_some(),
339 subscribed: CommunityFollower::to_subscribed_type(&a.8),
340 saved: a.9.is_some(),
341 creator_blocked: a.10.is_some(),
344 .collect::<Vec<Self>>()