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,
13 use lemmy_apub::fetcher::search::search_by_apub_id;
14 use lemmy_db_queries::{source::site::Site_, Crud, SearchType, SortType};
15 use lemmy_db_schema::source::{moderator::*, site::Site};
17 comment_view::CommentQueryBuilder,
18 post_view::PostQueryBuilder,
21 use lemmy_db_views_actor::{
22 community_view::CommunityQueryBuilder,
23 person_view::{PersonQueryBuilder, PersonViewSafe},
25 use lemmy_db_views_moderator::{
26 mod_add_community_view::ModAddCommunityView,
27 mod_add_view::ModAddView,
28 mod_ban_from_community_view::ModBanFromCommunityView,
29 mod_ban_view::ModBanView,
30 mod_lock_post_view::ModLockPostView,
31 mod_remove_comment_view::ModRemoveCommentView,
32 mod_remove_community_view::ModRemoveCommunityView,
33 mod_remove_post_view::ModRemovePostView,
34 mod_sticky_post_view::ModStickyPostView,
38 settings::structs::Settings,
44 use lemmy_websocket::LemmyContext;
46 use std::str::FromStr;
48 #[async_trait::async_trait(?Send)]
49 impl Perform for GetModlog {
50 type Response = GetModlogResponse;
54 context: &Data<LemmyContext>,
55 _websocket_id: Option<ConnectionId>,
56 ) -> Result<GetModlogResponse, LemmyError> {
57 let data: &GetModlog = &self;
59 let community_id = data.community_id;
60 let mod_person_id = data.mod_person_id;
62 let limit = data.limit;
63 let removed_posts = blocking(context.pool(), move |conn| {
64 ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
68 let locked_posts = blocking(context.pool(), move |conn| {
69 ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
73 let stickied_posts = blocking(context.pool(), move |conn| {
74 ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
78 let removed_comments = blocking(context.pool(), move |conn| {
79 ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
83 let banned_from_community = blocking(context.pool(), move |conn| {
84 ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
88 let added_to_community = blocking(context.pool(), move |conn| {
89 ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
93 // These arrays are only for the full modlog, when a community isn't given
94 let (removed_communities, banned, added) = if data.community_id.is_none() {
95 blocking(context.pool(), move |conn| {
97 ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
98 ModBanView::list(conn, mod_person_id, page, limit)?,
99 ModAddView::list(conn, mod_person_id, page, limit)?,
100 )) as Result<_, LemmyError>
104 (Vec::new(), Vec::new(), Vec::new())
108 Ok(GetModlogResponse {
114 banned_from_community,
122 #[async_trait::async_trait(?Send)]
123 impl Perform for Search {
124 type Response = SearchResponse;
128 context: &Data<LemmyContext>,
129 _websocket_id: Option<ConnectionId>,
130 ) -> Result<SearchResponse, LemmyError> {
131 let data: &Search = &self;
133 match search_by_apub_id(&data.q, context).await {
134 Ok(r) => return Ok(r),
135 Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
138 let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
139 let person_id = local_user_view.map(|u| u.person.id);
141 let type_ = SearchType::from_str(&data.type_)?;
143 let mut posts = Vec::new();
144 let mut comments = Vec::new();
145 let mut communities = Vec::new();
146 let mut users = Vec::new();
148 // TODO no clean / non-nsfw searching rn
150 let q = data.q.to_owned();
151 let page = data.page;
152 let limit = data.limit;
153 let sort = SortType::from_str(&data.sort)?;
154 let community_id = data.community_id;
155 let community_name = data.community_name.to_owned();
157 SearchType::Posts => {
158 posts = blocking(context.pool(), move |conn| {
159 PostQueryBuilder::create(conn)
162 .community_id(community_id)
163 .community_name(community_name)
164 .my_person_id(person_id)
172 SearchType::Comments => {
173 comments = blocking(context.pool(), move |conn| {
174 CommentQueryBuilder::create(&conn)
177 .my_person_id(person_id)
184 SearchType::Communities => {
185 communities = blocking(context.pool(), move |conn| {
186 CommunityQueryBuilder::create(conn)
189 .my_person_id(person_id)
196 SearchType::Users => {
197 users = blocking(context.pool(), move |conn| {
198 PersonQueryBuilder::create(conn)
208 posts = blocking(context.pool(), move |conn| {
209 PostQueryBuilder::create(conn)
212 .community_id(community_id)
213 .community_name(community_name)
214 .my_person_id(person_id)
222 let q = data.q.to_owned();
223 let sort = SortType::from_str(&data.sort)?;
225 comments = blocking(context.pool(), move |conn| {
226 CommentQueryBuilder::create(conn)
229 .my_person_id(person_id)
236 let q = data.q.to_owned();
237 let sort = SortType::from_str(&data.sort)?;
239 communities = blocking(context.pool(), move |conn| {
240 CommunityQueryBuilder::create(conn)
243 .my_person_id(person_id)
250 let q = data.q.to_owned();
251 let sort = SortType::from_str(&data.sort)?;
253 users = blocking(context.pool(), move |conn| {
254 PersonQueryBuilder::create(conn)
264 posts = blocking(context.pool(), move |conn| {
265 PostQueryBuilder::create(conn)
268 .my_person_id(person_id)
269 .community_id(community_id)
270 .community_name(community_name)
282 type_: data.type_.to_owned(),
291 #[async_trait::async_trait(?Send)]
292 impl Perform for TransferSite {
293 type Response = GetSiteResponse;
297 context: &Data<LemmyContext>,
298 _websocket_id: Option<ConnectionId>,
299 ) -> Result<GetSiteResponse, LemmyError> {
300 let data: &TransferSite = &self;
301 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
303 is_admin(&local_user_view)?;
305 let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
307 // Make sure user is the creator
308 if read_site.creator_id != local_user_view.person.id {
309 return Err(ApiError::err("not_an_admin").into());
312 let new_creator_id = data.person_id;
313 let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
314 if blocking(context.pool(), transfer_site).await?.is_err() {
315 return Err(ApiError::err("couldnt_update_site").into());
319 let form = ModAddForm {
320 mod_person_id: local_user_view.person.id,
321 other_person_id: data.person_id,
322 removed: Some(false),
325 blocking(context.pool(), move |conn| ModAdd::create(conn, &form)).await??;
327 let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
329 let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
330 let creator_index = admins
332 .position(|r| r.person.id == site_view.creator.id)
333 .context(location_info!())?;
334 let creator_person = admins.remove(creator_index);
335 admins.insert(0, creator_person);
337 let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
338 let federated_instances = build_federated_instances(context.pool()).await?;
340 let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
343 site_view: Some(site_view),
347 version: version::VERSION.to_string(),
354 #[async_trait::async_trait(?Send)]
355 impl Perform for GetSiteConfig {
356 type Response = GetSiteConfigResponse;
360 context: &Data<LemmyContext>,
361 _websocket_id: Option<ConnectionId>,
362 ) -> Result<GetSiteConfigResponse, LemmyError> {
363 let data: &GetSiteConfig = &self;
364 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
366 // Only let admins read this
367 is_admin(&local_user_view)?;
369 let config_hjson = Settings::read_config_file()?;
371 Ok(GetSiteConfigResponse { config_hjson })
375 #[async_trait::async_trait(?Send)]
376 impl Perform for SaveSiteConfig {
377 type Response = GetSiteConfigResponse;
381 context: &Data<LemmyContext>,
382 _websocket_id: Option<ConnectionId>,
383 ) -> Result<GetSiteConfigResponse, LemmyError> {
384 let data: &SaveSiteConfig = &self;
385 let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
387 // Only let admins read this
388 is_admin(&local_user_view)?;
390 // Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
391 let config_hjson = match Settings::save_config_file(&data.config_hjson) {
392 Ok(config_hjson) => config_hjson,
393 Err(_e) => return Err(ApiError::err("couldnt_update_site").into()),
396 Ok(GetSiteConfigResponse { config_hjson })