1 use crate::structs::CommentReplyView;
7 NullableExpressionMethods,
10 use diesel_async::RunQueryDsl;
11 use lemmy_db_schema::{
12 aggregates::structs::CommentAggregates,
13 newtypes::{CommentReplyId, PersonId},
28 comment::{Comment, CommentSaved},
29 comment_reply::CommentReply,
30 community::{Community, CommunityFollower, CommunityPersonBan},
32 person_block::PersonBlock,
36 utils::{get_conn, limit_and_offset, DbPool},
39 use typed_builder::TypedBuilder;
41 type CommentReplyViewTuple = (
49 Option<CommunityPersonBan>,
50 Option<CommunityFollower>,
56 impl CommentReplyView {
58 pool: &mut DbPool<'_>,
59 comment_reply_id: CommentReplyId,
60 my_person_id: Option<PersonId>,
61 ) -> Result<Self, Error> {
62 let conn = &mut get_conn(pool).await?;
63 let person_alias_1 = diesel::alias!(person as person1);
65 // The left join below will return None in this case
66 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
76 creator_banned_from_community,
81 ) = comment_reply::table
82 .find(comment_reply_id)
83 .inner_join(comment::table)
84 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
85 .inner_join(post::table.on(comment::post_id.eq(post::id)))
86 .inner_join(community::table.on(post::community_id.eq(community::id)))
87 .inner_join(person_alias_1)
88 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
90 community_person_ban::table.on(
92 .eq(community_person_ban::community_id)
93 .and(community_person_ban::person_id.eq(comment::creator_id)),
97 community_follower::table.on(
99 .eq(community_follower::community_id)
100 .and(community_follower::person_id.eq(person_id_join)),
104 comment_saved::table.on(
106 .eq(comment_saved::comment_id)
107 .and(comment_saved::person_id.eq(person_id_join)),
111 person_block::table.on(
113 .eq(person_block::target_id)
114 .and(person_block::person_id.eq(person_id_join)),
118 comment_like::table.on(
120 .eq(comment_like::comment_id)
121 .and(comment_like::person_id.eq(person_id_join)),
125 comment_reply::all_columns,
126 comment::all_columns,
129 community::all_columns,
130 person_alias_1.fields(person::all_columns),
131 comment_aggregates::all_columns,
132 community_person_ban::all_columns.nullable(),
133 community_follower::all_columns.nullable(),
134 comment_saved::all_columns.nullable(),
135 person_block::all_columns.nullable(),
136 comment_like::score.nullable(),
138 .first::<CommentReplyViewTuple>(conn)
141 Ok(CommentReplyView {
149 creator_banned_from_community: creator_banned_from_community.is_some(),
150 subscribed: CommunityFollower::to_subscribed_type(&follower),
151 saved: saved.is_some(),
152 creator_blocked: creator_blocked.is_some(),
157 /// Gets the number of unread replies
158 pub async fn get_unread_replies(
159 pool: &mut DbPool<'_>,
160 my_person_id: PersonId,
161 ) -> Result<i64, Error> {
162 use diesel::dsl::count;
164 let conn = &mut get_conn(pool).await?;
167 .inner_join(comment::table)
168 .filter(comment_reply::recipient_id.eq(my_person_id))
169 .filter(comment_reply::read.eq(false))
170 .filter(comment::deleted.eq(false))
171 .filter(comment::removed.eq(false))
172 .select(count(comment_reply::id))
178 #[derive(TypedBuilder)]
179 #[builder(field_defaults(default))]
180 pub struct CommentReplyQuery<'a, 'b: 'a> {
182 pool: &'a mut DbPool<'b>,
183 my_person_id: Option<PersonId>,
184 recipient_id: Option<PersonId>,
185 sort: Option<CommentSortType>,
186 unread_only: Option<bool>,
187 show_bot_accounts: Option<bool>,
192 impl<'a, 'b: 'a> CommentReplyQuery<'a, 'b> {
193 pub async fn list(self) -> Result<Vec<CommentReplyView>, Error> {
194 let conn = &mut get_conn(self.pool).await?;
196 let person_alias_1 = diesel::alias!(person as person1);
198 // The left join below will return None in this case
199 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
201 let mut query = comment_reply::table
202 .inner_join(comment::table)
203 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
204 .inner_join(post::table.on(comment::post_id.eq(post::id)))
205 .inner_join(community::table.on(post::community_id.eq(community::id)))
206 .inner_join(person_alias_1)
207 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
209 community_person_ban::table.on(
211 .eq(community_person_ban::community_id)
212 .and(community_person_ban::person_id.eq(comment::creator_id)),
216 community_follower::table.on(
218 .eq(community_follower::community_id)
219 .and(community_follower::person_id.eq(person_id_join)),
223 comment_saved::table.on(
225 .eq(comment_saved::comment_id)
226 .and(comment_saved::person_id.eq(person_id_join)),
230 person_block::table.on(
232 .eq(person_block::target_id)
233 .and(person_block::person_id.eq(person_id_join)),
237 comment_like::table.on(
239 .eq(comment_like::comment_id)
240 .and(comment_like::person_id.eq(person_id_join)),
244 comment_reply::all_columns,
245 comment::all_columns,
248 community::all_columns,
249 person_alias_1.fields(person::all_columns),
250 comment_aggregates::all_columns,
251 community_person_ban::all_columns.nullable(),
252 community_follower::all_columns.nullable(),
253 comment_saved::all_columns.nullable(),
254 person_block::all_columns.nullable(),
255 comment_like::score.nullable(),
259 if let Some(recipient_id) = self.recipient_id {
260 query = query.filter(comment_reply::recipient_id.eq(recipient_id));
263 if self.unread_only.unwrap_or(false) {
264 query = query.filter(comment_reply::read.eq(false));
267 if !self.show_bot_accounts.unwrap_or(true) {
268 query = query.filter(person::bot_account.eq(false));
271 query = match self.sort.unwrap_or(CommentSortType::New) {
272 CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()),
273 CommentSortType::New => query.then_order_by(comment_reply::published.desc()),
274 CommentSortType::Old => query.then_order_by(comment_reply::published.asc()),
275 CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
278 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
283 .load::<CommentReplyViewTuple>(conn)
286 Ok(res.into_iter().map(CommentReplyView::from_tuple).collect())
290 impl JoinView for CommentReplyView {
291 type JoinTuple = CommentReplyViewTuple;
292 fn from_tuple(a: Self::JoinTuple) -> Self {
301 creator_banned_from_community: a.7.is_some(),
302 subscribed: CommunityFollower::to_subscribed_type(&a.8),
303 saved: a.9.is_some(),
304 creator_blocked: a.10.is_some(),