2021-03-25 19:19:40 +00:00
|
|
|
use crate::Perform;
|
|
|
|
use actix_web::web::Data;
|
2021-09-25 15:44:52 +00:00
|
|
|
use diesel::NotFound;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
blocking,
|
2021-02-01 18:11:37 +00:00
|
|
|
build_federated_instances,
|
2021-12-15 19:49:59 +00:00
|
|
|
check_private_instance,
|
2021-03-10 22:33:55 +00:00
|
|
|
get_local_user_view_from_jwt,
|
|
|
|
get_local_user_view_from_jwt_opt,
|
2020-09-24 14:14:09 +00:00
|
|
|
is_admin,
|
2022-01-27 16:39:22 +00:00
|
|
|
resolve_actor_identifier,
|
2021-12-15 19:49:59 +00:00
|
|
|
send_application_approved_email,
|
2021-03-25 19:19:40 +00:00
|
|
|
site::*,
|
2020-05-16 14:04:08 +00:00
|
|
|
};
|
2022-01-27 16:39:22 +00:00
|
|
|
use lemmy_apub::fetcher::search::{search_by_apub_id, SearchableObjects};
|
2021-10-16 13:33:38 +00:00
|
|
|
use lemmy_db_schema::{
|
2021-12-15 19:49:59 +00:00
|
|
|
diesel_option_overwrite,
|
2021-04-15 03:37:51 +00:00
|
|
|
from_opt_str_to_opt_enum,
|
2021-10-16 13:33:38 +00:00
|
|
|
newtypes::PersonId,
|
2021-12-15 19:49:59 +00:00
|
|
|
source::{
|
2022-01-27 16:39:22 +00:00
|
|
|
community::Community,
|
2021-12-15 19:49:59 +00:00
|
|
|
local_user::{LocalUser, LocalUserForm},
|
|
|
|
moderator::*,
|
2022-01-26 17:57:16 +00:00
|
|
|
person::Person,
|
2021-12-15 19:49:59 +00:00
|
|
|
registration_application::{RegistrationApplication, RegistrationApplicationForm},
|
|
|
|
site::Site,
|
|
|
|
},
|
2021-10-16 13:33:38 +00:00
|
|
|
traits::{Crud, DeleteableOrRemoveable},
|
2021-09-25 15:44:52 +00:00
|
|
|
DbPool,
|
2021-04-15 03:37:51 +00:00
|
|
|
ListingType,
|
|
|
|
SearchType,
|
|
|
|
SortType,
|
|
|
|
};
|
2020-12-21 16:30:34 +00:00
|
|
|
use lemmy_db_views::{
|
2021-09-25 15:44:52 +00:00
|
|
|
comment_view::{CommentQueryBuilder, CommentView},
|
2021-12-15 19:49:59 +00:00
|
|
|
local_user_view::LocalUserView,
|
2021-09-25 15:44:52 +00:00
|
|
|
post_view::{PostQueryBuilder, PostView},
|
2021-12-15 19:49:59 +00:00
|
|
|
registration_application_view::{
|
|
|
|
RegistrationApplicationQueryBuilder,
|
|
|
|
RegistrationApplicationView,
|
|
|
|
},
|
2020-12-21 16:30:34 +00:00
|
|
|
site_view::SiteView,
|
2020-12-21 23:27:42 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_views_actor::{
|
2021-09-25 15:44:52 +00:00
|
|
|
community_view::{CommunityQueryBuilder, CommunityView},
|
2021-03-10 22:33:55 +00:00
|
|
|
person_view::{PersonQueryBuilder, PersonViewSafe},
|
2020-12-21 16:30:34 +00:00
|
|
|
};
|
2020-12-21 23:27:42 +00:00
|
|
|
use lemmy_db_views_moderator::{
|
|
|
|
mod_add_community_view::ModAddCommunityView,
|
|
|
|
mod_add_view::ModAddView,
|
|
|
|
mod_ban_from_community_view::ModBanFromCommunityView,
|
|
|
|
mod_ban_view::ModBanView,
|
2022-02-18 02:30:47 +00:00
|
|
|
mod_hide_community_view::ModHideCommunityView,
|
2020-12-21 23:27:42 +00:00
|
|
|
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,
|
2021-08-17 21:52:28 +00:00
|
|
|
mod_transfer_community_view::ModTransferCommunityView,
|
2020-12-21 23:27:42 +00:00
|
|
|
};
|
2022-01-26 17:57:16 +00:00
|
|
|
use lemmy_utils::{settings::structs::Settings, version, ConnectionId, LemmyError};
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_websocket::LemmyContext;
|
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;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
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> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetModlog = self;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-12-15 19:49:59 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
check_private_instance(&local_user_view, context.pool()).await?;
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
let community_id = data.community_id;
|
2021-03-10 22:33:55 +00:00
|
|
|
let mod_person_id = data.mod_person_id;
|
2020-07-01 12:54:29 +00:00
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
2020-08-18 13:43:50 +00:00
|
|
|
let removed_posts = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let locked_posts = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let stickied_posts = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let removed_comments = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let banned_from_community = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2020-08-18 13:43:50 +00:00
|
|
|
let added_to_community = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
|
2020-07-01 12:54:29 +00:00
|
|
|
})
|
|
|
|
.await??;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-08-17 21:52:28 +00:00
|
|
|
let transferred_to_community = blocking(context.pool(), move |conn| {
|
|
|
|
ModTransferCommunityView::list(conn, community_id, mod_person_id, page, limit)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2022-02-18 02:30:47 +00:00
|
|
|
let hidden_communities = blocking(context.pool(), move |conn| {
|
|
|
|
ModHideCommunityView::list(conn, community_id, mod_person_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((
|
2021-03-10 22:33:55 +00:00
|
|
|
ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
|
|
|
|
ModBanView::list(conn, mod_person_id, page, limit)?,
|
|
|
|
ModAddView::list(conn, mod_person_id, page, limit)?,
|
2020-07-01 12:54:29 +00:00
|
|
|
)) 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,
|
2021-08-17 21:52:28 +00:00
|
|
|
transferred_to_community,
|
2022-02-18 02:30:47 +00:00
|
|
|
hidden_communities,
|
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;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
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> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &Search = self;
|
2019-05-05 05:20:38 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
2021-12-06 14:54:47 +00:00
|
|
|
get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
|
|
|
|
.await?;
|
2021-04-21 21:41:14 +00:00
|
|
|
|
2021-12-15 19:49:59 +00:00
|
|
|
check_private_instance(&local_user_view, context.pool()).await?;
|
|
|
|
|
2021-04-26 14:44:19 +00:00
|
|
|
let show_nsfw = local_user_view.as_ref().map(|t| t.local_user.show_nsfw);
|
|
|
|
let show_bot_accounts = local_user_view
|
|
|
|
.as_ref()
|
|
|
|
.map(|t| t.local_user.show_bot_accounts);
|
|
|
|
let show_read_posts = local_user_view
|
|
|
|
.as_ref()
|
|
|
|
.map(|t| t.local_user.show_read_posts);
|
2021-04-21 21:41:14 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
let person_id = local_user_view.map(|u| u.person.id);
|
2020-01-20 23:39:45 +00:00
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
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;
|
2021-04-15 03:37:51 +00:00
|
|
|
let sort: Option<SortType> = from_opt_str_to_opt_enum(&data.sort);
|
|
|
|
let listing_type: Option<ListingType> = from_opt_str_to_opt_enum(&data.listing_type);
|
|
|
|
let search_type: SearchType = from_opt_str_to_opt_enum(&data.type_).unwrap_or(SearchType::All);
|
2020-07-01 12:54:29 +00:00
|
|
|
let community_id = data.community_id;
|
2021-10-25 16:09:21 +00:00
|
|
|
let community_actor_id = if let Some(name) = &data.community_name {
|
2022-01-27 16:39:22 +00:00
|
|
|
resolve_actor_identifier::<Community>(name, context.pool())
|
2021-10-25 16:09:21 +00:00
|
|
|
.await
|
|
|
|
.ok()
|
2022-01-27 16:39:22 +00:00
|
|
|
.map(|c| c.actor_id)
|
2021-10-25 16:09:21 +00:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2021-04-09 20:09:58 +00:00
|
|
|
let creator_id = data.creator_id;
|
2021-04-15 03:37:51 +00:00
|
|
|
match search_type {
|
2019-05-05 05:20:38 +00:00
|
|
|
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)
|
2021-04-15 03:37:51 +00:00
|
|
|
.sort(sort)
|
2021-04-21 21:41:14 +00:00
|
|
|
.show_nsfw(show_nsfw)
|
|
|
|
.show_bot_accounts(show_bot_accounts)
|
2021-04-24 22:26:50 +00:00
|
|
|
.show_read_posts(show_read_posts)
|
2021-04-15 03:37:51 +00:00
|
|
|
.listing_type(listing_type)
|
2020-12-16 18:59:43 +00:00
|
|
|
.community_id(community_id)
|
2021-07-20 04:29:50 +00:00
|
|
|
.community_actor_id(community_actor_id)
|
2021-04-09 20:09:58 +00:00
|
|
|
.creator_id(creator_id)
|
2021-03-10 22:33:55 +00:00
|
|
|
.my_person_id(person_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| {
|
2021-07-05 16:07:26 +00:00
|
|
|
CommentQueryBuilder::create(conn)
|
2021-04-15 03:37:51 +00:00
|
|
|
.sort(sort)
|
|
|
|
.listing_type(listing_type)
|
2020-07-01 12:54:29 +00:00
|
|
|
.search_term(q)
|
2021-04-21 21:41:14 +00:00
|
|
|
.show_bot_accounts(show_bot_accounts)
|
2021-04-09 20:09:58 +00:00
|
|
|
.community_id(community_id)
|
2021-07-20 04:29:50 +00:00
|
|
|
.community_actor_id(community_actor_id)
|
2021-04-09 20:09:58 +00:00
|
|
|
.creator_id(creator_id)
|
2021-03-10 22:33:55 +00:00
|
|
|
.my_person_id(person_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)
|
2021-04-15 03:37:51 +00:00
|
|
|
.sort(sort)
|
|
|
|
.listing_type(listing_type)
|
2020-07-01 12:54:29 +00:00
|
|
|
.search_term(q)
|
2021-03-10 22:33:55 +00:00
|
|
|
.my_person_id(person_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::Users => {
|
2020-08-18 13:43:50 +00:00
|
|
|
users = blocking(context.pool(), move |conn| {
|
2021-03-10 22:33:55 +00:00
|
|
|
PersonQueryBuilder::create(conn)
|
2021-04-15 03:37:51 +00:00
|
|
|
.sort(sort)
|
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-08-10 17:32:06 +00:00
|
|
|
SearchType::All => {
|
2021-04-23 15:54:38 +00:00
|
|
|
// If the community or creator is included, dont search communities or users
|
|
|
|
let community_or_creator_included =
|
|
|
|
data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some();
|
2021-07-20 04:29:50 +00:00
|
|
|
let community_actor_id_2 = community_actor_id.to_owned();
|
2021-04-23 06:09:47 +00:00
|
|
|
|
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)
|
2021-04-15 03:37:51 +00:00
|
|
|
.sort(sort)
|
2021-04-21 21:41:14 +00:00
|
|
|
.show_nsfw(show_nsfw)
|
|
|
|
.show_bot_accounts(show_bot_accounts)
|
2021-04-24 22:26:50 +00:00
|
|
|
.show_read_posts(show_read_posts)
|
2021-04-15 03:37:51 +00:00
|
|
|
.listing_type(listing_type)
|
2020-12-16 18:59:43 +00:00
|
|
|
.community_id(community_id)
|
2021-07-20 04:29:50 +00:00
|
|
|
.community_actor_id(community_actor_id_2)
|
2021-04-09 20:09:58 +00:00
|
|
|
.creator_id(creator_id)
|
2021-03-10 22:33:55 +00:00
|
|
|
.my_person_id(person_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.search_term(q)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let q = data.q.to_owned();
|
2021-07-20 04:29:50 +00:00
|
|
|
let community_actor_id = community_actor_id.to_owned();
|
2020-07-01 12:54:29 +00:00
|
|
|
|
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)
|
2021-04-15 03:37:51 +00:00
|
|
|
.sort(sort)
|
|
|
|
.listing_type(listing_type)
|
2020-07-01 12:54:29 +00:00
|
|
|
.search_term(q)
|
2021-04-21 21:41:14 +00:00
|
|
|
.show_bot_accounts(show_bot_accounts)
|
2021-04-09 20:09:58 +00:00
|
|
|
.community_id(community_id)
|
2021-07-20 04:29:50 +00:00
|
|
|
.community_actor_id(community_actor_id)
|
2021-04-09 20:09:58 +00:00
|
|
|
.creator_id(creator_id)
|
2021-03-10 22:33:55 +00:00
|
|
|
.my_person_id(person_id)
|
2020-07-01 12:54:29 +00:00
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let q = data.q.to_owned();
|
|
|
|
|
2021-04-23 15:54:38 +00:00
|
|
|
communities = if community_or_creator_included {
|
2021-04-23 06:09:47 +00:00
|
|
|
vec![]
|
|
|
|
} else {
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
CommunityQueryBuilder::create(conn)
|
|
|
|
.sort(sort)
|
|
|
|
.listing_type(listing_type)
|
|
|
|
.search_term(q)
|
|
|
|
.my_person_id(person_id)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??
|
|
|
|
};
|
2020-07-01 12:54:29 +00:00
|
|
|
|
|
|
|
let q = data.q.to_owned();
|
|
|
|
|
2021-04-23 15:54:38 +00:00
|
|
|
users = if community_or_creator_included {
|
2021-04-23 06:09:47 +00:00
|
|
|
vec![]
|
|
|
|
} else {
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
PersonQueryBuilder::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)
|
2021-04-15 03:37:51 +00:00
|
|
|
.sort(sort)
|
2021-04-21 21:41:14 +00:00
|
|
|
.show_nsfw(show_nsfw)
|
|
|
|
.show_bot_accounts(show_bot_accounts)
|
2021-04-24 22:26:50 +00:00
|
|
|
.show_read_posts(show_read_posts)
|
2021-04-15 03:37:51 +00:00
|
|
|
.listing_type(listing_type)
|
2021-03-10 22:33:55 +00:00
|
|
|
.my_person_id(person_id)
|
2020-12-16 18:59:43 +00:00
|
|
|
.community_id(community_id)
|
2021-07-20 04:29:50 +00:00
|
|
|
.community_actor_id(community_actor_id)
|
2021-04-09 20:09:58 +00:00
|
|
|
.creator_id(creator_id)
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-27 13:34:18 +00:00
|
|
|
// Blank out deleted or removed info for non logged in users
|
|
|
|
if person_id.is_none() {
|
|
|
|
for cv in communities
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|cv| cv.community.deleted || cv.community.removed)
|
|
|
|
{
|
|
|
|
cv.community = cv.to_owned().community.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
2021-07-30 18:44:15 +00:00
|
|
|
|
2021-10-27 13:34:18 +00:00
|
|
|
for pv in posts
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|p| p.post.deleted || p.post.removed)
|
|
|
|
{
|
|
|
|
pv.post = pv.to_owned().post.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
2021-07-30 18:44:15 +00:00
|
|
|
|
2021-10-27 13:34:18 +00:00
|
|
|
for cv in comments
|
|
|
|
.iter_mut()
|
|
|
|
.filter(|cv| cv.comment.deleted || cv.comment.removed)
|
|
|
|
{
|
|
|
|
cv.comment = cv.to_owned().comment.blank_out_deleted_or_removed_info();
|
|
|
|
}
|
2021-07-30 18:44:15 +00:00
|
|
|
}
|
|
|
|
|
2019-05-05 05:20:38 +00:00
|
|
|
// Return the jwt
|
2019-09-07 15:35:05 +00:00
|
|
|
Ok(SearchResponse {
|
2021-04-15 03:37:51 +00:00
|
|
|
type_: search_type.to_string(),
|
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
|
|
|
|
2021-07-20 07:00:20 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for ResolveObject {
|
|
|
|
type Response = ResolveObjectResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
2021-07-20 07:00:20 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<ResolveObjectResponse, LemmyError> {
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
2021-12-06 14:54:47 +00:00
|
|
|
get_local_user_view_from_jwt_opt(self.auth.as_ref(), context.pool(), context.secret())
|
|
|
|
.await?;
|
2021-12-15 19:49:59 +00:00
|
|
|
check_private_instance(&local_user_view, context.pool()).await?;
|
|
|
|
|
2021-09-25 15:44:52 +00:00
|
|
|
let res = search_by_apub_id(&self.q, context)
|
2021-08-23 15:25:39 +00:00
|
|
|
.await
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_find_object"))?;
|
2021-09-25 15:44:52 +00:00
|
|
|
convert_response(res, local_user_view.map(|l| l.person.id), context.pool())
|
|
|
|
.await
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_find_object"))
|
2021-09-25 15:44:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn convert_response(
|
|
|
|
object: SearchableObjects,
|
|
|
|
user_id: Option<PersonId>,
|
|
|
|
pool: &DbPool,
|
|
|
|
) -> Result<ResolveObjectResponse, LemmyError> {
|
|
|
|
let removed_or_deleted;
|
|
|
|
let mut res = ResolveObjectResponse {
|
|
|
|
comment: None,
|
|
|
|
post: None,
|
|
|
|
community: None,
|
|
|
|
person: None,
|
|
|
|
};
|
|
|
|
use SearchableObjects::*;
|
|
|
|
match object {
|
|
|
|
Person(p) => {
|
|
|
|
removed_or_deleted = p.deleted;
|
|
|
|
res.person = Some(blocking(pool, move |conn| PersonViewSafe::read(conn, p.id)).await??)
|
|
|
|
}
|
|
|
|
Community(c) => {
|
|
|
|
removed_or_deleted = c.deleted || c.removed;
|
|
|
|
res.community =
|
|
|
|
Some(blocking(pool, move |conn| CommunityView::read(conn, c.id, user_id)).await??)
|
|
|
|
}
|
|
|
|
Post(p) => {
|
|
|
|
removed_or_deleted = p.deleted || p.removed;
|
|
|
|
res.post = Some(blocking(pool, move |conn| PostView::read(conn, p.id, user_id)).await??)
|
|
|
|
}
|
|
|
|
Comment(c) => {
|
|
|
|
removed_or_deleted = c.deleted || c.removed;
|
|
|
|
res.comment = Some(blocking(pool, move |conn| CommentView::read(conn, c.id, user_id)).await??)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// if the object was deleted from database, dont return it
|
|
|
|
if removed_or_deleted {
|
|
|
|
return Err(NotFound {}.into());
|
2021-07-20 07:00:20 +00:00
|
|
|
}
|
2021-09-25 15:44:52 +00:00
|
|
|
Ok(res)
|
2021-07-20 07:00:20 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 12:54:29 +00:00
|
|
|
#[async_trait::async_trait(?Send)]
|
2022-01-26 17:57:16 +00:00
|
|
|
impl Perform for LeaveAdmin {
|
2020-04-20 03:59:07 +00:00
|
|
|
type Response = GetSiteResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
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> {
|
2022-01-26 17:57:16 +00:00
|
|
|
let data: &LeaveAdmin = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
2021-03-10 22:33:55 +00:00
|
|
|
is_admin(&local_user_view)?;
|
2020-09-16 01:36:11 +00:00
|
|
|
|
2022-01-26 17:57:16 +00:00
|
|
|
// Make sure there isn't just one admin (so if one leaves, there will still be one left)
|
|
|
|
let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
|
|
|
|
if admins.len() == 1 {
|
|
|
|
return Err(LemmyError::from_message("cannot_leave_admin"));
|
2019-08-24 02:40:41 +00:00
|
|
|
}
|
|
|
|
|
2022-01-26 17:57:16 +00:00
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
Person::leave_admin(conn, person_id)
|
|
|
|
})
|
|
|
|
.await??;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
|
|
|
// Mod tables
|
|
|
|
let form = ModAddForm {
|
2022-01-26 17:57:16 +00:00
|
|
|
mod_person_id: person_id,
|
|
|
|
other_person_id: person_id,
|
|
|
|
removed: Some(true),
|
2019-08-24 02:40:41 +00:00
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2022-01-26 17:57:16 +00:00
|
|
|
// Reread site and admins
|
2021-10-12 16:46:26 +00:00
|
|
|
let site_view = blocking(context.pool(), SiteView::read).await??;
|
2022-01-26 17:57:16 +00:00
|
|
|
let admins = blocking(context.pool(), PersonViewSafe::admins).await??;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
2021-09-22 15:57:09 +00:00
|
|
|
let federated_instances = build_federated_instances(
|
|
|
|
context.pool(),
|
|
|
|
&context.settings().federation,
|
|
|
|
&context.settings().hostname,
|
|
|
|
)
|
|
|
|
.await?;
|
2019-08-24 02:40:41 +00:00
|
|
|
|
2019-09-07 15:35:05 +00:00
|
|
|
Ok(GetSiteResponse {
|
2020-12-20 01:10:47 +00:00
|
|
|
site_view: Some(site_view),
|
2019-12-09 19:08:19 +00:00
|
|
|
admins,
|
2019-10-13 19:06:18 +00:00
|
|
|
online: 0,
|
2020-07-21 13:20:23 +00:00
|
|
|
version: version::VERSION.to_string(),
|
2021-08-19 20:54:15 +00:00
|
|
|
my_user: None,
|
2021-02-01 18:11:37 +00:00
|
|
|
federated_instances,
|
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;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
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> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &GetSiteConfig = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2020-04-10 20:55:57 +00:00
|
|
|
|
|
|
|
// Only let admins read this
|
2021-03-10 22:33:55 +00:00
|
|
|
is_admin(&local_user_view)?;
|
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;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(context, _websocket_id))]
|
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> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &SaveSiteConfig = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2020-04-10 20:55:57 +00:00
|
|
|
|
|
|
|
// Only let admins read this
|
2021-03-10 22:33:55 +00:00
|
|
|
is_admin(&local_user_view)?;
|
2020-04-10 20:55:57 +00:00
|
|
|
|
|
|
|
// Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
|
2021-04-16 13:10:43 +00:00
|
|
|
let config_hjson = Settings::save_config_file(&data.config_hjson)
|
2021-12-06 14:54:47 +00:00
|
|
|
.map_err(LemmyError::from)
|
|
|
|
.map_err(|e| e.with_message("couldnt_update_site"))?;
|
2020-04-10 20:55:57 +00:00
|
|
|
|
|
|
|
Ok(GetSiteConfigResponse { config_hjson })
|
|
|
|
}
|
|
|
|
}
|
2021-12-15 19:49:59 +00:00
|
|
|
|
|
|
|
/// Lists registration applications, filterable by undenied only.
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for ListRegistrationApplications {
|
|
|
|
type Response = ListRegistrationApplicationsResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<Self::Response, LemmyError> {
|
|
|
|
let data = self;
|
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
|
|
|
|
|
|
|
// Make sure user is an admin
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
|
|
|
let unread_only = data.unread_only;
|
2022-02-07 19:23:12 +00:00
|
|
|
let verified_email_only = blocking(context.pool(), Site::read_local_site)
|
2021-12-15 19:49:59 +00:00
|
|
|
.await??
|
|
|
|
.require_email_verification;
|
|
|
|
|
|
|
|
let page = data.page;
|
|
|
|
let limit = data.limit;
|
|
|
|
let registration_applications = blocking(context.pool(), move |conn| {
|
|
|
|
RegistrationApplicationQueryBuilder::create(conn)
|
|
|
|
.unread_only(unread_only)
|
|
|
|
.verified_email_only(verified_email_only)
|
|
|
|
.page(page)
|
|
|
|
.limit(limit)
|
|
|
|
.list()
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
let res = Self::Response {
|
|
|
|
registration_applications,
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for ApproveRegistrationApplication {
|
|
|
|
type Response = RegistrationApplicationResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<Self::Response, LemmyError> {
|
|
|
|
let data = self;
|
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
|
|
|
|
|
|
|
let app_id = data.id;
|
|
|
|
|
|
|
|
// Only let admins do this
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
|
|
|
// Update the registration with reason, admin_id
|
|
|
|
let deny_reason = diesel_option_overwrite(&data.deny_reason);
|
|
|
|
let app_form = RegistrationApplicationForm {
|
|
|
|
admin_id: Some(local_user_view.person.id),
|
|
|
|
deny_reason,
|
|
|
|
..RegistrationApplicationForm::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let registration_application = blocking(context.pool(), move |conn| {
|
|
|
|
RegistrationApplication::update(conn, app_id, &app_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
// Update the local_user row
|
|
|
|
let local_user_form = LocalUserForm {
|
|
|
|
accepted_application: Some(data.approve),
|
|
|
|
..LocalUserForm::default()
|
|
|
|
};
|
|
|
|
|
|
|
|
let approved_user_id = registration_application.local_user_id;
|
|
|
|
blocking(context.pool(), move |conn| {
|
|
|
|
LocalUser::update(conn, approved_user_id, &local_user_form)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
if data.approve {
|
|
|
|
let approved_local_user_view = blocking(context.pool(), move |conn| {
|
|
|
|
LocalUserView::read(conn, approved_user_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
if approved_local_user_view.local_user.email.is_some() {
|
|
|
|
send_application_approved_email(&approved_local_user_view, &context.settings())?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read the view
|
|
|
|
let registration_application = blocking(context.pool(), move |conn| {
|
|
|
|
RegistrationApplicationView::read(conn, app_id)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
Ok(Self::Response {
|
|
|
|
registration_application,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for GetUnreadRegistrationApplicationCount {
|
|
|
|
type Response = GetUnreadRegistrationApplicationCountResponse;
|
|
|
|
|
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
_websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<Self::Response, LemmyError> {
|
|
|
|
let data = self;
|
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
|
|
|
|
|
|
|
// Only let admins do this
|
|
|
|
is_admin(&local_user_view)?;
|
|
|
|
|
2022-02-07 19:23:12 +00:00
|
|
|
let verified_email_only = blocking(context.pool(), Site::read_local_site)
|
2021-12-15 19:49:59 +00:00
|
|
|
.await??
|
|
|
|
.require_email_verification;
|
|
|
|
|
|
|
|
let registration_applications = blocking(context.pool(), move |conn| {
|
|
|
|
RegistrationApplicationView::get_unread_count(conn, verified_email_only)
|
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
|
|
|
Ok(Self::Response {
|
|
|
|
registration_applications,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|