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();
162 let community_name_2 = data.community_name.to_owned();
163 let creator_id = data.creator_id;
165 SearchType::Posts => {
166 posts = blocking(context.pool(), move |conn| {
167 PostQueryBuilder::create(conn)
169 .show_nsfw(show_nsfw)
170 .show_bot_accounts(show_bot_accounts)
171 .community_id(community_id)
172 .community_name(community_name)
173 .creator_id(creator_id)
174 .my_person_id(person_id)
182 SearchType::Comments => {
183 comments = blocking(context.pool(), move |conn| {
184 CommentQueryBuilder::create(&conn)
187 .show_bot_accounts(show_bot_accounts)
188 .community_id(community_id)
189 .community_name(community_name)
190 .creator_id(creator_id)
191 .my_person_id(person_id)
198 SearchType::Communities => {
199 communities = blocking(context.pool(), move |conn| {
200 CommunityQueryBuilder::create(conn)
203 .my_person_id(person_id)
210 SearchType::Users => {
211 users = blocking(context.pool(), move |conn| {
212 PersonQueryBuilder::create(conn)
222 posts = blocking(context.pool(), move |conn| {
223 PostQueryBuilder::create(conn)
225 .show_nsfw(show_nsfw)
226 .show_bot_accounts(show_bot_accounts)
227 .community_id(community_id)
228 .community_name(community_name)
229 .creator_id(creator_id)
230 .my_person_id(person_id)
238 let q = data.q.to_owned();
239 let sort = SortType::from_str(&data.sort)?;
241 comments = blocking(context.pool(), move |conn| {
242 CommentQueryBuilder::create(conn)
245 .show_bot_accounts(show_bot_accounts)
246 .community_id(community_id)
247 .community_name(community_name_2)
248 .creator_id(creator_id)
249 .my_person_id(person_id)
256 let q = data.q.to_owned();
257 let sort = SortType::from_str(&data.sort)?;
259 communities = blocking(context.pool(), move |conn| {
260 CommunityQueryBuilder::create(conn)
263 .my_person_id(person_id)
270 let q = data.q.to_owned();
271 let sort = SortType::from_str(&data.sort)?;
273 users = blocking(context.pool(), move |conn| {
274 PersonQueryBuilder::create(conn)
284 posts = blocking(context.pool(), move |conn| {
285 PostQueryBuilder::create(conn)
287 .show_nsfw(show_nsfw)
288 .show_bot_accounts(show_bot_accounts)
289 .my_person_id(person_id)
290 .community_id(community_id)
291 .community_name(community_name)
292 .creator_id(creator_id)
304 type_: data.type_.to_owned(),
313 #[async_trait::async_trait(?Send)]
314 impl Perform for TransferSite {
315 type Response = GetSiteResponse;
319 context: &Data<LemmyContext>,
320 _websocket_id: Option<ConnectionId>,
321 ) -> Result<GetSiteResponse, LemmyError> {
322 let data: &TransferSite = &self;
323 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
325 is_admin(&local_user_view)?;
327 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
329 // Make sure user is the creator
330 if read_site.creator_id != local_user_view.person.id {
331 return Err(ApiError::err("not_an_admin").into());
334 let new_creator_id = data.person_id;
335 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
336 if blocking(context.pool(), transfer_site).await?.is_err() {
337 return Err(ApiError::err("couldnt_update_site").into());
341 let form = ModAddForm {
342 mod_person_id: local_user_view.person.id,
343 other_person_id: data.person_id,
344 removed: Some(false),
347 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
349 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
351 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
352 let creator_index = admins
354 .position(|r| r.person.id == site_view.creator.id)
355 .context(location_info!())?;
356 let creator_person = admins.remove(creator_index);
357 admins.insert(0, creator_person);
359 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
360 let federated_instances = build_federated_instances(context.pool()).await?;
362 let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
365 site_view: Some(site_view),
369 version: version::VERSION.to_string(),
376 #[async_trait::async_trait(?Send)]
377 impl Perform for GetSiteConfig {
378 type Response = GetSiteConfigResponse;
382 context: &Data<LemmyContext>,
383 _websocket_id: Option<ConnectionId>,
384 ) -> Result<GetSiteConfigResponse, LemmyError> {
385 let data: &GetSiteConfig = &self;
386 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
388 // Only let admins read this
389 is_admin(&local_user_view)?;
391 let config_hjson = Settings::read_config_file()?;
393 Ok(GetSiteConfigResponse { config_hjson })
397 #[async_trait::async_trait(?Send)]
398 impl Perform for SaveSiteConfig {
399 type Response = GetSiteConfigResponse;
403 context: &Data<LemmyContext>,
404 _websocket_id: Option<ConnectionId>,
405 ) -> Result<GetSiteConfigResponse, LemmyError> {
406 let data: &SaveSiteConfig = &self;
407 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
409 // Only let admins read this
410 is_admin(&local_user_view)?;
412 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
413 let config_hjson = Settings::save_config_file(&data.config_hjson)
414 .map_err(|_| ApiError::err("couldnt_update_site"))?;
416 Ok(GetSiteConfigResponse { config_hjson })