break out all role utils
authorAlec Armbruster <alectrocute@gmail.com>
Fri, 16 Jun 2023 21:07:55 +0000 (17:07 -0400)
committerAlec Armbruster <alectrocute@gmail.com>
Fri, 16 Jun 2023 21:07:55 +0000 (17:07 -0400)
21 files changed:
src/client/index.tsx
src/shared/components/app/navbar.tsx
src/shared/components/comment/comment-node.tsx
src/shared/components/community/sidebar.tsx
src/shared/components/home/home.tsx
src/shared/components/modlog.tsx
src/shared/components/person/profile.tsx
src/shared/components/person/reports.tsx
src/shared/components/post/post-listing.tsx
src/shared/utils.ts
src/shared/utils/roles/am-admin.ts [new file with mode: 0644]
src/shared/utils/roles/am-community-creator.ts [new file with mode: 0644]
src/shared/utils/roles/am-mod.ts [new file with mode: 0644]
src/shared/utils/roles/am-site-creator.ts [new file with mode: 0644]
src/shared/utils/roles/am-top-mod.ts [new file with mode: 0644]
src/shared/utils/roles/can-admin.ts [new file with mode: 0644]
src/shared/utils/roles/can-create-community.ts [new file with mode: 0644]
src/shared/utils/roles/can-mod.ts [new file with mode: 0644]
src/shared/utils/roles/is-admin.ts [new file with mode: 0644]
src/shared/utils/roles/is-banned.ts [new file with mode: 0644]
src/shared/utils/roles/is-mod.ts [new file with mode: 0644]

index 7b6b6b1cd662932736a83e4daca2b355d77f2196..860c07565dbad01861283893d209de7aad2099a4 100644 (file)
@@ -1,14 +1,13 @@
 import { hydrate } from "inferno-hydrate";
 import { Router } from "inferno-router";
 import { App } from "../shared/components/app/app";
+import { HistoryService } from "../shared/services/HistoryService";
 import { initializeSite } from "../shared/utils";
 
 import "bootstrap/js/dist/collapse";
 import "bootstrap/js/dist/dropdown";
-import { HistoryService } from "../shared/services/HistoryService";
 
