1 use crate::structs::PersonMentionView;
2 use diesel::{dsl::*, result::Error, *};
4 aggregates::structs::CommentAggregates,
5 newtypes::{PersonId, PersonMentionId},
21 comment::{Comment, CommentSaved},
22 community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
23 person::{Person, PersonAlias1, PersonSafe, PersonSafeAlias1},
24 person_block::PersonBlock,
25 person_mention::PersonMention,
28 traits::{ToSafe, ViewToVec},
29 utils::{functions::hot_rank, limit_and_offset},
32 use typed_builder::TypedBuilder;
34 type PersonMentionViewTuple = (
42 Option<CommunityPersonBan>,
43 Option<CommunityFollower>,
49 impl PersonMentionView {
52 person_mention_id: PersonMentionId,
53 my_person_id: Option<PersonId>,
54 ) -> Result<Self, Error> {
55 // The left join below will return None in this case
56 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
66 creator_banned_from_community,
71 ) = person_mention::table
72 .find(person_mention_id)
73 .inner_join(comment::table)
74 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
75 .inner_join(post::table.on(comment::post_id.eq(post::id)))
76 .inner_join(community::table.on(post::community_id.eq(community::id)))
77 .inner_join(person_alias_1::table)
78 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
80 community_person_ban::table.on(
82 .eq(community_person_ban::community_id)
83 .and(community_person_ban::person_id.eq(comment::creator_id))
85 community_person_ban::expires
87 .or(community_person_ban::expires.gt(now)),
92 community_follower::table.on(
94 .eq(community_follower::community_id)
95 .and(community_follower::person_id.eq(person_id_join)),
99 comment_saved::table.on(
101 .eq(comment_saved::comment_id)
102 .and(comment_saved::person_id.eq(person_id_join)),
106 person_block::table.on(
108 .eq(person_block::target_id)
109 .and(person_block::person_id.eq(person_id_join)),
113 comment_like::table.on(
115 .eq(comment_like::comment_id)
116 .and(comment_like::person_id.eq(person_id_join)),
120 person_mention::all_columns,
121 comment::all_columns,
122 Person::safe_columns_tuple(),
124 Community::safe_columns_tuple(),
125 PersonAlias1::safe_columns_tuple(),
126 comment_aggregates::all_columns,
127 community_person_ban::all_columns.nullable(),
128 community_follower::all_columns.nullable(),
129 comment_saved::all_columns.nullable(),
130 person_block::all_columns.nullable(),
131 comment_like::score.nullable(),
133 .first::<PersonMentionViewTuple>(conn)?;
135 Ok(PersonMentionView {
143 creator_banned_from_community: creator_banned_from_community.is_some(),
144 subscribed: CommunityFollower::to_subscribed_type(&follower),
145 saved: saved.is_some(),
146 creator_blocked: creator_blocked.is_some(),
151 /// Gets the number of unread mentions
152 pub fn get_unread_mentions(conn: &PgConnection, my_person_id: PersonId) -> Result<i64, Error> {
155 person_mention::table
156 .filter(person_mention::recipient_id.eq(my_person_id))
157 .filter(person_mention::read.eq(false))
158 .select(count(person_mention::id))
163 #[derive(TypedBuilder)]
164 #[builder(field_defaults(default))]
165 pub struct PersonMentionQuery<'a> {
167 conn: &'a PgConnection,
168 my_person_id: Option<PersonId>,
169 recipient_id: Option<PersonId>,
170 sort: Option<CommentSortType>,
171 unread_only: Option<bool>,
172 show_bot_accounts: Option<bool>,
177 impl<'a> PersonMentionQuery<'a> {
178 pub fn list(self) -> Result<Vec<PersonMentionView>, Error> {
181 // The left join below will return None in this case
182 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
184 let mut query = person_mention::table
185 .inner_join(comment::table)
186 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
187 .inner_join(post::table.on(comment::post_id.eq(post::id)))
188 .inner_join(community::table.on(post::community_id.eq(community::id)))
189 .inner_join(person_alias_1::table)
190 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
192 community_person_ban::table.on(
194 .eq(community_person_ban::community_id)
195 .and(community_person_ban::person_id.eq(comment::creator_id))
197 community_person_ban::expires
199 .or(community_person_ban::expires.gt(now)),
204 community_follower::table.on(
206 .eq(community_follower::community_id)
207 .and(community_follower::person_id.eq(person_id_join)),
211 comment_saved::table.on(
213 .eq(comment_saved::comment_id)
214 .and(comment_saved::person_id.eq(person_id_join)),
218 person_block::table.on(
220 .eq(person_block::target_id)
221 .and(person_block::person_id.eq(person_id_join)),
225 comment_like::table.on(
227 .eq(comment_like::comment_id)
228 .and(comment_like::person_id.eq(person_id_join)),
232 person_mention::all_columns,
233 comment::all_columns,
234 Person::safe_columns_tuple(),
236 Community::safe_columns_tuple(),
237 PersonAlias1::safe_columns_tuple(),
238 comment_aggregates::all_columns,
239 community_person_ban::all_columns.nullable(),
240 community_follower::all_columns.nullable(),
241 comment_saved::all_columns.nullable(),
242 person_block::all_columns.nullable(),
243 comment_like::score.nullable(),
247 if let Some(recipient_id) = self.recipient_id {
248 query = query.filter(person_mention::recipient_id.eq(recipient_id));
251 if self.unread_only.unwrap_or(false) {
252 query = query.filter(person_mention::read.eq(false));
255 if !self.show_bot_accounts.unwrap_or(true) {
256 query = query.filter(person::bot_account.eq(false));
259 query = match self.sort.unwrap_or(CommentSortType::Hot) {
260 CommentSortType::Hot => query
261 .then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
262 .then_order_by(comment_aggregates::published.desc()),
263 CommentSortType::New => query.then_order_by(comment::published.desc()),
264 CommentSortType::Old => query.then_order_by(comment::published.asc()),
265 CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
268 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
273 .load::<PersonMentionViewTuple>(self.conn)?;
275 Ok(PersonMentionView::from_tuple_to_vec(res))
279 impl ViewToVec for PersonMentionView {
280 type DbTuple = PersonMentionViewTuple;
281 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
292 creator_banned_from_community: a.7.is_some(),
293 subscribed: CommunityFollower::to_subscribed_type(&a.8),
294 saved: a.9.is_some(),
295 creator_blocked: a.10.is_some(),
298 .collect::<Vec<Self>>()