From 68cc228a96b58145959190ff8d2dec424a4ab093 Mon Sep 17 00:00:00 2001
From: Dessalines <tyhou13@gmx.com>
Date: Tue, 4 May 2021 18:17:15 -0400
Subject: [PATCH] Hide names of mods / admins without priveleges. Fixes #285

---
 src/shared/components/modlog.tsx | 50 ++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx
index 960eeb2..eb0dac8 100644
--- a/src/shared/components/modlog.tsx
+++ b/src/shared/components/modlog.tsx
@@ -15,8 +15,11 @@ import {
   ModBanView,
   ModAddCommunityView,
   ModAddView,
+  GetCommunity,
+  GetCommunityResponse,
+  CommunityModeratorView,
 } from "lemmy-js-client";
-import { WebSocketService } from "../services";
+import { WebSocketService, UserService } from "../services";
 import {
   wsJsonToRes,
   fetchLimit,
@@ -68,6 +71,7 @@ interface ModlogState {
   res: GetModlogResponse;
   communityId?: number;
   communityName?: string;
+  communityMods?: CommunityModeratorView[];
   page: number;
   site_view: SiteView;
   loading: boolean;
@@ -109,6 +113,11 @@ export class Modlog extends Component<any, ModlogState> {
       let data = this.isoData.routeData[0];
       this.state.res = data;
       this.state.loading = false;
+
+      // Getting the moderators
+      if (this.isoData.routeData[1]) {
+        this.state.communityMods = this.isoData.routeData[1].moderators;
+      }
     } else {
       this.refetch();
     }
@@ -353,7 +362,11 @@ export class Modlog extends Component<any, ModlogState> {
               <MomentTime data={i} />
             </td>
             <td>
-              <PersonListing person={i.view.moderator} />
+              {this.isAdminOrMod ? (
+                <PersonListing person={i.view.moderator} />
+              ) : (
+                <div>{i18n.t("mod")}</div>
+              )}
             </td>
             <td>{this.renderModlogType(i)}</td>
           </tr>
@@ -362,6 +375,21 @@ export class Modlog extends Component<any, ModlogState> {
     );
   }
 
+  get isAdminOrMod(): boolean {
+    let isAdmin =
+      UserService.Instance.localUserView &&
+      this.isoData.site_res.admins
+        .map(a => a.person.id)
+        .includes(UserService.Instance.localUserView.person.id);
+    let isMod =
+      UserService.Instance.localUserView &&
+      this.state.communityMods &&
+      this.state.communityMods
+        .map(m => m.moderator.id)
+        .includes(UserService.Instance.localUserView.person.id);
+    return isAdmin || isMod;
+  }
+
   get documentTitle(): string {
     return `Modlog - ${this.state.site_view.site.name}`;
   }
@@ -449,6 +477,14 @@ export class Modlog extends Component<any, ModlogState> {
       limit: fetchLimit,
     };
     WebSocketService.Instance.send(wsClient.getModlog(modlogForm));
+
+    if (this.state.communityId) {
+      let communityForm: GetCommunity = {
+        id: this.state.communityId,
+        name: this.state.communityName,
+      };
+      WebSocketService.Instance.send(wsClient.getCommunity(communityForm));
+    }
   }
 
   static fetchInitialData(req: InitialFetchRequest): Promise<any>[] {
@@ -466,6 +502,13 @@ export class Modlog extends Component<any, ModlogState> {
     }
 
     promises.push(req.client.getModlog(modlogForm));
+
+    if (communityId) {
+      let communityForm: GetCommunity = {
+        id: Number(communityId),
+      };
+      promises.push(req.client.getCommunity(communityForm));
+    }
     return promises;
   }
 
@@ -481,6 +524,9 @@ export class Modlog extends Component<any, ModlogState> {
       window.scrollTo(0, 0);
       this.state.res = data;
       this.setState(this.state);
+    } else if (op == UserOperation.GetCommunity) {
+      let data = wsJsonToRes<GetCommunityResponse>(msg).data;
+      this.state.communityMods = data.moderators;
     }
   }
 }
-- 
2.44.1