mirror of
https://github.com/LemmyNet/lemmy-ui.git
synced 2024-11-23 15:05:51 +00:00
break out all role utils
This commit is contained in:
parent
f2db90196d
commit
0854af3794
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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";
|
||||
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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";
|
||||
|
|
|
@ -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
|
||||
|
|
5
src/shared/utils/roles/am-admin.ts
Normal file
5
src/shared/utils/roles/am-admin.ts
Normal file
|
@ -0,0 +1,5 @@
|
|||
import { UserService } from "../../services";
|
||||
|
||||
export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean {
|
||||
return myUserInfo?.local_user_view.person.admin ?? false;
|
||||
}
|
12
src/shared/utils/roles/am-community-creator.ts
Normal file
12
src/shared/utils/roles/am-community-creator.ts
Normal file
|
@ -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;
|
||||
}
|
10
src/shared/utils/roles/am-mod.ts
Normal file
10
src/shared/utils/roles/am-mod.ts
Normal file
|
@ -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;
|
||||
}
|
11
src/shared/utils/roles/am-site-creator.ts
Normal file
11
src/shared/utils/roles/am-site-creator.ts
Normal file
|
@ -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;
|
||||
}
|
9
src/shared/utils/roles/am-top-mod.ts
Normal file
9
src/shared/utils/roles/am-top-mod.ts
Normal file
|
@ -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;
|
||||
}
|
12
src/shared/utils/roles/can-admin.ts
Normal file
12
src/shared/utils/roles/can-admin.ts
Normal file
|
@ -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);
|
||||
}
|
12
src/shared/utils/roles/can-create-community.ts
Normal file
12
src/shared/utils/roles/can-create-community.ts
Normal file
|
@ -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);
|
||||
}
|
31
src/shared/utils/roles/can-mod.ts
Normal file
31
src/shared/utils/roles/can-mod.ts
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
5
src/shared/utils/roles/is-admin.ts
Normal file
5
src/shared/utils/roles/is-admin.ts
Normal file
|
@ -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;
|
||||
}
|
16
src/shared/utils/roles/is-banned.ts
Normal file
16
src/shared/utils/roles/is-banned.ts
Normal file
|
@ -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;
|
||||
}
|
||||
}
|
8
src/shared/utils/roles/is-mod.ts
Normal file
8
src/shared/utils/roles/is-mod.ts
Normal file
|
@ -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;
|
||||
}
|
Loading…
Reference in a new issue