]> Untitled Git - lemmy.git/blob - crates/db_views_actor/src/comment_reply_view.rs
Get rid of Safe Views, use serde_skip (#2767)
[lemmy.git] / crates / db_views_actor / src / comment_reply_view.rs
1 use crate::structs::CommentReplyView;
2 use diesel::{
3   dsl::now,
4   result::Error,
5   BoolExpressionMethods,
6   ExpressionMethods,
7   JoinOnDsl,
8   NullableExpressionMethods,
9   QueryDsl,
10 };
11 use diesel_async::RunQueryDsl;
12 use lemmy_db_schema::{
13   aggregates::structs::CommentAggregates,
14   newtypes::{CommentReplyId, PersonId},
15   schema::{
16     comment,
17     comment_aggregates,
18     comment_like,
19     comment_reply,
20     comment_saved,
21     community,
22     community_follower,
23     community_person_ban,
24     person,
25     person_block,
26     post,
27   },
28   source::{
29     comment::{Comment, CommentSaved},
30     comment_reply::CommentReply,
31     community::{Community, CommunityFollower, CommunityPersonBan},
32     person::Person,
33     person_block::PersonBlock,
34     post::Post,
35   },
36   traits::JoinView,
37   utils::{functions::hot_rank, get_conn, limit_and_offset, DbPool},
38   CommentSortType,
39 };
40 use typed_builder::TypedBuilder;
41
42 type CommentReplyViewTuple = (
43   CommentReply,
44   Comment,
45   Person,
46   Post,
47   Community,
48   Person,
49   CommentAggregates,
50   Option<CommunityPersonBan>,
51   Option<CommunityFollower>,
52   Option<CommentSaved>,
53   Option<PersonBlock>,
54   Option<i16>,
55 );
56
57 impl CommentReplyView {
58   pub async fn read(
59     pool: &DbPool,
60     comment_reply_id: CommentReplyId,
61     my_person_id: Option<PersonId>,
62   ) -> Result<Self, Error> {
63     let conn = &mut get_conn(pool).await?;
64     let person_alias_1 = diesel::alias!(person as person1);
65
66     // The left join below will return None in this case
67     let person_id_join = my_person_id.unwrap_or(PersonId(-1));
68
69     let (
70       comment_reply,
71       comment,
72       creator,
73       post,
74       community,
75       recipient,
76       counts,
77       creator_banned_from_community,
78       follower,
79       saved,
80       creator_blocked,
81       my_vote,
82     ) = comment_reply::table
83       .find(comment_reply_id)
84       .inner_join(comment::table)
85       .inner_join(person::table.on(comment::creator_id.eq(person::id)))
86       .inner_join(post::table.on(comment::post_id.eq(post::id)))
87       .inner_join(community::table.on(post::community_id.eq(community::id)))
88       .inner_join(person_alias_1)
89       .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
90       .left_join(
91         community_person_ban::table.on(
92           community::id
93             .eq(community_person_ban::community_id)
94             .and(community_person_ban::person_id.eq(comment::creator_id))
95             .and(
96               community_person_ban::expires
97                 .is_null()
98                 .or(community_person_ban::expires.gt(now)),
99             ),
100         ),
101       )
102       .left_join(
103         community_follower::table.on(
104           post::community_id
105             .eq(community_follower::community_id)
106             .and(community_follower::person_id.eq(person_id_join)),
107         ),
108       )
109       .left_join(
110         comment_saved::table.on(
111           comment::id
112             .eq(comment_saved::comment_id)
113             .and(comment_saved::person_id.eq(person_id_join)),
114         ),
115       )
116       .left_join(
117         person_block::table.on(
118           comment::creator_id
119             .eq(person_block::target_id)
120             .and(person_block::person_id.eq(person_id_join)),
121         ),
122       )
123       .left_join(
124         comment_like::table.on(
125           comment::id
126             .eq(comment_like::comment_id)
127             .and(comment_like::person_id.eq(person_id_join)),
128         ),
129       )
130       .select((
131         comment_reply::all_columns,
132         comment::all_columns,
133         person::all_columns,
134         post::all_columns,
135         community::all_columns,
136         person_alias_1.fields(person::all_columns),
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(),
143       ))
144       .first::<CommentReplyViewTuple>(conn)
145       .await?;
146
147     Ok(CommentReplyView {
148       comment_reply,
149       comment,
150       creator,
151       post,
152       community,
153       recipient,
154       counts,
155       creator_banned_from_community: creator_banned_from_community.is_some(),
156       subscribed: CommunityFollower::to_subscribed_type(&follower),
157       saved: saved.is_some(),
158       creator_blocked: creator_blocked.is_some(),
159       my_vote,
160     })
161   }
162
163   /// Gets the number of unread replies
164   pub async fn get_unread_replies(pool: &DbPool, my_person_id: PersonId) -> Result<i64, Error> {
165     use diesel::dsl::count;
166
167     let conn = &mut get_conn(pool).await?;
168
169     comment_reply::table
170       .inner_join(comment::table)
171       .filter(comment_reply::recipient_id.eq(my_person_id))
172       .filter(comment_reply::read.eq(false))
173       .filter(comment::deleted.eq(false))
174       .filter(comment::removed.eq(false))
175       .select(count(comment_reply::id))
176       .first::<i64>(conn)
177       .await
178   }
179 }
180
181 #[derive(TypedBuilder)]
182 #[builder(field_defaults(default))]
183 pub struct CommentReplyQuery<'a> {
184   #[builder(!default)]
185   pool: &'a DbPool,
186   my_person_id: Option<PersonId>,
187   recipient_id: Option<PersonId>,
188   sort: Option<CommentSortType>,
189   unread_only: Option<bool>,
190   show_bot_accounts: Option<bool>,
191   page: Option<i64>,
192   limit: Option<i64>,
193 }
194
195 impl<'a> CommentReplyQuery<'a> {
196   pub async fn list(self) -> Result<Vec<CommentReplyView>, Error> {
197     let conn = &mut get_conn(self.pool).await?;
198
199     let person_alias_1 = diesel::alias!(person as person1);
200
201     // The left join below will return None in this case
202     let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
203
204     let mut query = comment_reply::table
205       .inner_join(comment::table)
206       .inner_join(person::table.on(comment::creator_id.eq(person::id)))
207       .inner_join(post::table.on(comment::post_id.eq(post::id)))
208       .inner_join(community::table.on(post::community_id.eq(community::id)))
209       .inner_join(person_alias_1)
210       .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
211       .left_join(
212         community_person_ban::table.on(
213           community::id
214             .eq(community_person_ban::community_id)
215             .and(community_person_ban::person_id.eq(comment::creator_id))
216             .and(
217               community_person_ban::expires
218                 .is_null()
219                 .or(community_person_ban::expires.gt(now)),
220             ),
221         ),
222       )
223       .left_join(
224         community_follower::table.on(
225           post::community_id
226             .eq(community_follower::community_id)
227             .and(community_follower::person_id.eq(person_id_join)),
228         ),
229       )
230       .left_join(
231         comment_saved::table.on(
232           comment::id
233             .eq(comment_saved::comment_id)
234             .and(comment_saved::person_id.eq(person_id_join)),
235         ),
236       )
237       .left_join(
238         person_block::table.on(
239           comment::creator_id
240             .eq(person_block::target_id)
241             .and(person_block::person_id.eq(person_id_join)),
242         ),
243       )
244       .left_join(
245         comment_like::table.on(
246           comment::id
247             .eq(comment_like::comment_id)
248             .and(comment_like::person_id.eq(person_id_join)),
249         ),
250       )
251       .select((
252         comment_reply::all_columns,
253         comment::all_columns,
254         person::all_columns,
255         post::all_columns,
256         community::all_columns,
257         person_alias_1.fields(person::all_columns),
258         comment_aggregates::all_columns,
259         community_person_ban::all_columns.nullable(),
260         community_follower::all_columns.nullable(),
261         comment_saved::all_columns.nullable(),
262         person_block::all_columns.nullable(),
263         comment_like::score.nullable(),
264       ))
265       .into_boxed();
266
267     if let Some(recipient_id) = self.recipient_id {
268       query = query.filter(comment_reply::recipient_id.eq(recipient_id));
269     }
270
271     if self.unread_only.unwrap_or(false) {
272       query = query.filter(comment_reply::read.eq(false));
273     }
274
275     if !self.show_bot_accounts.unwrap_or(true) {
276       query = query.filter(person::bot_account.eq(false));
277     };
278
279     query = match self.sort.unwrap_or(CommentSortType::Hot) {
280       CommentSortType::Hot => query
281         .then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
282         .then_order_by(comment_aggregates::published.desc()),
283       CommentSortType::New => query.then_order_by(comment::published.desc()),
284       CommentSortType::Old => query.then_order_by(comment::published.asc()),
285       CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
286     };
287
288     let (limit, offset) = limit_and_offset(self.page, self.limit)?;
289
290     let res = query
291       .limit(limit)
292       .offset(offset)
293       .load::<CommentReplyViewTuple>(conn)
294       .await?;
295
296     Ok(res.into_iter().map(CommentReplyView::from_tuple).collect())
297   }
298 }
299
300 impl JoinView for CommentReplyView {
301   type JoinTuple = CommentReplyViewTuple;
302   fn from_tuple(a: Self::JoinTuple) -> Self {
303     Self {
304       comment_reply: a.0,
305       comment: a.1,
306       creator: a.2,
307       post: a.3,
308       community: a.4,
309       recipient: a.5,
310       counts: a.6,
311       creator_banned_from_community: a.7.is_some(),
312       subscribed: CommunityFollower::to_subscribed_type(&a.8),
313       saved: a.9.is_some(),
314       creator_blocked: a.10.is_some(),
315       my_vote: a.11,
316     }
317   }
318 }