2 use actix_web::web::Data;
4 use lemmy_api_common::{
6 build_federated_instances,
7 get_local_user_settings_view_from_jwt,
8 get_local_user_view_from_jwt,
9 get_local_user_view_from_jwt_opt,
12 user_show_bot_accounts,
15 use lemmy_apub::fetcher::search::search_by_apub_id;
16 use lemmy_db_queries::{source::site::Site_, Crud, SearchType, SortType};
17 use lemmy_db_schema::source::{moderator::*, site::Site};
19 comment_view::CommentQueryBuilder,
20 post_view::PostQueryBuilder,
23 use lemmy_db_views_actor::{
24 community_view::CommunityQueryBuilder,
25 person_view::{PersonQueryBuilder, PersonViewSafe},
27 use lemmy_db_views_moderator::{
28 mod_add_community_view::ModAddCommunityView,
29 mod_add_view::ModAddView,
30 mod_ban_from_community_view::ModBanFromCommunityView,
31 mod_ban_view::ModBanView,
32 mod_lock_post_view::ModLockPostView,
33 mod_remove_comment_view::ModRemoveCommentView,
34 mod_remove_community_view::ModRemoveCommunityView,
35 mod_remove_post_view::ModRemovePostView,
36 mod_sticky_post_view::ModStickyPostView,
40 settings::structs::Settings,
46 use lemmy_websocket::LemmyContext;
48 use std::str::FromStr;
50 #[async_trait::async_trait(?Send)]
51 impl Perform for GetModlog {
52 type Response = GetModlogResponse;
56 context: &Data<LemmyContext>,
57 _websocket_id: Option<ConnectionId>,
58 ) -> Result<GetModlogResponse, LemmyError> {
59 let data: &GetModlog = &self;
61 let community_id = data.community_id;
62 let mod_person_id = data.mod_person_id;
64 let limit = data.limit;
65 let removed_posts = blocking(context.pool(), move |conn| {
66 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
70 let locked_posts = blocking(context.pool(), move |conn| {
71 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
75 let stickied_posts = blocking(context.pool(), move |conn| {
76 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
80 let removed_comments = blocking(context.pool(), move |conn| {
81 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
85 let banned_from_community = blocking(context.pool(), move |conn| {
86 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
90 let added_to_community = blocking(context.pool(), move |conn| {
91 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
95 // These arrays are only for the full modlog, when a community isn't given
96 let (removed_communities, banned, added) = if data.community_id.is_none() {
97 blocking(context.pool(), move |conn| {
99 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
100 ModBanView::list(conn, mod_person_id, page, limit)?,
101 ModAddView::list(conn, mod_person_id, page, limit)?,
102 )) as Result<_, LemmyError>
106 (Vec::new(), Vec::new(), Vec::new())
110 Ok(GetModlogResponse {
116 banned_from_community,
124 #[async_trait::async_trait(?Send)]
125 impl Perform for Search {
126 type Response = SearchResponse;
130 context: &Data<LemmyContext>,
131 _websocket_id: Option<ConnectionId>,
132 ) -> Result<SearchResponse, LemmyError> {
133 let data: &Search = &self;
135 match search_by_apub_id(&data.q, context).await {
136 Ok(r) => return Ok(r),
137 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
140 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
142 let show_nsfw = user_show_nsfw(&local_user_view);
143 let show_bot_accounts = user_show_bot_accounts(&local_user_view);
145 let person_id = local_user_view.map(|u| u.person.id);
147 let type_ = SearchType::from_str(&data.type_)?;
149 let mut posts = Vec::new();
150 let mut comments = Vec::new();
151 let mut communities = Vec::new();
152 let mut users = Vec::new();
154 // TODO no clean / non-nsfw searching rn
156 let q = data.q.to_owned();
157 let page = data.page;
158 let limit = data.limit;
159 let sort = SortType::from_str(&data.sort)?;
160 let community_id = data.community_id;
161 let community_name = data.community_name.to_owned();
163 SearchType::Posts => {
164 posts = blocking(context.pool(), move |conn| {
165 PostQueryBuilder::create(conn)
167 .show_nsfw(show_nsfw)
168 .show_bot_accounts(show_bot_accounts)
169 .community_id(community_id)
170 .community_name(community_name)
171 .my_person_id(person_id)
179 SearchType::Comments => {
180 comments = blocking(context.pool(), move |conn| {
181 CommentQueryBuilder::create(&conn)
184 .show_bot_accounts(show_bot_accounts)
185 .my_person_id(person_id)
192 SearchType::Communities => {
193 communities = blocking(context.pool(), move |conn| {
194 CommunityQueryBuilder::create(conn)
197 .my_person_id(person_id)
204 SearchType::Users => {
205 users = blocking(context.pool(), move |conn| {
206 PersonQueryBuilder::create(conn)
216 posts = blocking(context.pool(), move |conn| {
217 PostQueryBuilder::create(conn)
219 .show_nsfw(show_nsfw)
220 .show_bot_accounts(show_bot_accounts)
221 .community_id(community_id)
222 .community_name(community_name)
223 .my_person_id(person_id)
231 let q = data.q.to_owned();
232 let sort = SortType::from_str(&data.sort)?;
234 comments = blocking(context.pool(), move |conn| {
235 CommentQueryBuilder::create(conn)
238 .show_bot_accounts(show_bot_accounts)
239 .my_person_id(person_id)
246 let q = data.q.to_owned();
247 let sort = SortType::from_str(&data.sort)?;
249 communities = blocking(context.pool(), move |conn| {
250 CommunityQueryBuilder::create(conn)
253 .my_person_id(person_id)
260 let q = data.q.to_owned();
261 let sort = SortType::from_str(&data.sort)?;
263 users = blocking(context.pool(), move |conn| {
264 PersonQueryBuilder::create(conn)
274 posts = blocking(context.pool(), move |conn| {
275 PostQueryBuilder::create(conn)
277 .show_nsfw(show_nsfw)
278 .show_bot_accounts(show_bot_accounts)
279 .my_person_id(person_id)
280 .community_id(community_id)
281 .community_name(community_name)
293 type_: data.type_.to_owned(),
302 #[async_trait::async_trait(?Send)]
303 impl Perform for TransferSite {
304 type Response = GetSiteResponse;
308 context: &Data<LemmyContext>,
309 _websocket_id: Option<ConnectionId>,
310 ) -> Result<GetSiteResponse, LemmyError> {
311 let data: &TransferSite = &self;
312 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
314 is_admin(&local_user_view)?;
316 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
318 // Make sure user is the creator
319 if read_site.creator_id != local_user_view.person.id {
320 return Err(ApiError::err("not_an_admin").into());
323 let new_creator_id = data.person_id;
324 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
325 if blocking(context.pool(), transfer_site).await?.is_err() {
326 return Err(ApiError::err("couldnt_update_site").into());
330 let form = ModAddForm {
331 mod_person_id: local_user_view.person.id,
332 other_person_id: data.person_id,
333 removed: Some(false),
336 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
338 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
340 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
341 let creator_index = admins
343 .position(|r| r.person.id == site_view.creator.id)
344 .context(location_info!())?;
345 let creator_person = admins.remove(creator_index);
346 admins.insert(0, creator_person);
348 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
349 let federated_instances = build_federated_instances(context.pool()).await?;
351 let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
354 site_view: Some(site_view),
358 version: version::VERSION.to_string(),
365 #[async_trait::async_trait(?Send)]
366 impl Perform for GetSiteConfig {
367 type Response = GetSiteConfigResponse;
371 context: &Data<LemmyContext>,
372 _websocket_id: Option<ConnectionId>,
373 ) -> Result<GetSiteConfigResponse, LemmyError> {
374 let data: &GetSiteConfig = &self;
375 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
377 // Only let admins read this
378 is_admin(&local_user_view)?;
380 let config_hjson = Settings::read_config_file()?;
382 Ok(GetSiteConfigResponse { config_hjson })
386 #[async_trait::async_trait(?Send)]
387 impl Perform for SaveSiteConfig {
388 type Response = GetSiteConfigResponse;
392 context: &Data<LemmyContext>,
393 _websocket_id: Option<ConnectionId>,
394 ) -> Result<GetSiteConfigResponse, LemmyError> {
395 let data: &SaveSiteConfig = &self;
396 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
398 // Only let admins read this
399 is_admin(&local_user_view)?;
401 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
402 let config_hjson = Settings::save_config_file(&data.config_hjson)
403 .map_err(|_| ApiError::err("couldnt_update_site"))?;
405 Ok(GetSiteConfigResponse { config_hjson })