2023-03-27 16:49:46 +00:00
|
|
|
import { Picker } from "emoji-mart";
|
2021-07-17 20:42:55 +00:00
|
|
|
import emojiShortName from "emoji-short-name";
|
|
|
|
import {
|
2021-08-20 02:56:18 +00:00
|
|
|
BlockCommunityResponse,
|
|
|
|
BlockPersonResponse,
|
2022-07-30 13:28:08 +00:00
|
|
|
Comment as CommentI,
|
|
|
|
CommentNode as CommentNodeI,
|
2021-09-28 10:38:59 +00:00
|
|
|
CommentReportView,
|
2022-07-30 13:28:08 +00:00
|
|
|
CommentSortType,
|
2021-07-17 20:42:55 +00:00
|
|
|
CommentView,
|
2022-06-21 21:42:29 +00:00
|
|
|
CommunityModeratorView,
|
2021-07-17 20:42:55 +00:00
|
|
|
CommunityView,
|
2022-11-27 18:02:32 +00:00
|
|
|
CustomEmojiView,
|
2021-08-19 15:24:13 +00:00
|
|
|
GetSiteMetadata,
|
2021-07-17 20:42:55 +00:00
|
|
|
GetSiteResponse,
|
2022-12-19 15:57:29 +00:00
|
|
|
Language,
|
2021-07-17 20:42:55 +00:00
|
|
|
LemmyHttp,
|
|
|
|
LemmyWebsocket,
|
|
|
|
ListingType,
|
2022-01-09 17:53:11 +00:00
|
|
|
PersonSafe,
|
2021-07-17 20:42:55 +00:00
|
|
|
PersonViewSafe,
|
2021-09-28 10:38:59 +00:00
|
|
|
PostReportView,
|
2021-07-17 20:42:55 +00:00
|
|
|
PostView,
|
2022-09-28 12:50:47 +00:00
|
|
|
PrivateMessageReportView,
|
2021-07-17 20:42:55 +00:00
|
|
|
PrivateMessageView,
|
2021-12-30 15:26:45 +00:00
|
|
|
RegistrationApplicationView,
|
2021-07-17 20:42:55 +00:00
|
|
|
Search,
|
|
|
|
SearchType,
|
|
|
|
SortType,
|
2023-03-27 02:06:42 +00:00
|
|
|
UploadImageResponse,
|
2021-07-17 20:42:55 +00:00
|
|
|
} from "lemmy-js-client";
|
2022-11-19 02:02:38 +00:00
|
|
|
import { default as MarkdownIt } from "markdown-it";
|
2021-07-17 20:42:55 +00:00
|
|
|
import markdown_it_container from "markdown-it-container";
|
2023-03-27 16:49:46 +00:00
|
|
|
import markdown_it_emoji from "markdown-it-emoji/bare";
|
2022-02-02 14:53:09 +00:00
|
|
|
import markdown_it_footnote from "markdown-it-footnote";
|
2021-09-18 19:44:39 +00:00
|
|
|
import markdown_it_html5_embed from "markdown-it-html5-embed";
|
2021-07-17 20:42:55 +00:00
|
|
|
import markdown_it_sub from "markdown-it-sub";
|
|
|
|
import markdown_it_sup from "markdown-it-sup";
|
2023-03-27 16:49:46 +00:00
|
|
|
import Renderer from "markdown-it/lib/renderer";
|
|
|
|
import Token from "markdown-it/lib/token";
|
2021-07-17 20:42:55 +00:00
|
|
|
import moment from "moment";
|
|
|
|
import { Subscription } from "rxjs";
|
|
|
|
import { delay, retryWhen, take } from "rxjs/operators";
|
|
|
|
import tippy from "tippy.js";
|
|
|
|
import Toastify from "toastify-js";
|
|
|
|
import { httpBase } from "./env";
|
2022-02-18 19:34:11 +00:00
|
|
|
import { i18n, languages } from "./i18next";
|
2022-07-30 13:28:08 +00:00
|
|
|
import { DataType, IsoData } from "./interfaces";
|
2021-02-22 02:24:09 +00:00
|
|
|
import { UserService, WebSocketService } from "./services";
|
2021-07-17 20:42:55 +00:00
|
|
|
|
2021-01-23 21:40:24 +00:00
|
|
|
var Tribute: any;
|
2020-09-07 22:24:48 +00:00
|
|
|
if (isBrowser()) {
|
2021-02-22 02:24:09 +00:00
|
|
|
Tribute = require("tributejs");
|
|
|
|
}
|
2020-12-24 22:05:57 +00:00
|
|
|
|
|
|
|
export const wsClient = new LemmyWebsocket();
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2021-03-25 15:58:29 +00:00
|
|
|
export const favIconUrl = "/static/assets/icons/favicon.svg";
|
|
|
|
export const favIconPngUrl = "/static/assets/icons/apple-touch-icon.png";
|
2020-09-06 16:15:25 +00:00
|
|
|
// TODO
|
|
|
|
// export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
|
2021-02-22 02:24:09 +00:00
|
|
|
export const repoUrl = "https://github.com/LemmyNet";
|
2021-06-03 20:57:50 +00:00
|
|
|
export const joinLemmyUrl = "https://join-lemmy.org";
|
2021-09-18 20:40:59 +00:00
|
|
|
export const donateLemmyUrl = `${joinLemmyUrl}/donate`;
|
2021-06-03 20:57:50 +00:00
|
|
|
export const docsUrl = `${joinLemmyUrl}/docs/en/index.html`;
|
|
|
|
export const helpGuideUrl = `${joinLemmyUrl}/docs/en/about/guide.html`; // TODO find a way to redirect to the non-en folder
|
2021-11-05 20:34:57 +00:00
|
|
|
export const markdownHelpUrl = `${helpGuideUrl}#using-markdown`;
|
2020-09-06 16:15:25 +00:00
|
|
|
export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
|
2021-10-29 01:57:36 +00:00
|
|
|
export const archiveTodayUrl = "https://archive.today";
|
|
|
|
export const ghostArchiveUrl = "https://ghostarchive.org";
|
|
|
|
export const webArchiveUrl = "https://web.archive.org";
|
2021-09-18 20:40:59 +00:00
|
|
|
export const elementUrl = "https://element.io";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
export const postRefetchSeconds: number = 60 * 1000;
|
2022-09-21 14:06:24 +00:00
|
|
|
export const fetchLimit = 40;
|
2022-06-21 21:42:29 +00:00
|
|
|
export const trendingFetchLimit = 6;
|
2020-09-06 16:15:25 +00:00
|
|
|
export const mentionDropdownFetchLimit = 10;
|
2022-07-30 13:28:08 +00:00
|
|
|
export const commentTreeMaxDepth = 8;
|
2022-11-12 17:06:41 +00:00
|
|
|
export const markdownFieldCharacterLimit = 50000;
|
2023-04-04 12:40:00 +00:00
|
|
|
export const maxUploadImages = 20;
|
|
|
|
export const concurrentImageUpload = 4;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2022-02-24 15:31:44 +00:00
|
|
|
export const relTags = "noopener nofollow";
|
|
|
|
|
2023-04-04 12:40:00 +00:00
|
|
|
export type ThemeColor =
|
|
|
|
| "primary"
|
|
|
|
| "secondary"
|
|
|
|
| "light"
|
|
|
|
| "dark"
|
|
|
|
| "success"
|
|
|
|
| "danger"
|
|
|
|
| "warning"
|
|
|
|
| "info"
|
|
|
|
| "blue"
|
|
|
|
| "indigo"
|
|
|
|
| "purple"
|
|
|
|
| "pink"
|
|
|
|
| "red"
|
|
|
|
| "orange"
|
|
|
|
| "yellow"
|
|
|
|
| "green"
|
|
|
|
| "teal"
|
|
|
|
| "cyan"
|
|
|
|
| "white"
|
|
|
|
| "gray"
|
|
|
|
| "gray-dark";
|
|
|
|
|
2022-11-27 18:02:32 +00:00
|
|
|
let customEmojis: EmojiMartCategory[] = [];
|
2023-03-27 16:49:46 +00:00
|
|
|
export let customEmojisLookup: Map<string, CustomEmojiView> = new Map<
|
|
|
|
string,
|
|
|
|
CustomEmojiView
|
|
|
|
>();
|
2022-11-27 18:02:32 +00:00
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
const DEFAULT_ALPHABET =
|
2021-02-22 02:24:09 +00:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
function getRandomCharFromAlphabet(alphabet: string): string {
|
|
|
|
return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
|
|
|
|
}
|
|
|
|
|
|
|
|
export function randomStr(
|
2021-02-22 02:24:09 +00:00
|
|
|
idDesiredLength = 20,
|
2020-09-06 16:15:25 +00:00
|
|
|
alphabet = DEFAULT_ALPHABET
|
|
|
|
): string {
|
|
|
|
/**
|
|
|
|
* Create n-long array and map it to random chars from given alphabet.
|
|
|
|
* Then join individual chars as string
|
|
|
|
*/
|
|
|
|
return Array.from({ length: idDesiredLength })
|
|
|
|
.map(() => {
|
|
|
|
return getRandomCharFromAlphabet(alphabet);
|
|
|
|
})
|
2021-02-22 02:24:09 +00:00
|
|
|
.join("");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-11-19 02:02:38 +00:00
|
|
|
const html5EmbedConfig = {
|
|
|
|
html5embed: {
|
|
|
|
useImageSyntax: true, // Enables video/audio embed with ![]() syntax (default)
|
|
|
|
attributes: {
|
|
|
|
audio: 'controls preload="metadata"',
|
|
|
|
video: 'width="100%" max-height="100%" controls loop preload="metadata"',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const spoilerConfig = {
|
|
|
|
validate: (params: string) => {
|
|
|
|
return params.trim().match(/^spoiler\s+(.*)$/);
|
|
|
|
},
|
|
|
|
|
|
|
|
render: (tokens: any, idx: any) => {
|
|
|
|
var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
|
|
|
|
|
|
|
|
if (tokens[idx].nesting === 1) {
|
|
|
|
// opening tag
|
|
|
|
return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
|
|
|
|
} else {
|
|
|
|
// closing tag
|
|
|
|
return "</details>\n";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-11-27 18:02:32 +00:00
|
|
|
export let md: MarkdownIt = new MarkdownIt();
|
2022-11-19 02:02:38 +00:00
|
|
|
|
2022-11-27 18:02:32 +00:00
|
|
|
export let mdNoImages: MarkdownIt = new MarkdownIt();
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
export function hotRankComment(comment_view: CommentView): number {
|
|
|
|
return hotRank(comment_view.counts.score, comment_view.comment.published);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hotRankActivePost(post_view: PostView): number {
|
|
|
|
return hotRank(post_view.counts.score, post_view.counts.newest_comment_time);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
export function hotRankPost(post_view: PostView): number {
|
|
|
|
return hotRank(post_view.counts.score, post_view.post.published);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function hotRank(score: number, timeStr: string): number {
|
|
|
|
// Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
|
2021-02-22 02:24:09 +00:00
|
|
|
let date: Date = new Date(timeStr + "Z"); // Add Z to convert from UTC date
|
2020-09-06 16:15:25 +00:00
|
|
|
let now: Date = new Date();
|
|
|
|
let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
|
|
|
|
|
|
|
|
let rank =
|
|
|
|
(10000 * Math.log10(Math.max(1, 3 + score))) /
|
|
|
|
Math.pow(hoursElapsed + 2, 1.8);
|
|
|
|
|
|
|
|
// console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
|
|
|
|
|
|
|
|
return rank;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function mdToHtml(text: string) {
|
|
|
|
return { __html: md.render(text) };
|
|
|
|
}
|
|
|
|
|
2022-11-19 02:02:38 +00:00
|
|
|
export function mdToHtmlNoImages(text: string) {
|
|
|
|
return { __html: mdNoImages.render(text) };
|
|
|
|
}
|
|
|
|
|
2022-11-18 02:01:52 +00:00
|
|
|
export function mdToHtmlInline(text: string) {
|
|
|
|
return { __html: md.renderInline(text) };
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function getUnixTime(text?: string): number | undefined {
|
2020-09-06 16:15:25 +00:00
|
|
|
return text ? new Date(text).getTime() / 1000 : undefined;
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function futureDaysToUnixTime(days?: number): number | undefined {
|
2022-01-09 17:53:11 +00:00
|
|
|
return days
|
|
|
|
? Math.trunc(
|
|
|
|
new Date(Date.now() + 1000 * 60 * 60 * 24 * days).getTime() / 1000
|
|
|
|
)
|
|
|
|
: undefined;
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export function canMod(
|
|
|
|
creator_id: number,
|
2023-01-04 16:56:24 +00:00
|
|
|
mods?: CommunityModeratorView[],
|
|
|
|
admins?: PersonViewSafe[],
|
2022-06-21 21:42:29 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo,
|
2021-02-22 02:24:09 +00:00
|
|
|
onSelf = false
|
2020-09-06 16:15:25 +00:00
|
|
|
): boolean {
|
|
|
|
// You can do moderator actions only on the mods added after you.
|
2023-01-04 16:56:24 +00:00
|
|
|
let adminsThenMods =
|
|
|
|
admins
|
|
|
|
?.map(a => a.person.id)
|
|
|
|
.concat(mods?.map(m => m.moderator.id) ?? []) ?? [];
|
|
|
|
|
|
|
|
if (myUserInfo) {
|
|
|
|
let 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;
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function canAdmin(
|
2023-01-04 16:56:24 +00:00
|
|
|
creatorId: number,
|
|
|
|
admins?: PersonViewSafe[],
|
2022-08-17 23:26:50 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo,
|
|
|
|
onSelf = false
|
2022-06-21 21:42:29 +00:00
|
|
|
): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
return canMod(creatorId, undefined, admins, myUserInfo, onSelf);
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isMod(
|
2023-01-04 16:56:24 +00:00
|
|
|
creatorId: number,
|
|
|
|
mods?: CommunityModeratorView[]
|
2022-06-21 21:42:29 +00:00
|
|
|
): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
return mods?.map(m => m.moderator.id).includes(creatorId) ?? false;
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function amMod(
|
2023-01-04 16:56:24 +00:00
|
|
|
mods?: CommunityModeratorView[],
|
2022-06-21 21:42:29 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
return myUserInfo ? isMod(myUserInfo.local_user_view.person.id, mods) : false;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function isAdmin(creatorId: number, admins?: PersonViewSafe[]): boolean {
|
|
|
|
return admins?.map(a => a.person.id).includes(creatorId) ?? false;
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 12:50:47 +00:00
|
|
|
export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
return myUserInfo?.local_user_view.person.admin ?? false;
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function amCommunityCreator(
|
|
|
|
creator_id: number,
|
2023-01-04 16:56:24 +00:00
|
|
|
mods?: CommunityModeratorView[],
|
2022-06-21 21:42:29 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
let myId = myUserInfo?.local_user_view.person.id;
|
|
|
|
// Don't allow mod actions on yourself
|
|
|
|
return myId == mods?.at(0)?.moderator.id && myId != creator_id;
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function amSiteCreator(
|
|
|
|
creator_id: number,
|
2023-01-04 16:56:24 +00:00
|
|
|
admins?: PersonViewSafe[],
|
2022-06-21 21:42:29 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
let myId = myUserInfo?.local_user_view.person.id;
|
|
|
|
return myId == admins?.at(0)?.person.id && myId != creator_id;
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function amTopMod(
|
2023-01-04 16:56:24 +00:00
|
|
|
mods: CommunityModeratorView[],
|
2022-06-21 21:42:29 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
return mods.at(0)?.moderator.id == myUserInfo?.local_user_view.person.id;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 00:53:23 +00:00
|
|
|
const imageRegex = /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/;
|
|
|
|
const videoRegex = /(http)?s?:?(\/\/[^"']*\.(?:mp4|webm))/;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
export function isImage(url: string) {
|
|
|
|
return imageRegex.test(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isVideo(url: string) {
|
|
|
|
return videoRegex.test(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function validURL(str: string) {
|
|
|
|
return !!new URL(str);
|
|
|
|
}
|
|
|
|
|
2020-10-13 21:04:44 +00:00
|
|
|
export function communityRSSUrl(actorId: string, sort: string): string {
|
|
|
|
let url = new URL(actorId);
|
|
|
|
return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export function validEmail(email: string) {
|
2021-06-03 20:57:50 +00:00
|
|
|
let re =
|
|
|
|
/^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
|
2020-09-06 16:15:25 +00:00
|
|
|
return re.test(String(email).toLowerCase());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function capitalizeFirstLetter(str: string): string {
|
|
|
|
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function routeSortTypeToEnum(sort: string): SortType {
|
|
|
|
return SortType[sort];
|
|
|
|
}
|
|
|
|
|
2020-12-26 03:28:05 +00:00
|
|
|
export function listingTypeFromNum(type_: number): ListingType {
|
|
|
|
return Object.values(ListingType)[type_];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function sortTypeFromNum(type_: number): SortType {
|
|
|
|
return Object.values(SortType)[type_];
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export function routeListingTypeToEnum(type: string): ListingType {
|
|
|
|
return ListingType[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function routeDataTypeToEnum(type: string): DataType {
|
|
|
|
return DataType[capitalizeFirstLetter(type)];
|
|
|
|
}
|
|
|
|
|
|
|
|
export function routeSearchTypeToEnum(type: string): SearchType {
|
2020-09-09 02:40:36 +00:00
|
|
|
return SearchType[type];
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 15:24:13 +00:00
|
|
|
export async function getSiteMetadata(url: string) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let form: GetSiteMetadata = { url };
|
2021-08-19 15:24:13 +00:00
|
|
|
let client = new LemmyHttp(httpBase);
|
|
|
|
return client.getSiteMetadata(form);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 02:24:09 +00:00
|
|
|
export function debounce(func: any, wait = 1000, immediate = false) {
|
2020-09-06 16:15:25 +00:00
|
|
|
// '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: any;
|
|
|
|
|
|
|
|
// Calling debounce returns a new anonymous function
|
|
|
|
return function () {
|
|
|
|
// reference the context and args for the setTimeout function
|
2021-02-22 02:24:09 +00:00
|
|
|
var args = arguments;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
// Should the function be called now? If immediate is true
|
|
|
|
// and not already in a timeout then the answer is: Yes
|
|
|
|
var callNow = immediate && !timeout;
|
|
|
|
|
|
|
|
// This is the basic debounce behaviour 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);
|
|
|
|
|
|
|
|
// 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)
|
2021-02-22 02:24:09 +00:00
|
|
|
func.apply(this, args);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}, wait);
|
|
|
|
|
|
|
|
// Immediate mode and no wait timer? Execute the function..
|
2021-02-22 02:24:09 +00:00
|
|
|
if (callNow) func.apply(this, args);
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
export function getLanguages(
|
|
|
|
override?: string,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): string[] {
|
2023-01-04 16:56:24 +00:00
|
|
|
let myLang = myUserInfo?.local_user_view.local_user.interface_language;
|
|
|
|
let lang = override || myLang || "browser";
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2021-02-22 02:24:09 +00:00
|
|
|
if (lang == "browser" && isBrowser()) {
|
2022-02-18 19:34:11 +00:00
|
|
|
return getBrowserLanguages();
|
2020-09-09 20:33:40 +00:00
|
|
|
} else {
|
2022-02-18 19:34:11 +00:00
|
|
|
return [lang];
|
2020-09-09 20:33:40 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 19:34:11 +00:00
|
|
|
function getBrowserLanguages(): string[] {
|
2021-07-16 18:29:48 +00:00
|
|
|
// Intersect lemmy's langs, with the browser langs
|
|
|
|
let langs = languages ? languages.map(l => l.code) : ["en"];
|
2021-07-18 15:08:24 +00:00
|
|
|
|
|
|
|
// NOTE, mobile browsers seem to be missing this list, so append en
|
|
|
|
let allowedLangs = navigator.languages
|
|
|
|
.concat("en")
|
|
|
|
.filter(v => langs.includes(v));
|
2022-02-18 19:34:11 +00:00
|
|
|
return allowedLangs;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 15:35:59 +00:00
|
|
|
export async function fetchThemeList(): Promise<string[]> {
|
|
|
|
return fetch("/css/themelist").then(res => res.json());
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function setTheme(theme: string, forceReload = false) {
|
2020-10-26 07:09:44 +00:00
|
|
|
if (!isBrowser()) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-22 02:24:09 +00:00
|
|
|
if (theme === "browser" && !forceReload) {
|
2020-10-26 07:09:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// This is only run on a force reload
|
2021-02-22 02:24:09 +00:00
|
|
|
if (theme == "browser") {
|
|
|
|
theme = "darkly";
|
2020-10-26 07:09:44 +00:00
|
|
|
}
|
2020-09-24 22:03:03 +00:00
|
|
|
|
2022-03-02 15:35:59 +00:00
|
|
|
let themeList = await fetchThemeList();
|
|
|
|
|
2020-10-26 07:09:44 +00:00
|
|
|
// Unload all the other themes
|
2022-03-02 15:35:59 +00:00
|
|
|
for (var i = 0; i < themeList.length; i++) {
|
|
|
|
let styleSheet = document.getElementById(themeList[i]);
|
2020-10-26 07:09:44 +00:00
|
|
|
if (styleSheet) {
|
2021-02-22 02:24:09 +00:00
|
|
|
styleSheet.setAttribute("disabled", "disabled");
|
2020-09-11 02:40:41 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-26 07:09:44 +00:00
|
|
|
|
|
|
|
document
|
2021-02-22 02:24:09 +00:00
|
|
|
.getElementById("default-light")
|
|
|
|
?.setAttribute("disabled", "disabled");
|
|
|
|
document.getElementById("default-dark")?.setAttribute("disabled", "disabled");
|
2020-10-26 07:09:44 +00:00
|
|
|
|
|
|
|
// Load the theme dynamically
|
2022-03-03 17:55:26 +00:00
|
|
|
let cssLoc = `/css/themes/${theme}.css`;
|
2022-03-02 15:35:59 +00:00
|
|
|
|
2020-10-26 07:09:44 +00:00
|
|
|
loadCss(theme, cssLoc);
|
2023-01-04 16:56:24 +00:00
|
|
|
document.getElementById(theme)?.removeAttribute("disabled");
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 02:40:41 +00:00
|
|
|
export function loadCss(id: string, loc: string) {
|
|
|
|
if (!document.getElementById(id)) {
|
2021-02-22 02:24:09 +00:00
|
|
|
var head = document.getElementsByTagName("head")[0];
|
|
|
|
var link = document.createElement("link");
|
2020-09-11 02:40:41 +00:00
|
|
|
link.id = id;
|
2021-02-22 02:24:09 +00:00
|
|
|
link.rel = "stylesheet";
|
|
|
|
link.type = "text/css";
|
2020-09-11 02:40:41 +00:00
|
|
|
link.href = loc;
|
2021-02-22 02:24:09 +00:00
|
|
|
link.media = "all";
|
2020-09-11 02:40:41 +00:00
|
|
|
head.appendChild(link);
|
|
|
|
}
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
export function objectFlip(obj: any) {
|
|
|
|
const ret = {};
|
|
|
|
Object.keys(obj).forEach(key => {
|
|
|
|
ret[obj[key]] = key;
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
export function showAvatars(
|
2023-01-04 16:56:24 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
2022-06-21 21:42:29 +00:00
|
|
|
): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
return myUserInfo?.local_user_view.local_user.show_avatars ?? true;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
export function showScores(
|
2023-01-04 16:56:24 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
2022-06-21 21:42:29 +00:00
|
|
|
): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
return myUserInfo?.local_user_view.local_user.show_scores ?? true;
|
2021-04-09 16:23:30 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export function isCakeDay(published: string): boolean {
|
|
|
|
// moment(undefined) or moment.utc(undefined) returns the current date/time
|
|
|
|
// moment(null) or moment.utc(null) returns null
|
2022-06-21 21:42:29 +00:00
|
|
|
const createDate = moment.utc(published).local();
|
2020-09-06 16:15:25 +00:00
|
|
|
const currentDate = moment(new Date());
|
|
|
|
|
|
|
|
return (
|
2021-03-15 18:09:31 +00:00
|
|
|
createDate.date() === currentDate.date() &&
|
|
|
|
createDate.month() === currentDate.month() &&
|
|
|
|
createDate.year() !== currentDate.year()
|
2020-09-06 16:15:25 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-04-04 12:40:00 +00:00
|
|
|
export function toast(text: string, background: ThemeColor = "success") {
|
2020-12-06 02:26:01 +00:00
|
|
|
if (isBrowser()) {
|
2023-04-04 12:40:00 +00:00
|
|
|
const backgroundColor = `var(--${background})`;
|
2020-12-06 02:26:01 +00:00
|
|
|
Toastify({
|
|
|
|
text: text,
|
|
|
|
backgroundColor: backgroundColor,
|
2021-02-22 02:24:09 +00:00
|
|
|
gravity: "bottom",
|
|
|
|
position: "left",
|
2023-03-02 23:30:38 +00:00
|
|
|
duration: 5000,
|
2020-12-06 02:26:01 +00:00
|
|
|
}).showToast();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2023-04-04 12:40:00 +00:00
|
|
|
export function pictrsDeleteToast(filename: string, deleteUrl: string) {
|
2020-12-06 02:26:01 +00:00
|
|
|
if (isBrowser()) {
|
2023-04-04 12:40:00 +00:00
|
|
|
const clickToDeleteText = i18n.t("click_to_delete_picture", { filename });
|
|
|
|
const deletePictureText = i18n.t("picture_deleted", {
|
|
|
|
filename,
|
|
|
|
});
|
|
|
|
const failedDeletePictureText = i18n.t("failed_to_delete_picture", {
|
|
|
|
filename,
|
|
|
|
});
|
|
|
|
|
|
|
|
const backgroundColor = `var(--light)`;
|
|
|
|
|
|
|
|
const toast = Toastify({
|
2020-12-06 02:26:01 +00:00
|
|
|
text: clickToDeleteText,
|
|
|
|
backgroundColor: backgroundColor,
|
2021-02-22 02:24:09 +00:00
|
|
|
gravity: "top",
|
|
|
|
position: "right",
|
2020-12-06 02:26:01 +00:00
|
|
|
duration: 10000,
|
|
|
|
onClick: () => {
|
|
|
|
if (toast) {
|
2022-10-21 18:14:28 +00:00
|
|
|
fetch(deleteUrl).then(res => {
|
2022-09-30 20:26:32 +00:00
|
|
|
toast.hideToast();
|
2022-10-21 18:14:28 +00:00
|
|
|
if (res.ok === true) {
|
2022-09-30 20:26:32 +00:00
|
|
|
alert(deletePictureText);
|
2022-10-21 18:14:28 +00:00
|
|
|
} else {
|
2022-09-30 20:26:32 +00:00
|
|
|
alert(failedDeletePictureText);
|
|
|
|
}
|
2022-10-21 18:14:28 +00:00
|
|
|
});
|
2020-12-06 02:26:01 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
close: true,
|
2022-11-19 02:02:38 +00:00
|
|
|
});
|
2023-04-04 12:40:00 +00:00
|
|
|
|
2022-11-19 02:02:38 +00:00
|
|
|
toast.showToast();
|
2020-12-06 02:26:01 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface NotifyInfo {
|
|
|
|
name: string;
|
2023-01-04 16:56:24 +00:00
|
|
|
icon?: string;
|
2020-09-06 16:15:25 +00:00
|
|
|
link: string;
|
2023-01-04 16:56:24 +00:00
|
|
|
body?: string;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function messageToastify(info: NotifyInfo, router: any) {
|
2020-12-06 02:26:01 +00:00
|
|
|
if (isBrowser()) {
|
2021-02-22 02:24:09 +00:00
|
|
|
let htmlBody = info.body ? md.render(info.body) : "";
|
2020-12-06 02:26:01 +00:00
|
|
|
let backgroundColor = `var(--light)`;
|
|
|
|
|
|
|
|
let toast = Toastify({
|
|
|
|
text: `${htmlBody}<br />${info.name}`,
|
2023-01-04 16:56:24 +00:00
|
|
|
avatar: info.icon,
|
2020-12-06 02:26:01 +00:00
|
|
|
backgroundColor: backgroundColor,
|
2021-02-22 02:24:09 +00:00
|
|
|
className: "text-dark",
|
2020-12-06 02:26:01 +00:00
|
|
|
close: true,
|
2021-02-22 02:24:09 +00:00
|
|
|
gravity: "top",
|
|
|
|
position: "right",
|
2020-12-06 02:26:01 +00:00
|
|
|
duration: 5000,
|
2021-04-09 00:53:39 +00:00
|
|
|
escapeMarkup: false,
|
2020-12-06 02:26:01 +00:00
|
|
|
onClick: () => {
|
|
|
|
if (toast) {
|
|
|
|
toast.hideToast();
|
|
|
|
router.history.push(info.link);
|
|
|
|
}
|
|
|
|
},
|
2022-11-19 02:02:38 +00:00
|
|
|
});
|
|
|
|
toast.showToast();
|
2020-12-06 02:26:01 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
export function notifyPost(post_view: PostView, router: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
let info: NotifyInfo = {
|
2020-12-24 01:58:27 +00:00
|
|
|
name: post_view.community.name,
|
2021-01-06 21:06:42 +00:00
|
|
|
icon: post_view.community.icon,
|
2020-12-24 01:58:27 +00:00
|
|
|
link: `/post/${post_view.post.id}`,
|
|
|
|
body: post_view.post.name,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
notify(info, router);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
export function notifyComment(comment_view: CommentView, router: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
let info: NotifyInfo = {
|
2020-12-24 01:58:27 +00:00
|
|
|
name: comment_view.creator.name,
|
2021-01-06 21:06:42 +00:00
|
|
|
icon: comment_view.creator.avatar,
|
2022-07-30 13:28:08 +00:00
|
|
|
link: `/comment/${comment_view.comment.id}`,
|
2020-12-24 01:58:27 +00:00
|
|
|
body: comment_view.comment.content,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
notify(info, router);
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
export function notifyPrivateMessage(pmv: PrivateMessageView, router: any) {
|
2020-09-06 16:15:25 +00:00
|
|
|
let info: NotifyInfo = {
|
2020-12-24 01:58:27 +00:00
|
|
|
name: pmv.creator.name,
|
2021-01-06 21:06:42 +00:00
|
|
|
icon: pmv.creator.avatar,
|
2020-09-06 16:15:25 +00:00
|
|
|
link: `/inbox`,
|
2020-12-24 01:58:27 +00:00
|
|
|
body: pmv.private_message.content,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
|
|
|
notify(info, router);
|
|
|
|
}
|
|
|
|
|
|
|
|
function notify(info: NotifyInfo, router: any) {
|
|
|
|
messageToastify(info, router);
|
|
|
|
|
2022-07-24 12:08:23 +00:00
|
|
|
if (Notification.permission !== "granted") Notification.requestPermission();
|
|
|
|
else {
|
|
|
|
var notification = new Notification(info.name, {
|
2022-09-21 14:16:54 +00:00
|
|
|
...{ body: info.body },
|
2023-01-04 16:56:24 +00:00
|
|
|
...(info.icon && { icon: info.icon }),
|
2022-07-24 12:08:23 +00:00
|
|
|
});
|
2021-09-04 05:20:25 +00:00
|
|
|
|
2022-07-24 12:08:23 +00:00
|
|
|
notification.onclick = (ev: Event): any => {
|
|
|
|
ev.preventDefault();
|
|
|
|
router.history.push(info.link);
|
|
|
|
};
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-07 22:24:48 +00:00
|
|
|
export function setupTribute() {
|
|
|
|
return new Tribute({
|
|
|
|
noMatchTemplate: function () {
|
2021-02-22 02:24:09 +00:00
|
|
|
return "";
|
2020-09-07 22:24:48 +00:00
|
|
|
},
|
|
|
|
collection: [
|
|
|
|
// Emojis
|
|
|
|
{
|
2021-02-22 02:24:09 +00:00
|
|
|
trigger: ":",
|
2020-09-07 22:24:48 +00:00
|
|
|
menuItemTemplate: (item: any) => {
|
|
|
|
let shortName = `:${item.original.key}:`;
|
|
|
|
return `${item.original.val} ${shortName}`;
|
|
|
|
},
|
|
|
|
selectTemplate: (item: any) => {
|
2023-03-27 16:49:46 +00:00
|
|
|
let customEmoji = customEmojisLookup.get(
|
|
|
|
item.original.key
|
|
|
|
)?.custom_emoji;
|
|
|
|
if (customEmoji == undefined) return `${item.original.val}`;
|
2022-11-27 18:02:32 +00:00
|
|
|
else
|
|
|
|
return `![${customEmoji.alt_text}](${customEmoji.image_url} "${customEmoji.shortcode}")`;
|
2020-09-07 22:24:48 +00:00
|
|
|
},
|
2023-03-27 16:49:46 +00:00
|
|
|
values: Object.entries(emojiShortName)
|
|
|
|
.map(e => {
|
|
|
|
return { key: e[1], val: e[0] };
|
|
|
|
})
|
|
|
|
.concat(
|
|
|
|
Array.from(customEmojisLookup.entries()).map(k => ({
|
|
|
|
key: k[0],
|
|
|
|
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}" />`,
|
|
|
|
}))
|
|
|
|
),
|
2020-09-07 22:24:48 +00:00
|
|
|
allowSpaces: false,
|
|
|
|
autocompleteMode: true,
|
|
|
|
// TODO
|
|
|
|
// menuItemLimit: mentionDropdownFetchLimit,
|
|
|
|
menuShowMinLength: 2,
|
|
|
|
},
|
2021-03-15 18:09:31 +00:00
|
|
|
// Persons
|
2020-09-07 22:24:48 +00:00
|
|
|
{
|
2021-02-22 02:24:09 +00:00
|
|
|
trigger: "@",
|
2020-09-07 22:24:48 +00:00
|
|
|
selectTemplate: (item: any) => {
|
2021-03-15 18:09:31 +00:00
|
|
|
let it: PersonTribute = item.original;
|
|
|
|
return `[${it.key}](${it.view.person.actor_id})`;
|
2020-09-07 22:24:48 +00:00
|
|
|
},
|
2022-04-27 20:57:55 +00:00
|
|
|
values: debounce(async (text: string, cb: any) => {
|
|
|
|
cb(await personSearch(text));
|
|
|
|
}),
|
2020-09-07 22:24:48 +00:00
|
|
|
allowSpaces: false,
|
|
|
|
autocompleteMode: true,
|
|
|
|
// TODO
|
|
|
|
// menuItemLimit: mentionDropdownFetchLimit,
|
|
|
|
menuShowMinLength: 2,
|
|
|
|
},
|
|
|
|
|
|
|
|
// Communities
|
|
|
|
{
|
2021-02-22 02:24:09 +00:00
|
|
|
trigger: "!",
|
2020-09-07 22:24:48 +00:00
|
|
|
selectTemplate: (item: any) => {
|
2021-01-23 22:07:46 +00:00
|
|
|
let it: CommunityTribute = item.original;
|
|
|
|
return `[${it.key}](${it.view.community.actor_id})`;
|
2020-09-07 22:24:48 +00:00
|
|
|
},
|
2022-04-27 20:57:55 +00:00
|
|
|
values: debounce(async (text: string, cb: any) => {
|
|
|
|
cb(await communitySearch(text));
|
|
|
|
}),
|
2020-09-07 22:24:48 +00:00
|
|
|
allowSpaces: false,
|
|
|
|
autocompleteMode: true,
|
|
|
|
// TODO
|
|
|
|
// menuItemLimit: mentionDropdownFetchLimit,
|
|
|
|
menuShowMinLength: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2022-11-27 18:02:32 +00:00
|
|
|
function setupEmojiDataModel(custom_emoji_views: CustomEmojiView[]) {
|
|
|
|
let groupedEmojis = groupBy(custom_emoji_views, x => x.custom_emoji.category);
|
|
|
|
for (const [category, emojis] of Object.entries(groupedEmojis)) {
|
|
|
|
customEmojis.push({
|
|
|
|
id: category,
|
|
|
|
name: category,
|
2023-03-27 16:49:46 +00:00
|
|
|
emojis: emojis.map(emoji => ({
|
2022-11-27 18:02:32 +00:00
|
|
|
id: emoji.custom_emoji.shortcode,
|
|
|
|
name: emoji.custom_emoji.shortcode,
|
|
|
|
keywords: emoji.keywords.map(x => x.keyword),
|
2023-03-27 16:49:46 +00:00
|
|
|
skins: [{ src: emoji.custom_emoji.image_url }],
|
|
|
|
})),
|
|
|
|
});
|
2022-11-27 18:02:32 +00:00
|
|
|
}
|
2023-03-27 16:49:46 +00:00
|
|
|
customEmojisLookup = new Map(
|
|
|
|
custom_emoji_views.map(view => [view.custom_emoji.shortcode, view])
|
|
|
|
);
|
2022-11-27 18:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function updateEmojiDataModel(custom_emoji_view: CustomEmojiView) {
|
|
|
|
const emoji: EmojiMartCustomEmoji = {
|
|
|
|
id: custom_emoji_view.custom_emoji.shortcode,
|
|
|
|
name: custom_emoji_view.custom_emoji.shortcode,
|
|
|
|
keywords: custom_emoji_view.keywords.map(x => x.keyword),
|
2023-03-27 16:49:46 +00:00
|
|
|
skins: [{ src: custom_emoji_view.custom_emoji.image_url }],
|
2022-11-27 18:02:32 +00:00
|
|
|
};
|
2023-03-27 16:49:46 +00:00
|
|
|
let categoryIndex = customEmojis.findIndex(
|
|
|
|
x => x.id == custom_emoji_view.custom_emoji.category
|
|
|
|
);
|
2022-11-27 18:02:32 +00:00
|
|
|
if (categoryIndex == -1) {
|
|
|
|
customEmojis.push({
|
|
|
|
id: custom_emoji_view.custom_emoji.category,
|
|
|
|
name: custom_emoji_view.custom_emoji.category,
|
2023-03-27 16:49:46 +00:00
|
|
|
emojis: [emoji],
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let emojiIndex = customEmojis[categoryIndex].emojis.findIndex(
|
|
|
|
x => x.id == custom_emoji_view.custom_emoji.shortcode
|
|
|
|
);
|
2022-11-27 18:02:32 +00:00
|
|
|
if (emojiIndex == -1) {
|
2023-03-27 16:49:46 +00:00
|
|
|
customEmojis[categoryIndex].emojis.push(emoji);
|
|
|
|
} else {
|
2022-11-27 18:02:32 +00:00
|
|
|
customEmojis[categoryIndex].emojis[emojiIndex] = emoji;
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 16:49:46 +00:00
|
|
|
customEmojisLookup.set(
|
|
|
|
custom_emoji_view.custom_emoji.shortcode,
|
|
|
|
custom_emoji_view
|
|
|
|
);
|
2022-11-27 18:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function removeFromEmojiDataModel(id: number) {
|
|
|
|
let view: CustomEmojiView | undefined;
|
|
|
|
for (let item of customEmojisLookup.values()) {
|
|
|
|
if (item.custom_emoji.id === id) {
|
|
|
|
view = item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 16:49:46 +00:00
|
|
|
if (!view) return;
|
|
|
|
const categoryIndex = customEmojis.findIndex(
|
|
|
|
x => x.id == view?.custom_emoji.category
|
|
|
|
);
|
|
|
|
const emojiIndex = customEmojis[categoryIndex].emojis.findIndex(
|
|
|
|
x => x.id == view?.custom_emoji.shortcode
|
|
|
|
);
|
|
|
|
customEmojis[categoryIndex].emojis = customEmojis[
|
|
|
|
categoryIndex
|
|
|
|
].emojis.splice(emojiIndex, 1);
|
2022-11-27 18:02:32 +00:00
|
|
|
|
|
|
|
customEmojisLookup.delete(view?.custom_emoji.shortcode);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupMarkdown() {
|
|
|
|
const markdownItConfig: MarkdownIt.Options = {
|
|
|
|
html: false,
|
|
|
|
linkify: true,
|
|
|
|
typographer: true,
|
|
|
|
};
|
|
|
|
|
2023-03-27 16:49:46 +00:00
|
|
|
const emojiDefs = Array.from(customEmojisLookup.entries()).reduce(
|
|
|
|
(main, [key, value]) => ({ ...main, [key]: value }),
|
|
|
|
{}
|
|
|
|
);
|
2022-11-27 18:02:32 +00:00
|
|
|
md = new MarkdownIt(markdownItConfig)
|
|
|
|
.use(markdown_it_sub)
|
|
|
|
.use(markdown_it_sup)
|
|
|
|
.use(markdown_it_footnote)
|
|
|
|
.use(markdown_it_html5_embed, html5EmbedConfig)
|
|
|
|
.use(markdown_it_container, "spoiler", spoilerConfig)
|
2023-02-25 21:18:52 +00:00
|
|
|
.use(markdown_it_emoji, {
|
2023-03-27 16:49:46 +00:00
|
|
|
defs: emojiDefs,
|
2022-11-27 18:02:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
mdNoImages = new MarkdownIt(markdownItConfig)
|
|
|
|
.use(markdown_it_sub)
|
|
|
|
.use(markdown_it_sup)
|
|
|
|
.use(markdown_it_footnote)
|
|
|
|
.use(markdown_it_html5_embed, html5EmbedConfig)
|
|
|
|
.use(markdown_it_container, "spoiler", spoilerConfig)
|
2023-02-25 21:18:52 +00:00
|
|
|
.use(markdown_it_emoji, {
|
2023-03-27 16:49:46 +00:00
|
|
|
defs: emojiDefs,
|
2022-11-27 18:02:32 +00:00
|
|
|
})
|
|
|
|
.disable("image");
|
|
|
|
var defaultRenderer = md.renderer.rules.image;
|
2023-03-27 16:49:46 +00:00
|
|
|
md.renderer.rules.image = function (
|
|
|
|
tokens: Token[],
|
|
|
|
idx: number,
|
|
|
|
options: MarkdownIt.Options,
|
|
|
|
env: any,
|
|
|
|
self: Renderer
|
|
|
|
) {
|
2022-11-27 18:02:32 +00:00
|
|
|
//Provide custom renderer for our emojis to allow us to add a css class and force size dimensions on them.
|
|
|
|
const item = tokens[idx] as any;
|
|
|
|
const title = item.attrs.length >= 3 ? item.attrs[2][1] : "";
|
|
|
|
const src: string = item.attrs[0][1];
|
|
|
|
const isCustomEmoji = customEmojisLookup.get(title) != undefined;
|
|
|
|
if (!isCustomEmoji) {
|
|
|
|
return defaultRenderer?.(tokens, idx, options, env, self) ?? "";
|
|
|
|
}
|
|
|
|
const alt_text = item.content;
|
|
|
|
return `<img class="icon icon-emoji" src="${src}" title="${title}" alt="${alt_text}"/>`;
|
2023-03-27 16:49:46 +00:00
|
|
|
};
|
2022-11-27 18:02:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-27 16:49:46 +00:00
|
|
|
export function getEmojiMart(
|
|
|
|
onEmojiSelect: (e: any) => void,
|
|
|
|
customPickerOptions: any = {}
|
|
|
|
) {
|
|
|
|
const pickerOptions = {
|
|
|
|
...customPickerOptions,
|
|
|
|
onEmojiSelect: onEmojiSelect,
|
|
|
|
custom: customEmojis,
|
|
|
|
};
|
2022-11-27 18:02:32 +00:00
|
|
|
return new Picker(pickerOptions);
|
|
|
|
}
|
|
|
|
|
2021-01-23 21:40:24 +00:00
|
|
|
var tippyInstance: any;
|
2020-09-08 04:09:11 +00:00
|
|
|
if (isBrowser()) {
|
2021-02-22 02:24:09 +00:00
|
|
|
tippyInstance = tippy("[data-tippy-content]");
|
2020-09-08 04:09:11 +00:00
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
export function setupTippy() {
|
2020-09-08 04:09:11 +00:00
|
|
|
if (isBrowser()) {
|
2021-01-23 21:40:24 +00:00
|
|
|
tippyInstance.forEach((e: any) => e.destroy());
|
2021-02-22 02:24:09 +00:00
|
|
|
tippyInstance = tippy("[data-tippy-content]", {
|
2020-09-08 04:09:11 +00:00
|
|
|
delay: [500, 0],
|
|
|
|
// Display on "long press"
|
2021-02-22 02:24:09 +00:00
|
|
|
touch: ["hold", 500],
|
2020-09-08 04:09:11 +00:00
|
|
|
});
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-15 18:09:31 +00:00
|
|
|
interface PersonTribute {
|
2021-01-23 22:07:46 +00:00
|
|
|
key: string;
|
2021-03-15 18:09:31 +00:00
|
|
|
view: PersonViewSafe;
|
2021-01-23 22:07:46 +00:00
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:55 +00:00
|
|
|
async function personSearch(text: string): Promise<PersonTribute[]> {
|
|
|
|
let users = (await fetchUsers(text)).users;
|
|
|
|
let persons: PersonTribute[] = users.map(pv => {
|
|
|
|
let tribute: PersonTribute = {
|
|
|
|
key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
|
|
|
|
view: pv,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2022-04-27 20:57:55 +00:00
|
|
|
return tribute;
|
|
|
|
});
|
|
|
|
return persons;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-23 22:07:46 +00:00
|
|
|
interface CommunityTribute {
|
|
|
|
key: string;
|
|
|
|
view: CommunityView;
|
|
|
|
}
|
|
|
|
|
2022-04-27 20:57:55 +00:00
|
|
|
async function communitySearch(text: string): Promise<CommunityTribute[]> {
|
|
|
|
let comms = (await fetchCommunities(text)).communities;
|
|
|
|
let communities: CommunityTribute[] = comms.map(cv => {
|
|
|
|
let tribute: CommunityTribute = {
|
|
|
|
key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
|
|
|
|
view: cv,
|
2020-09-06 16:15:25 +00:00
|
|
|
};
|
2022-04-27 20:57:55 +00:00
|
|
|
return tribute;
|
|
|
|
});
|
|
|
|
return communities;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-23 19:19:14 +00:00
|
|
|
export function getListingTypeFromProps(
|
|
|
|
props: any,
|
2022-06-21 21:42:29 +00:00
|
|
|
defaultListingType: ListingType,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
2022-05-23 19:19:14 +00:00
|
|
|
): ListingType {
|
2023-01-04 16:56:24 +00:00
|
|
|
let myLt = myUserInfo?.local_user_view.local_user.default_listing_type;
|
2020-09-08 04:09:11 +00:00
|
|
|
return props.match.params.listing_type
|
|
|
|
? routeListingTypeToEnum(props.match.params.listing_type)
|
2023-01-04 16:56:24 +00:00
|
|
|
: myLt
|
|
|
|
? Object.values(ListingType)[myLt]
|
|
|
|
: defaultListingType;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 13:04:06 +00:00
|
|
|
export function getListingTypeFromPropsNoDefault(props: any): ListingType {
|
|
|
|
return props.match.params.listing_type
|
|
|
|
? routeListingTypeToEnum(props.match.params.listing_type)
|
|
|
|
: ListingType.Local;
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export function getDataTypeFromProps(props: any): DataType {
|
2020-09-08 04:09:11 +00:00
|
|
|
return props.match.params.data_type
|
|
|
|
? routeDataTypeToEnum(props.match.params.data_type)
|
|
|
|
: DataType.Post;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
export function getSortTypeFromProps(
|
|
|
|
props: any,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): SortType {
|
2023-01-04 16:56:24 +00:00
|
|
|
let mySortType = myUserInfo?.local_user_view.local_user.default_sort_type;
|
2020-09-08 04:09:11 +00:00
|
|
|
return props.match.params.sort
|
|
|
|
? routeSortTypeToEnum(props.match.params.sort)
|
2023-01-04 16:56:24 +00:00
|
|
|
: mySortType
|
|
|
|
? Object.values(SortType)[mySortType]
|
|
|
|
: SortType.Active;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getPageFromProps(props: any): number {
|
2020-09-07 15:32:07 +00:00
|
|
|
return props.match.params.page ? Number(props.match.params.page) : 1;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-09-08 18:44:55 +00:00
|
|
|
export function getRecipientIdFromProps(props: any): number {
|
|
|
|
return props.match.params.recipient_id
|
|
|
|
? Number(props.match.params.recipient_id)
|
|
|
|
: 1;
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function getIdFromProps(props: any): number | undefined {
|
|
|
|
let id = props.match.params.post_id;
|
|
|
|
return id ? Number(id) : undefined;
|
2020-09-09 00:48:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function getCommentIdFromProps(props: any): number | undefined {
|
|
|
|
let id = props.match.params.comment_id;
|
|
|
|
return id ? Number(id) : undefined;
|
2020-09-09 00:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getUsernameFromProps(props: any): string {
|
|
|
|
return props.match.params.username;
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function editCommentRes(data: CommentView, comments?: CommentView[]) {
|
|
|
|
let found = comments?.find(c => c.comment.id == data.comment.id);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (found) {
|
2020-12-24 01:58:27 +00:00
|
|
|
found.comment.content = data.comment.content;
|
2022-08-17 23:26:50 +00:00
|
|
|
found.comment.distinguished = data.comment.distinguished;
|
2020-12-24 01:58:27 +00:00
|
|
|
found.comment.updated = data.comment.updated;
|
|
|
|
found.comment.removed = data.comment.removed;
|
|
|
|
found.comment.deleted = data.comment.deleted;
|
|
|
|
found.counts.upvotes = data.counts.upvotes;
|
|
|
|
found.counts.downvotes = data.counts.downvotes;
|
|
|
|
found.counts.score = data.counts.score;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function saveCommentRes(data: CommentView, comments?: CommentView[]) {
|
|
|
|
let found = comments?.find(c => c.comment.id == data.comment.id);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (found) {
|
2020-12-24 01:58:27 +00:00
|
|
|
found.saved = data.saved;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 02:56:18 +00:00
|
|
|
export function updatePersonBlock(
|
2022-06-21 21:42:29 +00:00
|
|
|
data: BlockPersonResponse,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
2023-01-04 16:56:24 +00:00
|
|
|
) {
|
|
|
|
let mui = myUserInfo;
|
|
|
|
if (mui) {
|
|
|
|
if (data.blocked) {
|
|
|
|
mui.person_blocks.push({
|
|
|
|
person: mui.local_user_view.person,
|
|
|
|
target: data.person_view.person,
|
|
|
|
});
|
|
|
|
toast(`${i18n.t("blocked")} ${data.person_view.person.name}`);
|
|
|
|
} else {
|
|
|
|
mui.person_blocks = mui.person_blocks.filter(
|
|
|
|
i => i.target.id != data.person_view.person.id
|
|
|
|
);
|
|
|
|
toast(`${i18n.t("unblocked")} ${data.person_view.person.name}`);
|
|
|
|
}
|
|
|
|
}
|
2021-08-20 02:56:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function updateCommunityBlock(
|
2022-06-21 21:42:29 +00:00
|
|
|
data: BlockCommunityResponse,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
2023-01-04 16:56:24 +00:00
|
|
|
) {
|
|
|
|
let mui = myUserInfo;
|
|
|
|
if (mui) {
|
|
|
|
if (data.blocked) {
|
|
|
|
mui.community_blocks.push({
|
|
|
|
person: mui.local_user_view.person,
|
|
|
|
community: data.community_view.community,
|
|
|
|
});
|
|
|
|
toast(`${i18n.t("blocked")} ${data.community_view.community.name}`);
|
|
|
|
} else {
|
|
|
|
mui.community_blocks = mui.community_blocks.filter(
|
|
|
|
i => i.community.id != data.community_view.community.id
|
|
|
|
);
|
|
|
|
toast(`${i18n.t("unblocked")} ${data.community_view.community.name}`);
|
|
|
|
}
|
|
|
|
}
|
2021-08-20 02:56:18 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export function createCommentLikeRes(
|
2020-12-24 01:58:27 +00:00
|
|
|
data: CommentView,
|
2023-01-04 16:56:24 +00:00
|
|
|
comments?: CommentView[]
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let found = comments?.find(c => c.comment.id === data.comment.id);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (found) {
|
2020-12-24 01:58:27 +00:00
|
|
|
found.counts.score = data.counts.score;
|
|
|
|
found.counts.upvotes = data.counts.upvotes;
|
|
|
|
found.counts.downvotes = data.counts.downvotes;
|
|
|
|
if (data.my_vote !== null) {
|
|
|
|
found.my_vote = data.my_vote;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function createPostLikeFindRes(data: PostView, posts?: PostView[]) {
|
|
|
|
let found = posts?.find(p => p.post.id == data.post.id);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (found) {
|
|
|
|
createPostLikeRes(data, found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function createPostLikeRes(data: PostView, post_view?: PostView) {
|
2020-12-24 01:58:27 +00:00
|
|
|
if (post_view) {
|
|
|
|
post_view.counts.score = data.counts.score;
|
|
|
|
post_view.counts.upvotes = data.counts.upvotes;
|
|
|
|
post_view.counts.downvotes = data.counts.downvotes;
|
|
|
|
if (data.my_vote !== null) {
|
|
|
|
post_view.my_vote = data.my_vote;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function editPostFindRes(data: PostView, posts?: PostView[]) {
|
|
|
|
let found = posts?.find(p => p.post.id == data.post.id);
|
2020-09-06 16:15:25 +00:00
|
|
|
if (found) {
|
|
|
|
editPostRes(data, found);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
export function editPostRes(data: PostView, post: PostView) {
|
2020-09-06 16:15:25 +00:00
|
|
|
if (post) {
|
2020-12-24 01:58:27 +00:00
|
|
|
post.post.url = data.post.url;
|
|
|
|
post.post.name = data.post.name;
|
|
|
|
post.post.nsfw = data.post.nsfw;
|
|
|
|
post.post.deleted = data.post.deleted;
|
|
|
|
post.post.removed = data.post.removed;
|
2022-12-14 15:03:18 +00:00
|
|
|
post.post.featured_community = data.post.featured_community;
|
|
|
|
post.post.featured_local = data.post.featured_local;
|
2020-12-24 01:58:27 +00:00
|
|
|
post.post.body = data.post.body;
|
|
|
|
post.post.locked = data.post.locked;
|
|
|
|
post.saved = data.saved;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:50:47 +00:00
|
|
|
// TODO possible to make these generic?
|
2021-09-28 10:38:59 +00:00
|
|
|
export function updatePostReportRes(
|
|
|
|
data: PostReportView,
|
2023-01-04 16:56:24 +00:00
|
|
|
reports?: PostReportView[]
|
2021-09-28 10:38:59 +00:00
|
|
|
) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let found = reports?.find(p => p.post_report.id == data.post_report.id);
|
2021-09-28 10:38:59 +00:00
|
|
|
if (found) {
|
|
|
|
found.post_report = data.post_report;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function updateCommentReportRes(
|
|
|
|
data: CommentReportView,
|
2023-01-04 16:56:24 +00:00
|
|
|
reports?: CommentReportView[]
|
2021-09-28 10:38:59 +00:00
|
|
|
) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let found = reports?.find(c => c.comment_report.id == data.comment_report.id);
|
2021-09-28 10:38:59 +00:00
|
|
|
if (found) {
|
|
|
|
found.comment_report = data.comment_report;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:50:47 +00:00
|
|
|
export function updatePrivateMessageReportRes(
|
|
|
|
data: PrivateMessageReportView,
|
2023-01-04 16:56:24 +00:00
|
|
|
reports?: PrivateMessageReportView[]
|
2022-09-28 12:50:47 +00:00
|
|
|
) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let found = reports?.find(
|
2022-09-28 12:50:47 +00:00
|
|
|
c => c.private_message_report.id == data.private_message_report.id
|
|
|
|
);
|
|
|
|
if (found) {
|
|
|
|
found.private_message_report = data.private_message_report;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-30 15:26:45 +00:00
|
|
|
export function updateRegistrationApplicationRes(
|
|
|
|
data: RegistrationApplicationView,
|
2023-01-04 16:56:24 +00:00
|
|
|
applications?: RegistrationApplicationView[]
|
2021-12-30 15:26:45 +00:00
|
|
|
) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let found = applications?.find(
|
2021-12-30 15:26:45 +00:00
|
|
|
ra => ra.registration_application.id == data.registration_application.id
|
|
|
|
);
|
|
|
|
if (found) {
|
|
|
|
found.registration_application = data.registration_application;
|
|
|
|
found.admin = data.admin;
|
|
|
|
found.creator_local_user = data.creator_local_user;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
export function commentsToFlatNodes(comments: CommentView[]): CommentNodeI[] {
|
2020-09-06 16:15:25 +00:00
|
|
|
let nodes: CommentNodeI[] = [];
|
|
|
|
for (let comment of comments) {
|
2022-07-30 13:28:08 +00:00
|
|
|
nodes.push({ comment_view: comment, children: [], depth: 0 });
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
return nodes;
|
|
|
|
}
|
|
|
|
|
2022-07-30 13:28:08 +00:00
|
|
|
export function convertCommentSortType(sort: SortType): CommentSortType {
|
2020-09-06 16:15:25 +00:00
|
|
|
if (
|
|
|
|
sort == SortType.TopAll ||
|
|
|
|
sort == SortType.TopDay ||
|
|
|
|
sort == SortType.TopWeek ||
|
|
|
|
sort == SortType.TopMonth ||
|
|
|
|
sort == SortType.TopYear
|
|
|
|
) {
|
|
|
|
return CommentSortType.Top;
|
|
|
|
} else if (sort == SortType.New) {
|
|
|
|
return CommentSortType.New;
|
|
|
|
} else if (sort == SortType.Hot || sort == SortType.Active) {
|
|
|
|
return CommentSortType.Hot;
|
|
|
|
} else {
|
|
|
|
return CommentSortType.Hot;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-08 04:23:31 +00:00
|
|
|
export function buildCommentsTree(
|
|
|
|
comments: CommentView[],
|
2022-07-30 13:28:08 +00:00
|
|
|
parentComment: boolean
|
2021-02-08 04:23:31 +00:00
|
|
|
): CommentNodeI[] {
|
|
|
|
let map = new Map<number, CommentNodeI>();
|
2022-07-30 13:28:08 +00:00
|
|
|
let depthOffset = !parentComment
|
|
|
|
? 0
|
2023-01-04 16:56:24 +00:00
|
|
|
: getDepthFromComment(comments[0].comment) ?? 0;
|
2022-07-30 13:28:08 +00:00
|
|
|
|
2021-02-08 04:23:31 +00:00
|
|
|
for (let comment_view of comments) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let depthI = getDepthFromComment(comment_view.comment) ?? 0;
|
|
|
|
let depth = depthI ? depthI - depthOffset : 0;
|
2021-02-08 04:23:31 +00:00
|
|
|
let node: CommentNodeI = {
|
2023-01-04 16:56:24 +00:00
|
|
|
comment_view,
|
2021-02-08 04:23:31 +00:00
|
|
|
children: [],
|
2023-01-04 16:56:24 +00:00
|
|
|
depth,
|
2021-02-08 04:23:31 +00:00
|
|
|
};
|
|
|
|
map.set(comment_view.comment.id, { ...node });
|
|
|
|
}
|
2022-07-30 13:28:08 +00:00
|
|
|
|
2021-02-08 04:23:31 +00:00
|
|
|
let tree: CommentNodeI[] = [];
|
2022-07-30 13:28:08 +00:00
|
|
|
|
|
|
|
// if its a parent comment fetch, then push the first comment to the top node.
|
|
|
|
if (parentComment) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let cNode = map.get(comments[0].comment.id);
|
|
|
|
if (cNode) {
|
|
|
|
tree.push(cNode);
|
|
|
|
}
|
2022-07-30 13:28:08 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 04:23:31 +00:00
|
|
|
for (let comment_view of comments) {
|
|
|
|
let child = map.get(comment_view.comment.id);
|
2023-01-04 16:56:24 +00:00
|
|
|
if (child) {
|
|
|
|
let parent_id = getCommentParentId(comment_view.comment);
|
|
|
|
if (parent_id) {
|
|
|
|
let parent = map.get(parent_id);
|
2022-06-21 21:42:29 +00:00
|
|
|
// Necessary because blocked comment might not exist
|
|
|
|
if (parent) {
|
|
|
|
parent.children.push(child);
|
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
} else {
|
2022-07-30 13:28:08 +00:00
|
|
|
if (!parentComment) {
|
|
|
|
tree.push(child);
|
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-08 04:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function getCommentParentId(comment?: CommentI): number | undefined {
|
|
|
|
let split = comment?.path.split(".");
|
2022-07-30 13:28:08 +00:00
|
|
|
// remove the 0
|
2023-01-04 16:56:24 +00:00
|
|
|
split?.shift();
|
2022-07-30 13:28:08 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
return split && split.length > 1
|
|
|
|
? Number(split.at(split.length - 2))
|
|
|
|
: undefined;
|
2021-02-08 04:23:31 +00:00
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function getDepthFromComment(comment?: CommentI): number | undefined {
|
|
|
|
let len = comment?.path.split(".").length;
|
|
|
|
return len ? len - 2 : undefined;
|
2022-07-30 13:28:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function insertCommentIntoTree(
|
|
|
|
tree: CommentNodeI[],
|
|
|
|
cv: CommentView,
|
|
|
|
parentComment: boolean
|
|
|
|
) {
|
2021-02-08 04:23:31 +00:00
|
|
|
// Building a fake node to be used for later
|
|
|
|
let node: CommentNodeI = {
|
|
|
|
comment_view: cv,
|
|
|
|
children: [],
|
|
|
|
depth: 0,
|
|
|
|
};
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
let parentId = getCommentParentId(cv.comment);
|
|
|
|
if (parentId) {
|
|
|
|
let parent_comment = searchCommentTree(tree, parentId);
|
|
|
|
if (parent_comment) {
|
|
|
|
node.depth = parent_comment.depth + 1;
|
|
|
|
parent_comment.children.unshift(node);
|
|
|
|
}
|
|
|
|
} else if (!parentComment) {
|
|
|
|
tree.unshift(node);
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 04:23:31 +00:00
|
|
|
export function searchCommentTree(
|
|
|
|
tree: CommentNodeI[],
|
|
|
|
id: number
|
2023-01-04 16:56:24 +00:00
|
|
|
): CommentNodeI | undefined {
|
2021-02-08 04:23:31 +00:00
|
|
|
for (let node of tree) {
|
|
|
|
if (node.comment_view.comment.id === id) {
|
2023-01-04 16:56:24 +00:00
|
|
|
return node;
|
2021-02-08 04:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (const child of node.children) {
|
2022-06-21 21:42:29 +00:00
|
|
|
let res = searchCommentTree([child], id);
|
2021-02-08 04:23:31 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
if (res) {
|
2021-02-08 04:23:31 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-04 16:56:24 +00:00
|
|
|
return undefined;
|
2021-02-08 04:23:31 +00:00
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export const colorList: string[] = [
|
|
|
|
hsl(0),
|
2022-07-30 13:28:08 +00:00
|
|
|
hsl(50),
|
2020-09-06 16:15:25 +00:00
|
|
|
hsl(100),
|
|
|
|
hsl(150),
|
|
|
|
hsl(200),
|
|
|
|
hsl(250),
|
|
|
|
hsl(300),
|
|
|
|
];
|
|
|
|
|
|
|
|
function hsl(num: number) {
|
|
|
|
return `hsla(${num}, 35%, 50%, 1)`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function hostname(url: string): string {
|
|
|
|
let cUrl = new URL(url);
|
2020-09-11 18:09:21 +00:00
|
|
|
return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function validTitle(title?: string): boolean {
|
|
|
|
// Initial title is null, minimum length is taken care of by textarea's minLength={3}
|
2022-06-21 21:42:29 +00:00
|
|
|
if (!title || title.length < 3) return true;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2021-02-22 02:24:09 +00:00
|
|
|
const regex = new RegExp(/.*\S.*/, "g");
|
2020-09-06 16:15:25 +00:00
|
|
|
|
|
|
|
return regex.test(title);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function siteBannerCss(banner: string): string {
|
|
|
|
return ` \
|
|
|
|
background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
|
|
|
|
background-attachment: fixed; \
|
|
|
|
background-position: top; \
|
|
|
|
background-repeat: no-repeat; \
|
|
|
|
background-size: 100% cover; \
|
|
|
|
|
|
|
|
width: 100%; \
|
|
|
|
max-height: 100vh; \
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isBrowser() {
|
2021-02-22 02:24:09 +00:00
|
|
|
return typeof window !== "undefined";
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
2020-09-07 03:41:46 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function setIsoData(context: any): IsoData {
|
2022-06-21 21:42:29 +00:00
|
|
|
// If its the browser, you need to deserialize the data from the window
|
|
|
|
if (isBrowser()) {
|
|
|
|
let json = window.isoData;
|
|
|
|
let routeData = json.routeData;
|
2023-01-04 16:56:24 +00:00
|
|
|
let site_res = json.site_res;
|
2022-06-21 21:42:29 +00:00
|
|
|
|
|
|
|
let isoData: IsoData = {
|
|
|
|
path: json.path,
|
2022-06-27 20:50:47 +00:00
|
|
|
site_res,
|
2023-01-04 16:56:24 +00:00
|
|
|
routeData,
|
2022-06-21 21:42:29 +00:00
|
|
|
};
|
|
|
|
return isoData;
|
|
|
|
} else return context.router.staticContext;
|
|
|
|
}
|
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function wsSubscribe(parseMessage: any): Subscription | undefined {
|
2020-09-07 15:32:07 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
return WebSocketService.Instance.subject
|
|
|
|
.pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
|
|
|
|
.subscribe(
|
|
|
|
msg => parseMessage(msg),
|
|
|
|
err => console.error(err),
|
2021-02-22 02:24:09 +00:00
|
|
|
() => console.log("complete")
|
2020-09-07 15:32:07 +00:00
|
|
|
);
|
|
|
|
} else {
|
2023-01-04 16:56:24 +00:00
|
|
|
return undefined;
|
2020-09-07 15:32:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-25 21:16:41 +00:00
|
|
|
|
2021-02-22 02:24:09 +00:00
|
|
|
moment.updateLocale("en", {
|
2020-09-25 21:16:41 +00:00
|
|
|
relativeTime: {
|
2021-02-22 02:24:09 +00:00
|
|
|
future: "in %s",
|
|
|
|
past: "%s ago",
|
|
|
|
s: "<1m",
|
|
|
|
ss: "%ds",
|
|
|
|
m: "1m",
|
|
|
|
mm: "%dm",
|
|
|
|
h: "1h",
|
|
|
|
hh: "%dh",
|
|
|
|
d: "1d",
|
|
|
|
dd: "%dd",
|
|
|
|
w: "1w",
|
|
|
|
ww: "%dw",
|
|
|
|
M: "1M",
|
|
|
|
MM: "%dM",
|
|
|
|
y: "1Y",
|
|
|
|
yy: "%dY",
|
2020-09-25 21:16:41 +00:00
|
|
|
},
|
|
|
|
});
|
2021-02-02 18:36:59 +00:00
|
|
|
|
|
|
|
export function saveScrollPosition(context: any) {
|
|
|
|
let path: string = context.router.route.location.pathname;
|
|
|
|
let y = window.scrollY;
|
|
|
|
sessionStorage.setItem(`scrollPosition_${path}`, y.toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function restoreScrollPosition(context: any) {
|
|
|
|
let path: string = context.router.route.location.pathname;
|
|
|
|
let y = Number(sessionStorage.getItem(`scrollPosition_${path}`));
|
|
|
|
window.scrollTo(0, y);
|
|
|
|
}
|
2021-04-23 21:48:31 +00:00
|
|
|
|
|
|
|
export function showLocal(isoData: IsoData): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
let linked = isoData.site_res.federated_instances?.linked;
|
|
|
|
return linked ? linked.length > 0 : false;
|
2021-04-23 21:48:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-04 16:47:10 +00:00
|
|
|
export interface ChoicesValue {
|
2021-04-23 21:48:31 +00:00
|
|
|
value: string;
|
|
|
|
label: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function communityToChoice(cv: CommunityView): ChoicesValue {
|
|
|
|
let choice: ChoicesValue = {
|
|
|
|
value: cv.community.id.toString(),
|
2021-04-26 20:06:16 +00:00
|
|
|
label: communitySelectName(cv),
|
2021-04-23 21:48:31 +00:00
|
|
|
};
|
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function personToChoice(pvs: PersonViewSafe): ChoicesValue {
|
|
|
|
let choice: ChoicesValue = {
|
|
|
|
value: pvs.person.id.toString(),
|
2021-04-26 20:06:16 +00:00
|
|
|
label: personSelectName(pvs),
|
2021-04-23 21:48:31 +00:00
|
|
|
};
|
|
|
|
return choice;
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function fetchCommunities(q: string) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let form: Search = {
|
2021-04-23 21:48:31 +00:00
|
|
|
q,
|
2023-01-04 16:56:24 +00:00
|
|
|
type_: SearchType.Communities,
|
|
|
|
sort: SortType.TopAll,
|
|
|
|
listing_type: ListingType.All,
|
|
|
|
page: 1,
|
|
|
|
limit: fetchLimit,
|
|
|
|
auth: myAuth(false),
|
|
|
|
};
|
2021-04-23 21:48:31 +00:00
|
|
|
let client = new LemmyHttp(httpBase);
|
|
|
|
return client.search(form);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function fetchUsers(q: string) {
|
2023-01-04 16:56:24 +00:00
|
|
|
let form: Search = {
|
2021-04-23 21:48:31 +00:00
|
|
|
q,
|
2023-01-04 16:56:24 +00:00
|
|
|
type_: SearchType.Users,
|
|
|
|
sort: SortType.TopAll,
|
|
|
|
listing_type: ListingType.All,
|
|
|
|
page: 1,
|
|
|
|
limit: fetchLimit,
|
|
|
|
auth: myAuth(false),
|
|
|
|
};
|
2021-04-23 21:48:31 +00:00
|
|
|
let client = new LemmyHttp(httpBase);
|
|
|
|
return client.search(form);
|
|
|
|
}
|
|
|
|
|
|
|
|
export const choicesConfig = {
|
|
|
|
shouldSort: false,
|
|
|
|
searchResultLimit: fetchLimit,
|
|
|
|
classNames: {
|
2022-09-22 18:13:22 +00:00
|
|
|
containerOuter: "choices custom-select px-0",
|
2022-08-17 23:28:40 +00:00
|
|
|
containerInner:
|
|
|
|
"choices__inner bg-secondary border-0 py-0 modlog-choices-font-size",
|
2022-06-04 16:47:10 +00:00
|
|
|
input: "form-control",
|
2022-09-22 18:13:22 +00:00
|
|
|
inputCloned: "choices__input--cloned",
|
2022-06-04 16:47:10 +00:00
|
|
|
list: "choices__list",
|
|
|
|
listItems: "choices__list--multiple",
|
|
|
|
listSingle: "choices__list--single py-0",
|
|
|
|
listDropdown: "choices__list--dropdown",
|
|
|
|
item: "choices__item bg-secondary",
|
|
|
|
itemSelectable: "choices__item--selectable",
|
|
|
|
itemDisabled: "choices__item--disabled",
|
|
|
|
itemChoice: "choices__item--choice",
|
|
|
|
placeholder: "choices__placeholder",
|
|
|
|
group: "choices__group",
|
|
|
|
groupHeading: "choices__heading",
|
2021-04-23 21:48:31 +00:00
|
|
|
button: "choices__button",
|
|
|
|
activeState: "is-active",
|
|
|
|
focusState: "is-focused",
|
|
|
|
openState: "is-open",
|
|
|
|
disabledState: "is-disabled",
|
|
|
|
highlightedState: "text-info",
|
|
|
|
selectedState: "text-info",
|
|
|
|
flippedState: "is-flipped",
|
|
|
|
loadingState: "is-loading",
|
|
|
|
noResults: "has-no-results",
|
|
|
|
noChoices: "has-no-choices",
|
|
|
|
},
|
|
|
|
};
|
2021-04-26 20:06:16 +00:00
|
|
|
|
|
|
|
export function communitySelectName(cv: CommunityView): string {
|
|
|
|
return cv.community.local
|
2021-10-28 21:39:41 +00:00
|
|
|
? cv.community.title
|
2021-10-29 18:55:14 +00:00
|
|
|
: `${hostname(cv.community.actor_id)}/${cv.community.title}`;
|
2021-04-26 20:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function personSelectName(pvs: PersonViewSafe): string {
|
2023-01-04 16:56:24 +00:00
|
|
|
let pName = pvs.person.display_name ?? pvs.person.name;
|
2021-10-29 18:55:14 +00:00
|
|
|
return pvs.person.local ? pName : `${hostname(pvs.person.actor_id)}/${pName}`;
|
2021-04-26 20:06:16 +00:00
|
|
|
}
|
2021-07-17 20:42:55 +00:00
|
|
|
|
|
|
|
export function initializeSite(site: GetSiteResponse) {
|
2021-08-20 02:56:18 +00:00
|
|
|
UserService.Instance.myUserInfo = site.my_user;
|
2022-02-18 19:34:11 +00:00
|
|
|
i18n.changeLanguage(getLanguages()[0]);
|
2022-11-27 18:02:32 +00:00
|
|
|
setupEmojiDataModel(site.custom_emojis);
|
|
|
|
setupMarkdown();
|
2021-07-17 20:42:55 +00:00
|
|
|
}
|
2021-09-18 14:27:49 +00:00
|
|
|
|
2021-09-18 14:30:03 +00:00
|
|
|
const SHORTNUM_SI_FORMAT = new Intl.NumberFormat("en-US", {
|
|
|
|
maximumSignificantDigits: 3,
|
2021-09-18 14:27:49 +00:00
|
|
|
//@ts-ignore
|
|
|
|
notation: "compact",
|
|
|
|
compactDisplay: "short",
|
|
|
|
});
|
|
|
|
|
2021-09-18 16:35:49 +00:00
|
|
|
export function numToSI(value: number): string {
|
2021-09-18 14:27:49 +00:00
|
|
|
return SHORTNUM_SI_FORMAT.format(value);
|
|
|
|
}
|
2022-01-09 17:53:11 +00:00
|
|
|
|
|
|
|
export function isBanned(ps: PersonSafe): boolean {
|
2022-06-21 21:42:29 +00:00
|
|
|
let expires = ps.ban_expires;
|
2022-01-09 17:53:11 +00:00
|
|
|
// Add Z to convert from UTC date
|
2022-06-21 21:42:29 +00:00
|
|
|
// TODO this check probably isn't necessary anymore
|
2023-01-04 16:56:24 +00:00
|
|
|
if (expires) {
|
|
|
|
if (ps.banned && new Date(expires + "Z") > new Date()) {
|
2022-01-09 17:53:11 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return ps.banned;
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 10:41:50 +00:00
|
|
|
|
|
|
|
export function pushNotNull(array: any[], new_item?: any) {
|
|
|
|
if (new_item) {
|
|
|
|
array.push(...new_item);
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
|
2023-01-04 16:56:24 +00:00
|
|
|
export function myAuth(throwErr = true): string | undefined {
|
2022-06-21 21:42:29 +00:00
|
|
|
return UserService.Instance.auth(throwErr);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function enableDownvotes(siteRes: GetSiteResponse): boolean {
|
2022-11-09 19:53:07 +00:00
|
|
|
return siteRes.site_view.local_site.enable_downvotes;
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function enableNsfw(siteRes: GetSiteResponse): boolean {
|
2022-11-09 19:53:07 +00:00
|
|
|
return siteRes.site_view.local_site.enable_nsfw;
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
2022-07-30 13:28:08 +00:00
|
|
|
|
|
|
|
export function postToCommentSortType(sort: SortType): CommentSortType {
|
|
|
|
if ([SortType.Active, SortType.Hot].includes(sort)) {
|
|
|
|
return CommentSortType.Hot;
|
|
|
|
} else if ([SortType.New, SortType.NewComments].includes(sort)) {
|
|
|
|
return CommentSortType.New;
|
|
|
|
} else if (sort == SortType.Old) {
|
|
|
|
return CommentSortType.Old;
|
|
|
|
} else {
|
|
|
|
return CommentSortType.Top;
|
|
|
|
}
|
|
|
|
}
|
2022-09-22 15:14:58 +00:00
|
|
|
|
|
|
|
export function myFirstDiscussionLanguageId(
|
2022-12-19 15:57:29 +00:00
|
|
|
allLanguages: Language[],
|
|
|
|
siteLanguages: number[],
|
2022-09-22 15:14:58 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
2023-01-04 16:56:24 +00:00
|
|
|
): number | undefined {
|
|
|
|
return selectableLanguages(
|
|
|
|
allLanguages,
|
|
|
|
siteLanguages,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
myUserInfo
|
|
|
|
).at(0)?.id;
|
2022-09-22 15:14:58 +00:00
|
|
|
}
|
|
|
|
|
2022-09-28 12:50:47 +00:00
|
|
|
export function canCreateCommunity(
|
|
|
|
siteRes: GetSiteResponse,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
2022-11-09 19:53:07 +00:00
|
|
|
let adminOnly = siteRes.site_view.local_site.community_creation_admin_only;
|
2022-09-28 12:50:47 +00:00
|
|
|
return !adminOnly || amAdmin(myUserInfo);
|
2022-09-22 15:14:58 +00:00
|
|
|
}
|
2022-09-22 17:13:59 +00:00
|
|
|
|
|
|
|
export function isPostBlocked(
|
|
|
|
pv: PostView,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
2023-01-04 16:56:24 +00:00
|
|
|
return (
|
|
|
|
(myUserInfo?.community_blocks
|
|
|
|
.map(c => c.community.id)
|
|
|
|
.includes(pv.community.id) ||
|
|
|
|
myUserInfo?.person_blocks
|
|
|
|
.map(p => p.target.id)
|
|
|
|
.includes(pv.creator.id)) ??
|
|
|
|
false
|
|
|
|
);
|
2022-09-22 17:13:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks to make sure you can view NSFW posts. Returns true if you can.
|
|
|
|
export function nsfwCheck(
|
|
|
|
pv: PostView,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
|
|
|
let nsfw = pv.post.nsfw || pv.community.nsfw;
|
2023-01-04 16:56:24 +00:00
|
|
|
let myShowNsfw = myUserInfo?.local_user_view.local_user.show_nsfw ?? false;
|
|
|
|
return !nsfw || (nsfw && myShowNsfw);
|
2022-09-22 17:13:59 +00:00
|
|
|
}
|
2022-11-20 16:08:44 +00:00
|
|
|
|
2022-11-27 18:02:32 +00:00
|
|
|
export function getRandomFromList<T>(list: T[]): T | undefined {
|
2023-03-27 16:49:46 +00:00
|
|
|
return list.length == 0
|
|
|
|
? undefined
|
|
|
|
: list.at(Math.floor(Math.random() * list.length));
|
2022-11-20 16:08:44 +00:00
|
|
|
}
|
2022-12-19 15:57:29 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This shows what language you can select
|
|
|
|
*
|
|
|
|
* Use showAll for the site form
|
|
|
|
* Use showSite for the profile and community forms
|
|
|
|
* Use false for both those to filter on your profile and site ones
|
|
|
|
*/
|
|
|
|
export function selectableLanguages(
|
|
|
|
allLanguages: Language[],
|
|
|
|
siteLanguages: number[],
|
2023-01-04 16:56:24 +00:00
|
|
|
showAll?: boolean,
|
|
|
|
showSite?: boolean,
|
2022-12-19 15:57:29 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): Language[] {
|
|
|
|
let allLangIds = allLanguages.map(l => l.id);
|
2023-01-04 16:56:24 +00:00
|
|
|
let myLangs = myUserInfo?.discussion_languages ?? allLangIds;
|
2022-12-19 15:57:29 +00:00
|
|
|
myLangs = myLangs.length == 0 ? allLangIds : myLangs;
|
|
|
|
let siteLangs = siteLanguages.length == 0 ? allLangIds : siteLanguages;
|
|
|
|
|
|
|
|
if (showAll) {
|
|
|
|
return allLanguages;
|
|
|
|
} else {
|
|
|
|
if (showSite) {
|
|
|
|
return allLanguages.filter(x => siteLangs.includes(x.id));
|
|
|
|
} else {
|
|
|
|
return allLanguages
|
|
|
|
.filter(x => siteLangs.includes(x.id))
|
|
|
|
.filter(x => myLangs.includes(x.id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-27 02:06:42 +00:00
|
|
|
|
|
|
|
export function uploadImage(image: File): Promise<UploadImageResponse> {
|
|
|
|
const client = new LemmyHttp(httpBase);
|
|
|
|
|
|
|
|
return client.uploadImage({ image });
|
|
|
|
}
|
2023-03-27 16:49:46 +00:00
|
|
|
|
2022-11-27 18:02:32 +00:00
|
|
|
interface EmojiMartCategory {
|
2023-03-27 16:49:46 +00:00
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
emojis: EmojiMartCustomEmoji[];
|
2022-11-27 18:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface EmojiMartCustomEmoji {
|
2023-03-27 16:49:46 +00:00
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
keywords: string[];
|
|
|
|
skins: EmojiMartSkin[];
|
2022-11-27 18:02:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface EmojiMartSkin {
|
2023-03-27 16:49:46 +00:00
|
|
|
src: string;
|
2022-11-27 18:02:32 +00:00
|
|
|
}
|
|
|
|
|
2023-03-27 16:49:46 +00:00
|
|
|
const groupBy = <T>(
|
|
|
|
array: T[],
|
|
|
|
predicate: (value: T, index: number, array: T[]) => string
|
|
|
|
) =>
|
2022-11-27 18:02:32 +00:00
|
|
|
array.reduce((acc, value, index, array) => {
|
|
|
|
(acc[predicate(value, index, array)] ||= []).push(value);
|
|
|
|
return acc;
|
2023-03-27 16:49:46 +00:00
|
|
|
}, {} as { [key: string]: T[] });
|