-const site = window.isoData.site_res;
-initializeSite(site);
+initializeSite(window.isoData.site_res);
 
 const wrapper = (
   <Router history={HistoryService.history}>
@@ -17,6 +16,7 @@ const wrapper = (
 );
 
 const root = document.getElementById("root");
+
 if (root) {
   hydrate(wrapper, root);
 }
index 6d310eef36d3b10455638700192f4efd3aa630a7..8c7c9202f4cc10045494aa81897cd8fe433ec0df 100644 (file)
@@ -10,8 +10,6 @@ import { i18n } from "../../i18next";
 import { UserService } from "../../services";
 import { HttpService, RequestState } from "../../services/HttpService";
 import {
-  amAdmin,
-  canCreateCommunity,
   donateLemmyUrl,
   isBrowser,
   myAuth,
@@ -21,6 +19,8 @@ import {
   toast,
   updateUnreadCountsInterval,
 } from "../../utils";
+import { amAdmin } from "../../utils/roles/am-admin";
+import { canCreateCommunity } from "../../utils/roles/can-create-community";
 import { Icon } from "../common/icon";
 import { PictrsImage } from "../common/pictrs-image";
 
index 0380a72663935e8f119e309d1e5dc159723f3d7c..10b13c1268846ec0d0993d815b5116ec6e88b265 100644 (file)
@@ -40,16 +40,10 @@ import {
 } from "../../interfaces";
 import { UserService } from "../../services";
 import {
-  amCommunityCreator,
-  canAdmin,
-  canMod,
   colorList,
   commentTreeMaxDepth,
   futureDaysToUnixTime,
   getCommentParentId,
-  isAdmin,
-  isBanned,
-  isMod,
   mdToHtml,
   mdToHtmlNoImages,
   myAuth,
@@ -59,6 +53,12 @@ import {
   setupTippy,
   showScores,
 } from "../../utils";
+import { amCommunityCreator } from "../../utils/roles/am-community-creator";
+import { canAdmin } from "../../utils/roles/can-admin";
+import { canMod } from "../../utils/roles/can-mod";
+import { isAdmin } from "../../utils/roles/is-admin";
+import { isBanned } from "../../utils/roles/is-banned";
+import { isMod } from "../../utils/roles/is-mod";
 import { Icon, PurgeWarning, Spinner } from "../common/icon";
 import { MomentTime } from "../common/moment-time";
 import { CommunityLink } from "../community/community-link";
index a5c620f3b4b5704e8763a4d549af8fbd97c877d8..2774747b03e1dcc7c062e782d7aa6e5a6662e836 100644 (file)
@@ -16,15 +16,15 @@ import {
 import { i18n } from "../../i18next";
 import { UserService } from "../../services";
 import {
-  amAdmin,
-  amMod,
-  amTopMod,
   getUnixTime,
   hostname,
   mdToHtml,
   myAuthRequired,
   numToSI,
 } from "../../utils";
+import { amAdmin } from "../../utils/roles/am-admin";
+import { amMod } from "../../utils/roles/am-mod";
+import { amTopMod } from "../../utils/roles/am-top-mod";
 import { BannerIconHeader } from "../common/banner-icon-header";
 import { Icon, PurgeWarning, Spinner } from "../common/icon";
 import { CommunityForm } from "../community/community-form";
index 1abb1ee329f391ab52e93e1c33d389e173ddf834..4e23c03c96ddea5a803a422c3f5c4c16becc3fe0 100644 (file)
@@ -57,7 +57,6 @@ import { UserService } from "../../services";
 import { FirstLoadService } from "../../services/FirstLoadService";
 import { HttpService, RequestState } from "../../services/HttpService";
 import {
-  canCreateCommunity,
   commentsToFlatNodes,
   editComment,
   editPost,
@@ -85,6 +84,7 @@ import {
   trendingFetchLimit,
   updatePersonBlock,
 } from "../../utils";
+import { canCreateCommunity } from "../../utils/roles/can-create-community";
 import { CommentNodes } from "../comment/comment-nodes";
 import { DataTypeSelect } from "../common/data-type-select";
 import { HtmlTags } from "../common/html-tags";
index d917f5f35ed675842e43169fa6da129b696335b6..4ab5ec563fd2339a54eff06e8901f9ae356c4e65 100644 (file)
@@ -34,8 +34,6 @@ import { HttpService, RequestState } from "../services/HttpService";
 import {
   Choice,
   QueryParams,
-  amAdmin,
-  amMod,
   debounce,
   fetchLimit,
   fetchUsers,
@@ -48,6 +46,8 @@ import {
   personToChoice,
   setIsoData,
 } from "../utils";
+import { amAdmin } from "../utils/roles/am-admin";
+import { amMod } from "../utils/roles/am-mod";
 import { HtmlTags } from "./common/html-tags";
 import { Icon, Spinner } from "./common/icon";
 import { MomentTime } from "./common/moment-time";
index f80d5b907a2f7f1972dc0b62d1bebb51d06df1fe..4d9f8f77bf1946ae34f1357c6ece409c2b5846c0 100644 (file)
@@ -54,7 +54,6 @@ import { FirstLoadService } from "../../services/FirstLoadService";
 import { HttpService, RequestState } from "../../services/HttpService";
 import {
   QueryParams,
-  canMod,
   capitalizeFirstLetter,
   editComment,
   editPost,
@@ -67,8 +66,6 @@ import {
   getPageFromString,
   getQueryParams,
   getQueryString,
-  isAdmin,
-  isBanned,
   mdToHtml,
   myAuth,
   myAuthRequired,
@@ -81,6 +78,9 @@ import {
   toast,
   updatePersonBlock,
 } from "../../utils";
+import { canMod } from "../../utils/roles/can-mod";
+import { isAdmin } from "../../utils/roles/is-admin";
+import { isBanned } from "../../utils/roles/is-banned";
 import { BannerIconHeader } from "../common/banner-icon-header";
 import { HtmlTags } from "../common/html-tags";
 import { Icon, Spinner } from "../common/icon";
index 29daa3ff6c92dbad9d621d8c2ed9be2e6945985c..187fe4c2e0e06d71548793e49df30728e62ec2ce 100644 (file)
@@ -23,7 +23,6 @@ import { HttpService, UserService } from "../../services";
 import { FirstLoadService } from "../../services/FirstLoadService";
 import { RequestState } from "../../services/HttpService";
 import {
-  amAdmin,
   editCommentReport,
   editPostReport,
   editPrivateMessageReport,
@@ -31,6 +30,7 @@ import {
   myAuthRequired,
   setIsoData,
 } from "../../utils";
+import { amAdmin } from "../../utils/roles/am-admin";
 import { CommentReport } from "../comment/comment-report";
 import { HtmlTags } from "../common/html-tags";
 import { Spinner } from "../common/icon";
index 60e188a33b77c942ba5e9f33cdceb29a8e07e6bb..3ec1a456704d503d732a46b962157eedc8924728 100644 (file)
@@ -28,18 +28,10 @@ import { i18n } from "../../i18next";
 import { BanType, PostFormParams, PurgeType, VoteType } from "../../interfaces";
 import { UserService } from "../../services";
 import {
-  amAdmin,
-  amCommunityCreator,
-  amMod,
-  canAdmin,
-  canMod,
   canShare,
   futureDaysToUnixTime,
   hostname,
-  isAdmin,
-  isBanned,
   isImage,
-  isMod,
   isVideo,
   mdNoImages,
   mdToHtml,
@@ -52,6 +44,14 @@ import {
   share,
   showScores,
 } from "../../utils";
+import { amAdmin } from "../../utils/roles/am-admin";
+import { amCommunityCreator } from "../../utils/roles/am-community-creator";
+import { amMod } from "../../utils/roles/am-mod";
+import { canAdmin } from "../../utils/roles/can-admin";
+import { canMod } from "../../utils/roles/can-mod";
+import { isAdmin } from "../../utils/roles/is-admin";
+import { isBanned } from "../../utils/roles/is-banned";
+import { isMod } from "../../utils/roles/is-mod";
 import { Icon, PurgeWarning, Spinner } from "../common/icon";
 import { MomentTime } from "../common/moment-time";
 import { PictrsImage } from "../common/pictrs-image";
index 4a3b298a83bd91ce7e90a5ae1a9ba1e9156257a9..b1b06e2ec0a8ba6cc804618b2854b7554288cc26 100644 (file)
@@ -9,7 +9,6 @@ import {
   CommentReportView,
   CommentSortType,
   CommentView,
-  CommunityModeratorView,
   CommunityView,
   CustomEmojiView,
   GetSiteMetadata,
@@ -17,7 +16,6 @@ import {
   Language,
   LemmyHttp,
   MyUserInfo,
-  Person,
   PersonMentionView,
   PersonView,
   PostReportView,
@@ -228,92 +226,6 @@ export function futureDaysToUnixTime(days?: number): number | undefined {
     : undefined;
 }
 
-export function canMod(
-  creator_id: number,
-  mods?: CommunityModeratorView[],
-  admins?: PersonView[],
-  myUserInfo = UserService.Instance.myUserInfo,
-  onSelf = false
-): boolean {
-  // You can do moderator actions only on the mods added after you.
-  let adminsThenMods =
-    admins
-      ?.map(a => a.person.id)
-      .concat(mods?.map(m => m.moderator.id) ?? []) ?? [];
-
-  if (myUserInfo) {
-    const myIndex = adminsThenMods.findIndex(
-      id => id == myUserInfo.local_user_view.person.id
-    );
-    if (myIndex == -1) {
-      return false;
-    } else {
-      // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
-      adminsThenMods = adminsThenMods.slice(0, myIndex + (onSelf ? 0 : 1));
-      return !adminsThenMods.includes(creator_id);
-    }
-  } else {
-    return false;
-  }
-}
-
-export function canAdmin(
-  creatorId: number,
-  admins?: PersonView[],
-  myUserInfo = UserService.Instance.myUserInfo,
-  onSelf = false
-): boolean {
-  return canMod(creatorId, undefined, admins, myUserInfo, onSelf);
-}
-
-export function isMod(
-  creatorId: number,
-  mods?: CommunityModeratorView[]
-): boolean {
-  return mods?.map(m => m.moderator.id).includes(creatorId) ?? false;
-}
-
-export function amMod(
-  mods?: CommunityModeratorView[],
-  myUserInfo = UserService.Instance.myUserInfo
-): boolean {
-  return myUserInfo ? isMod(myUserInfo.local_user_view.person.id, mods) : false;
-}
-
-export function isAdmin(creatorId: number, admins?: PersonView[]): boolean {
-  return admins?.map(a => a.person.id).includes(creatorId) ?? false;
-}
-
-export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean {
-  return myUserInfo?.local_user_view.person.admin ?? false;
-}
-
-export function amCommunityCreator(
-  creator_id: number,
-  mods?: CommunityModeratorView[],
-  myUserInfo = UserService.Instance.myUserInfo
-): boolean {
-  const myId = myUserInfo?.local_user_view.person.id;
-  // Don't allow mod actions on yourself
-  return myId == mods?.at(0)?.moderator.id && myId != creator_id;
-}
-
-export function amSiteCreator(
-  creator_id: number,
-  admins?: PersonView[],
-  myUserInfo = UserService.Instance.myUserInfo
-): boolean {
-  const myId = myUserInfo?.local_user_view.person.id;
-  return myId == admins?.at(0)?.person.id && myId != creator_id;
-}
-
-export function amTopMod(
-  mods: CommunityModeratorView[],
-  myUserInfo = UserService.Instance.myUserInfo
-): boolean {
-  return mods.at(0)?.moderator.id == myUserInfo?.local_user_view.person.id;
-}
-
 const imageRegex = /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/;
 const videoRegex = /(http)?s?:?(\/\/[^"']*\.(?:mp4|webm))/;
 
@@ -1291,21 +1203,6 @@ export function numToSI(value: number): string {
   return SHORTNUM_SI_FORMAT.format(value);
 }
 
-export function isBanned(ps: Person): boolean {
-  const expires = ps.ban_expires;
-  // Add Z to convert from UTC date
-  // TODO this check probably isn't necessary anymore
-  if (expires) {
-    if (ps.banned && new Date(expires + "Z") > new Date()) {
-      return true;
-    } else {
-      return false;
-    }
-  } else {
-    return ps.banned;
-  }
-}
-
 export function myAuth(): string | undefined {
   return UserService.Instance.auth();
 }
@@ -1337,15 +1234,6 @@ export function postToCommentSortType(sort: SortType): CommentSortType {
   }
 }
 
-export function canCreateCommunity(
-  siteRes: GetSiteResponse,
-  myUserInfo = UserService.Instance.myUserInfo
-): boolean {
-  const adminOnly = siteRes.site_view.local_site.community_creation_admin_only;
-  // TODO: Make this check if user is logged on as well
-  return !adminOnly || amAdmin(myUserInfo);
-}
-
 export function isPostBlocked(
   pv: PostView,
   myUserInfo: MyUserInfo | undefined = UserService.Instance.myUserInfo
diff --git a/src/shared/utils/roles/am-admin.ts b/src/shared/utils/roles/am-admin.ts
new file mode 100644 (file)
index 0000000..aadf52c
--- /dev/null
@@ -0,0 +1,5 @@
+import { UserService } from "../../services";
+
+export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean {
+  return myUserInfo?.local_user_view.person.admin ?? false;
+}
diff --git a/src/shared/utils/roles/am-community-creator.ts b/src/shared/utils/roles/am-community-creator.ts
new file mode 100644 (file)
index 0000000..20f9b1d
--- /dev/null
@@ -0,0 +1,12 @@
+import { CommunityModeratorView } from "lemmy-js-client";
+import { UserService } from "../../services";
+
+export function amCommunityCreator(
+  creator_id: number,
+  mods?: CommunityModeratorView[],
+  myUserInfo = UserService.Instance.myUserInfo
+): boolean {
+  const myId = myUserInfo?.local_user_view.person.id;
+  // Don't allow mod actions on yourself
+  return myId == mods?.at(0)?.moderator.id && myId != creator_id;
+}
diff --git a/src/shared/utils/roles/am-mod.ts b/src/shared/utils/roles/am-mod.ts
new file mode 100644 (file)
index 0000000..7b792b3
--- /dev/null
@@ -0,0 +1,10 @@
+import { CommunityModeratorView } from "lemmy-js-client";
+import { UserService } from "../../services";
+import { isMod } from "./is-mod";
+
+export function amMod(
+  mods?: CommunityModeratorView[],
+  myUserInfo = UserService.Instance.myUserInfo
+): boolean {
+  return myUserInfo ? isMod(myUserInfo.local_user_view.person.id, mods) : false;
+}
diff --git a/src/shared/utils/roles/am-site-creator.ts b/src/shared/utils/roles/am-site-creator.ts
new file mode 100644 (file)
index 0000000..323ac0a
--- /dev/null
@@ -0,0 +1,11 @@
+import { PersonView } from "lemmy-js-client";
+import { UserService } from "../../services";
+
+export function amSiteCreator(
+  creator_id: number,
+  admins?: PersonView[],
+  myUserInfo = UserService.Instance.myUserInfo
+): boolean {
+  const myId = myUserInfo?.local_user_view.person.id;
+  return myId == admins?.at(0)?.person.id && myId != creator_id;
+}
diff --git a/src/shared/utils/roles/am-top-mod.ts b/src/shared/utils/roles/am-top-mod.ts
new file mode 100644 (file)
index 0000000..4b942da
--- /dev/null
@@ -0,0 +1,9 @@
+import { CommunityModeratorView } from "lemmy-js-client";
+import { UserService } from "../../services";
+
+export function amTopMod(
+  mods: CommunityModeratorView[],
+  myUserInfo = UserService.Instance.myUserInfo
+): boolean {
+  return mods.at(0)?.moderator.id == myUserInfo?.local_user_view.person.id;
+}
diff --git a/src/shared/utils/roles/can-admin.ts b/src/shared/utils/roles/can-admin.ts
new file mode 100644 (file)
index 0000000..080c7ac
--- /dev/null
@@ -0,0 +1,12 @@
+import { PersonView } from "lemmy-js-client";
+import { UserService } from "../../services";
+import { canMod } from "./can-mod";
+
+export function canAdmin(
+  creatorId: number,
+  admins?: PersonView[],
+  myUserInfo = UserService.Instance.myUserInfo,
+  onSelf = false
+): boolean {
+  return canMod(creatorId, undefined, admins, myUserInfo, onSelf);
+}
diff --git a/src/shared/utils/roles/can-create-community.ts b/src/shared/utils/roles/can-create-community.ts
new file mode 100644 (file)
index 0000000..202290d
--- /dev/null
@@ -0,0 +1,12 @@
+import { GetSiteResponse } from "lemmy-js-client";
+import { UserService } from "../../services";
+import { amAdmin } from "./am-admin";
+
+export function canCreateCommunity(
+  siteRes: GetSiteResponse,
+  myUserInfo = UserService.Instance.myUserInfo
+): boolean {
+  const adminOnly = siteRes.site_view.local_site.community_creation_admin_only;
+  // TODO: Make this check if user is logged on as well
+  return !adminOnly || amAdmin(myUserInfo);
+}
diff --git a/src/shared/utils/roles/can-mod.ts b/src/shared/utils/roles/can-mod.ts
new file mode 100644 (file)
index 0000000..2892304
--- /dev/null
@@ -0,0 +1,31 @@
+import { CommunityModeratorView, PersonView } from "lemmy-js-client";
+import { UserService } from "../../services";
+
+export function canMod(
+  creator_id: number,
+  mods?: CommunityModeratorView[],
+  admins?: PersonView[],
+  myUserInfo = UserService.Instance.myUserInfo,
+  onSelf = false
+): boolean {
+  // You can do moderator actions only on the mods added after you.
+  let adminsThenMods =
+    admins
+      ?.map(a => a.person.id)
+      .concat(mods?.map(m => m.moderator.id) ?? []) ?? [];
+
+  if (myUserInfo) {
+    const myIndex = adminsThenMods.findIndex(
+      id => id == myUserInfo.local_user_view.person.id
+    );
+    if (myIndex == -1) {
+      return false;
+    } else {
+      // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
+      adminsThenMods = adminsThenMods.slice(0, myIndex + (onSelf ? 0 : 1));
+      return !adminsThenMods.includes(creator_id);
+    }
+  } else {
+    return false;
+  }
+}
diff --git a/src/shared/utils/roles/is-admin.ts b/src/shared/utils/roles/is-admin.ts
new file mode 100644 (file)
index 0000000..fbf662b
--- /dev/null
@@ -0,0 +1,5 @@
+import { PersonView } from "lemmy-js-client";
+
+export function isAdmin(creatorId: number, admins?: PersonView[]): boolean {
+  return admins?.map(a => a.person.id).includes(creatorId) ?? false;
+}
diff --git a/src/shared/utils/roles/is-banned.ts b/src/shared/utils/roles/is-banned.ts
new file mode 100644 (file)
index 0000000..dd5ffe6
--- /dev/null
@@ -0,0 +1,16 @@
+import { Person } from "lemmy-js-client";
+
+export function isBanned(ps: Person): boolean {
+  const expires = ps.ban_expires;
+  // Add Z to convert from UTC date
+  // TODO this check probably isn't necessary anymore
+  if (expires) {
+    if (ps.banned && new Date(expires + "Z") > new Date()) {
+      return true;
+    } else {
+      return false;
+    }
+  } else {
+    return ps.banned;
+  }
+}
diff --git a/src/shared/utils/roles/is-mod.ts b/src/shared/utils/roles/is-mod.ts
new file mode 100644 (file)
index 0000000..8731107
--- /dev/null
@@ -0,0 +1,8 @@
+import { CommunityModeratorView } from "lemmy-js-client";
+
+export function isMod(
+  creatorId: number,
+  mods?: CommunityModeratorView[]
+): boolean {
+  return mods?.map(m => m.moderator.id).includes(creatorId) ?? false;
+}