2020-07-22 18:20:08 +00:00
|
|
|
use crate::{blocking, websocket::WebsocketInfo, DbPool, LemmyError};
|
2020-07-01 12:54:29 +00:00
|
|
|
use actix_web::client::Client;
|
2020-07-22 18:20:08 +00:00
|
|
|
use lemmy_db::{
|
|
|
|
community::*,
|
|
|
|
community_view::*,
|
|
|
|
moderator::*,
|
|
|
|
site::*,
|
|
|
|
user::*,
|
|
|
|
user_view::*,
|
|
|
|
Crud,
|
|
|
|
};
|
2020-08-01 14:04:42 +00:00
|
|
|
use thiserror::Error;
|
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-08-01 14:04:42 +00:00
|
|
|
#[derive(Debug, Error)]
|
|
|
|
#[error("{{\"error\":\"{message}\"}}")]
|
2019-05-05 05:20:38 +00:00
|
|
|
pub struct APIError {
|
2019-05-05 16:20:30 +00:00
|
|
|
pub message: String,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl APIError {
|
2020-01-16 14:39:08 +00:00
|
|
|
pub fn err(msg: &str) -> Self {
|
2019-05-05 05:20:38 +00:00
|
|
|
APIError {
|
|
|
|
message: msg.to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Oper<T> {
|
2019-09-07 15:35:05 +00:00
|
|
|
data: T,
|
2020-07-01 12:54:29 +00:00
|
|
|
client: Client,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 14:04:36 +00:00
|
|
|
impl<Data> Oper<Data> {
|
2020-07-01 12:54:29 +00:00
|
|
|
pub fn new(data: Data, client: Client) -> Oper<Data> {
|
|
|
|
Oper { data, client }
|
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-07-01 12:54:29 +00:00
|
|
|
pool: &DbPool,
|
2020-04-19 22:08:25 +00:00
|
|
|
websocket_info: Option<WebsocketInfo>,
|
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
|
|
|
|
|
|
|
pub async fn is_mod_or_admin(
|
|
|
|
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 {
|
|
|
|
return Err(APIError::err("not_an_admin").into());
|
|
|
|
}
|
|
|
|
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(())
|
|
|
|
}
|