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