2020-04-19 22:08:25 +00:00
|
|
|
use super::user::Register;
|
2019-05-05 05:20:38 +00:00
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2020-01-28 15:50:40 +00:00
|
|
|
pub struct ListCategories {}
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct ListCategoriesResponse {
|
2019-09-07 15:35:05 +00:00
|
|
|
categories: Vec<Category>,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct Search {
|
|
|
|
q: String,
|
|
|
|
type_: String,
|
|
|
|
community_id: Option<i32>,
|
|
|
|
sort: String,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
2020-01-20 23:39:45 +00:00
|
|
|
auth: Option<String>,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SearchResponse {
|
2019-08-22 05:17:15 +00:00
|
|
|
type_: String,
|
2019-05-05 05:20:38 +00:00
|
|
|
comments: Vec<CommentView>,
|
|
|
|
posts: Vec<PostView>,
|
2019-08-10 17:32:06 +00:00
|
|
|
communities: Vec<CommunityView>,
|
|
|
|
users: Vec<UserView>,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetModlog {
|
|
|
|
mod_user_id: Option<i32>,
|
|
|
|
community_id: Option<i32>,
|
|
|
|
page: Option<i64>,
|
|
|
|
limit: Option<i64>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetModlogResponse {
|
|
|
|
removed_posts: Vec<ModRemovePostView>,
|
|
|
|
locked_posts: Vec<ModLockPostView>,
|
2019-09-09 06:14:13 +00:00
|
|
|
stickied_posts: Vec<ModStickyPostView>,
|
2019-05-05 05:20:38 +00:00
|
|
|
removed_comments: Vec<ModRemoveCommentView>,
|
|
|
|
removed_communities: Vec<ModRemoveCommunityView>,
|
|
|
|
banned_from_community: Vec<ModBanFromCommunityView>,
|
|
|
|
banned: Vec<ModBanView>,
|
|
|
|
added_to_community: Vec<ModAddCommunityView>,
|
|
|
|
added: Vec<ModAddView>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct CreateSite {
|
2020-03-26 14:23:15 +00:00
|
|
|
pub name: String,
|
|
|
|
pub description: Option<String>,
|
|
|
|
pub enable_downvotes: bool,
|
|
|
|
pub open_registration: bool,
|
|
|
|
pub enable_nsfw: bool,
|
|
|
|
pub auth: String,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct EditSite {
|
|
|
|
name: String,
|
|
|
|
description: Option<String>,
|
2019-12-11 20:21:47 +00:00
|
|
|
enable_downvotes: bool,
|
|
|
|
open_registration: bool,
|
|
|
|
enable_nsfw: bool,
|
2019-09-07 15:35:05 +00:00
|
|
|
auth: String,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
2020-01-28 15:50:40 +00:00
|
|
|
pub struct GetSite {}
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
#[derive(Serialize, Deserialize, Clone)]
|
2019-05-05 05:20:38 +00:00
|
|
|
pub struct SiteResponse {
|
|
|
|
site: SiteView,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetSiteResponse {
|
|
|
|
site: Option<SiteView>,
|
|
|
|
admins: Vec<UserView>,
|
|
|
|
banned: Vec<UserView>,
|
2019-09-13 16:09:01 +00:00
|
|
|
pub online: usize,
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
2019-08-24 02:40:41 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct TransferSite {
|
|
|
|
user_id: i32,
|
2019-09-07 15:35:05 +00:00
|
|
|
auth: String,
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 20:55:57 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetSiteConfig {
|
|
|
|
auth: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct GetSiteConfigResponse {
|
|
|
|
config_hjson: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
pub struct SaveSiteConfig {
|
|
|
|
config_hjson: String,
|
|
|
|
auth: String,
|
|
|
|
}
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
impl Perform<ListCategoriesResponse> for Oper<ListCategories> {
|
2020-04-19 22:08:25 +00:00
|
|
|
fn perform(
|
|
|
|
&self,
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
_websocket_info: Option<WebsocketInfo>,
|
|
|
|
rate_limit_info: Option<RateLimitInfo>,
|
|
|
|
) -> Result<ListCategoriesResponse, Error> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let _data: &ListCategories = &self.data;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(rl) = rate_limit_info {
|
|
|
|
rl.rate_limiter
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.check_rate_limit_message(&rl.ip, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = pool.get()?;
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
let categories: Vec<Category> = Category::list_all(&conn)?;
|
|
|
|
|
|
|
|
// Return the jwt
|
2020-01-16 14:39:08 +00:00
|
|
|
Ok(ListCategoriesResponse { categories })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform<GetModlogResponse> for Oper<GetModlog> {
|
2020-04-19 22:08:25 +00:00
|
|
|
fn perform(
|
|
|
|
&self,
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
_websocket_info: Option<WebsocketInfo>,
|
|
|
|
rate_limit_info: Option<RateLimitInfo>,
|
|
|
|
) -> Result<GetModlogResponse, Error> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &GetModlog = &self.data;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(rl) = rate_limit_info {
|
|
|
|
rl.rate_limiter
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.check_rate_limit_message(&rl.ip, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = pool.get()?;
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
let removed_posts = ModRemovePostView::list(
|
|
|
|
&conn,
|
|
|
|
data.community_id,
|
|
|
|
data.mod_user_id,
|
|
|
|
data.page,
|
|
|
|
data.limit,
|
|
|
|
)?;
|
|
|
|
let locked_posts = ModLockPostView::list(
|
|
|
|
&conn,
|
|
|
|
data.community_id,
|
|
|
|
data.mod_user_id,
|
|
|
|
data.page,
|
|
|
|
data.limit,
|
|
|
|
)?;
|
2019-09-09 06:14:13 +00:00
|
|
|
let stickied_posts = ModStickyPostView::list(
|
|
|
|
&conn,
|
|
|
|
data.community_id,
|
|
|
|
data.mod_user_id,
|
|
|
|
data.page,
|
|
|
|
data.limit,
|
|
|
|
)?;
|
2019-09-07 15:35:05 +00:00
|
|
|
let removed_comments = ModRemoveCommentView::list(
|
|
|
|
&conn,
|
|
|
|
data.community_id,
|
|
|
|
data.mod_user_id,
|
|
|
|
data.page,
|
|
|
|
data.limit,
|
|
|
|
)?;
|
|
|
|
let banned_from_community = ModBanFromCommunityView::list(
|
|
|
|
&conn,
|
|
|
|
data.community_id,
|
|
|
|
data.mod_user_id,
|
|
|
|
data.page,
|
|
|
|
data.limit,
|
|
|
|
)?;
|
|
|
|
let added_to_community = ModAddCommunityView::list(
|
|
|
|
&conn,
|
|
|
|
data.community_id,
|
|
|
|
data.mod_user_id,
|
|
|
|
data.page,
|
|
|
|
data.limit,
|
|
|
|
)?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// These arrays are only for the full modlog, when a community isn't given
|
2020-01-02 11:30:00 +00:00
|
|
|
let (removed_communities, banned, added) = if data.community_id.is_none() {
|
|
|
|
(
|
|
|
|
ModRemoveCommunityView::list(&conn, data.mod_user_id, data.page, data.limit)?,
|
|
|
|
ModBanView::list(&conn, data.mod_user_id, data.page, data.limit)?,
|
|
|
|
ModAddView::list(&conn, data.mod_user_id, data.page, data.limit)?,
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
(Vec::new(), Vec::new(), Vec::new())
|
|
|
|
};
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Return the jwt
|
2019-09-07 15:35:05 +00:00
|
|
|
Ok(GetModlogResponse {
|
2019-12-09 19:08:19 +00:00
|
|
|
removed_posts,
|
|
|
|
locked_posts,
|
|
|
|
stickied_posts,
|
|
|
|
removed_comments,
|
|
|
|
removed_communities,
|
|
|
|
banned_from_community,
|
|
|
|
banned,
|
|
|
|
added_to_community,
|
|
|
|
added,
|
2019-09-07 15:35:05 +00:00
|
|
|
})
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform<SiteResponse> for Oper<CreateSite> {
|
2020-04-19 22:08:25 +00:00
|
|
|
fn perform(
|
|
|
|
&self,
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
_websocket_info: Option<WebsocketInfo>,
|
|
|
|
rate_limit_info: Option<RateLimitInfo>,
|
|
|
|
) -> Result<SiteResponse, Error> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &CreateSite = &self.data;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let claims = match Claims::decode(&data.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
2020-01-16 14:39:08 +00:00
|
|
|
Err(_e) => return Err(APIError::err("not_logged_in").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-02-03 03:51:54 +00:00
|
|
|
if let Err(slurs) = slur_check(&data.name) {
|
|
|
|
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(description) = &data.description {
|
|
|
|
if let Err(slurs) = slur_check(description) {
|
|
|
|
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
|
|
|
|
}
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(rl) = rate_limit_info {
|
|
|
|
rl.rate_limiter
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.check_rate_limit_message(&rl.ip, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = pool.get()?;
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// Make sure user is an admin
|
|
|
|
if !UserView::read(&conn, user_id)?.admin {
|
2020-01-16 14:39:08 +00:00
|
|
|
return Err(APIError::err("not_an_admin").into());
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let site_form = SiteForm {
|
|
|
|
name: data.name.to_owned(),
|
|
|
|
description: data.description.to_owned(),
|
|
|
|
creator_id: user_id,
|
2019-12-11 20:21:47 +00:00
|
|
|
enable_downvotes: data.enable_downvotes,
|
|
|
|
open_registration: data.open_registration,
|
|
|
|
enable_nsfw: data.enable_nsfw,
|
2019-05-05 05:20:38 +00:00
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
|
|
|
|
match Site::create(&conn, &site_form) {
|
|
|
|
Ok(site) => site,
|
2020-01-16 14:39:08 +00:00
|
|
|
Err(_e) => return Err(APIError::err("site_already_exists").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let site_view = SiteView::read(&conn)?;
|
|
|
|
|
2020-01-16 14:39:08 +00:00
|
|
|
Ok(SiteResponse { site: site_view })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform<SiteResponse> for Oper<EditSite> {
|
2020-04-19 22:08:25 +00:00
|
|
|
fn perform(
|
|
|
|
&self,
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
websocket_info: Option<WebsocketInfo>,
|
|
|
|
rate_limit_info: Option<RateLimitInfo>,
|
|
|
|
) -> Result<SiteResponse, Error> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &EditSite = &self.data;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let claims = match Claims::decode(&data.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
2020-01-16 14:39:08 +00:00
|
|
|
Err(_e) => return Err(APIError::err("not_logged_in").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-02-03 03:51:54 +00:00
|
|
|
if let Err(slurs) = slur_check(&data.name) {
|
|
|
|
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(description) = &data.description {
|
|
|
|
if let Err(slurs) = slur_check(description) {
|
|
|
|
return Err(APIError::err(&slurs_vec_to_str(slurs)).into());
|
|
|
|
}
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(rl) = rate_limit_info {
|
|
|
|
rl.rate_limiter
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.check_rate_limit_message(&rl.ip, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = pool.get()?;
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// Make sure user is an admin
|
2020-01-02 11:30:00 +00:00
|
|
|
if !UserView::read(&conn, user_id)?.admin {
|
2020-01-16 14:39:08 +00:00
|
|
|
return Err(APIError::err("not_an_admin").into());
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let found_site = Site::read(&conn, 1)?;
|
|
|
|
|
|
|
|
let site_form = SiteForm {
|
|
|
|
name: data.name.to_owned(),
|
|
|
|
description: data.description.to_owned(),
|
|
|
|
creator_id: found_site.creator_id,
|
|
|
|
updated: Some(naive_now()),
|
2019-12-11 20:21:47 +00:00
|
|
|
enable_downvotes: data.enable_downvotes,
|
|
|
|
open_registration: data.open_registration,
|
|
|
|
enable_nsfw: data.enable_nsfw,
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match Site::update(&conn, 1, &site_form) {
|
|
|
|
Ok(site) => site,
|
2020-01-16 14:39:08 +00:00
|
|
|
Err(_e) => return Err(APIError::err("couldnt_update_site").into()),
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let site_view = SiteView::read(&conn)?;
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
let res = SiteResponse { site: site_view };
|
|
|
|
|
|
|
|
if let Some(ws) = websocket_info {
|
|
|
|
ws.chatserver.do_send(SendAllMessage {
|
|
|
|
op: UserOperation::EditSite,
|
|
|
|
response: res.clone(),
|
|
|
|
my_id: ws.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform<GetSiteResponse> for Oper<GetSite> {
|
2020-04-19 22:08:25 +00:00
|
|
|
fn perform(
|
|
|
|
&self,
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
websocket_info: Option<WebsocketInfo>,
|
|
|
|
rate_limit_info: Option<RateLimitInfo>,
|
|
|
|
) -> Result<GetSiteResponse, Error> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let _data: &GetSite = &self.data;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(rl) = &rate_limit_info {
|
|
|
|
rl.rate_limiter
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.check_rate_limit_message(&rl.ip, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = pool.get()?;
|
|
|
|
|
|
|
|
// TODO refactor this a little
|
2020-03-26 14:23:15 +00:00
|
|
|
let site = Site::read(&conn, 1);
|
|
|
|
let site_view = if site.is_ok() {
|
|
|
|
Some(SiteView::read(&conn)?)
|
|
|
|
} else if let Some(setup) = Settings::get().setup.as_ref() {
|
|
|
|
let register = Register {
|
|
|
|
username: setup.admin_username.to_owned(),
|
|
|
|
email: setup.admin_email.to_owned(),
|
|
|
|
password: setup.admin_password.to_owned(),
|
|
|
|
password_verify: setup.admin_password.to_owned(),
|
|
|
|
admin: true,
|
|
|
|
show_nsfw: true,
|
|
|
|
};
|
2020-04-19 22:08:25 +00:00
|
|
|
let login_response = Oper::new(register).perform(
|
|
|
|
pool.clone(),
|
|
|
|
websocket_info.clone(),
|
|
|
|
rate_limit_info.clone(),
|
|
|
|
)?;
|
2020-03-26 14:23:15 +00:00
|
|
|
info!("Admin {} created", setup.admin_username);
|
|
|
|
|
|
|
|
let create_site = CreateSite {
|
|
|
|
name: setup.site_name.to_owned(),
|
|
|
|
description: None,
|
|
|
|
enable_downvotes: false,
|
|
|
|
open_registration: false,
|
|
|
|
enable_nsfw: false,
|
|
|
|
auth: login_response.jwt,
|
|
|
|
};
|
2020-04-19 22:08:25 +00:00
|
|
|
Oper::new(create_site).perform(pool, websocket_info.clone(), rate_limit_info)?;
|
2020-03-26 14:23:15 +00:00
|
|
|
info!("Site {} created", setup.site_name);
|
|
|
|
Some(SiteView::read(&conn)?)
|
|
|
|
} else {
|
|
|
|
None
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2019-08-24 02:40:41 +00:00
|
|
|
let mut admins = UserView::admins(&conn)?;
|
|
|
|
if site_view.is_some() {
|
|
|
|
let site_creator_id = site_view.to_owned().unwrap().creator_id;
|
|
|
|
let creator_index = admins.iter().position(|r| r.id == site_creator_id).unwrap();
|
|
|
|
let creator_user = admins.remove(creator_index);
|
|
|
|
admins.insert(0, creator_user);
|
|
|
|
}
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
let banned = UserView::banned(&conn)?;
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
let online = if let Some(_ws) = websocket_info {
|
|
|
|
// TODO
|
|
|
|
1
|
|
|
|
// let fut = async {
|
|
|
|
// ws.chatserver.send(GetUsersOnline).await.unwrap()
|
|
|
|
// };
|
|
|
|
// Runtime::new().unwrap().block_on(fut)
|
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
Ok(GetSiteResponse {
|
|
|
|
site: site_view,
|
2019-12-09 19:08:19 +00:00
|
|
|
admins,
|
|
|
|
banned,
|
2020-04-19 22:08:25 +00:00
|
|
|
online,
|
2019-09-07 15:35:05 +00:00
|
|
|
})
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform<SearchResponse> for Oper<Search> {
|
2020-04-19 22:08:25 +00:00
|
|
|
fn perform(
|
|
|
|
&self,
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
_websocket_info: Option<WebsocketInfo>,
|
|
|
|
rate_limit_info: Option<RateLimitInfo>,
|
|
|
|
) -> Result<SearchResponse, Error> {
|
2019-05-05 16:20:30 +00:00
|
|
|
let data: &Search = &self.data;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-01-20 23:39:45 +00:00
|
|
|
let user_id: Option<i32> = match &data.auth {
|
|
|
|
Some(auth) => match Claims::decode(&auth) {
|
|
|
|
Ok(claims) => {
|
|
|
|
let user_id = claims.claims.id;
|
|
|
|
Some(user_id)
|
|
|
|
}
|
|
|
|
Err(_e) => None,
|
|
|
|
},
|
|
|
|
None => None,
|
|
|
|
};
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
let sort = SortType::from_str(&data.sort)?;
|
|
|
|
let type_ = SearchType::from_str(&data.type_)?;
|
|
|
|
|
|
|
|
let mut posts = Vec::new();
|
|
|
|
let mut comments = Vec::new();
|
2019-08-10 17:32:06 +00:00
|
|
|
let mut communities = Vec::new();
|
|
|
|
let mut users = Vec::new();
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2019-08-14 02:52:43 +00:00
|
|
|
// TODO no clean / non-nsfw searching rn
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(rl) = rate_limit_info {
|
|
|
|
rl.rate_limiter
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.check_rate_limit_message(&rl.ip, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = pool.get()?;
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
match type_ {
|
|
|
|
SearchType::Posts => {
|
2019-12-07 22:54:42 +00:00
|
|
|
posts = PostQueryBuilder::create(&conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.show_nsfw(true)
|
2019-12-10 23:10:39 +00:00
|
|
|
.for_community_id(data.community_id)
|
2019-12-07 12:03:03 +00:00
|
|
|
.search_term(data.q.to_owned())
|
2020-01-20 23:39:45 +00:00
|
|
|
.my_user_id(user_id)
|
2019-12-10 23:10:39 +00:00
|
|
|
.page(data.page)
|
|
|
|
.limit(data.limit)
|
2019-12-07 12:03:03 +00:00
|
|
|
.list()?;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
SearchType::Comments => {
|
2019-12-08 20:39:54 +00:00
|
|
|
comments = CommentQueryBuilder::create(&conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.search_term(data.q.to_owned())
|
2020-01-20 23:39:45 +00:00
|
|
|
.my_user_id(user_id)
|
2019-12-10 23:10:39 +00:00
|
|
|
.page(data.page)
|
|
|
|
.limit(data.limit)
|
2019-12-08 20:39:54 +00:00
|
|
|
.list()?;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-10 17:32:06 +00:00
|
|
|
SearchType::Communities => {
|
2019-12-08 20:39:54 +00:00
|
|
|
communities = CommunityQueryBuilder::create(&conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.search_term(data.q.to_owned())
|
2019-12-10 23:10:39 +00:00
|
|
|
.page(data.page)
|
|
|
|
.limit(data.limit)
|
2019-12-08 20:39:54 +00:00
|
|
|
.list()?;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-10 17:32:06 +00:00
|
|
|
SearchType::Users => {
|
2019-12-08 20:39:54 +00:00
|
|
|
users = UserQueryBuilder::create(&conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.search_term(data.q.to_owned())
|
2019-12-10 23:10:39 +00:00
|
|
|
.page(data.page)
|
|
|
|
.limit(data.limit)
|
2019-12-08 20:39:54 +00:00
|
|
|
.list()?;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-10 17:32:06 +00:00
|
|
|
SearchType::All => {
|
2019-12-07 22:54:42 +00:00
|
|
|
posts = PostQueryBuilder::create(&conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.show_nsfw(true)
|
2019-12-10 23:10:39 +00:00
|
|
|
.for_community_id(data.community_id)
|
2019-12-07 12:03:03 +00:00
|
|
|
.search_term(data.q.to_owned())
|
2020-01-20 23:39:45 +00:00
|
|
|
.my_user_id(user_id)
|
2019-12-10 23:10:39 +00:00
|
|
|
.page(data.page)
|
|
|
|
.limit(data.limit)
|
2019-12-07 12:03:03 +00:00
|
|
|
.list()?;
|
|
|
|
|
2019-12-08 20:39:54 +00:00
|
|
|
comments = CommentQueryBuilder::create(&conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.search_term(data.q.to_owned())
|
2020-01-20 23:39:45 +00:00
|
|
|
.my_user_id(user_id)
|
2019-12-10 23:10:39 +00:00
|
|
|
.page(data.page)
|
|
|
|
.limit(data.limit)
|
2019-12-08 20:39:54 +00:00
|
|
|
.list()?;
|
|
|
|
|
|
|
|
communities = CommunityQueryBuilder::create(&conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.search_term(data.q.to_owned())
|
2019-12-10 23:10:39 +00:00
|
|
|
.page(data.page)
|
|
|
|
.limit(data.limit)
|
2019-12-08 20:39:54 +00:00
|
|
|
.list()?;
|
|
|
|
|
|
|
|
users = UserQueryBuilder::create(&conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.search_term(data.q.to_owned())
|
2019-12-10 23:10:39 +00:00
|
|
|
.page(data.page)
|
|
|
|
.limit(data.limit)
|
2019-12-08 20:39:54 +00:00
|
|
|
.list()?;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-22 05:17:15 +00:00
|
|
|
SearchType::Url => {
|
2019-12-07 22:54:42 +00:00
|
|
|
posts = PostQueryBuilder::create(&conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.show_nsfw(true)
|
2019-12-10 23:10:39 +00:00
|
|
|
.for_community_id(data.community_id)
|
2019-12-07 12:03:03 +00:00
|
|
|
.url_search(data.q.to_owned())
|
2019-12-10 23:10:39 +00:00
|
|
|
.page(data.page)
|
|
|
|
.limit(data.limit)
|
2019-12-07 12:03:03 +00:00
|
|
|
.list()?;
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Return the jwt
|
2019-09-07 15:35:05 +00:00
|
|
|
Ok(SearchResponse {
|
|
|
|
type_: data.type_.to_owned(),
|
2019-12-09 19:08:19 +00:00
|
|
|
comments,
|
|
|
|
posts,
|
|
|
|
communities,
|
|
|
|
users,
|
2019-09-07 15:35:05 +00:00
|
|
|
})
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-24 02:40:41 +00:00
|
|
|
|
|
|
|
impl Perform<GetSiteResponse> for Oper<TransferSite> {
|
2020-04-19 22:08:25 +00:00
|
|
|
fn perform(
|
|
|
|
&self,
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
_websocket_info: Option<WebsocketInfo>,
|
|
|
|
rate_limit_info: Option<RateLimitInfo>,
|
|
|
|
) -> Result<GetSiteResponse, Error> {
|
2019-08-24 02:40:41 +00:00
|
|
|
let data: &TransferSite = &self.data;
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&data.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
2020-01-16 14:39:08 +00:00
|
|
|
Err(_e) => return Err(APIError::err("not_logged_in").into()),
|
2019-08-24 02:40:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(rl) = rate_limit_info {
|
|
|
|
rl.rate_limiter
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.check_rate_limit_message(&rl.ip, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = pool.get()?;
|
|
|
|
|
2019-08-24 02:40:41 +00:00
|
|
|
let read_site = Site::read(&conn, 1)?;
|
|
|
|
|
|
|
|
// Make sure user is the creator
|
|
|
|
if read_site.creator_id != user_id {
|
2020-01-16 14:39:08 +00:00
|
|
|
return Err(APIError::err("not_an_admin").into());
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let site_form = SiteForm {
|
|
|
|
name: read_site.name,
|
|
|
|
description: read_site.description,
|
|
|
|
creator_id: data.user_id,
|
|
|
|
updated: Some(naive_now()),
|
2019-12-11 20:21:47 +00:00
|
|
|
enable_downvotes: read_site.enable_downvotes,
|
|
|
|
open_registration: read_site.open_registration,
|
|
|
|
enable_nsfw: read_site.enable_nsfw,
|
2019-08-24 02:40:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
match Site::update(&conn, 1, &site_form) {
|
|
|
|
Ok(site) => site,
|
2020-01-16 14:39:08 +00:00
|
|
|
Err(_e) => return Err(APIError::err("couldnt_update_site").into()),
|
2019-08-24 02:40:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModAddForm {
|
|
|
|
mod_user_id: user_id,
|
|
|
|
other_user_id: data.user_id,
|
|
|
|
removed: Some(false),
|
|
|
|
};
|
|
|
|
|
|
|
|
ModAdd::create(&conn, &form)?;
|
|
|
|
|
|
|
|
let site_view = SiteView::read(&conn)?;
|
|
|
|
|
|
|
|
let mut admins = UserView::admins(&conn)?;
|
2019-09-07 15:35:05 +00:00
|
|
|
let creator_index = admins
|
|
|
|
.iter()
|
|
|
|
.position(|r| r.id == site_view.creator_id)
|
|
|
|
.unwrap();
|
2019-08-24 02:40:41 +00:00
|
|
|
let creator_user = admins.remove(creator_index);
|
|
|
|
admins.insert(0, creator_user);
|
|
|
|
|
|
|
|
let banned = UserView::banned(&conn)?;
|
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
Ok(GetSiteResponse {
|
|
|
|
site: Some(site_view),
|
2019-12-09 19:08:19 +00:00
|
|
|
admins,
|
|
|
|
banned,
|
2019-10-13 19:06:18 +00:00
|
|
|
online: 0,
|
2019-09-07 15:35:05 +00:00
|
|
|
})
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 20:55:57 +00:00
|
|
|
|
|
|
|
impl Perform<GetSiteConfigResponse> for Oper<GetSiteConfig> {
|
2020-04-19 22:08:25 +00:00
|
|
|
fn perform(
|
|
|
|
&self,
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
_websocket_info: Option<WebsocketInfo>,
|
|
|
|
rate_limit_info: Option<RateLimitInfo>,
|
|
|
|
) -> Result<GetSiteConfigResponse, Error> {
|
2020-04-10 20:55:57 +00:00
|
|
|
let data: &GetSiteConfig = &self.data;
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&data.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => return Err(APIError::err("not_logged_in").into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(rl) = rate_limit_info {
|
|
|
|
rl.rate_limiter
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.check_rate_limit_message(&rl.ip, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = pool.get()?;
|
|
|
|
|
2020-04-10 20:55:57 +00:00
|
|
|
// Only let admins read this
|
|
|
|
let admins = UserView::admins(&conn)?;
|
|
|
|
let admin_ids: Vec<i32> = admins.into_iter().map(|m| m.id).collect();
|
|
|
|
|
|
|
|
if !admin_ids.contains(&user_id) {
|
|
|
|
return Err(APIError::err("not_an_admin").into());
|
|
|
|
}
|
|
|
|
|
|
|
|
let config_hjson = Settings::read_config_file()?;
|
|
|
|
|
|
|
|
Ok(GetSiteConfigResponse { config_hjson })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Perform<GetSiteConfigResponse> for Oper<SaveSiteConfig> {
|
2020-04-19 22:08:25 +00:00
|
|
|
fn perform(
|
|
|
|
&self,
|
|
|
|
pool: Pool<ConnectionManager<PgConnection>>,
|
|
|
|
_websocket_info: Option<WebsocketInfo>,
|
|
|
|
rate_limit_info: Option<RateLimitInfo>,
|
|
|
|
) -> Result<GetSiteConfigResponse, Error> {
|
2020-04-10 20:55:57 +00:00
|
|
|
let data: &SaveSiteConfig = &self.data;
|
|
|
|
|
|
|
|
let claims = match Claims::decode(&data.auth) {
|
|
|
|
Ok(claims) => claims.claims,
|
|
|
|
Err(_e) => return Err(APIError::err("not_logged_in").into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
let user_id = claims.id;
|
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
if let Some(rl) = rate_limit_info {
|
|
|
|
rl.rate_limiter
|
|
|
|
.lock()
|
|
|
|
.unwrap()
|
|
|
|
.check_rate_limit_message(&rl.ip, false)?;
|
|
|
|
}
|
|
|
|
|
|
|
|
let conn = pool.get()?;
|
|
|
|
|
2020-04-10 20:55:57 +00:00
|
|
|
// Only let admins read this
|
|
|
|
let admins = UserView::admins(&conn)?;
|
|
|
|
let admin_ids: Vec<i32> = admins.into_iter().map(|m| m.id).collect();
|
|
|
|
|
|
|
|
if !admin_ids.contains(&user_id) {
|
|
|
|
return Err(APIError::err("not_an_admin").into());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
|
|
|
|
let config_hjson = match Settings::save_config_file(&data.config_hjson) {
|
|
|
|
Ok(config_hjson) => config_hjson,
|
|
|
|
Err(_e) => return Err(APIError::err("couldnt_update_site").into()),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(GetSiteConfigResponse { config_hjson })
|
|
|
|
}
|
|
|
|
}
|