From 0854af3794f7fb2b7712c9b30ab1147cc18583a3 Mon Sep 17 00:00:00 2001 From: Alec Armbruster Date: Fri, 16 Jun 2023 17:07:55 -0400 Subject: [PATCH] break out all role utils --- src/client/index.tsx | 6 +- src/shared/components/app/navbar.tsx | 4 +- .../components/comment/comment-node.tsx | 12 +- src/shared/components/community/sidebar.tsx | 6 +- src/shared/components/home/home.tsx | 2 +- src/shared/components/modlog.tsx | 4 +- src/shared/components/person/profile.tsx | 6 +- src/shared/components/person/reports.tsx | 2 +- src/shared/components/post/post-listing.tsx | 16 +-- src/shared/utils.ts | 112 ------------------ src/shared/utils/roles/am-admin.ts | 5 + .../utils/roles/am-community-creator.ts | 12 ++ src/shared/utils/roles/am-mod.ts | 10 ++ src/shared/utils/roles/am-site-creator.ts | 11 ++ src/shared/utils/roles/am-top-mod.ts | 9 ++ src/shared/utils/roles/can-admin.ts | 12 ++ .../utils/roles/can-create-community.ts | 12 ++ src/shared/utils/roles/can-mod.ts | 31 +++++ src/shared/utils/roles/is-admin.ts | 5 + src/shared/utils/roles/is-banned.ts | 16 +++ src/shared/utils/roles/is-mod.ts | 8 ++ 21 files changed, 160 insertions(+), 141 deletions(-) create mode 100644 src/shared/utils/roles/am-admin.ts create mode 100644 src/shared/utils/roles/am-community-creator.ts create mode 100644 src/shared/utils/roles/am-mod.ts create mode 100644 src/shared/utils/roles/am-site-creator.ts create mode 100644 src/shared/utils/roles/am-top-mod.ts create mode 100644 src/shared/utils/roles/can-admin.ts create mode 100644 src/shared/utils/roles/can-create-community.ts create mode 100644 src/shared/utils/roles/can-mod.ts create mode 100644 src/shared/utils/roles/is-admin.ts create mode 100644 src/shared/utils/roles/is-banned.ts create mode 100644 src/shared/utils/roles/is-mod.ts diff --git a/src/client/index.tsx b/src/client/index.tsx index 7b6b6b1c..860c0756 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -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 = ( @@ -17,6 +16,7 @@ const wrapper = ( ); const root = document.getElementById("root"); + if (root) { hydrate(wrapper, root); } diff --git a/src/shared/components/app/navbar.tsx b/src/shared/components/app/navbar.tsx index 6d310eef..8c7c9202 100644 --- a/src/shared/components/app/navbar.tsx +++ b/src/shared/components/app/navbar.tsx @@ -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"; diff --git a/src/shared/components/comment/comment-node.tsx b/src/shared/components/comment/comment-node.tsx index 0380a726..10b13c12 100644 --- a/src/shared/components/comment/comment-node.tsx +++ b/src/shared/components/comment/comment-node.tsx @@ -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"; diff --git a/src/shared/components/community/sidebar.tsx b/src/shared/components/community/sidebar.tsx index a5c620f3..2774747b 100644 --- a/src/shared/components/community/sidebar.tsx +++ b/src/shared/components/community/sidebar.tsx @@ -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"; diff --git a/src/shared/components/home/home.tsx b/src/shared/components/home/home.tsx index 1abb1ee3..4e23c03c 100644 --- a/src/shared/components/home/home.tsx +++ b/src/shared/components/home/home.tsx @@ -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"; diff --git a/src/shared/components/modlog.tsx b/src/shared/components/modlog.tsx index d917f5f3..4ab5ec56 100644 --- a/src/shared/components/modlog.tsx +++ b/src/shared/components/modlog.tsx @@ -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"; diff --git a/src/shared/components/person/profile.tsx b/src/shared/components/person/profile.tsx index f80d5b90..4d9f8f77 100644 --- a/src/shared/components/person/profile.tsx +++ b/src/shared/components/person/profile.tsx @@ -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"; diff --git a/src/shared/components/person/reports.tsx b/src/shared/components/person/reports.tsx index 29daa3ff..187fe4c2 100644 --- a/src/shared/components/person/reports.tsx +++ b/src/shared/components/person/reports.tsx @@ -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"; diff --git a/src/shared/components/post/post-listing.tsx b/src/shared/components/post/post-listing.tsx index 60e188a3..3ec1a456 100644 --- a/src/shared/components/post/post-listing.tsx +++ b/src/shared/components/post/post-listing.tsx @@ -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"; diff --git a/src/shared/utils.ts b/src/shared/utils.ts index 4a3b298a..b1b06e2e 100644 --- a/src/shared/utils.ts +++ b/src/shared/utils.ts @@ -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 index 00000000..aadf52ce --- /dev/null +++ b/src/shared/utils/roles/am-admin.ts @@ -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 index 00000000..20f9b1dd --- /dev/null +++ b/src/shared/utils/roles/am-community-creator.ts @@ -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 index 00000000..7b792b39 --- /dev/null +++ b/src/shared/utils/roles/am-mod.ts @@ -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 index 00000000..323ac0a4 --- /dev/null +++ b/src/shared/utils/roles/am-site-creator.ts @@ -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 index 00000000..4b942da7 --- /dev/null +++ b/src/shared/utils/roles/am-top-mod.ts @@ -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 index 00000000..080c7acc --- /dev/null +++ b/src/shared/utils/roles/can-admin.ts @@ -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 index 00000000..202290d2 --- /dev/null +++ b/src/shared/utils/roles/can-create-community.ts @@ -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 index 00000000..2892304d --- /dev/null +++ b/src/shared/utils/roles/can-mod.ts @@ -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 index 00000000..fbf662b8 --- /dev/null +++ b/src/shared/utils/roles/is-admin.ts @@ -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 index 00000000..dd5ffe6c --- /dev/null +++ b/src/shared/utils/roles/is-banned.ts @@ -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 index 00000000..87311072 --- /dev/null +++ b/src/shared/utils/roles/is-mod.ts @@ -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; +}