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(),
168 pub struct PersonMentionQueryBuilder<'a> {
169 conn: &'a PgConnection,
170 my_person_id: Option<PersonId>,
171 recipient_id: Option<PersonId>,
172 sort: Option<SortType>,
173 unread_only: Option<bool>,
178 impl<'a> PersonMentionQueryBuilder<'a> {
179 pub fn create(conn: &'a PgConnection) -> Self {
180 PersonMentionQueryBuilder {
191 pub fn sort<T: MaybeOptional<SortType>>(mut self, sort: T) -> Self {
192 self.sort = sort.get_optional();
196 pub fn unread_only<T: MaybeOptional<bool>>(mut self, unread_only: T) -> Self {
197 self.unread_only = unread_only.get_optional();
201 pub fn recipient_id<T: MaybeOptional<PersonId>>(mut self, recipient_id: T) -> Self {
202 self.recipient_id = recipient_id.get_optional();
206 pub fn my_person_id<T: MaybeOptional<PersonId>>(mut self, my_person_id: T) -> Self {
207 self.my_person_id = my_person_id.get_optional();
211 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
212 self.page = page.get_optional();
216 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
217 self.limit = limit.get_optional();
221 pub fn list(self) -> Result<Vec<PersonMentionView>, Error> {
224 // The left join below will return None in this case
225 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
227 let mut query = person_mention::table
228 .inner_join(comment::table)
229 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
230 .inner_join(post::table.on(comment::post_id.eq(post::id)))
231 .inner_join(community::table.on(post::community_id.eq(community::id)))
232 .inner_join(person_alias_1::table)
233 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
235 community_person_ban::table.on(
237 .eq(community_person_ban::community_id)
238 .and(community_person_ban::person_id.eq(comment::creator_id)),
242 community_follower::table.on(
244 .eq(community_follower::community_id)
245 .and(community_follower::person_id.eq(person_id_join)),
249 comment_saved::table.on(
251 .eq(comment_saved::comment_id)
252 .and(comment_saved::person_id.eq(person_id_join)),
256 person_block::table.on(
258 .eq(person_block::target_id)
259 .and(person_block::person_id.eq(person_id_join)),
263 comment_like::table.on(
265 .eq(comment_like::comment_id)
266 .and(comment_like::person_id.eq(person_id_join)),
270 person_mention::all_columns,
271 comment::all_columns,
272 Person::safe_columns_tuple(),
274 Community::safe_columns_tuple(),
275 PersonAlias1::safe_columns_tuple(),
276 comment_aggregates::all_columns,
277 community_person_ban::all_columns.nullable(),
278 community_follower::all_columns.nullable(),
279 comment_saved::all_columns.nullable(),
280 person_block::all_columns.nullable(),
281 comment_like::score.nullable(),
285 if let Some(recipient_id) = self.recipient_id {
286 query = query.filter(person_mention::recipient_id.eq(recipient_id));
289 if self.unread_only.unwrap_or(false) {
290 query = query.filter(person_mention::read.eq(false));
293 query = match self.sort.unwrap_or(SortType::Hot) {
294 SortType::Hot | SortType::Active => query
295 .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
296 .then_order_by(comment_aggregates::published.desc()),
297 SortType::New | SortType::MostComments | SortType::NewComments => {
298 query.order_by(comment::published.desc())
300 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
301 SortType::TopYear => query
302 .filter(comment::published.gt(now - 1.years()))
303 .order_by(comment_aggregates::score.desc()),
304 SortType::TopMonth => query
305 .filter(comment::published.gt(now - 1.months()))
306 .order_by(comment_aggregates::score.desc()),
307 SortType::TopWeek => query
308 .filter(comment::published.gt(now - 1.weeks()))
309 .order_by(comment_aggregates::score.desc()),
310 SortType::TopDay => query
311 .filter(comment::published.gt(now - 1.days()))
312 .order_by(comment_aggregates::score.desc()),
315 let (limit, offset) = limit_and_offset(self.page, self.limit);
320 .load::<PersonMentionViewTuple>(self.conn)?;
322 Ok(PersonMentionView::from_tuple_to_vec(res))
326 impl ViewToVec for PersonMentionView {
327 type DbTuple = PersonMentionViewTuple;
328 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
332 person_mention: a.0.to_owned(),
333 comment: a.1.to_owned(),
334 creator: a.2.to_owned(),
335 post: a.3.to_owned(),
336 community: a.4.to_owned(),
337 recipient: a.5.to_owned(),
338 counts: a.6.to_owned(),
339 creator_banned_from_community: a.7.is_some(),
340 subscribed: a.8.is_some(),
341 saved: a.9.is_some(),
342 creator_blocked: a.10.is_some(),
345 .collect::<Vec<Self>>()