2020-09-14 15:29:50 +00:00
|
|
|
use crate::{api::claims::Claims, DbPool, LemmyContext};
|
2020-08-18 13:43:50 +00:00
|
|
|
use actix_web::web::Data;
|
2020-09-14 15:29:50 +00:00
|
|
|
use lemmy_api_structs::blocking;
|
2020-07-22 18:20:08 +00:00
|
|
|
use lemmy_db::{
|
2020-09-01 13:20:22 +00:00
|
|
|
community::Community,
|
|
|
|
community_view::CommunityUserBanView,
|
2020-08-20 12:44:22 +00:00
|
|
|
post::Post,
|
2020-09-01 13:20:22 +00:00
|
|
|
user::User_,
|
2020-07-22 18:20:08 +00:00
|
|
|
Crud,
|
|
|
|
};
|
2020-09-14 15:29:50 +00:00
|
|
|
use lemmy_utils::{APIError, ConnectionId, LemmyError};
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-10 18:15:41 +00:00
|
|
|
pub mod claims;
|
2019-09-07 15:35:05 +00:00
|
|
|
pub mod comment;
|
2019-05-05 05:20:38 +00:00
|
|
|
pub mod community;
|
|
|
|
pub mod post;
|
|
|
|
pub mod site;
|
2019-09-07 15:35:05 +00:00
|
|
|
pub mod user;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-04-20 03:59:07 +00:00
|
|
|
pub trait Perform {
|
2020-04-21 20:40:03 +00:00
|
|
|
type Response: serde::ser::Serialize + Send;
|
2020-04-20 03:59:07 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
async fn perform(
|
2020-04-19 22:08:25 +00:00
|
|
|
&self,
|
2020-08-18 13:43:50 +00:00
|
|
|
context: &Data<LemmyContext>,
|
2020-08-24 11:58:24 +00:00
|
|
|
websocket_id: Option<ConnectionId>,
|
2020-07-01 12:54:29 +00:00
|
|
|
) -> Result<Self::Response, LemmyError>;
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
2020-07-22 18:20:08 +00:00
|
|
|
|
2020-08-06 19:44:47 +00:00
|
|
|
pub(in crate::api) async fn is_mod_or_admin(
|
2020-07-22 18:20:08 +00:00
|
|
|
pool: &DbPool,
|
|
|
|
user_id: i32,
|
|
|
|
community_id: i32,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let is_mod_or_admin = blocking(pool, move |conn| {
|
|
|
|
Community::is_mod_or_admin(conn, user_id, community_id)
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
if !is_mod_or_admin {
|
2020-08-06 19:44:47 +00:00
|
|
|
return Err(APIError::err("not_a_mod_or_admin").into());
|
2020-07-22 18:20:08 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
pub async fn is_admin(pool: &DbPool, user_id: i32) -> Result<(), LemmyError> {
|
|
|
|
let user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
|
|
|
|
if !user.admin {
|
|
|
|
return Err(APIError::err("not_an_admin").into());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-08-04 14:39:55 +00:00
|
|
|
|
2020-08-20 12:44:22 +00:00
|
|
|
pub(in crate::api) async fn get_post(post_id: i32, pool: &DbPool) -> Result<Post, LemmyError> {
|
|
|
|
match blocking(pool, move |conn| Post::read(conn, post_id)).await? {
|
|
|
|
Ok(post) => Ok(post),
|
|
|
|
Err(_e) => Err(APIError::err("couldnt_find_post").into()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
pub(in crate::api) async fn get_user_from_jwt(
|
|
|
|
jwt: &str,
|
|
|
|
pool: &DbPool,
|
|
|
|
) -> Result<User_, LemmyError> {
|
|
|
|
let claims = match Claims::decode(&jwt) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => return Err(APIError::err("not_logged_in").into()),
|
|
|
|
};
|
|
|
|
let user_id = claims.id;
|
|
|
|
let user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
|
|
|
|
// Check for a site ban
|
|
|
|
if user.banned {
|
|
|
|
return Err(APIError::err("site_ban").into());
|
|
|
|
}
|
|
|
|
Ok(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(in crate::api) async fn get_user_from_jwt_opt(
|
|
|
|
jwt: &Option<String>,
|
|
|
|
pool: &DbPool,
|
|
|
|
) -> Result<Option<User_>, LemmyError> {
|
|
|
|
match jwt {
|
|
|
|
Some(jwt) => Ok(Some(get_user_from_jwt(jwt, pool).await?)),
|
|
|
|
None => Ok(None),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(in crate::api) async fn check_community_ban(
|
|
|
|
user_id: i32,
|
|
|
|
community_id: i32,
|
|
|
|
pool: &DbPool,
|
|
|
|
) -> Result<(), LemmyError> {
|
|
|
|
let is_banned = move |conn: &'_ _| CommunityUserBanView::get(conn, user_id, community_id).is_ok();
|
|
|
|
if blocking(pool, is_banned).await? {
|
|
|
|
Err(APIError::err("community_ban").into())
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|