1 use crate::{api::claims::Claims, DbPool, LemmyContext};
2 use actix_web::web::Data;
5 community_view::CommunityUserBanView,
10 use lemmy_structs::blocking;
11 use lemmy_utils::{APIError, ConnectionId, LemmyError};
20 #[async_trait::async_trait(?Send)]
22 type Response: serde::ser::Serialize + Send;
26 context: &Data<LemmyContext>,
27 websocket_id: Option<ConnectionId>,
28 ) -> Result<Self::Response, LemmyError>;
31 pub(in crate::api) async fn is_mod_or_admin(
35 ) -> Result<(), LemmyError> {
36 let is_mod_or_admin = blocking(pool, move |conn| {
37 Community::is_mod_or_admin(conn, user_id, community_id)
41 return Err(APIError::err("not_a_mod_or_admin").into());
45 pub async fn is_admin(pool: &DbPool, user_id: i32) -> Result<(), LemmyError> {
46 let user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
48 return Err(APIError::err("not_an_admin").into());
53 pub(in crate::api) async fn get_post(post_id: i32, pool: &DbPool) -> Result<Post, LemmyError> {
54 match blocking(pool, move |conn| Post::read(conn, post_id)).await? {
56 Err(_e) => Err(APIError::err("couldnt_find_post").into()),
60 pub(in crate::api) async fn get_user_from_jwt(
63 ) -> Result<User_, LemmyError> {
64 let claims = match Claims::decode(&jwt) {
65 Ok(claims) => claims.claims,
66 Err(_e) => return Err(APIError::err("not_logged_in").into()),
68 let user_id = claims.id;
69 let user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
70 // Check for a site ban
72 return Err(APIError::err("site_ban").into());
77 pub(in crate::api) async fn get_user_from_jwt_opt(
80 ) -> Result<Option<User_>, LemmyError> {
82 Some(jwt) => Ok(Some(get_user_from_jwt(jwt, pool).await?)),
87 pub(in crate::api) async fn check_community_ban(
91 ) -> Result<(), LemmyError> {
92 let is_banned = move |conn: &'_ _| CommunityUserBanView::get(conn, user_id, community_id).is_ok();
93 if blocking(pool, is_banned).await? {
94 Err(APIError::err("community_ban").into())