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 .filter(person_mention::recipient_id.eq(my_person_id))
161 .filter(person_mention::read.eq(false))
162 .select(count(person_mention::id))
167 #[derive(TypedBuilder)]
168 #[builder(field_defaults(default))]
169 pub struct PersonMentionQuery<'a> {
171 conn: &'a mut PgConnection,
172 my_person_id: Option<PersonId>,
173 recipient_id: Option<PersonId>,
174 sort: Option<CommentSortType>,
175 unread_only: Option<bool>,
176 show_bot_accounts: Option<bool>,
181 impl<'a> PersonMentionQuery<'a> {
182 pub fn list(self) -> Result<Vec<PersonMentionView>, Error> {
185 let person_alias_1 = diesel::alias!(person as person1);
187 // The left join below will return None in this case
188 let person_id_join = self.my_person_id.unwrap_or(PersonId(-1));
190 let mut query = person_mention::table
191 .inner_join(comment::table)
192 .inner_join(person::table.on(comment::creator_id.eq(person::id)))
193 .inner_join(post::table.on(comment::post_id.eq(post::id)))
194 .inner_join(community::table.on(post::community_id.eq(community::id)))
195 .inner_join(person_alias_1)
196 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
198 community_person_ban::table.on(
200 .eq(community_person_ban::community_id)
201 .and(community_person_ban::person_id.eq(comment::creator_id))
203 community_person_ban::expires
205 .or(community_person_ban::expires.gt(now)),
210 community_follower::table.on(
212 .eq(community_follower::community_id)
213 .and(community_follower::person_id.eq(person_id_join)),
217 comment_saved::table.on(
219 .eq(comment_saved::comment_id)
220 .and(comment_saved::person_id.eq(person_id_join)),
224 person_block::table.on(
226 .eq(person_block::target_id)
227 .and(person_block::person_id.eq(person_id_join)),
231 comment_like::table.on(
233 .eq(comment_like::comment_id)
234 .and(comment_like::person_id.eq(person_id_join)),
238 person_mention::all_columns,
239 comment::all_columns,
240 Person::safe_columns_tuple(),
242 Community::safe_columns_tuple(),
243 person_alias_1.fields(Person::safe_columns_tuple()),
244 comment_aggregates::all_columns,
245 community_person_ban::all_columns.nullable(),
246 community_follower::all_columns.nullable(),
247 comment_saved::all_columns.nullable(),
248 person_block::all_columns.nullable(),
249 comment_like::score.nullable(),
253 if let Some(recipient_id) = self.recipient_id {
254 query = query.filter(person_mention::recipient_id.eq(recipient_id));
257 if self.unread_only.unwrap_or(false) {
258 query = query.filter(person_mention::read.eq(false));
261 if !self.show_bot_accounts.unwrap_or(true) {
262 query = query.filter(person::bot_account.eq(false));
265 query = match self.sort.unwrap_or(CommentSortType::Hot) {
266 CommentSortType::Hot => query
267 .then_order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
268 .then_order_by(comment_aggregates::published.desc()),
269 CommentSortType::New => query.then_order_by(comment::published.desc()),
270 CommentSortType::Old => query.then_order_by(comment::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::<PersonMentionViewTuple>(self.conn)?;
281 Ok(PersonMentionView::from_tuple_to_vec(res))
285 impl ViewToVec for PersonMentionView {
286 type DbTuple = PersonMentionViewTuple;
287 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
298 creator_banned_from_community: a.7.is_some(),
299 subscribed: CommunityFollower::to_subscribed_type(&a.8),
300 saved: a.9.is_some(),
301 creator_blocked: a.10.is_some(),
304 .collect::<Vec<Self>>()