From: Alec Armbruster Date: Fri, 16 Jun 2023 21:07:55 +0000 (-0400) Subject: break out all role utils X-Git-Url: http://these/git/%7B%60%24%7BwebArchiveUrl%7D/%22%7B%7D/%22https:/hacktivis.me/README.es.md?a=commitdiff_plain;h=0854af3794f7fb2b7712c9b30ab1147cc18583a3;p=lemmy-ui.git break out all role utils --- diff --git a/src/client/index.tsx b/src/client/index.tsx index 7b6b6b1..860c075 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 6d310ee..8c7c920 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 0380a72..10b13c1 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 a5c620f..2774747 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 1abb1ee..4e23c03 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 d917f5f..4ab5ec5 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 f80d5b9..4d9f8f7 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 29daa3f..187fe4c 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 60e188a..3ec1a45 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 4a3b298..b1b06e2 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 0000000..aadf52c --- /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 0000000..20f9b1d --- /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 0000000..7b792b3 --- /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 0000000..323ac0a --- /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 0000000..4b942da --- /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 0000000..080c7ac --- /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 0000000..202290d --- /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 0000000..2892304 --- /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 0000000..fbf662b --- /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 0000000..dd5ffe6 --- /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 0000000..8731107 --- /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; +}