1 use diesel::{result::Error, *};
2 use lemmy_db_queries::{
3 aggregates::comment_aggregates::CommentAggregates,
11 use lemmy_db_schema::{
26 comment::{Comment, CommentSaved},
27 community::{Community, CommunityFollower, CommunitySafe, CommunityUserBan},
29 user::{UserAlias1, UserSafe, UserSafeAlias1, User_},
30 user_mention::UserMention,
35 #[derive(Debug, PartialEq, Serialize, Clone)]
36 pub struct UserMentionView {
37 pub user_mention: UserMention,
39 pub creator: UserSafe,
41 pub community: CommunitySafe,
42 pub recipient: UserSafeAlias1,
43 pub counts: CommentAggregates,
44 pub creator_banned_from_community: bool, // Left Join to CommunityUserBan
45 pub subscribed: bool, // Left join to CommunityFollower
46 pub saved: bool, // Left join to CommentSaved
47 pub my_vote: Option<i16>, // Left join to CommentLike
50 type UserMentionViewTuple = (
58 Option<CommunityUserBan>,
59 Option<CommunityFollower>,
64 impl UserMentionView {
68 my_user_id: Option<i32>,
69 ) -> Result<Self, Error> {
70 // The left join below will return None in this case
71 let user_id_join = my_user_id.unwrap_or(-1);
81 creator_banned_from_community,
85 ) = user_mention::table
86 .find(user_mention_id)
87 .inner_join(comment::table)
88 .inner_join(user_::table.on(comment::creator_id.eq(user_::id)))
89 .inner_join(post::table.on(comment::post_id.eq(post::id)))
90 .inner_join(community::table.on(post::community_id.eq(community::id)))
91 .inner_join(user_alias_1::table)
92 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
94 community_user_ban::table.on(
96 .eq(community_user_ban::community_id)
97 .and(community_user_ban::user_id.eq(comment::creator_id)),
101 community_follower::table.on(
103 .eq(community_follower::community_id)
104 .and(community_follower::user_id.eq(user_id_join)),
108 comment_saved::table.on(
110 .eq(comment_saved::comment_id)
111 .and(comment_saved::user_id.eq(user_id_join)),
115 comment_like::table.on(
117 .eq(comment_like::comment_id)
118 .and(comment_like::user_id.eq(user_id_join)),
122 user_mention::all_columns,
123 comment::all_columns,
124 User_::safe_columns_tuple(),
126 Community::safe_columns_tuple(),
127 UserAlias1::safe_columns_tuple(),
128 comment_aggregates::all_columns,
129 community_user_ban::all_columns.nullable(),
130 community_follower::all_columns.nullable(),
131 comment_saved::all_columns.nullable(),
132 comment_like::score.nullable(),
134 .first::<UserMentionViewTuple>(conn)?;
144 creator_banned_from_community: creator_banned_from_community.is_some(),
145 subscribed: subscribed.is_some(),
146 saved: saved.is_some(),
152 pub struct UserMentionQueryBuilder<'a> {
153 conn: &'a PgConnection,
154 my_user_id: Option<i32>,
155 recipient_id: Option<i32>,
162 impl<'a> UserMentionQueryBuilder<'a> {
163 pub fn create(conn: &'a PgConnection) -> Self {
164 UserMentionQueryBuilder {
168 sort: &SortType::New,
175 pub fn sort(mut self, sort: &'a SortType) -> Self {
180 pub fn unread_only(mut self, unread_only: bool) -> Self {
181 self.unread_only = unread_only;
185 pub fn recipient_id<T: MaybeOptional<i32>>(mut self, recipient_id: T) -> Self {
186 self.recipient_id = recipient_id.get_optional();
190 pub fn my_user_id<T: MaybeOptional<i32>>(mut self, my_user_id: T) -> Self {
191 self.my_user_id = my_user_id.get_optional();
195 pub fn page<T: MaybeOptional<i64>>(mut self, page: T) -> Self {
196 self.page = page.get_optional();
200 pub fn limit<T: MaybeOptional<i64>>(mut self, limit: T) -> Self {
201 self.limit = limit.get_optional();
205 pub fn list(self) -> Result<Vec<UserMentionView>, Error> {
208 // The left join below will return None in this case
209 let user_id_join = self.my_user_id.unwrap_or(-1);
211 let mut query = user_mention::table
212 .inner_join(comment::table)
213 .inner_join(user_::table.on(comment::creator_id.eq(user_::id)))
214 .inner_join(post::table.on(comment::post_id.eq(post::id)))
215 .inner_join(community::table.on(post::community_id.eq(community::id)))
216 .inner_join(user_alias_1::table)
217 .inner_join(comment_aggregates::table.on(comment::id.eq(comment_aggregates::comment_id)))
219 community_user_ban::table.on(
221 .eq(community_user_ban::community_id)
222 .and(community_user_ban::user_id.eq(comment::creator_id)),
226 community_follower::table.on(
228 .eq(community_follower::community_id)
229 .and(community_follower::user_id.eq(user_id_join)),
233 comment_saved::table.on(
235 .eq(comment_saved::comment_id)
236 .and(comment_saved::user_id.eq(user_id_join)),
240 comment_like::table.on(
242 .eq(comment_like::comment_id)
243 .and(comment_like::user_id.eq(user_id_join)),
247 user_mention::all_columns,
248 comment::all_columns,
249 User_::safe_columns_tuple(),
251 Community::safe_columns_tuple(),
252 UserAlias1::safe_columns_tuple(),
253 comment_aggregates::all_columns,
254 community_user_ban::all_columns.nullable(),
255 community_follower::all_columns.nullable(),
256 comment_saved::all_columns.nullable(),
257 comment_like::score.nullable(),
261 if let Some(recipient_id) = self.recipient_id {
262 query = query.filter(user_mention::recipient_id.eq(recipient_id));
265 if self.unread_only {
266 query = query.filter(user_mention::read.eq(false));
269 query = match self.sort {
270 SortType::Hot | SortType::Active => query
271 .order_by(hot_rank(comment_aggregates::score, comment_aggregates::published).desc())
272 .then_order_by(comment_aggregates::published.desc()),
273 SortType::New | SortType::MostComments => query.order_by(comment::published.desc()),
274 SortType::TopAll => query.order_by(comment_aggregates::score.desc()),
275 SortType::TopYear => query
276 .filter(comment::published.gt(now - 1.years()))
277 .order_by(comment_aggregates::score.desc()),
278 SortType::TopMonth => query
279 .filter(comment::published.gt(now - 1.months()))
280 .order_by(comment_aggregates::score.desc()),
281 SortType::TopWeek => query
282 .filter(comment::published.gt(now - 1.weeks()))
283 .order_by(comment_aggregates::score.desc()),
284 SortType::TopDay => query
285 .filter(comment::published.gt(now - 1.days()))
286 .order_by(comment_aggregates::score.desc()),
289 let (limit, offset) = limit_and_offset(self.page, self.limit);
294 .load::<UserMentionViewTuple>(self.conn)?;
296 Ok(UserMentionView::from_tuple_to_vec(res))
300 impl ViewToVec for UserMentionView {
301 type DbTuple = UserMentionViewTuple;
302 fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> {
306 user_mention: a.0.to_owned(),
307 comment: a.1.to_owned(),
308 creator: a.2.to_owned(),
309 post: a.3.to_owned(),
310 community: a.4.to_owned(),
311 recipient: a.5.to_owned(),
312 counts: a.6.to_owned(),
313 creator_banned_from_community: a.7.is_some(),
314 subscribed: a.8.is_some(),
315 saved: a.9.is_some(),
318 .collect::<Vec<Self>>()