1 use crate::structs::PersonMentionView;
2 use diesel::{dsl::*, result::Error, *};
4 aggregates::structs::CommentAggregates,
5 newtypes::{PersonId, PersonMentionId},
20 comment::{Comment, CommentSaved},
21 community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe},
22 person::{Person, PersonSafe},
23 person_block::PersonBlock,
24 person_mention::PersonMention,
27 traits::{ToSafe, ViewToVec},
28 utils::{functions::hot_rank, limit_and_offset},
31 use typed_builder::TypedBuilder;
33 type PersonMentionViewTuple = (
41 Option<CommunityPersonBan>,
42 Option<CommunityFollower>,
48 impl PersonMentionView {
50 conn: &mut PgConnection,
51 person_mention_id: PersonMentionId,
52 my_person_id: Option<PersonId>,
53 ) -> Result<Self, Error> {
54 let person_alias_1 = diesel::alias!(person as person1);
56 // The left join below will return None in this case
57 let person_id_join = my_person_id.unwrap_or(PersonId(-1));
67 creator_banned_from_community,
72 ) = person_mention::table
73 .find(person_mention_id)
74 .inner_join(comment::table)
75 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
76 .inner_join(post::table.on(comment::post_id.eq(post::id)))
77 .inner_join(community::table.on(post::community_id.eq(community::id)))
78 .inner_join(person_alias_1)
79 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
81 community_person_ban::table.on(
83 .eq(community_person_ban::community_id)
84 .and(community_person_ban::person_id.eq(comment::creator_id))
86 community_person_ban::expires
88 .or(community_person_ban::expires.gt(now)),
93 community_follower::table.on(
95 .eq(community_follower::community_id)
96 .and(community_follower::person_id.eq(person_id_join)),
100 comment_saved::table.on(
102 .eq(comment_saved::comment_id)
103 .and(comment_saved::person_id.eq(person_id_join)),
107 person_block::table.on(
109 .eq(person_block::target_id)
110 .and(person_block::person_id.eq(person_id_join)),
114 comment_like::table.on(
116 .eq(comment_like::comment_id)
117 .and(comment_like::person_id.eq(person_id_join)),
121 person_mention::all_columns,
122 comment::all_columns,
123 Person::safe_columns_tuple(),
125 Community::safe_columns_tuple(),
126 person_alias_1.fields(Person::safe_columns_tuple()),
127 comment_aggregates::all_columns,
128 community_person_ban::all_columns.nullable(),
129 community_follower::all_columns.nullable(),
130 comment_saved::all_columns.nullable(),
131 person_block::all_columns.nullable(),
132 comment_like::score.nullable(),
134 .first::<PersonMentionViewTuple>(conn)?;
136 Ok(PersonMentionView {
144 creator_banned_from_community: creator_banned_from_community.is_some(),
145 subscribed: CommunityFollower::to_subscribed_type(&follower),
146 saved: saved.is_some(),
147 creator_blocked: creator_blocked.is_some(),
152 /// Gets the number of unread mentions
153 pub fn get_unread_mentions(
154 conn: &mut PgConnection,
155 my_person_id: PersonId,
156 ) -> Result<i64, Error> {
159 person_mention::table
160 .inner_join(comment::table)
161 .filter(person_mention::recipient_id.eq(my_person_id))
162 .filter(person_mention::read.eq(false))
163 .filter(comment::deleted.eq(false))
164 .filter(comment::removed.eq(false))
165 .select(count(person_mention::id))
170 #[derive(TypedBuilder)]
171 #[builder(field_defaults(default))]
172 pub struct PersonMentionQuery<'a> {
174 conn: &'a mut PgConnection,
175 my_person_id: Option<PersonId>,
176 recipient_id: Option<PersonId>,
177 sort: Option<CommentSortType>,
178 unread_only: Option<bool>,
179 show_bot_accounts: Option<bool>,
184 impl<'a> PersonMentionQuery<'a> {
185 pub fn list(self) -> Result<Vec<PersonMentionView>, Error> {
188 let person_alias_1 = diesel::alias!(person as person1);
190 // The left join below will return None in this case
191 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
193 let mut query = person_mention::table
194 .inner_join(comment::table)
195 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
196 .inner_join(post::table.on(comment::post_id.eq(post::id)))
197 .inner_join(community::table.on(post::community_id.eq(community::id)))
198 .inner_join(person_alias_1)
199 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
201 community_person_ban::table.on(
203 .eq(community_person_ban::community_id)
204 .and(community_person_ban::person_id.eq(comment::creator_id))
206 community_person_ban::expires
208 .or(community_person_ban::expires.gt(now)),
213 community_follower::table.on(
215 .eq(community_follower::community_id)
216 .and(community_follower::person_id.eq(person_id_join)),
220 comment_saved::table.on(
222 .eq(comment_saved::comment_id)
223 .and(comment_saved::person_id.eq(person_id_join)),
227 person_block::table.on(
229 .eq(person_block::target_id)
230 .and(person_block::person_id.eq(person_id_join)),
234 comment_like::table.on(
236 .eq(comment_like::comment_id)
237 .and(comment_like::person_id.eq(person_id_join)),
241 person_mention::all_columns,
242 comment::all_columns,
243 Person::safe_columns_tuple(),
245 Community::safe_columns_tuple(),
246 person_alias_1.fields(Person::safe_columns_tuple()),
247 comment_aggregates::all_columns,
248 community_person_ban::all_columns.nullable(),
249 community_follower::all_columns.nullable(),
250 comment_saved::all_columns.nullable(),
251 person_block::all_columns.nullable(),
252 comment_like::score.nullable(),
256 if let Some(recipient_id) = self.recipient_id {
257 query = query.filter(person_mention::recipient_id.eq(recipient_id));
260 if self.unread_only.unwrap_or(false) {
261 query = query.filter(person_mention::read.eq(false));
264 if !self.show_bot_accounts.unwrap_or(true) {
265 query = query.filter(person::bot_account.eq(false));
268 query = match self.sort.unwrap_or(CommentSortType::Hot) {
269 CommentSortType::Hot => query
270 .then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
271 .then_order_by(comment_aggregates::published.desc()),
272 CommentSortType::New => query.then_order_by(comment::published.desc()),
273 CommentSortType::Old => query.then_order_by(comment::published.asc()),
274 CommentSortType::Top => query.order_by(comment_aggregates::score.desc()),
277 let (limit, offset) = limit_and_offset(self.page, self.limit)?;
282 .load::<PersonMentionViewTuple>(self.conn)?;
284 Ok(PersonMentionView::from_tuple_to_vec(res))
288 impl ViewToVec for PersonMentionView {
289 type DbTuple = PersonMentionViewTuple;
290 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<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(),
307 .collect::<Vec<Self>>()