1 import emojiShortName from "emoji-short-name";
2 import { Picker } from 'emoji-mart'
4 BlockCommunityResponse,
7 CommentNode as CommentNodeI,
11 CommunityModeratorView,
24 PrivateMessageReportView,
26 RegistrationApplicationView,
30 } from "lemmy-js-client";
31 import { default as MarkdownIt } from "markdown-it";
32 import markdown_it_container from "markdown-it-container";
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 markdown_it_emoji from 'markdown-it-emoji/bare';
38 import moment from "moment";
39 import { Subscription } from "rxjs";
40 import { delay, retryWhen, take } from "rxjs/operators";
41 import tippy from "tippy.js";
42 import Toastify from "toastify-js";
43 import { httpBase } from "./env";
44 import { i18n, languages } from "./i18next";
45 import { DataType, IsoData } from "./interfaces";
46 import { UserService, WebSocketService } from "./services";
47 import Renderer from "markdown-it/lib/renderer";
48 import Token from "markdown-it/lib/token";
52 Tribute = require("tributejs");
55 export const wsClient = new LemmyWebsocket();
57 export const favIconUrl = "/static/assets/icons/favicon.svg";
58 export const favIconPngUrl = "/static/assets/icons/apple-touch-icon.png";
60 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
61 export const repoUrl = "https://github.com/LemmyNet";
62 export const joinLemmyUrl = "https://join-lemmy.org";
63 export const donateLemmyUrl = `${joinLemmyUrl}/donate`;
64 export const docsUrl = `${joinLemmyUrl}/docs/en/index.html`;
65 export const helpGuideUrl = `${joinLemmyUrl}/docs/en/about/guide.html`; // TODO find a way to redirect to the non-en folder
66 export const markdownHelpUrl = `${helpGuideUrl}#using-markdown`;
67 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
68 export const archiveTodayUrl = "https://archive.today";
69 export const ghostArchiveUrl = "https://ghostarchive.org";
70 export const webArchiveUrl = "https://web.archive.org";
71 export const elementUrl = "https://element.io";
73 export const postRefetchSeconds: number = 60 * 1000;
74 export const fetchLimit = 40;
75 export const trendingFetchLimit = 6;
76 export const mentionDropdownFetchLimit = 10;
77 export const commentTreeMaxDepth = 8;
78 export const markdownFieldCharacterLimit = 50000;
80 export const relTags = "noopener nofollow";
82 let customEmojis: EmojiMartCategory[] = [];
83 export let customEmojisLookup: Map<string, CustomEmojiView> = new Map<string, CustomEmojiView>();
85 const DEFAULT_ALPHABET =
86 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
88 function getRandomCharFromAlphabet(alphabet: string): string {
89 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
92 export function randomStr(
94 alphabet = DEFAULT_ALPHABET
97 * Create n-long array and map it to random chars from given alphabet.
98 * Then join individual chars as string
100 return Array.from({ length: idDesiredLength })
102 return getRandomCharFromAlphabet(alphabet);
107 const html5EmbedConfig = {
109 useImageSyntax: true, // Enables video/audio embed with ![]() syntax (default)
111 audio: 'controls preload="metadata"',
112 video: 'width="100%" max-height="100%" controls loop preload="metadata"',
117 const spoilerConfig = {
118 validate: (params: string) => {
119 return params.trim().match(/^spoiler\s+(.*)$/);
122 render: (tokens: any, idx: any) => {
123 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
125 if (tokens[idx].nesting === 1) {
127 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
130 return "</details>\n";
135 export let md: MarkdownIt = new MarkdownIt();
137 export let mdNoImages: MarkdownIt = new MarkdownIt();
139 export function hotRankComment(comment_view: CommentView): number {
140 return hotRank(comment_view.counts.score, comment_view.comment.published);
143 export function hotRankActivePost(post_view: PostView): number {
144 return hotRank(post_view.counts.score, post_view.counts.newest_comment_time);
147 export function hotRankPost(post_view: PostView): number {
148 return hotRank(post_view.counts.score, post_view.post.published);
151 export function hotRank(score: number, timeStr: string): number {
152 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
153 let date: Date = new Date(timeStr + "Z"); // Add Z to convert from UTC date
154 let now: Date = new Date();
155 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
158 (10000 * Math.log10(Math.max(1, 3 + score))) /
159 Math.pow(hoursElapsed + 2, 1.8);
161 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
166 export function mdToHtml(text: string) {
167 return { __html: md.render(text) };
170 export function mdToHtmlNoImages(text: string) {
171 return { __html: mdNoImages.render(text) };
174 export function mdToHtmlInline(text: string) {
175 return { __html: md.renderInline(text) };
178 export function getUnixTime(text?: string): number | undefined {
179 return text ? new Date(text).getTime() / 1000 : undefined;
182 export function futureDaysToUnixTime(days?: number): number | undefined {
185 new Date(Date.now() + 1000 * 60 * 60 * 24 * days).getTime() / 1000
190 export function canMod(
192 mods?: CommunityModeratorView[],
193 admins?: PersonViewSafe[],
194 myUserInfo = UserService.Instance.myUserInfo,
197 // You can do moderator actions only on the mods added after you.
200 ?.map(a => a.person.id)
201 .concat(mods?.map(m => m.moderator.id) ?? []) ?? [];
204 let myIndex = adminsThenMods.findIndex(
205 id => id == myUserInfo.local_user_view.person.id
210 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
211 adminsThenMods = adminsThenMods.slice(0, myIndex + (onSelf ? 0 : 1));
212 return !adminsThenMods.includes(creator_id);
219 export function canAdmin(
221 admins?: PersonViewSafe[],
222 myUserInfo = UserService.Instance.myUserInfo,
225 return canMod(creatorId, undefined, admins, myUserInfo, onSelf);
228 export function isMod(
230 mods?: CommunityModeratorView[]
232 return mods?.map(m => m.moderator.id).includes(creatorId) ?? false;
235 export function amMod(
236 mods?: CommunityModeratorView[],
237 myUserInfo = UserService.Instance.myUserInfo
239 return myUserInfo ? isMod(myUserInfo.local_user_view.person.id, mods) : false;
242 export function isAdmin(creatorId: number, admins?: PersonViewSafe[]): boolean {
243 return admins?.map(a => a.person.id).includes(creatorId) ?? false;
246 export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean {
247 return myUserInfo?.local_user_view.person.admin ?? false;
250 export function amCommunityCreator(
252 mods?: CommunityModeratorView[],
253 myUserInfo = UserService.Instance.myUserInfo
255 let myId = myUserInfo?.local_user_view.person.id;
256 // Don't allow mod actions on yourself
257 return myId == mods?.at(0)?.moderator.id && myId != creator_id;
260 export function amSiteCreator(
262 admins?: PersonViewSafe[],
263 myUserInfo = UserService.Instance.myUserInfo
265 let myId = myUserInfo?.local_user_view.person.id;
266 return myId == admins?.at(0)?.person.id && myId != creator_id;
269 export function amTopMod(
270 mods: CommunityModeratorView[],
271 myUserInfo = UserService.Instance.myUserInfo
273 return mods.at(0)?.moderator.id == myUserInfo?.local_user_view.person.id;
276 const imageRegex = /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/;
277 const videoRegex = /(http)?s?:?(\/\/[^"']*\.(?:mp4|webm))/;
279 export function isImage(url: string) {
280 return imageRegex.test(url);
283 export function isVideo(url: string) {
284 return videoRegex.test(url);
287 export function validURL(str: string) {
288 return !!new URL(str);
291 export function communityRSSUrl(actorId: string, sort: string): string {
292 let url = new URL(actorId);
293 return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
296 export function validEmail(email: string) {
298 /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
299 return re.test(String(email).toLowerCase());
302 export function capitalizeFirstLetter(str: string): string {
303 return str.charAt(0).toUpperCase() + str.slice(1);
306 export function routeSortTypeToEnum(sort: string): SortType {
307 return SortType[sort];
310 export function listingTypeFromNum(type_: number): ListingType {
311 return Object.values(ListingType)[type_];
314 export function sortTypeFromNum(type_: number): SortType {
315 return Object.values(SortType)[type_];
318 export function routeListingTypeToEnum(type: string): ListingType {
319 return ListingType[type];
322 export function routeDataTypeToEnum(type: string): DataType {
323 return DataType[capitalizeFirstLetter(type)];
326 export function routeSearchTypeToEnum(type: string): SearchType {
327 return SearchType[type];
330 export async function getSiteMetadata(url: string) {
331 let form: GetSiteMetadata = { url };
332 let client = new LemmyHttp(httpBase);
333 return client.getSiteMetadata(form);
336 export function debounce(func: any, wait = 1000, immediate = false) {
337 // 'private' variable for instance
338 // The returned function will be able to reference this due to closure.
339 // Each call to the returned function will share this common timer.
342 // Calling debounce returns a new anonymous function
344 // reference the context and args for the setTimeout function
345 var args = arguments;
347 // Should the function be called now? If immediate is true
348 // and not already in a timeout then the answer is: Yes
349 var callNow = immediate && !timeout;
351 // This is the basic debounce behaviour where you can call this
352 // function several times, but it will only execute once
353 // [before or after imposing a delay].
354 // Each time the returned function is called, the timer starts over.
355 clearTimeout(timeout);
357 // Set the new timeout
358 timeout = setTimeout(function () {
359 // Inside the timeout function, clear the timeout variable
360 // which will let the next execution run when in 'immediate' mode
363 // Check if the function already ran with the immediate flag
365 // Call the original function with apply
366 // apply lets you define the 'this' object as well as the arguments
367 // (both captured before setTimeout)
368 func.apply(this, args);
372 // Immediate mode and no wait timer? Execute the function..
373 if (callNow) func.apply(this, args);
377 export function getLanguages(
379 myUserInfo = UserService.Instance.myUserInfo
381 let myLang = myUserInfo?.local_user_view.local_user.interface_language;
382 let lang = override || myLang || "browser";
384 if (lang == "browser" && isBrowser()) {
385 return getBrowserLanguages();
391 function getBrowserLanguages(): string[] {
392 // Intersect lemmy's langs, with the browser langs
393 let langs = languages ? languages.map(l => l.code) : ["en"];
395 // NOTE, mobile browsers seem to be missing this list, so append en
396 let allowedLangs = navigator.languages
398 .filter(v => langs.includes(v));
402 export async function fetchThemeList(): Promise<string[]> {
403 return fetch("/css/themelist").then(res => res.json());
406 export async function setTheme(theme: string, forceReload = false) {
410 if (theme === "browser" && !forceReload) {
413 // This is only run on a force reload
414 if (theme == "browser") {
418 let themeList = await fetchThemeList();
420 // Unload all the other themes
421 for (var i = 0; i < themeList.length; i++) {
422 let styleSheet = document.getElementById(themeList[i]);
424 styleSheet.setAttribute("disabled", "disabled");
429 .getElementById("default-light")
430 ?.setAttribute("disabled", "disabled");
431 document.getElementById("default-dark")?.setAttribute("disabled", "disabled");
433 // Load the theme dynamically
434 let cssLoc = `/css/themes/${theme}.css`;
436 loadCss(theme, cssLoc);
437 document.getElementById(theme)?.removeAttribute("disabled");
440 export function loadCss(id: string, loc: string) {
441 if (!document.getElementById(id)) {
442 var head = document.getElementsByTagName("head")[0];
443 var link = document.createElement("link");
445 link.rel = "stylesheet";
446 link.type = "text/css";
449 head.appendChild(link);
453 export function objectFlip(obj: any) {
455 Object.keys(obj).forEach(key => {
461 export function showAvatars(
462 myUserInfo = UserService.Instance.myUserInfo
464 return myUserInfo?.local_user_view.local_user.show_avatars ?? true;
467 export function showScores(
468 myUserInfo = UserService.Instance.myUserInfo
470 return myUserInfo?.local_user_view.local_user.show_scores ?? true;
473 export function isCakeDay(published: string): boolean {
474 // moment(undefined) or moment.utc(undefined) returns the current date/time
475 // moment(null) or moment.utc(null) returns null
476 const createDate = moment.utc(published).local();
477 const currentDate = moment(new Date());
480 createDate.date() === currentDate.date() &&
481 createDate.month() === currentDate.month() &&
482 createDate.year() !== currentDate.year()
486 export function toast(text: string, background = "success") {
488 let backgroundColor = `var(--${background})`;
491 backgroundColor: backgroundColor,
498 export function pictrsDeleteToast(
499 clickToDeleteText: string,
500 deletePictureText: string,
501 failedDeletePictureText: string,
505 let backgroundColor = `var(--light)`;
506 let toast = Toastify({
507 text: clickToDeleteText,
508 backgroundColor: backgroundColor,
514 fetch(deleteUrl).then(res => {
516 if (res.ok === true) {
517 alert(deletePictureText);
519 alert(failedDeletePictureText);
530 interface NotifyInfo {
537 export function messageToastify(info: NotifyInfo, router: any) {
539 let htmlBody = info.body ? md.render(info.body) : "";
540 let backgroundColor = `var(--light)`;
542 let toast = Toastify({
543 text: `${htmlBody}<br />${info.name}`,
545 backgroundColor: backgroundColor,
546 className: "text-dark",
555 router.history.push(info.link);
563 export function notifyPost(post_view: PostView, router: any) {
564 let info: NotifyInfo = {
565 name: post_view.community.name,
566 icon: post_view.community.icon,
567 link: `/post/${post_view.post.id}`,
568 body: post_view.post.name,
570 notify(info, router);
573 export function notifyComment(comment_view: CommentView, router: any) {
574 let info: NotifyInfo = {
575 name: comment_view.creator.name,
576 icon: comment_view.creator.avatar,
577 link: `/comment/${comment_view.comment.id}`,
578 body: comment_view.comment.content,
580 notify(info, router);
583 export function notifyPrivateMessage(pmv: PrivateMessageView, router: any) {
584 let info: NotifyInfo = {
585 name: pmv.creator.name,
586 icon: pmv.creator.avatar,
588 body: pmv.private_message.content,
590 notify(info, router);
593 function notify(info: NotifyInfo, router: any) {
594 messageToastify(info, router);
596 if (Notification.permission !== "granted") Notification.requestPermission();
598 var notification = new Notification(info.name, {
599 ...{ body: info.body },
600 ...(info.icon && { icon: info.icon }),
603 notification.onclick = (ev: Event): any => {
605 router.history.push(info.link);
610 export function setupTribute() {
612 noMatchTemplate: function () {
619 menuItemTemplate: (item: any) => {
620 let shortName = `:${item.original.key}:`;
621 return `${item.original.val} ${shortName}`;
623 selectTemplate: (item: any) => {
624 let customEmoji = customEmojisLookup[item.original.key]?.custom_emoji;
625 if (customEmoji == undefined)
626 return `${item.original.val}`;
628 return `![${customEmoji.alt_text}](${customEmoji.image_url} "${customEmoji.shortcode}")`;
630 values: Object.entries(emojiShortName).map(e => {
631 return { key: e[1], val: e[0] };
633 Array.from(customEmojisLookup.entries()).map((k) => ({
635 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}" />`
639 autocompleteMode: true,
641 // menuItemLimit: mentionDropdownFetchLimit,
642 menuShowMinLength: 2,
647 selectTemplate: (item: any) => {
648 let it: PersonTribute = item.original;
649 return `[${it.key}](${it.view.person.actor_id})`;
651 values: debounce(async (text: string, cb: any) => {
652 cb(await personSearch(text));
655 autocompleteMode: true,
657 // menuItemLimit: mentionDropdownFetchLimit,
658 menuShowMinLength: 2,
664 selectTemplate: (item: any) => {
665 let it: CommunityTribute = item.original;
666 return `[${it.key}](${it.view.community.actor_id})`;
668 values: debounce(async (text: string, cb: any) => {
669 cb(await communitySearch(text));
672 autocompleteMode: true,
674 // menuItemLimit: mentionDropdownFetchLimit,
675 menuShowMinLength: 2,
683 function setupEmojiDataModel(custom_emoji_views: CustomEmojiView[]) {
684 let groupedEmojis = groupBy(custom_emoji_views, x => x.custom_emoji.category);
685 for (const [category, emojis] of Object.entries(groupedEmojis)) {
689 emojis: emojis.map(emoji =>
691 id: emoji.custom_emoji.shortcode,
692 name: emoji.custom_emoji.shortcode,
693 keywords: emoji.keywords.map(x => x.keyword),
694 skins: [{ src: emoji.custom_emoji.image_url }]
698 customEmojisLookup = new Map(custom_emoji_views.map(view => [view.custom_emoji.shortcode, view]));
701 export function updateEmojiDataModel(custom_emoji_view: CustomEmojiView) {
702 const emoji: EmojiMartCustomEmoji = {
703 id: custom_emoji_view.custom_emoji.shortcode,
704 name: custom_emoji_view.custom_emoji.shortcode,
705 keywords: custom_emoji_view.keywords.map(x => x.keyword),
706 skins: [{ src: custom_emoji_view.custom_emoji.image_url }]
708 let categoryIndex = customEmojis.findIndex(x => x.id == custom_emoji_view.custom_emoji.category);
709 if (categoryIndex == -1) {
711 id: custom_emoji_view.custom_emoji.category,
712 name: custom_emoji_view.custom_emoji.category,
717 let emojiIndex = customEmojis[categoryIndex].emojis.findIndex(x => x.id == custom_emoji_view.custom_emoji.shortcode);
718 if (emojiIndex == -1) {
719 customEmojis[categoryIndex].emojis.push(emoji)
722 customEmojis[categoryIndex].emojis[emojiIndex] = emoji;
725 customEmojisLookup[custom_emoji_view.custom_emoji.shortcode] = custom_emoji_view;
728 export function removeFromEmojiDataModel(id: number) {
729 let view: CustomEmojiView | undefined;
730 for (let item of customEmojisLookup.values()) {
731 if (item.custom_emoji.id === id) {
738 const categoryIndex = customEmojis.findIndex(x => x.id == view?.custom_emoji.category);
739 const emojiIndex = customEmojis[categoryIndex].emojis.findIndex(x => x.id == view?.custom_emoji.shortcode)
740 customEmojis[categoryIndex].emojis = customEmojis[categoryIndex].emojis.splice(emojiIndex, 1);
742 customEmojisLookup.delete(view?.custom_emoji.shortcode);
745 function setupMarkdown() {
746 const markdownItConfig: MarkdownIt.Options = {
752 const emojiDefs = Array.from(customEmojisLookup.entries()).reduce((main, [key, value]) => ({...main, [key]: value}), {})
753 md = new MarkdownIt(markdownItConfig)
754 .use(markdown_it_sub)
755 .use(markdown_it_sup)
756 .use(markdown_it_footnote)
757 .use(markdown_it_html5_embed, html5EmbedConfig)
758 .use(markdown_it_container, "spoiler", spoilerConfig)
759 .use(markdown_it_emoji, {
763 mdNoImages = new MarkdownIt(markdownItConfig)
764 .use(markdown_it_sub)
765 .use(markdown_it_sup)
766 .use(markdown_it_footnote)
767 .use(markdown_it_html5_embed, html5EmbedConfig)
768 .use(markdown_it_container, "spoiler", spoilerConfig)
769 .use(markdown_it_emoji, {
773 var defaultRenderer = md.renderer.rules.image;
774 md.renderer.rules.image = function (tokens: Token[], idx: number, options: MarkdownIt.Options, env: any, self: Renderer) {
775 //Provide custom renderer for our emojis to allow us to add a css class and force size dimensions on them.
776 const item = tokens[idx] as any;
777 const title = item.attrs.length >= 3 ? item.attrs[2][1] : "";
778 const src: string = item.attrs[0][1];
779 const isCustomEmoji = customEmojisLookup.get(title) != undefined;
780 if (!isCustomEmoji) {
781 return defaultRenderer?.(tokens, idx, options, env, self) ?? "";
783 const alt_text = item.content;
784 return `<img class="icon icon-emoji" src="${src}" title="${title}" alt="${alt_text}"/>`;
788 export function getEmojiMart(onEmojiSelect: (e: any) => void, customPickerOptions: any = {}) {
789 const pickerOptions = { ...customPickerOptions, onEmojiSelect: onEmojiSelect, custom: customEmojis }
790 return new Picker(pickerOptions);
793 var tippyInstance: any;
795 tippyInstance = tippy("[data-tippy-content]");
798 export function setupTippy() {
800 tippyInstance.forEach((e: any) => e.destroy());
801 tippyInstance = tippy("[data-tippy-content]", {
803 // Display on "long press"
804 touch: ["hold", 500],
809 interface PersonTribute {
811 view: PersonViewSafe;
814 async function personSearch(text: string): Promise<PersonTribute[]> {
815 let users = (await fetchUsers(text)).users;
816 let persons: PersonTribute[] = users.map(pv => {
817 let tribute: PersonTribute = {
818 key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
826 interface CommunityTribute {
831 async function communitySearch(text: string): Promise<CommunityTribute[]> {
832 let comms = (await fetchCommunities(text)).communities;
833 let communities: CommunityTribute[] = comms.map(cv => {
834 let tribute: CommunityTribute = {
835 key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
843 export function getListingTypeFromProps(
845 defaultListingType: ListingType,
846 myUserInfo = UserService.Instance.myUserInfo
848 let myLt = myUserInfo?.local_user_view.local_user.default_listing_type;
849 return props.match.params.listing_type
850 ? routeListingTypeToEnum(props.match.params.listing_type)
852 ? Object.values(ListingType)[myLt]
853 : defaultListingType;
856 export function getListingTypeFromPropsNoDefault(props: any): ListingType {
857 return props.match.params.listing_type
858 ? routeListingTypeToEnum(props.match.params.listing_type)
862 export function getDataTypeFromProps(props: any): DataType {
863 return props.match.params.data_type
864 ? routeDataTypeToEnum(props.match.params.data_type)
868 export function getSortTypeFromProps(
870 myUserInfo = UserService.Instance.myUserInfo
872 let mySortType = myUserInfo?.local_user_view.local_user.default_sort_type;
873 return props.match.params.sort
874 ? routeSortTypeToEnum(props.match.params.sort)
876 ? Object.values(SortType)[mySortType]
880 export function getPageFromProps(props: any): number {
881 return props.match.params.page ? Number(props.match.params.page) : 1;
884 export function getRecipientIdFromProps(props: any): number {
885 return props.match.params.recipient_id
886 ? Number(props.match.params.recipient_id)
890 export function getIdFromProps(props: any): number | undefined {
891 let id = props.match.params.post_id;
892 return id ? Number(id) : undefined;
895 export function getCommentIdFromProps(props: any): number | undefined {
896 let id = props.match.params.comment_id;
897 return id ? Number(id) : undefined;
900 export function getUsernameFromProps(props: any): string {
901 return props.match.params.username;
904 export function editCommentRes(data: CommentView, comments?: CommentView[]) {
905 let found = comments?.find(c => c.comment.id == data.comment.id);
907 found.comment.content = data.comment.content;
908 found.comment.distinguished = data.comment.distinguished;
909 found.comment.updated = data.comment.updated;
910 found.comment.removed = data.comment.removed;
911 found.comment.deleted = data.comment.deleted;
912 found.counts.upvotes = data.counts.upvotes;
913 found.counts.downvotes = data.counts.downvotes;
914 found.counts.score = data.counts.score;
918 export function saveCommentRes(data: CommentView, comments?: CommentView[]) {
919 let found = comments?.find(c => c.comment.id == data.comment.id);
921 found.saved = data.saved;
925 export function updatePersonBlock(
926 data: BlockPersonResponse,
927 myUserInfo = UserService.Instance.myUserInfo
929 let mui = myUserInfo;
932 mui.person_blocks.push({
933 person: mui.local_user_view.person,
934 target: data.person_view.person,
936 toast(`${i18n.t("blocked")} ${data.person_view.person.name}`);
938 mui.person_blocks = mui.person_blocks.filter(
939 i => i.target.id != data.person_view.person.id
941 toast(`${i18n.t("unblocked")} ${data.person_view.person.name}`);
946 export function updateCommunityBlock(
947 data: BlockCommunityResponse,
948 myUserInfo = UserService.Instance.myUserInfo
950 let mui = myUserInfo;
953 mui.community_blocks.push({
954 person: mui.local_user_view.person,
955 community: data.community_view.community,
957 toast(`${i18n.t("blocked")} ${data.community_view.community.name}`);
959 mui.community_blocks = mui.community_blocks.filter(
960 i => i.community.id != data.community_view.community.id
962 toast(`${i18n.t("unblocked")} ${data.community_view.community.name}`);
967 export function createCommentLikeRes(
969 comments?: CommentView[]
971 let found = comments?.find(c => c.comment.id === data.comment.id);
973 found.counts.score = data.counts.score;
974 found.counts.upvotes = data.counts.upvotes;
975 found.counts.downvotes = data.counts.downvotes;
976 if (data.my_vote !== null) {
977 found.my_vote = data.my_vote;
982 export function createPostLikeFindRes(data: PostView, posts?: PostView[]) {
983 let found = posts?.find(p => p.post.id == data.post.id);
985 createPostLikeRes(data, found);
989 export function createPostLikeRes(data: PostView, post_view?: PostView) {
991 post_view.counts.score = data.counts.score;
992 post_view.counts.upvotes = data.counts.upvotes;
993 post_view.counts.downvotes = data.counts.downvotes;
994 if (data.my_vote !== null) {
995 post_view.my_vote = data.my_vote;
1000 export function editPostFindRes(data: PostView, posts?: PostView[]) {
1001 let found = posts?.find(p => p.post.id == data.post.id);
1003 editPostRes(data, found);
1007 export function editPostRes(data: PostView, post: PostView) {
1009 post.post.url = data.post.url;
1010 post.post.name = data.post.name;
1011 post.post.nsfw = data.post.nsfw;
1012 post.post.deleted = data.post.deleted;
1013 post.post.removed = data.post.removed;
1014 post.post.featured_community = data.post.featured_community;
1015 post.post.featured_local = data.post.featured_local;
1016 post.post.body = data.post.body;
1017 post.post.locked = data.post.locked;
1018 post.saved = data.saved;
1022 // TODO possible to make these generic?
1023 export function updatePostReportRes(
1024 data: PostReportView,
1025 reports?: PostReportView[]
1027 let found = reports?.find(p => p.post_report.id == data.post_report.id);
1029 found.post_report = data.post_report;
1033 export function updateCommentReportRes(
1034 data: CommentReportView,
1035 reports?: CommentReportView[]
1037 let found = reports?.find(c => c.comment_report.id == data.comment_report.id);
1039 found.comment_report = data.comment_report;
1043 export function updatePrivateMessageReportRes(
1044 data: PrivateMessageReportView,
1045 reports?: PrivateMessageReportView[]
1047 let found = reports?.find(
1048 c => c.private_message_report.id == data.private_message_report.id
1051 found.private_message_report = data.private_message_report;
1055 export function updateRegistrationApplicationRes(
1056 data: RegistrationApplicationView,
1057 applications?: RegistrationApplicationView[]
1059 let found = applications?.find(
1060 ra => ra.registration_application.id == data.registration_application.id
1063 found.registration_application = data.registration_application;
1064 found.admin = data.admin;
1065 found.creator_local_user = data.creator_local_user;
1069 export function commentsToFlatNodes(comments: CommentView[]): CommentNodeI[] {
1070 let nodes: CommentNodeI[] = [];
1071 for (let comment of comments) {
1072 nodes.push({ comment_view: comment, children: [], depth: 0 });
1077 export function convertCommentSortType(sort: SortType): CommentSortType {
1079 sort == SortType.TopAll ||
1080 sort == SortType.TopDay ||
1081 sort == SortType.TopWeek ||
1082 sort == SortType.TopMonth ||
1083 sort == SortType.TopYear
1085 return CommentSortType.Top;
1086 } else if (sort == SortType.New) {
1087 return CommentSortType.New;
1088 } else if (sort == SortType.Hot || sort == SortType.Active) {
1089 return CommentSortType.Hot;
1091 return CommentSortType.Hot;
1095 export function buildCommentsTree(
1096 comments: CommentView[],
1097 parentComment: boolean
1099 let map = new Map<number, CommentNodeI>();
1100 let depthOffset = !parentComment
1102 : getDepthFromComment(comments[0].comment) ?? 0;
1104 for (let comment_view of comments) {
1105 let depthI = getDepthFromComment(comment_view.comment) ?? 0;
1106 let depth = depthI ? depthI - depthOffset : 0;
1107 let node: CommentNodeI = {
1112 map.set(comment_view.comment.id, { ...node });
1115 let tree: CommentNodeI[] = [];
1117 // if its a parent comment fetch, then push the first comment to the top node.
1118 if (parentComment) {
1119 let cNode = map.get(comments[0].comment.id);
1125 for (let comment_view of comments) {
1126 let child = map.get(comment_view.comment.id);
1128 let parent_id = getCommentParentId(comment_view.comment);
1130 let parent = map.get(parent_id);
1131 // Necessary because blocked comment might not exist
1133 parent.children.push(child);
1136 if (!parentComment) {
1146 export function getCommentParentId(comment?: CommentI): number | undefined {
1147 let split = comment?.path.split(".");
1151 return split && split.length > 1
1152 ? Number(split.at(split.length - 2))
1156 export function getDepthFromComment(comment?: CommentI): number | undefined {
1157 let len = comment?.path.split(".").length;
1158 return len ? len - 2 : undefined;
1161 export function insertCommentIntoTree(
1162 tree: CommentNodeI[],
1164 parentComment: boolean
1166 // Building a fake node to be used for later
1167 let node: CommentNodeI = {
1173 let parentId = getCommentParentId(cv.comment);
1175 let parent_comment = searchCommentTree(tree, parentId);
1176 if (parent_comment) {
1177 node.depth = parent_comment.depth + 1;
1178 parent_comment.children.unshift(node);
1180 } else if (!parentComment) {
1185 export function searchCommentTree(
1186 tree: CommentNodeI[],
1188 ): CommentNodeI | undefined {
1189 for (let node of tree) {
1190 if (node.comment_view.comment.id === id) {
1194 for (const child of node.children) {
1195 let res = searchCommentTree([child], id);
1205 export const colorList: string[] = [
1215 function hsl(num: number) {
1216 return `hsla(${num}, 35%, 50%, 1)`;
1219 export function hostname(url: string): string {
1220 let cUrl = new URL(url);
1221 return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1224 export function validTitle(title?: string): boolean {
1225 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1226 if (!title || title.length < 3) return true;
1228 const regex = new RegExp(/.*\S.*/, "g");
1230 return regex.test(title);
1233 export function siteBannerCss(banner: string): string {
1235 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1236 background-attachment: fixed; \
1237 background-position: top; \
1238 background-repeat: no-repeat; \
1239 background-size: 100% cover; \
1242 max-height: 100vh; \
1246 export function isBrowser() {
1247 return typeof window !== "undefined";
1250 export function setIsoData(context: any): IsoData {
1251 // If its the browser, you need to deserialize the data from the window
1253 let json = window.isoData;
1254 let routeData = json.routeData;
1255 let site_res = json.site_res;
1257 let isoData: IsoData = {
1263 } else return context.router.staticContext;
1266 export function wsSubscribe(parseMessage: any): Subscription | undefined {
1268 return WebSocketService.Instance.subject
1269 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1271 msg => parseMessage(msg),
1272 err => console.error(err),
1273 () => console.log("complete")
1280 moment.updateLocale("en", {
1301 export function saveScrollPosition(context: any) {
1302 let path: string = context.router.route.location.pathname;
1303 let y = window.scrollY;
1304 sessionStorage.setItem(`scrollPosition_${path}`, y.toString());
1307 export function restoreScrollPosition(context: any) {
1308 let path: string = context.router.route.location.pathname;
1309 let y = Number(sessionStorage.getItem(`scrollPosition_${path}`));
1310 window.scrollTo(0, y);
1313 export function showLocal(isoData: IsoData): boolean {
1314 let linked = isoData.site_res.federated_instances?.linked;
1315 return linked ? linked.length > 0 : false;
1318 export interface ChoicesValue {
1323 export function communityToChoice(cv: CommunityView): ChoicesValue {
1324 let choice: ChoicesValue = {
1325 value: cv.community.id.toString(),
1326 label: communitySelectName(cv),
1331 export function personToChoice(pvs: PersonViewSafe): ChoicesValue {
1332 let choice: ChoicesValue = {
1333 value: pvs.person.id.toString(),
1334 label: personSelectName(pvs),
1339 export async function fetchCommunities(q: string) {
1340 let form: Search = {
1342 type_: SearchType.Communities,
1343 sort: SortType.TopAll,
1344 listing_type: ListingType.All,
1347 auth: myAuth(false),
1349 let client = new LemmyHttp(httpBase);
1350 return client.search(form);
1353 export async function fetchUsers(q: string) {
1354 let form: Search = {
1356 type_: SearchType.Users,
1357 sort: SortType.TopAll,
1358 listing_type: ListingType.All,
1361 auth: myAuth(false),
1363 let client = new LemmyHttp(httpBase);
1364 return client.search(form);
1367 export const choicesConfig = {
1369 searchResultLimit: fetchLimit,
1371 containerOuter: "choices custom-select px-0",
1373 "choices__inner bg-secondary border-0 py-0 modlog-choices-font-size",
1374 input: "form-control",
1375 inputCloned: "choices__input--cloned",
1376 list: "choices__list",
1377 listItems: "choices__list--multiple",
1378 listSingle: "choices__list--single py-0",
1379 listDropdown: "choices__list--dropdown",
1380 item: "choices__item bg-secondary",
1381 itemSelectable: "choices__item--selectable",
1382 itemDisabled: "choices__item--disabled",
1383 itemChoice: "choices__item--choice",
1384 placeholder: "choices__placeholder",
1385 group: "choices__group",
1386 groupHeading: "choices__heading",
1387 button: "choices__button",
1388 activeState: "is-active",
1389 focusState: "is-focused",
1390 openState: "is-open",
1391 disabledState: "is-disabled",
1392 highlightedState: "text-info",
1393 selectedState: "text-info",
1394 flippedState: "is-flipped",
1395 loadingState: "is-loading",
1396 noResults: "has-no-results",
1397 noChoices: "has-no-choices",
1401 export function communitySelectName(cv: CommunityView): string {
1402 return cv.community.local
1403 ? cv.community.title
1404 : `${hostname(cv.community.actor_id)}/${cv.community.title}`;
1407 export function personSelectName(pvs: PersonViewSafe): string {
1408 let pName = pvs.person.display_name ?? pvs.person.name;
1409 return pvs.person.local ? pName : `${hostname(pvs.person.actor_id)}/${pName}`;
1412 export function initializeSite(site: GetSiteResponse) {
1413 UserService.Instance.myUserInfo = site.my_user;
1414 i18n.changeLanguage(getLanguages()[0]);
1415 setupEmojiDataModel(site.custom_emojis);
1419 const SHORTNUM_SI_FORMAT = new Intl.NumberFormat("en-US", {
1420 maximumSignificantDigits: 3,
1422 notation: "compact",
1423 compactDisplay: "short",
1426 export function numToSI(value: number): string {
1427 return SHORTNUM_SI_FORMAT.format(value);
1430 export function isBanned(ps: PersonSafe): boolean {
1431 let expires = ps.ban_expires;
1432 // Add Z to convert from UTC date
1433 // TODO this check probably isn't necessary anymore
1435 if (ps.banned && new Date(expires + "Z") > new Date()) {
1445 export function pushNotNull(array: any[], new_item?: any) {
1447 array.push(...new_item);
1451 export function myAuth(throwErr = true): string | undefined {
1452 return UserService.Instance.auth(throwErr);
1455 export function enableDownvotes(siteRes: GetSiteResponse): boolean {
1456 return siteRes.site_view.local_site.enable_downvotes;
1459 export function enableNsfw(siteRes: GetSiteResponse): boolean {
1460 return siteRes.site_view.local_site.enable_nsfw;
1463 export function postToCommentSortType(sort: SortType): CommentSortType {
1464 if ([SortType.Active, SortType.Hot].includes(sort)) {
1465 return CommentSortType.Hot;
1466 } else if ([SortType.New, SortType.NewComments].includes(sort)) {
1467 return CommentSortType.New;
1468 } else if (sort == SortType.Old) {
1469 return CommentSortType.Old;
1471 return CommentSortType.Top;
1475 export function myFirstDiscussionLanguageId(
1476 allLanguages: Language[],
1477 siteLanguages: number[],
1478 myUserInfo = UserService.Instance.myUserInfo
1479 ): number | undefined {
1480 return selectableLanguages(
1489 export function canCreateCommunity(
1490 siteRes: GetSiteResponse,
1491 myUserInfo = UserService.Instance.myUserInfo
1493 let adminOnly = siteRes.site_view.local_site.community_creation_admin_only;
1494 return !adminOnly || amAdmin(myUserInfo);
1497 export function isPostBlocked(
1499 myUserInfo = UserService.Instance.myUserInfo
1502 (myUserInfo?.community_blocks
1503 .map(c => c.community.id)
1504 .includes(pv.community.id) ||
1505 myUserInfo?.person_blocks
1506 .map(p => p.target.id)
1507 .includes(pv.creator.id)) ??
1512 /// Checks to make sure you can view NSFW posts. Returns true if you can.
1513 export function nsfwCheck(
1515 myUserInfo = UserService.Instance.myUserInfo
1517 let nsfw = pv.post.nsfw || pv.community.nsfw;
1518 let myShowNsfw = myUserInfo?.local_user_view.local_user.show_nsfw ?? false;
1519 return !nsfw || (nsfw && myShowNsfw);
1522 export function getRandomFromList<T>(list: T[]): T | undefined {
1523 return list.length == 0 ? undefined : list.at(Math.floor(Math.random() * list.length));
1527 * This shows what language you can select
1529 * Use showAll for the site form
1530 * Use showSite for the profile and community forms
1531 * Use false for both those to filter on your profile and site ones
1533 export function selectableLanguages(
1534 allLanguages: Language[],
1535 siteLanguages: number[],
1538 myUserInfo = UserService.Instance.myUserInfo
1540 let allLangIds = allLanguages.map(l => l.id);
1541 let myLangs = myUserInfo?.discussion_languages ?? allLangIds;
1542 myLangs = myLangs.length == 0 ? allLangIds : myLangs;
1543 let siteLangs = siteLanguages.length == 0 ? allLangIds : siteLanguages;
1546 return allLanguages;
1549 return allLanguages.filter(x => siteLangs.includes(x.id));
1552 .filter(x => siteLangs.includes(x.id))
1553 .filter(x => myLangs.includes(x.id));
1557 interface EmojiMartCategory {
1560 emojis: EmojiMartCustomEmoji[]
1563 interface EmojiMartCustomEmoji {
1567 skins: EmojiMartSkin[]
1570 interface EmojiMartSkin {
1574 const groupBy = <T>(array: T[], predicate: (value: T, index: number, array: T[]) => string) =>
1575 array.reduce((acc, value, index, array) => {
1576 (acc[predicate(value, index, array)] ||= []).push(value);
1578 }, {} as { [key: string]: T[] });