]> Untitled Git - lemmy.git/blob - crates/api/src/local_user/notifications/list_mentions.rs
Add cargo feature for building lemmy_api_common with mininum deps (#2243)
[lemmy.git] / crates / api / src / local_user / notifications / list_mentions.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   person::{GetPersonMentions, GetPersonMentionsResponse},
5   utils::{blocking, get_local_user_view_from_jwt},
6 };
7 use lemmy_db_schema::utils::{from_opt_str_to_opt_enum, SortType};
8 use lemmy_db_views_actor::person_mention_view::PersonMentionQueryBuilder;
9 use lemmy_utils::{ConnectionId, LemmyError};
10 use lemmy_websocket::LemmyContext;
11
12 #[async_trait::async_trait(?Send)]
13 impl Perform for GetPersonMentions {
14   type Response = GetPersonMentionsResponse;
15
16   #[tracing::instrument(skip(context, _websocket_id))]
17   async fn perform(
18     &self,
19     context: &Data<LemmyContext>,
20     _websocket_id: Option<ConnectionId>,
21   ) -> Result<GetPersonMentionsResponse, LemmyError> {
22     let data: &GetPersonMentions = self;
23     let local_user_view =
24       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
25
26     let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
27
28     let page = data.page;
29     let limit = data.limit;
30     let unread_only = data.unread_only;
31     let person_id = local_user_view.person.id;
32     let mentions = blocking(context.pool(), move |conn| {
33       PersonMentionQueryBuilder::create(conn)
34         .recipient_id(person_id)
35         .my_person_id(person_id)
36         .sort(sort)
37         .unread_only(unread_only)
38         .page(page)
39         .limit(limit)
40         .list()
41     })
42     .await??;
43
44     Ok(GetPersonMentionsResponse { mentions })
45   }
46 }