2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 site::{GetModlog, GetModlogResponse},
7 check_private_instance,
8 get_local_user_view_from_jwt_opt,
13 use lemmy_db_schema::{
14 newtypes::{CommunityId, PersonId},
18 use lemmy_db_views_moderator::structs::{
19 AdminPurgeCommentView,
20 AdminPurgeCommunityView,
25 ModBanFromCommunityView,
30 ModRemoveCommunityView,
33 ModTransferCommunityView,
36 use lemmy_utils::{error::LemmyError, ConnectionId};
37 use lemmy_websocket::LemmyContext;
38 use ModlogActionType::*;
40 #[async_trait::async_trait(?Send)]
41 impl Perform for GetModlog {
42 type Response = GetModlogResponse;
44 #[tracing::instrument(skip(context, _websocket_id))]
47 context: &Data<LemmyContext>,
48 _websocket_id: Option<ConnectionId>,
49 ) -> Result<GetModlogResponse, LemmyError> {
50 let data: &GetModlog = self;
53 get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret())
56 check_private_instance(&local_user_view, context.pool()).await?;
58 let type_ = data.type_.unwrap_or(All);
59 let community_id = data.community_id;
61 let site = blocking(context.pool(), Site::read_local).await??;
62 let (local_person_id, is_admin) = match local_user_view {
63 Some(s) => (s.person.id, is_admin(&s).is_ok()),
64 None => (PersonId(-1), false),
66 let community_id_value = match community_id {
68 None => CommunityId(-1),
70 let is_mod_of_community = data.community_id.is_some()
71 && is_mod_or_admin(context.pool(), local_person_id, community_id_value)
74 let hide_modlog_names = site.hide_modlog_mod_names && !is_mod_of_community && !is_admin;
76 let mod_person_id = if hide_modlog_names {
81 let other_person_id = data.other_person_id;
82 let params = ModlogListParams {
90 let removed_posts = match type_ {
91 All | ModRemovePost => {
92 blocking(context.pool(), move |conn| {
93 ModRemovePostView::list(conn, params)
97 _ => Default::default(),
100 let locked_posts = match type_ {
101 All | ModLockPost => {
102 blocking(context.pool(), move |conn| {
103 ModLockPostView::list(conn, params)
107 _ => Default::default(),
110 let stickied_posts = match type_ {
111 All | ModStickyPost => {
112 blocking(context.pool(), move |conn| {
113 ModStickyPostView::list(conn, params)
117 _ => Default::default(),
120 let removed_comments = match type_ {
121 All | ModRemoveComment => {
122 blocking(context.pool(), move |conn| {
123 ModRemoveCommentView::list(conn, params)
127 _ => Default::default(),
130 let banned_from_community = match type_ {
131 All | ModBanFromCommunity => {
132 blocking(context.pool(), move |conn| {
133 ModBanFromCommunityView::list(conn, params)
137 _ => Default::default(),
140 let added_to_community = match type_ {
141 All | ModAddCommunity => {
142 blocking(context.pool(), move |conn| {
143 ModAddCommunityView::list(conn, params)
147 _ => Default::default(),
150 let transferred_to_community = match type_ {
151 All | ModTransferCommunity => {
152 blocking(context.pool(), move |conn| {
153 ModTransferCommunityView::list(conn, params)
157 _ => Default::default(),
160 let hidden_communities = match type_ {
161 All | ModHideCommunity if other_person_id.is_none() => {
162 blocking(context.pool(), move |conn| {
163 ModHideCommunityView::list(conn, params)
167 _ => Default::default(),
170 // These arrays are only for the full modlog, when a community isn't given
175 admin_purged_persons,
176 admin_purged_communities,
178 admin_purged_comments,
179 ) = if data.community_id.is_none() {
180 blocking(context.pool(), move |conn| {
183 All | ModBan => ModBanView::list(conn, params)?,
184 _ => Default::default(),
187 All | ModAdd => ModAddView::list(conn, params)?,
188 _ => Default::default(),
191 All | ModRemoveCommunity if other_person_id.is_none() => {
192 ModRemoveCommunityView::list(conn, params)?
194 _ => Default::default(),
197 All | AdminPurgePerson if other_person_id.is_none() => {
198 AdminPurgePersonView::list(conn, params)?
200 _ => Default::default(),
203 All | AdminPurgeCommunity if other_person_id.is_none() => {
204 AdminPurgeCommunityView::list(conn, params)?
206 _ => Default::default(),
209 All | AdminPurgePost if other_person_id.is_none() => {
210 AdminPurgePostView::list(conn, params)?
212 _ => Default::default(),
215 All | AdminPurgeComment if other_person_id.is_none() => {
216 AdminPurgeCommentView::list(conn, params)?
218 _ => Default::default(),
220 )) as Result<_, LemmyError>
228 Ok(GetModlogResponse {
234 banned_from_community,
238 transferred_to_community,
239 admin_purged_persons,
240 admin_purged_communities,
242 admin_purged_comments,