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}>
);
const root = document.getElementById("root");
+
if (root) {
hydrate(wrapper, root);
}
import { UserService } from "../../services";
import { HttpService, RequestState } from "../../services/HttpService";
import {
- amAdmin,
- canCreateCommunity,
donateLemmyUrl,
- isBrowser,
myAuth,
numToSI,
- poll,
showAvatars,
toast,
updateUnreadCountsInterval,
} from "../../utils";
+import { isBrowser } from "../../utils/browser/is-browser";
+import { poll } from "../../utils/helpers/poll";
+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";
} from "../../interfaces";
import { UserService } from "../../services";
import {
- amCommunityCreator,
- canAdmin,
- canMod,
colorList,
commentTreeMaxDepth,
futureDaysToUnixTime,
getCommentParentId,
- isAdmin,
- isBanned,
- isMod,
mdToHtml,
mdToHtmlNoImages,
myAuth,
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";
import {
concurrentImageUpload,
customEmojisLookup,
- isBrowser,
markdownFieldCharacterLimit,
markdownHelpUrl,
maxUploadImages,
setupTribute,
toast,
} from "../../utils";
+import { isBrowser } from "../../utils/browser/is-browser";
import { EmojiPicker } from "./emoji-picker";
import { Icon, Spinner } from "./icon";
import { LanguageSelect } from "./language-select";
import { FirstLoadService } from "../../services/FirstLoadService";
import { HttpService, RequestState } from "../../services/HttpService";
import {
- QueryParams,
- RouteDataResponse,
editCommunity,
getPageFromString,
- getQueryParams,
- getQueryString,
myAuth,
myAuthRequired,
numToSI,
setIsoData,
showLocal,
} from "../../utils";
+import { getQueryParams } from "../../utils/helpers/get-query-params";
+import { getQueryString } from "../../utils/helpers/get-query-string";
+import type { QueryParams } from "../../utils/types/query-params";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
import { ListingTypeSelect } from "../common/listing-type-select";
import { FirstLoadService } from "../../services/FirstLoadService";
import { HttpService, RequestState } from "../../services/HttpService";
import {
- QueryParams,
RouteDataResponse,
commentsToFlatNodes,
communityRSSUrl,
getCommentParentId,
getDataTypeString,
getPageFromString,
- getQueryParams,
- getQueryString,
myAuth,
postToCommentSortType,
relTags,
updateCommunityBlock,
updatePersonBlock,
} from "../../utils";
+import { getQueryParams } from "../../utils/helpers/get-query-params";
+import { getQueryString } from "../../utils/helpers/get-query-string";
+import type { QueryParams } from "../../utils/types/query-params";
import { CommentNodes } from "../comment/comment-nodes";
import { BannerIconHeader } from "../common/banner-icon-header";
import { DataTypeSelect } from "../common/data-type-select";
import { i18n } from "../../i18next";
import { UserService } from "../../services";
import {
- amAdmin,
- amMod,
- amTopMod,
getUnixTime,
hostname,
mdToHtml,
myAuthRequired,
} 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 { Badges } from "../common/badges";
import { BannerIconHeader } from "../common/banner-icon-header";
import { Icon, PurgeWarning, Spinner } from "../common/icon";
import { FirstLoadService } from "../../services/FirstLoadService";
import { HttpService, RequestState } from "../../services/HttpService";
import {
- canCreateCommunity,
commentsToFlatNodes,
editComment,
editPost,
getCommentParentId,
getDataTypeString,
getPageFromString,
- getQueryParams,
- getQueryString,
getRandomFromList,
mdToHtml,
myAuth,
postToCommentSortType,
- QueryParams,
relTags,
restoreScrollPosition,
RouteDataResponse,
trendingFetchLimit,
updatePersonBlock,
} from "../../utils";
+import { getQueryParams } from "../../utils/helpers/get-query-params";
+import { getQueryString } from "../../utils/helpers/get-query-string";
+import { canCreateCommunity } from "../../utils/roles/can-create-community";
+import type { QueryParams } from "../../utils/types/query-params";
import { CommentNodes } from "../comment/comment-nodes";
import { DataTypeSelect } from "../common/data-type-select";
import { HtmlTags } from "../common/html-tags";
import { i18n } from "../../i18next";
import { UserService } from "../../services";
import { HttpService, RequestState } from "../../services/HttpService";
-import { isBrowser, myAuth, setIsoData, toast, validEmail } from "../../utils";
+import { myAuth, setIsoData, toast, validEmail } from "../../utils";
+import { isBrowser } from "../../utils/browser/is-browser";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
import { UserService } from "../../services";
import { HttpService, RequestState } from "../../services/HttpService";
import {
- isBrowser,
joinLemmyUrl,
mdToHtml,
myAuth,
toast,
validEmail,
} from "../../utils";
+import { isBrowser } from "../../utils/browser/is-browser";
import { HtmlTags } from "../common/html-tags";
import { Icon, Spinner } from "../common/icon";
import { MarkdownTextArea } from "../common/markdown-textarea";
import { HttpService, RequestState } from "../services/HttpService";
import {
Choice,
- QueryParams,
- RouteDataResponse,
- amAdmin,
- amMod,
- debounce,
fetchLimit,
fetchUsers,
getIdFromString,
getPageFromString,
- getQueryParams,
- getQueryString,
getUpdatedSearchId,
myAuth,
personToChoice,
setIsoData,
} from "../utils";
+import { debounce } from "../utils/helpers/debounce";
+import { getQueryParams } from "../utils/helpers/get-query-params";
+import { getQueryString } from "../utils/helpers/get-query-string";
+import { amAdmin } from "../utils/roles/am-admin";
+import { amMod } from "../utils/roles/am-mod";
+import type { QueryParams } from "../utils/types/query-params";
import { HtmlTags } from "./common/html-tags";
import { Icon, Spinner } from "./common/icon";
import { MomentTime } from "./common/moment-time";
import { FirstLoadService } from "../../services/FirstLoadService";
import { HttpService, RequestState } from "../../services/HttpService";
import {
- QueryParams,
- RouteDataResponse,
- canMod,
capitalizeFirstLetter,
editComment,
editPost,
futureDaysToUnixTime,
getCommentParentId,
getPageFromString,
- getQueryParams,
- getQueryString,
- isAdmin,
- isBanned,
mdToHtml,
myAuth,
myAuthRequired,
toast,
updatePersonBlock,
} from "../../utils";
+import { getQueryParams } from "../../utils/helpers/get-query-params";
+import { getQueryString } from "../../utils/helpers/get-query-string";
+import { canMod } from "../../utils/roles/can-mod";
+import { isAdmin } from "../../utils/roles/is-admin";
+import { isBanned } from "../../utils/roles/is-banned";
+import type { QueryParams } from "../../utils/types/query-params";
import { BannerIconHeader } from "../common/banner-icon-header";
import { HtmlTags } from "../common/html-tags";
import { Icon, Spinner } from "../common/icon";
import { RequestState } from "../../services/HttpService";
import {
RouteDataResponse,
- amAdmin,
editCommentReport,
editPostReport,
editPrivateMessageReport,
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";
Choice,
capitalizeFirstLetter,
communityToChoice,
- debounce,
elementUrl,
emDash,
fetchCommunities,
updateCommunityBlock,
updatePersonBlock,
} from "../../utils";
+import { debounce } from "../../utils/helpers/debounce";
import { HtmlTags } from "../common/html-tags";
import { Icon, Spinner } from "../common/icon";
import { ImageUploadForm } from "../common/image-upload-form";
} from "../../services/HttpService";
import {
Choice,
- QueryParams,
RouteDataResponse,
enableDownvotes,
enableNsfw,
getIdFromString,
- getQueryParams,
myAuth,
setIsoData,
} from "../../utils";
+import { getQueryParams } from "../../utils/helpers/get-query-params";
+import type { QueryParams } from "../../utils/types/query-params";
import { HtmlTags } from "../common/html-tags";
import { Spinner } from "../common/icon";
import { PostForm } from "./post-form";
archiveTodayUrl,
capitalizeFirstLetter,
communityToChoice,
- debounce,
fetchCommunities,
getIdFromString,
ghostArchiveUrl,
validURL,
webArchiveUrl,
} from "../../utils";
+import { debounce } from "../../utils/helpers/debounce";
import { Icon, Spinner } from "../common/icon";
import { LanguageSelect } from "../common/language-select";
import { MarkdownTextArea } from "../common/markdown-textarea";
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,
numToSI,
relTags,
setupTippy,
- share,
showScores,
} from "../../utils";
+import { canShare } from "../../utils/browser/can-share";
+import { share } from "../../utils/browser/share";
+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";
buildCommentsTree,
commentsToFlatNodes,
commentTreeMaxDepth,
- debounce,
editComment,
editWith,
enableDownvotes,
getCommentParentId,
getDepthFromComment,
getIdFromProps,
- isBrowser,
isImage,
myAuth,
restoreScrollPosition,
updateCommunityBlock,
updatePersonBlock,
} from "../../utils";
+import { isBrowser } from "../../utils/browser/is-browser";
+import { debounce } from "../../utils/helpers/debounce";
import { CommentForm } from "../comment/comment-form";
import { CommentNodes } from "../comment/comment-nodes";
import { HtmlTags } from "../common/html-tags";
import { HttpService, RequestState } from "../services/HttpService";
import {
Choice,
- QueryParams,
RouteDataResponse,
capitalizeFirstLetter,
commentsToFlatNodes,
communityToChoice,
- debounce,
enableDownvotes,
enableNsfw,
fetchCommunities,
fetchUsers,
getIdFromString,
getPageFromString,
- getQueryParams,
- getQueryString,
getUpdatedSearchId,
myAuth,
numToSI,
setIsoData,
showLocal,
} from "../utils";
+import { debounce } from "../utils/helpers/debounce";
+import { getQueryParams } from "../utils/helpers/get-query-params";
+import { getQueryString } from "../utils/helpers/get-query-string";
+import type { QueryParams } from "../utils/types/query-params";
import { CommentNodes } from "./comment/comment-nodes";
import { HtmlTags } from "./common/html-tags";
import { Spinner } from "./common/icon";
-import { isBrowser } from "./utils";
+import { isBrowser } from "./utils/browser/is-browser";
const testHost = "0.0.0.0:8536";
import { vi } from "./translations/vi";
import { zh } from "./translations/zh";
import { zh_Hant } from "./translations/zh_Hant";
-import { isBrowser } from "./utils";
+import { isBrowser } from "./utils/browser/is-browser";
export const languages = [
{ resource: ar, code: "ar", name: "العربية" },
import { LoginResponse, MyUserInfo } from "lemmy-js-client";
import { isHttps } from "../env";
import { i18n } from "../i18next";
-import { isAuthPath, isBrowser, toast } from "../utils";
+import { isAuthPath, toast } from "../utils";
+import { isBrowser } from "../utils/browser/is-browser";
interface Claims {
sub: number;
CommentReportView,
CommentSortType,
CommentView,
- CommunityModeratorView,
CommunityView,
CustomEmojiView,
GetSiteMetadata,
Language,
LemmyHttp,
MyUserInfo,
- Person,
PersonMentionView,
PersonView,
PostReportView,
VoteType,
} from "./interfaces";
import { HttpService, UserService } from "./services";
+import { isBrowser } from "./utils/browser/is-browser";
+import { debounce } from "./utils/helpers/debounce";
+import { groupBy } from "./utils/helpers/group-by";
import { RequestState } from "./services/HttpService";
let Tribute: any;
: 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))/;
const tldRegex = /([a-z0-9]+\.)*[a-z0-9]+\.[a-z]+/;
return dt === DataType.Post ? "Post" : "Comment";
}
-export function debounce<T extends any[], R>(
- func: (...e: T) => R,
- wait = 1000,
- immediate = false
-) {
- // 'private' variable for instance
- // The returned function will be able to reference this due to closure.
- // Each call to the returned function will share this common timer.
- let timeout: NodeJS.Timeout | null;
-
- // Calling debounce returns a new anonymous function
- return function () {
- // reference the context and args for the setTimeout function
- const args = arguments;
-
- // Should the function be called now? If immediate is true
- // and not already in a timeout then the answer is: Yes
- const callNow = immediate && !timeout;
-
- // This is the basic debounce behavior where you can call this
- // function several times, but it will only execute once
- // [before or after imposing a delay].
- // Each time the returned function is called, the timer starts over.
- clearTimeout(timeout ?? undefined);
-
- // Set the new timeout
- timeout = setTimeout(function () {
- // Inside the timeout function, clear the timeout variable
- // which will let the next execution run when in 'immediate' mode
- timeout = null;
-
- // Check if the function already ran with the immediate flag
- if (!immediate) {
- // Call the original function with apply
- // apply lets you define the 'this' object as well as the arguments
- // (both captured before setTimeout)
- func.apply(this, args);
- }
- }, wait);
-
- // Immediate mode and no wait timer? Execute the function..
- if (callNow) func.apply(this, args);
- } as (...e: T) => R;
-}
-
export async function fetchThemeList(): Promise<string[]> {
return fetch("/css/themelist").then(res => res.json());
}
`;
}
-export function isBrowser() {
- return typeof window !== "undefined";
-}
-
export function setIsoData<T extends RouteData>(context: any): IsoData<T> {
// If its the browser, you need to deserialize the data from the window
if (isBrowser()) {
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();
}
}
}
-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
src: string;
}
-const groupBy = <T>(
- array: T[],
- predicate: (value: T, index: number, array: T[]) => string
-) =>
- array.reduce((acc, value, index, array) => {
- (acc[predicate(value, index, array)] ||= []).push(value);
- return acc;
- }, {} as { [key: string]: T[] });
-
-export type QueryParams<T extends Record<string, any>> = {
- [key in keyof T]?: string;
-};
-
-export function getQueryParams<T extends Record<string, any>>(processors: {
- [K in keyof T]: (param: string) => T[K];
-}): T {
- if (isBrowser()) {
- const searchParams = new URLSearchParams(window.location.search);
-
- return Array.from(Object.entries(processors)).reduce(
- (acc, [key, process]) => ({
- ...acc,
- [key]: process(searchParams.get(key)),
- }),
- {} as T
- );
- }
-
- return {} as T;
-}
-
-export function getQueryString<T extends Record<string, string | undefined>>(
- obj: T
-) {
- return Object.entries(obj)
- .filter(([, val]) => val !== undefined && val !== null)
- .reduce(
- (acc, [key, val], index) => `${acc}${index > 0 ? "&" : ""}${key}=${val}`,
- "?"
- );
-}
-
export function isAuthPath(pathname: string) {
return /create_.*|inbox|settings|admin|reports|registration_applications/g.test(
pathname
);
}
-export function canShare() {
- return isBrowser() && !!navigator.canShare;
-}
-
-export function share(shareData: ShareData) {
- if (isBrowser()) {
- navigator.share(shareData);
- }
-}
-
export function newVote(voteType: VoteType, myVote?: number): number {
if (voteType == VoteType.Upvote) {
return myVote == 1 ? 0 : 1;
export type RouteDataResponse<T extends Record<string, any>> = {
[K in keyof T]: RequestState<T[K]>;
};
-
-function sleep(millis: number): Promise<void> {
- return new Promise(resolve => setTimeout(resolve, millis));
-}
-
-/**
- * Polls / repeatedly runs a promise, every X milliseconds
- */
-export async function poll(promiseFn: any, millis: number) {
- if (window.document.visibilityState !== "hidden") {
- await promiseFn();
- }
- await sleep(millis);
- return poll(promiseFn, millis);
-}
--- /dev/null
+import { isBrowser } from "./is-browser";
+
+export function canShare() {
+ return isBrowser() && !!navigator.canShare;
+}
--- /dev/null
+export function isBrowser() {
+ return typeof window !== "undefined";
+}
--- /dev/null
+import { isBrowser } from "./is-browser";
+
+export function share(shareData: ShareData) {
+ if (isBrowser()) {
+ navigator.share(shareData);
+ }
+}
--- /dev/null
+export function debounce<T extends any[], R>(
+ func: (...e: T) => R,
+ wait = 1000,
+ immediate = false
+) {
+ let timeout: NodeJS.Timeout | null;
+
+ return function () {
+ const args = arguments;
+ const callNow = immediate && !timeout;
+
+ clearTimeout(timeout ?? undefined);
+
+ timeout = setTimeout(function () {
+ timeout = null;
+
+ if (!immediate) {
+ func.apply(this, args);
+ }
+ }, wait);
+
+ if (callNow) func.apply(this, args);
+ } as (...e: T) => R;
+}
--- /dev/null
+import { isBrowser } from "../browser/is-browser";
+
+export function getQueryParams<T extends Record<string, any>>(processors: {
+ [K in keyof T]: (param: string) => T[K];
+}): T {
+ if (isBrowser()) {
+ const searchParams = new URLSearchParams(window.location.search);
+
+ return Array.from(Object.entries(processors)).reduce(
+ (acc, [key, process]) => ({
+ ...acc,
+ [key]: process(searchParams.get(key)),
+ }),
+ {} as T
+ );
+ }
+
+ return {} as T;
+}
--- /dev/null
+export function getQueryString<T extends Record<string, string | undefined>>(
+ obj: T
+) {
+ return Object.entries(obj)
+ .filter(([, val]) => val !== undefined && val !== null)
+ .reduce(
+ (acc, [key, val], index) => `${acc}${index > 0 ? "&" : ""}${key}=${val}`,
+ "?"
+ );
+}
--- /dev/null
+export const groupBy = <T>(
+ array: T[],
+ predicate: (value: T, index: number, array: T[]) => string
+) =>
+ array.reduce((acc, value, index, array) => {
+ (acc[predicate(value, index, array)] ||= []).push(value);
+ return acc;
+ }, {} as { [key: string]: T[] });
--- /dev/null
+import { sleep } from "./sleep";
+
+/**
+ * Polls / repeatedly runs a promise, every X milliseconds
+ */
+export async function poll(promiseFn: any, millis: number) {
+ if (window.document.visibilityState !== "hidden") {
+ await promiseFn();
+ }
+ await sleep(millis);
+ return poll(promiseFn, millis);
+}
--- /dev/null
+export function sleep(millis: number): Promise<void> {
+ return new Promise(resolve => setTimeout(resolve, millis));
+}
--- /dev/null
+import { UserService } from "../../services";
+
+export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean {
+ return myUserInfo?.local_user_view.person.admin ?? false;
+}
--- /dev/null
+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;
+}
--- /dev/null
+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;
+}
--- /dev/null
+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;
+}
--- /dev/null
+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;
+}
--- /dev/null
+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);
+}
--- /dev/null
+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);
+}
--- /dev/null
+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;
+ }
+}
--- /dev/null
+import { PersonView } from "lemmy-js-client";
+
+export function isAdmin(creatorId: number, admins?: PersonView[]): boolean {
+ return admins?.map(a => a.person.id).includes(creatorId) ?? false;
+}
--- /dev/null
+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;
+ }
+}
--- /dev/null
+import { CommunityModeratorView } from "lemmy-js-client";
+
+export function isMod(
+ creatorId: number,
+ mods?: CommunityModeratorView[]
+): boolean {
+ return mods?.map(m => m.moderator.id).includes(creatorId) ?? false;
+}
--- /dev/null
+export type QueryParams<T extends Record<string, any>> = {
+ [key in keyof T]?: string;
+};