1 use diesel::{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)),
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 PersonAlias1::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)?;
146 Ok(PersonMentionView {
154 creator_banned_from_community: creator_banned_from_community.is_some(),
155 subscribed: subscribed.is_some(),
156 saved: saved.is_some(),
157 creator_blocked: creator_blocked.is_some(),
162 /// Gets the number of unread mentions
163 pub fn get_unread_mentions(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
166 person_mention::table
167 .filter(person_mention::recipient_id.eq(my_person_id))
168 .filter(person_mention::read.eq(false))
169 .select(count(person_mention::id))
174 pub struct PersonMentionQueryBuilder<'a> {
175 conn: &'a PgConnection,
176 my_person_id: Option<PersonId>,
177 recipient_id: Option<PersonId>,
178 sort: Option<SortType>,
179 unread_only: Option<bool>,
184 impl<'a> PersonMentionQueryBuilder<'a> {
185 pub fn create(conn: &'a PgConnection) -> Self {
186 PersonMentionQueryBuilder {
197 pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
198 self.sort = sort.get_optional();
202 pub fn unread_only<T: MaybeOptional<bool>>(mut self, unread_only: T) -> Self {
203 self.unread_only = unread_only.get_optional();
207 pub fn recipient_id<T: MaybeOptional<PersonId>>(mut self, recipient_id: T) -> Self {
208 self.recipient_id = recipient_id.get_optional();
212 pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
213 self.my_person_id = my_person_id.get_optional();
217 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
218 self.page = page.get_optional();
222 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
223 self.limit = limit.get_optional();
227 pub fn list(self) -> Result<Vec<PersonMentionView>, Error> {
230 // The left join below will return None in this case
231 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
233 let mut query = person_mention::table
234 .inner_join(comment::table)
235 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
236 .inner_join(post::table.on(comment::post_id.eq(post::id)))
237 .inner_join(community::table.on(post::community_id.eq(community::id)))
238 .inner_join(person_alias_1::table)
239 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
241 community_person_ban::table.on(
243 .eq(community_person_ban::community_id)
244 .and(community_person_ban::person_id.eq(comment::creator_id)),
248 community_follower::table.on(
250 .eq(community_follower::community_id)
251 .and(community_follower::person_id.eq(person_id_join)),
255 comment_saved::table.on(
257 .eq(comment_saved::comment_id)
258 .and(comment_saved::person_id.eq(person_id_join)),
262 person_block::table.on(
264 .eq(person_block::target_id)
265 .and(person_block::person_id.eq(person_id_join)),
269 comment_like::table.on(
271 .eq(comment_like::comment_id)
272 .and(comment_like::person_id.eq(person_id_join)),
276 person_mention::all_columns,
277 comment::all_columns,
278 Person::safe_columns_tuple(),
280 Community::safe_columns_tuple(),
281 PersonAlias1::safe_columns_tuple(),
282 comment_aggregates::all_columns,
283 community_person_ban::all_columns.nullable(),
284 community_follower::all_columns.nullable(),
285 comment_saved::all_columns.nullable(),
286 person_block::all_columns.nullable(),
287 comment_like::score.nullable(),
291 if let Some(recipient_id) = self.recipient_id {
292 query = query.filter(person_mention::recipient_id.eq(recipient_id));
295 if self.unread_only.unwrap_or(false) {
296 query = query.filter(person_mention::read.eq(false));
299 query = match self.sort.unwrap_or(SortType::Hot) {
300 SortType::Hot | SortType::Active => query
301 .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
302 .then_order_by(comment_aggregates::published.desc()),
303 SortType::New | SortType::MostComments | SortType::NewComments => {
304 query.order_by(comment::published.desc())
306 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
307 SortType::TopYear => query
308 .filter(comment::published.gt(now - 1.years()))
309 .order_by(comment_aggregates::score.desc()),
310 SortType::TopMonth => query
311 .filter(comment::published.gt(now - 1.months()))
312 .order_by(comment_aggregates::score.desc()),
313 SortType::TopWeek => query
314 .filter(comment::published.gt(now - 1.weeks()))
315 .order_by(comment_aggregates::score.desc()),
316 SortType::TopDay => query
317 .filter(comment::published.gt(now - 1.days()))
318 .order_by(comment_aggregates::score.desc()),
321 let (limit, offset) = limit_and_offset(self.page, self.limit);
326 .load::<PersonMentionViewTuple>(self.conn)?;
328 Ok(PersonMentionView::from_tuple_to_vec(res))
332 impl ViewToVec for PersonMentionView {
333 type DbTuple = PersonMentionViewTuple;
334 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
338 person_mention: a.0.to_owned(),
339 comment: a.1.to_owned(),
340 creator: a.2.to_owned(),
341 post: a.3.to_owned(),
342 community: a.4.to_owned(),
343 recipient: a.5.to_owned(),
344 counts: a.6.to_owned(),
345 creator_banned_from_community: a.7.is_some(),
346 subscribed: a.8.is_some(),
347 saved: a.9.is_some(),
348 creator_blocked: a.10.is_some(),
351 .collect::<Vec<Self>>()