1 import { Picker } from "emoji-mart";
2 import emojiShortName from "emoji-short-name";
4 BlockCommunityResponse,
23 PrivateMessageReportView,
25 RegistrationApplicationView,
29 } from "lemmy-js-client";
30 import { default as MarkdownIt } from "markdown-it";
31 import markdown_it_container from "markdown-it-container";
32 import markdown_it_emoji from "markdown-it-emoji/bare";
33 import markdown_it_footnote from "markdown-it-footnote";
34 import markdown_it_html5_embed from "markdown-it-html5-embed";
35 import markdown_it_sub from "markdown-it-sub";
36 import markdown_it_sup from "markdown-it-sup";
37 import Renderer from "markdown-it/lib/renderer";
38 import Token from "markdown-it/lib/token";
39 import moment from "moment";
40 import tippy from "tippy.js";
41 import Toastify from "toastify-js";
42 import { getHttpBase } from "./env";
43 import { i18n, languages } from "./i18next";
44 import { CommentNodeI, DataType, IsoData, VoteType } from "./interfaces";
45 import { HttpService, UserService } from "./services";
49 Tribute = require("tributejs");
52 export const favIconUrl = "/static/assets/icons/favicon.svg";
53 export const favIconPngUrl = "/static/assets/icons/apple-touch-icon.png";
55 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
56 export const repoUrl = "https://github.com/LemmyNet";
57 export const joinLemmyUrl = "https://join-lemmy.org";
58 export const donateLemmyUrl = `${joinLemmyUrl}/donate`;
59 export const docsUrl = `${joinLemmyUrl}/docs/en/index.html`;
60 export const helpGuideUrl = `${joinLemmyUrl}/docs/en/users/01-getting-started.html`; // TODO find a way to redirect to the non-en folder
61 export const markdownHelpUrl = `${joinLemmyUrl}/docs/en/users/02-media.html`;
62 export const sortingHelpUrl = `${joinLemmyUrl}/docs/en/users/03-votes-and-ranking.html`;
63 export const archiveTodayUrl = "https://archive.today";
64 export const ghostArchiveUrl = "https://ghostarchive.org";
65 export const webArchiveUrl = "https://web.archive.org";
66 export const elementUrl = "https://element.io";
68 export const postRefetchSeconds: number = 60 * 1000;
69 export const fetchLimit = 40;
70 export const trendingFetchLimit = 6;
71 export const mentionDropdownFetchLimit = 10;
72 export const commentTreeMaxDepth = 8;
73 export const markdownFieldCharacterLimit = 50000;
74 export const maxUploadImages = 20;
75 export const concurrentImageUpload = 4;
76 export const updateUnreadCountsInterval = 30000;
78 export const relTags = "noopener nofollow";
80 export const emDash = "\u2014";
82 export type ThemeColor =
105 export interface ErrorPageData {
107 adminMatrixIds?: string[];
110 const customEmojis: EmojiMartCategory[] = [];
111 export let customEmojisLookup: Map<string, CustomEmojiView> = new Map<
116 const DEFAULT_ALPHABET =
117 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
119 function getRandomCharFromAlphabet(alphabet: string): string {
120 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
123 export function getIdFromString(id?: string): number | undefined {
124 return id && id !== "0" && !Number.isNaN(Number(id)) ? Number(id) : undefined;
127 export function getPageFromString(page?: string): number {
128 return page && !Number.isNaN(Number(page)) ? Number(page) : 1;
131 export function randomStr(
132 idDesiredLength = 20,
133 alphabet = DEFAULT_ALPHABET
136 * Create n-long array and map it to random chars from given alphabet.
137 * Then join individual chars as string
139 return Array.from({ length: idDesiredLength })
141 return getRandomCharFromAlphabet(alphabet);
146 const html5EmbedConfig = {
148 useImageSyntax: true, // Enables video/audio embed with ![]() syntax (default)
150 audio: 'controls preload="metadata"',
151 video: 'width="100%" max-height="100%" controls loop preload="metadata"',
156 const spoilerConfig = {
157 validate: (params: string) => {
158 return params.trim().match(/^spoiler\s+(.*)$/);
161 render: (tokens: any, idx: any) => {
162 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
164 if (tokens[idx].nesting === 1) {
166 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
169 return "</details>\n";
174 export let md: MarkdownIt = new MarkdownIt();
176 export let mdNoImages: MarkdownIt = new MarkdownIt();
178 export function hotRankComment(comment_view: CommentView): number {
179 return hotRank(comment_view.counts.score, comment_view.comment.published);
182 export function hotRankActivePost(post_view: PostView): number {
183 return hotRank(post_view.counts.score, post_view.counts.newest_comment_time);
186 export function hotRankPost(post_view: PostView): number {
187 return hotRank(post_view.counts.score, post_view.post.published);
190 export function hotRank(score: number, timeStr: string): number {
191 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
192 const date: Date = new Date(timeStr + "Z"); // Add Z to convert from UTC date
193 const now: Date = new Date();
194 const hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
197 (10000 * Math.log10(Math.max(1, 3 + Number(score)))) /
198 Math.pow(hoursElapsed + 2, 1.8);
200 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
205 export function mdToHtml(text: string) {
206 return { __html: md.render(text) };
209 export function mdToHtmlNoImages(text: string) {
210 return { __html: mdNoImages.render(text) };
213 export function mdToHtmlInline(text: string) {
214 return { __html: md.renderInline(text) };
217 export function getUnixTime(text?: string): number | undefined {
218 return text ? new Date(text).getTime() / 1000 : undefined;
221 export function futureDaysToUnixTime(days?: number): number | undefined {
224 new Date(Date.now() + 1000 * 60 * 60 * 24 * days).getTime() / 1000
229 const imageRegex = /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/;
230 const videoRegex = /(http)?s?:?(\/\/[^"']*\.(?:mp4|webm))/;
232 export function isImage(url: string) {
233 return imageRegex.test(url);
236 export function isVideo(url: string) {
237 return videoRegex.test(url);
240 export function validURL(str: string) {
241 return !!new URL(str);
244 export function communityRSSUrl(actorId: string, sort: string): string {
245 const url = new URL(actorId);
246 return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
249 export function validEmail(email: string) {
251 /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
252 return re.test(String(email).toLowerCase());
255 export function capitalizeFirstLetter(str: string): string {
256 return str.charAt(0).toUpperCase() + str.slice(1);
259 export async function getSiteMetadata(url: string) {
260 const form: GetSiteMetadata = { url };
261 const client = new LemmyHttp(getHttpBase());
262 return client.getSiteMetadata(form);
265 export function getDataTypeString(dt: DataType) {
266 return dt === DataType.Post ? "Post" : "Comment";
269 export function debounce<T extends any[], R>(
270 func: (...e: T) => R,
274 // 'private' variable for instance
275 // The returned function will be able to reference this due to closure.
276 // Each call to the returned function will share this common timer.
277 let timeout: NodeJS.Timeout | null;
279 // Calling debounce returns a new anonymous function
281 // reference the context and args for the setTimeout function
282 const args = arguments;
284 // Should the function be called now? If immediate is true
285 // and not already in a timeout then the answer is: Yes
286 const callNow = immediate && !timeout;
288 // This is the basic debounce behavior where you can call this
289 // function several times, but it will only execute once
290 // [before or after imposing a delay].
291 // Each time the returned function is called, the timer starts over.
292 clearTimeout(timeout ?? undefined);
294 // Set the new timeout
295 timeout = setTimeout(function () {
296 // Inside the timeout function, clear the timeout variable
297 // which will let the next execution run when in 'immediate' mode
300 // Check if the function already ran with the immediate flag
302 // Call the original function with apply
303 // apply lets you define the 'this' object as well as the arguments
304 // (both captured before setTimeout)
305 func.apply(this, args);
309 // Immediate mode and no wait timer? Execute the function..
310 if (callNow) func.apply(this, args);
314 export function getLanguages(
316 myUserInfo = UserService.Instance.myUserInfo
318 const myLang = myUserInfo?.local_user_view.local_user.interface_language;
319 const lang = override || myLang || "browser";
321 if (lang == "browser" && isBrowser()) {
322 return getBrowserLanguages();
328 function getBrowserLanguages(): string[] {
329 // Intersect lemmy's langs, with the browser langs
330 const langs = languages ? languages.map(l => l.code) : ["en"];
332 // NOTE, mobile browsers seem to be missing this list, so append en
333 const allowedLangs = navigator.languages
335 .filter(v => langs.includes(v));
339 export async function fetchThemeList(): Promise<string[]> {
340 return fetch("/css/themelist").then(res => res.json());
343 export async function setTheme(theme: string, forceReload = false) {
347 if (theme === "browser" && !forceReload) {
350 // This is only run on a force reload
351 if (theme == "browser") {
355 const themeList = await fetchThemeList();
357 // Unload all the other themes
358 for (var i = 0; i < themeList.length; i++) {
359 const styleSheet = document.getElementById(themeList[i]);
361 styleSheet.setAttribute("disabled", "disabled");
366 .getElementById("default-light")
367 ?.setAttribute("disabled", "disabled");
368 document.getElementById("default-dark")?.setAttribute("disabled", "disabled");
370 // Load the theme dynamically
371 const cssLoc = `/css/themes/${theme}.css`;
373 loadCss(theme, cssLoc);
374 document.getElementById(theme)?.removeAttribute("disabled");
377 export function loadCss(id: string, loc: string) {
378 if (!document.getElementById(id)) {
379 var head = document.getElementsByTagName("head")[0];
380 var link = document.createElement("link");
382 link.rel = "stylesheet";
383 link.type = "text/css";
386 head.appendChild(link);
390 export function objectFlip(obj: any) {
392 Object.keys(obj).forEach(key => {
398 export function showAvatars(
399 myUserInfo = UserService.Instance.myUserInfo
401 return myUserInfo?.local_user_view.local_user.show_avatars ?? true;
404 export function showScores(
405 myUserInfo = UserService.Instance.myUserInfo
407 return myUserInfo?.local_user_view.local_user.show_scores ?? true;
410 export function isCakeDay(published: string): boolean {
411 // moment(undefined) or moment.utc(undefined) returns the current date/time
412 // moment(null) or moment.utc(null) returns null
413 const createDate = moment.utc(published).local();
414 const currentDate = moment(new Date());
417 createDate.date() === currentDate.date() &&
418 createDate.month() === currentDate.month() &&
419 createDate.year() !== currentDate.year()
423 export function toast(text: string, background: ThemeColor = "success") {
425 const backgroundColor = `var(--${background})`;
428 backgroundColor: backgroundColor,
436 export function pictrsDeleteToast(filename: string, deleteUrl: string) {
438 const clickToDeleteText = i18n.t("click_to_delete_picture", { filename });
439 const deletePictureText = i18n.t("picture_deleted", {
442 const failedDeletePictureText = i18n.t("failed_to_delete_picture", {
446 const backgroundColor = `var(--light)`;
448 const toast = Toastify({
449 text: clickToDeleteText,
450 backgroundColor: backgroundColor,
456 fetch(deleteUrl).then(res => {
458 if (res.ok === true) {
459 alert(deletePictureText);
461 alert(failedDeletePictureText);
473 export function setupTribute() {
475 noMatchTemplate: function () {
482 menuItemTemplate: (item: any) => {
483 const shortName = `:${item.original.key}:`;
484 return `${item.original.val} ${shortName}`;
486 selectTemplate: (item: any) => {
487 const customEmoji = customEmojisLookup.get(
490 if (customEmoji == undefined) return `${item.original.val}`;
492 return `![${customEmoji.alt_text}](${customEmoji.image_url} "${customEmoji.shortcode}")`;
494 values: Object.entries(emojiShortName)
496 return { key: e[1], val: e[0] };
499 Array.from(customEmojisLookup.entries()).map(k => ({
501 val: `<img class="icon icon-emoji" src="${k[1].custom_emoji.image_url}" title="${k[1].custom_emoji.shortcode}" alt="${k[1].custom_emoji.alt_text}" />`,
505 autocompleteMode: true,
507 // menuItemLimit: mentionDropdownFetchLimit,
508 menuShowMinLength: 2,
513 selectTemplate: (item: any) => {
514 const it: PersonTribute = item.original;
515 return `[${it.key}](${it.view.person.actor_id})`;
517 values: debounce(async (text: string, cb: any) => {
518 cb(await personSearch(text));
521 autocompleteMode: true,
523 // menuItemLimit: mentionDropdownFetchLimit,
524 menuShowMinLength: 2,
530 selectTemplate: (item: any) => {
531 const it: CommunityTribute = item.original;
532 return `[${it.key}](${it.view.community.actor_id})`;
534 values: debounce(async (text: string, cb: any) => {
535 cb(await communitySearch(text));
538 autocompleteMode: true,
540 // menuItemLimit: mentionDropdownFetchLimit,
541 menuShowMinLength: 2,
547 function setupEmojiDataModel(custom_emoji_views: CustomEmojiView[]) {
548 const groupedEmojis = groupBy(
550 x => x.custom_emoji.category
552 for (const [category, emojis] of Object.entries(groupedEmojis)) {
556 emojis: emojis.map(emoji => ({
557 id: emoji.custom_emoji.shortcode,
558 name: emoji.custom_emoji.shortcode,
559 keywords: emoji.keywords.map(x => x.keyword),
560 skins: [{ src: emoji.custom_emoji.image_url }],
564 customEmojisLookup = new Map(
565 custom_emoji_views.map(view => [view.custom_emoji.shortcode, view])
569 export function updateEmojiDataModel(custom_emoji_view: CustomEmojiView) {
570 const emoji: EmojiMartCustomEmoji = {
571 id: custom_emoji_view.custom_emoji.shortcode,
572 name: custom_emoji_view.custom_emoji.shortcode,
573 keywords: custom_emoji_view.keywords.map(x => x.keyword),
574 skins: [{ src: custom_emoji_view.custom_emoji.image_url }],
576 const categoryIndex = customEmojis.findIndex(
577 x => x.id == custom_emoji_view.custom_emoji.category
579 if (categoryIndex == -1) {
581 id: custom_emoji_view.custom_emoji.category,
582 name: custom_emoji_view.custom_emoji.category,
586 const emojiIndex = customEmojis[categoryIndex].emojis.findIndex(
587 x => x.id == custom_emoji_view.custom_emoji.shortcode
589 if (emojiIndex == -1) {
590 customEmojis[categoryIndex].emojis.push(emoji);
592 customEmojis[categoryIndex].emojis[emojiIndex] = emoji;
595 customEmojisLookup.set(
596 custom_emoji_view.custom_emoji.shortcode,
601 export function removeFromEmojiDataModel(id: number) {
602 let view: CustomEmojiView | undefined;
603 for (const item of customEmojisLookup.values()) {
604 if (item.custom_emoji.id === id) {
610 const categoryIndex = customEmojis.findIndex(
611 x => x.id == view?.custom_emoji.category
613 const emojiIndex = customEmojis[categoryIndex].emojis.findIndex(
614 x => x.id == view?.custom_emoji.shortcode
616 customEmojis[categoryIndex].emojis = customEmojis[
618 ].emojis.splice(emojiIndex, 1);
620 customEmojisLookup.delete(view?.custom_emoji.shortcode);
623 function setupMarkdown() {
624 const markdownItConfig: MarkdownIt.Options = {
630 const emojiDefs = Array.from(customEmojisLookup.entries()).reduce(
631 (main, [key, value]) => ({ ...main, [key]: value }),
634 md = new MarkdownIt(markdownItConfig)
635 .use(markdown_it_sub)
636 .use(markdown_it_sup)
637 .use(markdown_it_footnote)
638 .use(markdown_it_html5_embed, html5EmbedConfig)
639 .use(markdown_it_container, "spoiler", spoilerConfig)
640 .use(markdown_it_emoji, {
644 mdNoImages = new MarkdownIt(markdownItConfig)
645 .use(markdown_it_sub)
646 .use(markdown_it_sup)
647 .use(markdown_it_footnote)
648 .use(markdown_it_html5_embed, html5EmbedConfig)
649 .use(markdown_it_container, "spoiler", spoilerConfig)
650 .use(markdown_it_emoji, {
654 var defaultRenderer = md.renderer.rules.image;
655 md.renderer.rules.image = function (
658 options: MarkdownIt.Options,
662 //Provide custom renderer for our emojis to allow us to add a css class and force size dimensions on them.
663 const item = tokens[idx] as any;
664 const title = item.attrs.length >= 3 ? item.attrs[2][1] : "";
665 const src: string = item.attrs[0][1];
666 const isCustomEmoji = customEmojisLookup.get(title) != undefined;
667 if (!isCustomEmoji) {
668 return defaultRenderer?.(tokens, idx, options, env, self) ?? "";
670 const alt_text = item.content;
671 return `<img class="icon icon-emoji" src="${src}" title="${title}" alt="${alt_text}"/>`;
675 export function getEmojiMart(
676 onEmojiSelect: (e: any) => void,
677 customPickerOptions: any = {}
679 const pickerOptions = {
680 ...customPickerOptions,
681 onEmojiSelect: onEmojiSelect,
682 custom: customEmojis,
684 return new Picker(pickerOptions);
687 var tippyInstance: any;
689 tippyInstance = tippy("[data-tippy-content]");
692 export function setupTippy() {
694 tippyInstance.forEach((e: any) => e.destroy());
695 tippyInstance = tippy("[data-tippy-content]", {
697 // Display on "long press"
698 touch: ["hold", 500],
703 interface PersonTribute {
708 async function personSearch(text: string): Promise<PersonTribute[]> {
709 const usersResponse = await fetchUsers(text);
711 return usersResponse.map(pv => ({
712 key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
717 interface CommunityTribute {
722 async function communitySearch(text: string): Promise<CommunityTribute[]> {
723 const communitiesResponse = await fetchCommunities(text);
725 return communitiesResponse.map(cv => ({
726 key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
731 export function getRecipientIdFromProps(props: any): number {
732 return props.match.params.recipient_id
733 ? Number(props.match.params.recipient_id)
737 export function getIdFromProps(props: any): number | undefined {
738 const id = props.match.params.post_id;
739 return id ? Number(id) : undefined;
742 export function getCommentIdFromProps(props: any): number | undefined {
743 const id = props.match.params.comment_id;
744 return id ? Number(id) : undefined;
747 type ImmutableListKey =
756 | "private_message_report"
757 | "registration_application";
759 function editListImmutable<
760 T extends { [key in F]: { id: number } },
761 F extends ImmutableListKey
762 >(fieldName: F, data: T, list: T[]): T[] {
764 ...list.map(c => (c[fieldName].id === data[fieldName].id ? data : c)),
768 export function editComment(
770 comments: CommentView[]
772 return editListImmutable("comment", data, comments);
775 export function editCommentReply(
776 data: CommentReplyView,
777 replies: CommentReplyView[]
778 ): CommentReplyView[] {
779 return editListImmutable("comment_reply", data, replies);
782 interface WithComment {
784 counts: CommentAggregates;
789 export function editMention(
790 data: PersonMentionView,
791 comments: PersonMentionView[]
792 ): PersonMentionView[] {
793 return editListImmutable("person_mention", data, comments);
796 export function editCommunity(
798 communities: CommunityView[]
800 return editListImmutable("community", data, communities);
803 export function editPrivateMessage(
804 data: PrivateMessageView,
805 messages: PrivateMessageView[]
806 ): PrivateMessageView[] {
807 return editListImmutable("private_message", data, messages);
810 export function editPost(data: PostView, posts: PostView[]): PostView[] {
811 return editListImmutable("post", data, posts);
814 export function editPostReport(
815 data: PostReportView,
816 reports: PostReportView[]
818 return editListImmutable("post_report", data, reports);
821 export function editCommentReport(
822 data: CommentReportView,
823 reports: CommentReportView[]
824 ): CommentReportView[] {
825 return editListImmutable("comment_report", data, reports);
828 export function editPrivateMessageReport(
829 data: PrivateMessageReportView,
830 reports: PrivateMessageReportView[]
831 ): PrivateMessageReportView[] {
832 return editListImmutable("private_message_report", data, reports);
835 export function editRegistrationApplication(
836 data: RegistrationApplicationView,
837 apps: RegistrationApplicationView[]
838 ): RegistrationApplicationView[] {
839 return editListImmutable("registration_application", data, apps);
842 export function editWith<D extends WithComment, L extends WithComment>(
843 { comment, counts, saved, my_vote }: D,
848 c.comment.id === comment.id
849 ? { ...c, comment, counts, saved, my_vote }
855 export function updatePersonBlock(
856 data: BlockPersonResponse,
857 myUserInfo: MyUserInfo | undefined = UserService.Instance.myUserInfo
861 myUserInfo.person_blocks.push({
862 person: myUserInfo.local_user_view.person,
863 target: data.person_view.person,
865 toast(`${i18n.t("blocked")} ${data.person_view.person.name}`);
867 myUserInfo.person_blocks = myUserInfo.person_blocks.filter(
868 i => i.target.id !== data.person_view.person.id
870 toast(`${i18n.t("unblocked")} ${data.person_view.person.name}`);
875 export function updateCommunityBlock(
876 data: BlockCommunityResponse,
877 myUserInfo: MyUserInfo | undefined = UserService.Instance.myUserInfo
881 myUserInfo.community_blocks.push({
882 person: myUserInfo.local_user_view.person,
883 community: data.community_view.community,
885 toast(`${i18n.t("blocked")} ${data.community_view.community.name}`);
887 myUserInfo.community_blocks = myUserInfo.community_blocks.filter(
888 i => i.community.id !== data.community_view.community.id
890 toast(`${i18n.t("unblocked")} ${data.community_view.community.name}`);
895 export function commentsToFlatNodes(comments: CommentView[]): CommentNodeI[] {
896 const nodes: CommentNodeI[] = [];
897 for (const comment of comments) {
898 nodes.push({ comment_view: comment, children: [], depth: 0 });
903 export function convertCommentSortType(sort: SortType): CommentSortType {
908 sort == "TopMonth" ||
912 } else if (sort == "New") {
914 } else if (sort == "Hot" || sort == "Active") {
921 export function buildCommentsTree(
922 comments: CommentView[],
923 parentComment: boolean
925 const map = new Map<number, CommentNodeI>();
926 const depthOffset = !parentComment
928 : getDepthFromComment(comments[0].comment) ?? 0;
930 for (const comment_view of comments) {
931 const depthI = getDepthFromComment(comment_view.comment) ?? 0;
932 const depth = depthI ? depthI - depthOffset : 0;
933 const node: CommentNodeI = {
938 map.set(comment_view.comment.id, { ...node });
941 const tree: CommentNodeI[] = [];
943 // if its a parent comment fetch, then push the first comment to the top node.
945 const cNode = map.get(comments[0].comment.id);
951 for (const comment_view of comments) {
952 const child = map.get(comment_view.comment.id);
954 const parent_id = getCommentParentId(comment_view.comment);
956 const parent = map.get(parent_id);
957 // Necessary because blocked comment might not exist
959 parent.children.push(child);
962 if (!parentComment) {
972 export function getCommentParentId(comment?: CommentI): number | undefined {
973 const split = comment?.path.split(".");
977 return split && split.length > 1
978 ? Number(split.at(split.length - 2))
982 export function getDepthFromComment(comment?: CommentI): number | undefined {
983 const len = comment?.path.split(".").length;
984 return len ? len - 2 : undefined;
987 // TODO make immutable
988 export function insertCommentIntoTree(
989 tree: CommentNodeI[],
991 parentComment: boolean
993 // Building a fake node to be used for later
994 const node: CommentNodeI = {
1000 const parentId = getCommentParentId(cv.comment);
1002 const parent_comment = searchCommentTree(tree, parentId);
1003 if (parent_comment) {
1004 node.depth = parent_comment.depth + 1;
1005 parent_comment.children.unshift(node);
1007 } else if (!parentComment) {
1012 export function searchCommentTree(
1013 tree: CommentNodeI[],
1015 ): CommentNodeI | undefined {
1016 for (const node of tree) {
1017 if (node.comment_view.comment.id === id) {
1021 for (const child of node.children) {
1022 const res = searchCommentTree([child], id);
1032 export const colorList: string[] = [
1042 function hsl(num: number) {
1043 return `hsla(${num}, 35%, 50%, 0.5)`;
1046 export function hostname(url: string): string {
1047 const cUrl = new URL(url);
1048 return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1051 export function validTitle(title?: string): boolean {
1052 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1053 if (!title || title.length < 3) return true;
1055 const regex = new RegExp(/.*\S.*/, "g");
1057 return regex.test(title);
1060 export function siteBannerCss(banner: string): string {
1062 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1063 background-attachment: fixed; \
1064 background-position: top; \
1065 background-repeat: no-repeat; \
1066 background-size: 100% cover; \
1069 max-height: 100vh; \
1073 export function isBrowser() {
1074 return typeof window !== "undefined";
1077 export function setIsoData(context: any): IsoData {
1078 // If its the browser, you need to deserialize the data from the window
1080 return window.isoData;
1081 } else return context.router.staticContext;
1084 moment.updateLocale("en", {
1105 export function saveScrollPosition(context: any) {
1106 const path: string = context.router.route.location.pathname;
1107 const y = window.scrollY;
1108 sessionStorage.setItem(`scrollPosition_${path}`, y.toString());
1111 export function restoreScrollPosition(context: any) {
1112 const path: string = context.router.route.location.pathname;
1113 const y = Number(sessionStorage.getItem(`scrollPosition_${path}`));
1114 window.scrollTo(0, y);
1117 export function showLocal(isoData: IsoData): boolean {
1118 return isoData.site_res.site_view.local_site.federation_enabled;
1121 export interface Choice {
1127 export function getUpdatedSearchId(id?: number | null, urlId?: number | null) {
1130 : ((id ?? urlId) === 0 ? undefined : id ?? urlId)?.toString();
1133 export function communityToChoice(cv: CommunityView): Choice {
1135 value: cv.community.id.toString(),
1136 label: communitySelectName(cv),
1140 export function personToChoice(pvs: PersonView): Choice {
1142 value: pvs.person.id.toString(),
1143 label: personSelectName(pvs),
1147 function fetchSearchResults(q: string, type_: SearchType) {
1148 const form: Search = {
1152 listing_type: "All",
1158 return HttpService.client.search(form);
1161 export async function fetchCommunities(q: string) {
1162 const res = await fetchSearchResults(q, "Communities");
1164 return res.state === "success" ? res.data.communities : [];
1167 export async function fetchUsers(q: string) {
1168 const res = await fetchSearchResults(q, "Users");
1170 return res.state === "success" ? res.data.users : [];
1173 export function communitySelectName(cv: CommunityView): string {
1174 return cv.community.local
1175 ? cv.community.title
1176 : `${hostname(cv.community.actor_id)}/${cv.community.title}`;
1179 export function personSelectName({
1180 person: { display_name, name, local, actor_id },
1181 }: PersonView): string {
1182 const pName = display_name ?? name;
1183 return local ? pName : `${hostname(actor_id)}/${pName}`;
1186 export function initializeSite(site?: GetSiteResponse) {
1187 UserService.Instance.myUserInfo = site?.my_user;
1188 i18n.changeLanguage(getLanguages()[0]);
1190 setupEmojiDataModel(site.custom_emojis ?? []);
1195 const SHORTNUM_SI_FORMAT = new Intl.NumberFormat("en-US", {
1196 maximumSignificantDigits: 3,
1198 notation: "compact",
1199 compactDisplay: "short",
1202 export function numToSI(value: number): string {
1203 return SHORTNUM_SI_FORMAT.format(value);
1206 export function myAuth(): string | undefined {
1207 return UserService.Instance.auth();
1210 export function myAuthRequired(): string {
1211 return UserService.Instance.auth(true) ?? "";
1214 export function enableDownvotes(siteRes: GetSiteResponse): boolean {
1215 return siteRes.site_view.local_site.enable_downvotes;
1218 export function enableNsfw(siteRes: GetSiteResponse): boolean {
1219 return siteRes.site_view.local_site.enable_nsfw;
1222 export function postToCommentSortType(sort: SortType): CommentSortType {
1237 export function isPostBlocked(
1239 myUserInfo: MyUserInfo | undefined = UserService.Instance.myUserInfo
1242 (myUserInfo?.community_blocks
1243 .map(c => c.community.id)
1244 .includes(pv.community.id) ||
1245 myUserInfo?.person_blocks
1246 .map(p => p.target.id)
1247 .includes(pv.creator.id)) ??
1252 /// Checks to make sure you can view NSFW posts. Returns true if you can.
1253 export function nsfwCheck(
1255 myUserInfo = UserService.Instance.myUserInfo
1257 const nsfw = pv.post.nsfw || pv.community.nsfw;
1258 const myShowNsfw = myUserInfo?.local_user_view.local_user.show_nsfw ?? false;
1259 return !nsfw || (nsfw && myShowNsfw);
1262 export function getRandomFromList<T>(list: T[]): T | undefined {
1263 return list.length == 0
1265 : list.at(Math.floor(Math.random() * list.length));
1269 * This shows what language you can select
1271 * Use showAll for the site form
1272 * Use showSite for the profile and community forms
1273 * Use false for both those to filter on your profile and site ones
1275 export function selectableLanguages(
1276 allLanguages: Language[],
1277 siteLanguages: number[],
1280 myUserInfo = UserService.Instance.myUserInfo
1282 const allLangIds = allLanguages.map(l => l.id);
1283 let myLangs = myUserInfo?.discussion_languages ?? allLangIds;
1284 myLangs = myLangs.length == 0 ? allLangIds : myLangs;
1285 const siteLangs = siteLanguages.length == 0 ? allLangIds : siteLanguages;
1288 return allLanguages;
1291 return allLanguages.filter(x => siteLangs.includes(x.id));
1294 .filter(x => siteLangs.includes(x.id))
1295 .filter(x => myLangs.includes(x.id));
1300 interface EmojiMartCategory {
1303 emojis: EmojiMartCustomEmoji[];
1306 interface EmojiMartCustomEmoji {
1310 skins: EmojiMartSkin[];
1313 interface EmojiMartSkin {
1317 const groupBy = <T>(
1319 predicate: (value: T, index: number, array: T[]) => string
1321 array.reduce((acc, value, index, array) => {
1322 (acc[predicate(value, index, array)] ||= []).push(value);
1324 }, {} as { [key: string]: T[] });
1326 export type QueryParams<T extends Record<string, any>> = {
1327 [key in keyof T]?: string;
1330 export function getQueryParams<T extends Record<string, any>>(processors: {
1331 [K in keyof T]: (param: string) => T[K];
1334 const searchParams = new URLSearchParams(window.location.search);
1336 return Array.from(Object.entries(processors)).reduce(
1337 (acc, [key, process]) => ({
1339 [key]: process(searchParams.get(key)),
1348 export function getQueryString<T extends Record<string, string | undefined>>(
1351 return Object.entries(obj)
1352 .filter(([, val]) => val !== undefined && val !== null)
1354 (acc, [key, val], index) => `${acc}${index > 0 ? "&" : ""}${key}=${val}`,
1359 export function isAuthPath(pathname: string) {
1360 return /create_.*|inbox|settings|admin|reports|registration_applications/g.test(
1365 export function canShare() {
1366 return isBrowser() && !!navigator.canShare;
1369 export function share(shareData: ShareData) {
1371 navigator.share(shareData);
1375 export function newVote(voteType: VoteType, myVote?: number): number {
1376 if (voteType == VoteType.Upvote) {
1377 return myVote == 1 ? 0 : 1;
1379 return myVote == -1 ? 0 : -1;
1383 function sleep(millis: number): Promise<void> {
1384 return new Promise(resolve => setTimeout(resolve, millis));
1388 * Polls / repeatedly runs a promise, every X milliseconds
1390 export async function poll(promiseFn: any, millis: number) {
1391 if (window.document.visibilityState !== "hidden") {
1394 await sleep(millis);
1395 return poll(promiseFn, millis);