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},
40 type CommentReplyViewTuple = (
48 Option<CommunityPersonBan>,
49 Option<CommunityFollower>,
55 impl CommentReplyView {
57 pool: &mut DbPool<'_>,
58 comment_reply_id: CommentReplyId,
59 my_person_id: Option<PersonId>,
60 ) -> Result<Self, Error> {
61 let conn = &mut get_conn(pool).await?;
62 let person_alias_1 = diesel::alias!(person as person1);
64 // The left join below will return None in this case
65 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
75 creator_banned_from_community,
80 ) = comment_reply::table
81 .find(comment_reply_id)
82 .inner_join(comment::table)
83 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
84 .inner_join(post::table.on(comment::post_id.eq(post::id)))
85 .inner_join(community::table.on(post::community_id.eq(community::id)))
86 .inner_join(person_alias_1)
87 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
89 community_person_ban::table.on(
91 .eq(community_person_ban::community_id)
92 .and(community_person_ban::person_id.eq(comment::creator_id)),
96 community_follower::table.on(
98 .eq(community_follower::community_id)
99 .and(community_follower::person_id.eq(person_id_join)),
103 comment_saved::table.on(
105 .eq(comment_saved::comment_id)
106 .and(comment_saved::person_id.eq(person_id_join)),
110 person_block::table.on(
112 .eq(person_block::target_id)
113 .and(person_block::person_id.eq(person_id_join)),
117 comment_like::table.on(
119 .eq(comment_like::comment_id)
120 .and(comment_like::person_id.eq(person_id_join)),
124 comment_reply::all_columns,
125 comment::all_columns,
128 community::all_columns,
129 person_alias_1.fields(person::all_columns),
130 comment_aggregates::all_columns,
131 community_person_ban::all_columns.nullable(),
132 community_follower::all_columns.nullable(),
133 comment_saved::all_columns.nullable(),
134 person_block::all_columns.nullable(),
135 comment_like::score.nullable(),
137 .first::<CommentReplyViewTuple>(conn)
140 Ok(CommentReplyView {
148 creator_banned_from_community: creator_banned_from_community.is_some(),
149 subscribed: CommunityFollower::to_subscribed_type(&follower),
150 saved: saved.is_some(),
151 creator_blocked: creator_blocked.is_some(),
156 /// Gets the number of unread replies
157 pub async fn get_unread_replies(
158 pool: &mut DbPool<'_>,
159 my_person_id: PersonId,
160 ) -> Result<i64, Error> {
161 use diesel::dsl::count;
163 let conn = &mut get_conn(pool).await?;
166 .inner_join(comment::table)
167 .filter(comment_reply::recipient_id.eq(my_person_id))
168 .filter(comment_reply::read.eq(false))
169 .filter(comment::deleted.eq(false))
170 .filter(comment::removed.eq(false))
171 .select(count(comment_reply::id))
178 pub struct CommentReplyQuery {
179 pub my_person_id: Option<PersonId>,
180 pub recipient_id: Option<PersonId>,
181 pub sort: Option<CommentSortType>,
182 pub unread_only: Option<bool>,
183 pub show_bot_accounts: Option<bool>,
184 pub page: Option<i64>,
185 pub limit: Option<i64>,
188 impl CommentReplyQuery {
189 pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommentReplyView>, Error> {
190 let conn = &mut get_conn(pool).await?;
192 let person_alias_1 = diesel::alias!(person as person1);
194 // The left join below will return None in this case
195 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
197 let mut query = comment_reply::table
198 .inner_join(comment::table)
199 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
200 .inner_join(post::table.on(comment::post_id.eq(post::id)))
201 .inner_join(community::table.on(post::community_id.eq(community::id)))
202 .inner_join(person_alias_1)
203 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
205 community_person_ban::table.on(
207 .eq(community_person_ban::community_id)
208 .and(community_person_ban::person_id.eq(comment::creator_id)),
212 community_follower::table.on(
214 .eq(community_follower::community_id)
215 .and(community_follower::person_id.eq(person_id_join)),
219 comment_saved::table.on(
221 .eq(comment_saved::comment_id)
222 .and(comment_saved::person_id.eq(person_id_join)),
226 person_block::table.on(
228 .eq(person_block::target_id)
229 .and(person_block::person_id.eq(person_id_join)),
233 comment_like::table.on(
235 .eq(comment_like::comment_id)
236 .and(comment_like::person_id.eq(person_id_join)),
240 comment_reply::all_columns,
241 comment::all_columns,
244 community::all_columns,
245 person_alias_1.fields(person::all_columns),
246 comment_aggregates::all_columns,
247 community_person_ban::all_columns.nullable(),
248 community_follower::all_columns.nullable(),
249 comment_saved::all_columns.nullable(),
250 person_block::all_columns.nullable(),
251 comment_like::score.nullable(),
255 if let Some(recipient_id) = self.recipient_id {
256 query = query.filter(comment_reply::recipient_id.eq(recipient_id));
259 if self.unread_only.unwrap_or(false) {
260 query = query.filter(comment_reply::read.eq(false));
263 if !self.show_bot_accounts.unwrap_or(true) {
264 query = query.filter(person::bot_account.eq(false));
267 query = match self.sort.unwrap_or(CommentSortType::New) {
268 CommentSortType::Hot => query.then_order_by(comment_aggregates::hot_rank.desc()),
269 CommentSortType::New => query.then_order_by(comment_reply::published.desc()),
270 CommentSortType::Old => query.then_order_by(comment_reply::published.asc()),
271 CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
274 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
279 .load::<CommentReplyViewTuple>(conn)
282 Ok(res.into_iter().map(CommentReplyView::from_tuple).collect())
286 impl JoinView for CommentReplyView {
287 type JoinTuple = CommentReplyViewTuple;
288 fn from_tuple(a: Self::JoinTuple) -> Self {
297 creator_banned_from_community: a.7.is_some(),
298 subscribed: CommunityFollower::to_subscribed_type(&a.8),
299 saved: a.9.is_some(),
300 creator_blocked: a.10.is_some(),