2020-05-16 14:04:08 +00:00
|
|
|
use crate::{
|
2020-09-24 14:14:09 +00:00
|
|
|
get_user_from_jwt,
|
|
|
|
get_user_from_jwt_opt,
|
|
|
|
is_admin,
|
|
|
|
linked_instances,
|
2020-07-21 13:20:23 +00:00
|
|
|
version,
|
2020-09-24 14:14:09 +00:00
|
|
|
Perform,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2020-08-18 13:43:50 +00:00
|
|
|
use actix_web::web::Data;
|
2020-08-13 15:46:31 +00:00
|
|
|
use anyhow::Context;
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_apub::fetcher::search_by_apub_id;
|
2020-07-10 18:15:41 +00:00
|
|
|
use lemmy_db::{
|
2020-12-03 18:39:56 +00:00
|
|
|
aggregates::site_aggregates::SiteAggregates,
|
2020-08-05 16:03:46 +00:00
|
|
|
diesel_option_overwrite,
|
2020-07-10 18:15:41 +00:00
|
|
|
naive_now,
|
2020-12-13 17:04:42 +00:00
|
|
|
source::{category::*, moderator::*, site::*},
|
2020-12-04 00:47:58 +00:00
|
|
|
views::{
|
2020-12-15 19:39:18 +00:00
|
|
|
comment_view::CommentQueryBuilder,
|
2020-12-16 21:28:18 +00:00
|
|
|
community::community_view::CommunityQueryBuilder,
|
|
|
|
moderator::{
|
|
|
|
mod_add_community_view::ModAddCommunityView,
|
|
|
|
mod_add_view::ModAddView,
|
|
|
|
mod_ban_from_community_view::ModBanFromCommunityView,
|
|
|
|
mod_ban_view::ModBanView,
|
|
|
|
mod_lock_post_view::ModLockPostView,
|
|
|
|
mod_remove_comment_view::ModRemoveCommentView,
|
|
|
|
mod_remove_community_view::ModRemoveCommunityView,
|
|
|
|
mod_remove_post_view::ModRemovePostView,
|
|
|
|
mod_sticky_post_view::ModStickyPostView,
|
|
|
|
},
|
2020-12-11 15:27:33 +00:00
|
|
|
post_view::PostQueryBuilder,
|
2020-12-04 00:47:58 +00:00
|
|
|
site_view::SiteView,
|
|
|
|
user_view::{UserQueryBuilder, UserViewSafe},
|
|
|
|
},
|
2020-07-10 18:15:41 +00:00
|
|
|
Crud,
|
|
|
|
SearchType,
|
|
|
|
SortType,
|
|
|
|
};
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_structs::{blocking, site::*, user::Register};
|
2020-09-14 15:29:50 +00:00
|
|
|
use lemmy_utils::{
|
|
|
|
location_info,
|
|
|
|
settings::Settings,
|
|
|
|
utils::{check_slurs, check_slurs_opt},
|
|
|
|
APIError,
|
|
|
|
ConnectionId,
|
|
|
|
LemmyError,
|
|
|
|
};
|
2020-09-24 13:53:21 +00:00
|
|
|
use lemmy_websocket::{
|
|
|
|
messages::{GetUsersOnline, SendAllMessage},
|
|
|
|
LemmyContext,
|
|
|
|
UserOperation,
|
|
|
|
};
|
2020-05-16 14:04:08 +00:00
|
|
|
use log::{debug, info};
|
|
|
|
use std::str::FromStr;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for ListCategories {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = ListCategoriesResponse;
|
|
|
|
|
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<ListCategoriesResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let _data: &ListCategories = &self;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let categories = blocking(context.pool(), move |conn| Category::list_all(conn)).await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Return the jwt
|
2020-01-16 14:39:08 +00:00
|
|
|
Ok(ListCategoriesResponse { categories })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for GetModlog {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetModlogResponse;
|
|
|
|
|
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<GetModlogResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &GetModlog = &self;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let community_id = data.community_id;
|
|
|
|
let mod_user_id = data.mod_user_id;
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
2020-08-18 13:43:50 +00:00
|
|
|
let removed_posts = blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
ModRemovePostView::list(conn, community_id, mod_user_id, page, limit)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let locked_posts = blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
ModLockPostView::list(conn, community_id, mod_user_id, page, limit)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let stickied_posts = blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
ModStickyPostView::list(conn, community_id, mod_user_id, page, limit)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let removed_comments = blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
ModRemoveCommentView::list(conn, community_id, mod_user_id, page, limit)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let banned_from_community = blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
ModBanFromCommunityView::list(conn, community_id, mod_user_id, page, limit)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let added_to_community = blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
ModAddCommunityView::list(conn, community_id, mod_user_id, page, limit)
|
|
|
|
})
|
|
|
|
.await??;
|
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() {
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
Ok((
|
|
|
|
ModRemoveCommunityView::list(conn, mod_user_id, page, limit)?,
|
|
|
|
ModBanView::list(conn, mod_user_id, page, limit)?,
|
|
|
|
ModAddView::list(conn, mod_user_id, page, limit)?,
|
|
|
|
)) as Result<_, LemmyError>
|
|
|
|
})
|
|
|
|
.await??
|
2020-01-02 11:30:00 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for CreateSite {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = SiteResponse;
|
|
|
|
|
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<SiteResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &CreateSite = &self;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-09-16 13:29:51 +00:00
|
|
|
let read_site = move |conn: &'_ _| Site::read(conn, 1);
|
|
|
|
if blocking(context.pool(), read_site).await?.is_ok() {
|
|
|
|
return Err(APIError::err("site_already_exists").into());
|
2020-09-16 01:36:11 +00:00
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_slurs(&data.name)?;
|
|
|
|
check_slurs_opt(&data.description)?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Make sure user is an admin
|
2020-08-18 13:43:50 +00:00
|
|
|
is_admin(context.pool(), user.id).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
let site_form = SiteForm {
|
|
|
|
name: data.name.to_owned(),
|
|
|
|
description: data.description.to_owned(),
|
2020-08-05 16:03:46 +00:00
|
|
|
icon: Some(data.icon.to_owned()),
|
|
|
|
banner: Some(data.banner.to_owned()),
|
2020-08-04 14:39:55 +00:00
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
|
2020-08-18 13:43:50 +00:00
|
|
|
if blocking(context.pool(), create_site).await?.is_err() {
|
2020-07-01 12:54:29 +00:00
|
|
|
return Err(APIError::err("site_already_exists").into());
|
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-01-16 14:39:08 +00:00
|
|
|
Ok(SiteResponse { site: site_view })
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for EditSite {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = SiteResponse;
|
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<SiteResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &EditSite = &self;
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
check_slurs(&data.name)?;
|
|
|
|
check_slurs_opt(&data.description)?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
|
|
|
// Make sure user is an admin
|
2020-08-18 13:43:50 +00:00
|
|
|
is_admin(context.pool(), user.id).await?;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let found_site = blocking(context.pool(), move |conn| Site::read(conn, 1)).await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-05 16:03:46 +00:00
|
|
|
let icon = diesel_option_overwrite(&data.icon);
|
|
|
|
let banner = diesel_option_overwrite(&data.banner);
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
let site_form = SiteForm {
|
|
|
|
name: data.name.to_owned(),
|
|
|
|
description: data.description.to_owned(),
|
2020-08-05 16:03:46 +00:00
|
|
|
icon,
|
|
|
|
banner,
|
2019-05-05 05:20:38 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
|
2020-08-18 13:43:50 +00:00
|
|
|
if blocking(context.pool(), update_site).await?.is_err() {
|
2020-07-01 12:54:29 +00:00
|
|
|
return Err(APIError::err("couldnt_update_site").into());
|
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
let res = SiteResponse { site: site_view };
|
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
context.chat_server().do_send(SendAllMessage {
|
|
|
|
op: UserOperation::EditSite,
|
|
|
|
response: res.clone(),
|
|
|
|
websocket_id,
|
|
|
|
});
|
2020-04-19 22:08:25 +00:00
|
|
|
|
|
|
|
Ok(res)
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for GetSite {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetSiteResponse;
|
|
|
|
|
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<GetSiteResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &GetSite = &self;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-04-19 22:08:25 +00:00
|
|
|
// TODO refactor this a little
|
2020-08-18 13:43:50 +00:00
|
|
|
let res = blocking(context.pool(), move |conn| Site::read(conn, 1)).await?;
|
2020-07-01 12:54:29 +00:00
|
|
|
let site_view = if res.is_ok() {
|
2020-08-18 13:43:50 +00:00
|
|
|
Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
|
2020-03-26 14:23:15 +00:00
|
|
|
} 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-07-29 13:02:46 +00:00
|
|
|
captcha_uuid: None,
|
|
|
|
captcha_answer: None,
|
2020-03-26 14:23:15 +00:00
|
|
|
};
|
2020-08-24 11:58:24 +00:00
|
|
|
let login_response = register.perform(context, websocket_id).await?;
|
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,
|
2020-08-05 16:03:46 +00:00
|
|
|
icon: None,
|
|
|
|
banner: None,
|
2020-05-05 00:04:48 +00:00
|
|
|
enable_downvotes: true,
|
|
|
|
open_registration: true,
|
|
|
|
enable_nsfw: true,
|
2020-03-26 14:23:15 +00:00
|
|
|
auth: login_response.jwt,
|
|
|
|
};
|
2020-08-24 11:58:24 +00:00
|
|
|
create_site.perform(context, websocket_id).await?;
|
2020-03-26 14:23:15 +00:00
|
|
|
info!("Site {} created", setup.site_name);
|
2020-08-18 13:43:50 +00:00
|
|
|
Some(blocking(context.pool(), move |conn| SiteView::read(conn)).await??)
|
2020-03-26 14:23:15 +00:00
|
|
|
} else {
|
|
|
|
None
|
2019-05-05 05:20:38 +00:00
|
|
|
};
|
|
|
|
|
2020-12-04 00:47:58 +00:00
|
|
|
let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
|
2020-04-14 16:24:05 +00:00
|
|
|
|
|
|
|
// Make sure the site creator is the top admin
|
|
|
|
if let Some(site_view) = site_view.to_owned() {
|
2020-12-02 19:32:47 +00:00
|
|
|
let site_creator_id = site_view.creator.id;
|
2020-04-14 16:24:05 +00:00
|
|
|
// TODO investigate why this is sometimes coming back null
|
|
|
|
// Maybe user_.admin isn't being set to true?
|
2020-12-04 00:47:58 +00:00
|
|
|
if let Some(creator_index) = admins.iter().position(|r| r.user.id == site_creator_id) {
|
2020-04-14 16:24:05 +00:00
|
|
|
let creator_user = admins.remove(creator_index);
|
|
|
|
admins.insert(0, creator_user);
|
|
|
|
}
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 00:47:58 +00:00
|
|
|
let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-24 11:58:24 +00:00
|
|
|
let online = context
|
|
|
|
.chat_server()
|
|
|
|
.send(GetUsersOnline)
|
|
|
|
.await
|
|
|
|
.unwrap_or(1);
|
2020-04-19 22:08:25 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let my_user = get_user_from_jwt_opt(&data.auth, context.pool())
|
|
|
|
.await?
|
|
|
|
.map(|mut u| {
|
|
|
|
u.password_encrypted = "".to_string();
|
|
|
|
u.private_key = None;
|
|
|
|
u.public_key = None;
|
|
|
|
u
|
|
|
|
});
|
2020-07-27 13:23:08 +00:00
|
|
|
|
2020-12-03 03:39:31 +00:00
|
|
|
let counts = blocking(context.pool(), move |conn| SiteAggregates::read(conn)).await??;
|
|
|
|
|
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,
|
2020-07-21 13:20:23 +00:00
|
|
|
version: version::VERSION.to_string(),
|
2020-07-27 13:23:08 +00:00
|
|
|
my_user,
|
2020-09-24 13:46:57 +00:00
|
|
|
federated_instances: linked_instances(context.pool()).await?,
|
2020-12-03 03:39:31 +00:00
|
|
|
counts,
|
2019-09-07 15:35:05 +00:00
|
|
|
})
|
2019-05-05 05:20:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for Search {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = SearchResponse;
|
|
|
|
|
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<SearchResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &Search = &self;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
match search_by_apub_id(&data.q, context).await {
|
2020-04-17 13:46:08 +00:00
|
|
|
Ok(r) => return Ok(r),
|
|
|
|
Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
|
|
|
|
}
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
|
2020-08-04 14:39:55 +00:00
|
|
|
let user_id = user.map(|u| u.id);
|
2020-01-20 23:39:45 +00:00
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
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-07-01 12:54:29 +00:00
|
|
|
let q = data.q.to_owned();
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let sort = SortType::from_str(&data.sort)?;
|
|
|
|
let community_id = data.community_id;
|
2020-10-05 00:57:35 +00:00
|
|
|
let community_name = data.community_name.to_owned();
|
2019-05-05 05:20:38 +00:00
|
|
|
match type_ {
|
|
|
|
SearchType::Posts => {
|
2020-08-18 13:43:50 +00:00
|
|
|
posts = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
PostQueryBuilder::create(conn)
|
2020-07-01 12:54:29 +00:00
|
|
|
.sort(&sort)
|
|
|
|
.show_nsfw(true)
|
2020-12-16 18:59:43 +00:00
|
|
|
.community_id(community_id)
|
|
|
|
.community_name(community_name)
|
|
|
|
.my_user_id(user_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.search_term(q)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-05-05 05:20:38 +00:00
|
|
|
SearchType::Comments => {
|
2020-08-18 13:43:50 +00:00
|
|
|
comments = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
CommentQueryBuilder::create(&conn)
|
2020-07-01 12:54:29 +00:00
|
|
|
.sort(&sort)
|
|
|
|
.search_term(q)
|
2020-12-16 18:59:43 +00:00
|
|
|
.my_user_id(user_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-10 17:32:06 +00:00
|
|
|
SearchType::Communities => {
|
2020-08-18 13:43:50 +00:00
|
|
|
communities = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
CommunityQueryBuilder::create(conn)
|
2020-07-01 12:54:29 +00:00
|
|
|
.sort(&sort)
|
|
|
|
.search_term(q)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-10 17:32:06 +00:00
|
|
|
SearchType::Users => {
|
2020-08-18 13:43:50 +00:00
|
|
|
users = blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
UserQueryBuilder::create(conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.search_term(q)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-10 17:32:06 +00:00
|
|
|
SearchType::All => {
|
2020-08-18 13:43:50 +00:00
|
|
|
posts = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
PostQueryBuilder::create(conn)
|
2020-07-01 12:54:29 +00:00
|
|
|
.sort(&sort)
|
|
|
|
.show_nsfw(true)
|
2020-12-16 18:59:43 +00:00
|
|
|
.community_id(community_id)
|
|
|
|
.community_name(community_name)
|
|
|
|
.my_user_id(user_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.search_term(q)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let q = data.q.to_owned();
|
|
|
|
let sort = SortType::from_str(&data.sort)?;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
comments = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
CommentQueryBuilder::create(conn)
|
2020-07-01 12:54:29 +00:00
|
|
|
.sort(&sort)
|
|
|
|
.search_term(q)
|
2020-12-16 18:59:43 +00:00
|
|
|
.my_user_id(user_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let q = data.q.to_owned();
|
|
|
|
let sort = SortType::from_str(&data.sort)?;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
communities = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
CommunityQueryBuilder::create(conn)
|
2020-07-01 12:54:29 +00:00
|
|
|
.sort(&sort)
|
|
|
|
.search_term(q)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let q = data.q.to_owned();
|
|
|
|
let sort = SortType::from_str(&data.sort)?;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
users = blocking(context.pool(), move |conn| {
|
2020-07-01 12:54:29 +00:00
|
|
|
UserQueryBuilder::create(conn)
|
|
|
|
.sort(&sort)
|
|
|
|
.search_term(q)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
2019-09-07 15:35:05 +00:00
|
|
|
}
|
2019-08-22 05:17:15 +00:00
|
|
|
SearchType::Url => {
|
2020-08-18 13:43:50 +00:00
|
|
|
posts = blocking(context.pool(), move |conn| {
|
2020-12-16 18:59:43 +00:00
|
|
|
PostQueryBuilder::create(conn)
|
2020-07-01 12:54:29 +00:00
|
|
|
.sort(&sort)
|
|
|
|
.show_nsfw(true)
|
2020-12-16 18:59:43 +00:00
|
|
|
.community_id(community_id)
|
|
|
|
.community_name(community_name)
|
2020-07-01 12:54:29 +00:00
|
|
|
.url_search(q)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
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
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for TransferSite {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetSiteResponse;
|
|
|
|
|
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<GetSiteResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &TransferSite = &self;
|
2020-08-18 13:43:50 +00:00
|
|
|
let mut user = get_user_from_jwt(&data.auth, context.pool()).await?;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
2020-09-16 01:36:11 +00:00
|
|
|
is_admin(context.pool(), user.id).await?;
|
|
|
|
|
2020-07-27 13:23:08 +00:00
|
|
|
// TODO add a User_::read_safe() for this.
|
|
|
|
user.password_encrypted = "".to_string();
|
|
|
|
user.private_key = None;
|
|
|
|
user.public_key = None;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let read_site = blocking(context.pool(), move |conn| Site::read(conn, 1)).await??;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
|
|
|
// Make sure user is the creator
|
2020-08-04 14:39:55 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-08-05 16:03:46 +00:00
|
|
|
let new_creator_id = data.user_id;
|
|
|
|
let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
|
2020-08-18 13:43:50 +00:00
|
|
|
if blocking(context.pool(), transfer_site).await?.is_err() {
|
2020-07-01 12:54:29 +00:00
|
|
|
return Err(APIError::err("couldnt_update_site").into());
|
2019-08-24 02:40:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModAddForm {
|
2020-08-04 14:39:55 +00:00
|
|
|
mod_user_id: user.id,
|
2019-08-24 02:40:41 +00:00
|
|
|
other_user_id: data.user_id,
|
|
|
|
removed: Some(false),
|
|
|
|
};
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
2020-12-04 00:47:58 +00:00
|
|
|
let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
|
2019-09-07 15:35:05 +00:00
|
|
|
let creator_index = admins
|
|
|
|
.iter()
|
2020-12-04 00:47:58 +00:00
|
|
|
.position(|r| r.user.id == site_view.creator.id)
|
2020-08-13 15:46:31 +00:00
|
|
|
.context(location_info!())?;
|
2019-08-24 02:40:41 +00:00
|
|
|
let creator_user = admins.remove(creator_index);
|
|
|
|
admins.insert(0, creator_user);
|
|
|
|
|
2020-12-04 00:47:58 +00:00
|
|
|
let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
2020-12-03 03:39:31 +00:00
|
|
|
let counts = blocking(context.pool(), move |conn| SiteAggregates::read(conn)).await??;
|
|
|
|
|
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,
|
2020-07-21 13:20:23 +00:00
|
|
|
version: version::VERSION.to_string(),
|
2020-07-27 13:23:08 +00:00
|
|
|
my_user: Some(user),
|
2020-09-24 13:46:57 +00:00
|
|
|
federated_instances: linked_instances(context.pool()).await?,
|
2020-12-03 03:39:31 +00:00
|
|
|
counts,
|
2019-09-07 15:35:05 +00:00
|
|
|
})
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-10 20:55:57 +00:00
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for GetSiteConfig {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetSiteConfigResponse;
|
|
|
|
|
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<GetSiteConfigResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &GetSiteConfig = &self;
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
|
2020-04-10 20:55:57 +00:00
|
|
|
|
|
|
|
// Only let admins read this
|
2020-08-18 13:43:50 +00:00
|
|
|
is_admin(context.pool(), user.id).await?;
|
2020-04-10 20:55:57 +00:00
|
|
|
|
|
|
|
let config_hjson = Settings::read_config_file()?;
|
|
|
|
|
|
|
|
Ok(GetSiteConfigResponse { config_hjson })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2020-08-12 11:31:45 +00:00
|
|
|
impl Perform for SaveSiteConfig {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetSiteConfigResponse;
|
|
|
|
|
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<GetSiteConfigResponse, LemmyError> {
|
2020-08-12 11:31:45 +00:00
|
|
|
let data: &SaveSiteConfig = &self;
|
2020-08-18 13:43:50 +00:00
|
|
|
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
|
2020-04-10 20:55:57 +00:00
|
|
|
|
|
|
|
// Only let admins read this
|
2020-12-04 00:47:58 +00:00
|
|
|
let admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
|
|
|
|
let admin_ids: Vec<i32> = admins.into_iter().map(|m| m.user.id).collect();
|
2020-04-10 20:55:57 +00:00
|
|
|
|
2020-08-04 14:39:55 +00:00
|
|
|
if !admin_ids.contains(&user.id) {
|
2020-04-10 20:55:57 +00:00
|
|
|
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 })
|
|
|
|
}
|
|
|
|
}
|