2022-09-22 15:14:58 +00:00
|
|
|
import { Err, None, Ok, Option, Result, Some } from "@sniptt/monads";
|
2022-06-21 21:42:29 +00:00
|
|
|
import { ClassConstructor, deserialize, serialize } from "class-transformer";
|
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,
|
2021-08-20 02:56:18 +00:00
|
|
|
CommunityBlockView,
|
2022-06-21 21:42:29 +00:00
|
|
|
CommunityModeratorView,
|
2021-07-17 20:42:55 +00:00
|
|
|
CommunityView,
|
2021-08-19 15:24:13 +00:00
|
|
|
GetSiteMetadata,
|
2021-07-17 20:42:55 +00:00
|
|
|
GetSiteResponse,
|
|
|
|
LemmyHttp,
|
|
|
|
LemmyWebsocket,
|
|
|
|
ListingType,
|
2021-08-20 02:56:18 +00:00
|
|
|
MyUserInfo,
|
|
|
|
PersonBlockView,
|
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,
|
|
|
|
} from "lemmy-js-client";
|
|
|
|
import markdown_it from "markdown-it";
|
|
|
|
import markdown_it_container from "markdown-it-container";
|
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";
|
|
|
|
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;
|
2020-09-06 16:15:25 +00:00
|
|
|
|
2022-02-24 15:31:44 +00:00
|
|
|
export const relTags = "noopener nofollow";
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
export const md = new markdown_it({
|
2022-05-24 16:40:31 +00:00
|
|
|
html: false,
|
2020-09-06 16:15:25 +00:00
|
|
|
linkify: true,
|
|
|
|
typographer: true,
|
|
|
|
})
|
|
|
|
.use(markdown_it_sub)
|
|
|
|
.use(markdown_it_sup)
|
2022-02-02 14:53:09 +00:00
|
|
|
.use(markdown_it_footnote)
|
2021-09-18 19:44:39 +00:00
|
|
|
.use(markdown_it_html5_embed, {
|
|
|
|
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"',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
2021-02-22 02:24:09 +00:00
|
|
|
.use(markdown_it_container, "spoiler", {
|
2020-09-06 16:15:25 +00:00
|
|
|
validate: function (params: any) {
|
|
|
|
return params.trim().match(/^spoiler\s+(.*)$/);
|
|
|
|
},
|
|
|
|
|
|
|
|
render: function (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
|
2021-02-22 02:24:09 +00:00
|
|
|
return "</details>\n";
|
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-18 02:01:52 +00:00
|
|
|
export function mdToHtmlInline(text: string) {
|
|
|
|
return { __html: md.renderInline(text) };
|
|
|
|
}
|
|
|
|
|
2020-09-06 16:15:25 +00:00
|
|
|
export function getUnixTime(text: string): number {
|
|
|
|
return text ? new Date(text).getTime() / 1000 : undefined;
|
|
|
|
}
|
|
|
|
|
2022-01-09 17:53:11 +00:00
|
|
|
export function futureDaysToUnixTime(days: number): number {
|
|
|
|
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(
|
2022-06-21 21:42:29 +00:00
|
|
|
mods: Option<CommunityModeratorView[]>,
|
|
|
|
admins: Option<PersonViewSafe[]>,
|
2020-09-06 16:15:25 +00:00
|
|
|
creator_id: number,
|
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.
|
2022-06-21 21:42:29 +00:00
|
|
|
let adminsThenMods = admins
|
|
|
|
.unwrapOr([])
|
|
|
|
.map(a => a.person.id)
|
|
|
|
.concat(mods.unwrapOr([]).map(m => m.moderator.id));
|
|
|
|
|
|
|
|
return myUserInfo.match({
|
|
|
|
some: me => {
|
|
|
|
let myIndex = adminsThenMods.findIndex(
|
|
|
|
id => id == me.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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
none: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function canAdmin(
|
|
|
|
admins: Option<PersonViewSafe[]>,
|
|
|
|
creator_id: number,
|
2022-08-17 23:26:50 +00:00
|
|
|
myUserInfo = UserService.Instance.myUserInfo,
|
|
|
|
onSelf = false
|
2022-06-21 21:42:29 +00:00
|
|
|
): boolean {
|
2022-08-17 23:26:50 +00:00
|
|
|
return canMod(None, admins, creator_id, myUserInfo, onSelf);
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isMod(
|
|
|
|
mods: Option<CommunityModeratorView[]>,
|
|
|
|
creator_id: number
|
|
|
|
): boolean {
|
|
|
|
return mods.match({
|
|
|
|
some: mods => mods.map(m => m.moderator.id).includes(creator_id),
|
|
|
|
none: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function amMod(
|
|
|
|
mods: Option<CommunityModeratorView[]>,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
|
|
|
return myUserInfo.match({
|
|
|
|
some: mui => isMod(mods, mui.local_user_view.person.id),
|
|
|
|
none: false,
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
export function isAdmin(
|
|
|
|
admins: Option<PersonViewSafe[]>,
|
|
|
|
creator_id: number
|
|
|
|
): boolean {
|
|
|
|
return admins.match({
|
|
|
|
some: admins => admins.map(a => a.person.id).includes(creator_id),
|
|
|
|
none: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-28 12:50:47 +00:00
|
|
|
export function amAdmin(myUserInfo = UserService.Instance.myUserInfo): boolean {
|
|
|
|
return myUserInfo
|
|
|
|
.map(mui => mui.local_user_view.person.admin)
|
|
|
|
.unwrapOr(false);
|
2022-06-21 21:42:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function amCommunityCreator(
|
|
|
|
mods: Option<CommunityModeratorView[]>,
|
|
|
|
creator_id: number,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
|
|
|
return mods.match({
|
|
|
|
some: mods =>
|
|
|
|
myUserInfo
|
|
|
|
.map(mui => mui.local_user_view.person.id)
|
|
|
|
.match({
|
|
|
|
some: myId =>
|
|
|
|
myId == mods[0].moderator.id &&
|
|
|
|
// Don't allow mod actions on yourself
|
|
|
|
myId != creator_id,
|
|
|
|
none: false,
|
|
|
|
}),
|
|
|
|
none: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function amSiteCreator(
|
|
|
|
admins: Option<PersonViewSafe[]>,
|
|
|
|
creator_id: number,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
|
|
|
return admins.match({
|
|
|
|
some: admins =>
|
|
|
|
myUserInfo
|
|
|
|
.map(mui => mui.local_user_view.person.id)
|
|
|
|
.match({
|
|
|
|
some: myId =>
|
|
|
|
myId == admins[0].person.id &&
|
|
|
|
// Don't allow mod actions on yourself
|
|
|
|
myId != creator_id,
|
|
|
|
none: false,
|
|
|
|
}),
|
|
|
|
none: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function amTopMod(
|
|
|
|
mods: Option<CommunityModeratorView[]>,
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
|
|
|
return mods.match({
|
|
|
|
some: mods =>
|
|
|
|
myUserInfo.match({
|
|
|
|
some: mui => mods[0].moderator.id == mui.local_user_view.person.id,
|
|
|
|
none: false,
|
|
|
|
}),
|
|
|
|
none: false,
|
|
|
|
});
|
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) {
|
2022-07-18 19:58:37 +00:00
|
|
|
let form = new GetSiteMetadata({
|
2021-08-19 15:24:13 +00:00
|
|
|
url,
|
2022-07-18 19:58:37 +00:00
|
|
|
});
|
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[] {
|
|
|
|
let myLang = myUserInfo
|
2022-09-22 15:14:58 +00:00
|
|
|
.map(m => m.local_user_view.local_user.interface_language)
|
2022-06-21 21:42:29 +00:00
|
|
|
.unwrapOr("browser");
|
|
|
|
let lang = override || myLang;
|
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);
|
2021-02-22 02:24:09 +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(
|
|
|
|
myUserInfo: Option<MyUserInfo> = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
|
|
|
return myUserInfo
|
|
|
|
.map(m => m.local_user_view.local_user.show_avatars)
|
|
|
|
.unwrapOr(true);
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
export function showScores(
|
|
|
|
myUserInfo: Option<MyUserInfo> = UserService.Instance.myUserInfo
|
|
|
|
): boolean {
|
|
|
|
return myUserInfo
|
|
|
|
.map(m => m.local_user_view.local_user.show_scores)
|
|
|
|
.unwrapOr(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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-02-22 02:24:09 +00:00
|
|
|
export function toast(text: string, background = "success") {
|
2020-12-06 02:26:01 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
let backgroundColor = `var(--${background})`;
|
|
|
|
Toastify({
|
|
|
|
text: text,
|
|
|
|
backgroundColor: backgroundColor,
|
2021-02-22 02:24:09 +00:00
|
|
|
gravity: "bottom",
|
|
|
|
position: "left",
|
2020-12-06 02:26:01 +00:00
|
|
|
}).showToast();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function pictrsDeleteToast(
|
|
|
|
clickToDeleteText: string,
|
|
|
|
deletePictureText: string,
|
2022-09-30 20:26:32 +00:00
|
|
|
failedDeletePictureText: string,
|
2020-09-06 16:15:25 +00:00
|
|
|
deleteUrl: string
|
|
|
|
) {
|
2020-12-06 02:26:01 +00:00
|
|
|
if (isBrowser()) {
|
|
|
|
let backgroundColor = `var(--light)`;
|
|
|
|
let toast = Toastify({
|
|
|
|
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,
|
|
|
|
}).showToast();
|
|
|
|
}
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface NotifyInfo {
|
|
|
|
name: string;
|
2022-06-21 21:42:29 +00:00
|
|
|
icon: Option<string>;
|
2020-09-06 16:15:25 +00:00
|
|
|
link: string;
|
|
|
|
body: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
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}`,
|
2022-06-21 21:42:29 +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);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}).showToast();
|
|
|
|
}
|
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 },
|
|
|
|
...(info.icon.isSome() && { icon: info.icon.unwrap() }),
|
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) => {
|
2021-02-21 21:22:53 +00:00
|
|
|
return `${item.original.val}`;
|
2020-09-07 22:24:48 +00:00
|
|
|
},
|
|
|
|
values: Object.entries(emojiShortName).map(e => {
|
|
|
|
return { key: e[1], val: e[0] };
|
|
|
|
}),
|
|
|
|
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
|
|
|
|
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 {
|
2020-09-08 04:09:11 +00:00
|
|
|
return props.match.params.listing_type
|
|
|
|
? routeListingTypeToEnum(props.match.params.listing_type)
|
2022-06-21 21:42:29 +00:00
|
|
|
: myUserInfo.match({
|
|
|
|
some: me =>
|
|
|
|
Object.values(ListingType)[
|
|
|
|
me.local_user_view.local_user.default_listing_type
|
|
|
|
],
|
|
|
|
none: 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 {
|
2020-09-08 04:09:11 +00:00
|
|
|
return props.match.params.sort
|
|
|
|
? routeSortTypeToEnum(props.match.params.sort)
|
2022-06-21 21:42:29 +00:00
|
|
|
: myUserInfo.match({
|
|
|
|
some: mui =>
|
|
|
|
Object.values(SortType)[
|
|
|
|
mui.local_user_view.local_user.default_sort_type
|
|
|
|
],
|
|
|
|
none: 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;
|
|
|
|
}
|
|
|
|
|
2022-07-30 13:28:08 +00:00
|
|
|
export function getIdFromProps(props: any): Option<number> {
|
|
|
|
let id: string = props.match.params.post_id;
|
|
|
|
return id ? Some(Number(id)) : None;
|
2020-09-09 00:48:17 +00:00
|
|
|
}
|
|
|
|
|
2022-07-30 13:28:08 +00:00
|
|
|
export function getCommentIdFromProps(props: any): Option<number> {
|
|
|
|
let id: string = props.match.params.comment_id;
|
|
|
|
return id ? Some(Number(id)) : None;
|
2020-09-09 00:48:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function getUsernameFromProps(props: any): string {
|
|
|
|
return props.match.params.username;
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
// TODO Should only use the return now, no state?
|
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
|
|
|
|
): Option<PersonBlockView[]> {
|
|
|
|
return myUserInfo.match({
|
|
|
|
some: (mui: MyUserInfo) => {
|
|
|
|
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}`);
|
|
|
|
}
|
|
|
|
return Some(mui.person_blocks);
|
|
|
|
},
|
|
|
|
none: None,
|
|
|
|
});
|
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
|
|
|
|
): Option<CommunityBlockView[]> {
|
|
|
|
return myUserInfo.match({
|
|
|
|
some: (mui: MyUserInfo) => {
|
|
|
|
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}`);
|
|
|
|
}
|
|
|
|
return Some(mui.community_blocks);
|
|
|
|
},
|
|
|
|
none: None,
|
|
|
|
});
|
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,
|
|
|
|
comments: CommentView[]
|
2020-09-06 16:15:25 +00:00
|
|
|
) {
|
2020-12-24 01:58:27 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +00:00
|
|
|
export function createPostLikeRes(data: PostView, post_view: PostView) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:58:27 +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;
|
|
|
|
post.post.stickied = data.post.stickied;
|
|
|
|
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,
|
|
|
|
reports: PostReportView[]
|
|
|
|
) {
|
2022-02-02 14:56:43 +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,
|
|
|
|
reports: CommentReportView[]
|
|
|
|
) {
|
2022-02-02 14:56:43 +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,
|
|
|
|
reports: PrivateMessageReportView[]
|
|
|
|
) {
|
|
|
|
let found = reports.find(
|
|
|
|
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,
|
|
|
|
applications: RegistrationApplicationView[]
|
|
|
|
) {
|
|
|
|
let found = applications.find(
|
|
|
|
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
|
|
|
|
: getDepthFromComment(comments[0].comment);
|
|
|
|
|
2021-02-08 04:23:31 +00:00
|
|
|
for (let comment_view of comments) {
|
|
|
|
let node: CommentNodeI = {
|
|
|
|
comment_view: comment_view,
|
|
|
|
children: [],
|
2022-07-30 13:28:08 +00:00
|
|
|
depth: getDepthFromComment(comment_view.comment) - depthOffset,
|
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) {
|
|
|
|
tree.push(map.get(comments[0].comment.id));
|
|
|
|
}
|
|
|
|
|
2021-02-08 04:23:31 +00:00
|
|
|
for (let comment_view of comments) {
|
|
|
|
let child = map.get(comment_view.comment.id);
|
2022-07-30 13:28:08 +00:00
|
|
|
let parent_id = getCommentParentId(comment_view.comment);
|
2022-06-21 21:42:29 +00:00
|
|
|
parent_id.match({
|
|
|
|
some: parentId => {
|
|
|
|
let parent = map.get(parentId);
|
|
|
|
// Necessary because blocked comment might not exist
|
|
|
|
if (parent) {
|
|
|
|
parent.children.push(child);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
none: () => {
|
2022-07-30 13:28:08 +00:00
|
|
|
if (!parentComment) {
|
|
|
|
tree.push(child);
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
},
|
|
|
|
});
|
2021-02-08 04:23:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2022-07-30 13:28:08 +00:00
|
|
|
export function getCommentParentId(comment: CommentI): Option<number> {
|
|
|
|
let split = comment.path.split(".");
|
|
|
|
// remove the 0
|
|
|
|
split.shift();
|
|
|
|
|
|
|
|
if (split.length > 1) {
|
|
|
|
return Some(Number(split[split.length - 2]));
|
|
|
|
} else {
|
|
|
|
return None;
|
2021-02-08 04:23:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-30 13:28:08 +00:00
|
|
|
export function getDepthFromComment(comment: CommentI): number {
|
|
|
|
return comment.path.split(".").length - 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
2022-07-30 13:28:08 +00:00
|
|
|
getCommentParentId(cv.comment).match({
|
2022-06-21 21:42:29 +00:00
|
|
|
some: parentId => {
|
|
|
|
let parentComment = searchCommentTree(tree, parentId);
|
|
|
|
parentComment.match({
|
|
|
|
some: pComment => {
|
|
|
|
node.depth = pComment.depth + 1;
|
|
|
|
pComment.children.unshift(node);
|
|
|
|
},
|
|
|
|
none: void 0,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
none: () => {
|
2022-07-30 13:28:08 +00:00
|
|
|
if (!parentComment) {
|
|
|
|
tree.unshift(node);
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
},
|
|
|
|
});
|
2020-09-06 16:15:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 04:23:31 +00:00
|
|
|
export function searchCommentTree(
|
|
|
|
tree: CommentNodeI[],
|
|
|
|
id: number
|
2022-06-21 21:42:29 +00:00
|
|
|
): Option<CommentNodeI> {
|
2021-02-08 04:23:31 +00:00
|
|
|
for (let node of tree) {
|
|
|
|
if (node.comment_view.comment.id === id) {
|
2022-06-21 21:42:29 +00:00
|
|
|
return Some(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
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
if (res.isSome()) {
|
2021-02-08 04:23:31 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 21:42:29 +00:00
|
|
|
return None;
|
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
|
|
|
|
2022-06-21 21:42:29 +00:00
|
|
|
export function setIsoData<Type1, Type2, Type3, Type4, Type5>(
|
|
|
|
context: any,
|
|
|
|
cls1?: ClassConstructor<Type1>,
|
|
|
|
cls2?: ClassConstructor<Type2>,
|
|
|
|
cls3?: ClassConstructor<Type3>,
|
|
|
|
cls4?: ClassConstructor<Type4>,
|
|
|
|
cls5?: ClassConstructor<Type5>
|
|
|
|
): IsoData {
|
|
|
|
// If its the browser, you need to deserialize the data from the window
|
|
|
|
if (isBrowser()) {
|
|
|
|
let json = window.isoData;
|
|
|
|
let routeData = json.routeData;
|
|
|
|
let routeDataOut: any[] = [];
|
|
|
|
|
|
|
|
// Can't do array looping because of specific type constructor required
|
|
|
|
if (routeData[0]) {
|
|
|
|
routeDataOut[0] = convertWindowJson(cls1, routeData[0]);
|
|
|
|
}
|
|
|
|
if (routeData[1]) {
|
|
|
|
routeDataOut[1] = convertWindowJson(cls2, routeData[1]);
|
|
|
|
}
|
|
|
|
if (routeData[2]) {
|
|
|
|
routeDataOut[2] = convertWindowJson(cls3, routeData[2]);
|
|
|
|
}
|
|
|
|
if (routeData[3]) {
|
|
|
|
routeDataOut[3] = convertWindowJson(cls4, routeData[3]);
|
|
|
|
}
|
|
|
|
if (routeData[4]) {
|
|
|
|
routeDataOut[4] = convertWindowJson(cls5, routeData[4]);
|
|
|
|
}
|
2022-06-27 20:50:47 +00:00
|
|
|
let site_res = convertWindowJson(GetSiteResponse, 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,
|
2022-06-21 21:42:29 +00:00
|
|
|
routeData: routeDataOut,
|
|
|
|
};
|
|
|
|
return isoData;
|
|
|
|
} else return context.router.staticContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Necessary since window ISOData can't store function types like Option
|
|
|
|
*/
|
|
|
|
export function convertWindowJson<T>(cls: ClassConstructor<T>, data: any): T {
|
|
|
|
return deserialize(cls, serialize(data));
|
2020-09-07 15:32:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function wsSubscribe(parseMessage: any): Subscription {
|
|
|
|
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 {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
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 {
|
2022-06-21 21:42:29 +00:00
|
|
|
return isoData.site_res.federated_instances
|
|
|
|
.map(f => f.linked.length > 0)
|
|
|
|
.unwrapOr(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) {
|
2022-07-18 19:58:37 +00:00
|
|
|
let form = new Search({
|
2021-04-23 21:48:31 +00:00
|
|
|
q,
|
2022-06-21 21:42:29 +00:00
|
|
|
type_: Some(SearchType.Communities),
|
|
|
|
sort: Some(SortType.TopAll),
|
|
|
|
listing_type: Some(ListingType.All),
|
|
|
|
page: Some(1),
|
|
|
|
limit: Some(fetchLimit),
|
|
|
|
community_id: None,
|
|
|
|
community_name: None,
|
|
|
|
creator_id: None,
|
|
|
|
auth: auth(false).ok(),
|
2022-07-18 19:58:37 +00:00
|
|
|
});
|
2021-04-23 21:48:31 +00:00
|
|
|
let client = new LemmyHttp(httpBase);
|
|
|
|
return client.search(form);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function fetchUsers(q: string) {
|
2022-07-18 19:58:37 +00:00
|
|
|
let form = new Search({
|
2021-04-23 21:48:31 +00:00
|
|
|
q,
|
2022-06-21 21:42:29 +00:00
|
|
|
type_: Some(SearchType.Users),
|
|
|
|
sort: Some(SortType.TopAll),
|
|
|
|
listing_type: Some(ListingType.All),
|
|
|
|
page: Some(1),
|
|
|
|
limit: Some(fetchLimit),
|
|
|
|
community_id: None,
|
|
|
|
community_name: None,
|
|
|
|
creator_id: None,
|
|
|
|
auth: auth(false).ok(),
|
2022-07-18 19:58:37 +00:00
|
|
|
});
|
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 {
|
2022-06-21 21:42:29 +00:00
|
|
|
let pName = pvs.person.display_name.unwrapOr(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]);
|
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
|
|
|
|
if (expires.isSome()) {
|
|
|
|
if (ps.banned && new Date(expires.unwrap() + "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
|
|
|
|
|
|
|
export function auth(throwErr = true): Result<string, string> {
|
|
|
|
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 arrayGet<T>(arr: Array<T>, index: number): Result<T, string> {
|
|
|
|
let out = arr.at(index);
|
|
|
|
if (out == undefined) {
|
|
|
|
return Err("Index undefined");
|
|
|
|
} else {
|
|
|
|
return Ok(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function myFirstDiscussionLanguageId(
|
|
|
|
myUserInfo = UserService.Instance.myUserInfo
|
|
|
|
): Option<number> {
|
|
|
|
return myUserInfo.andThen(mui =>
|
|
|
|
arrayGet(mui.discussion_languages, 0)
|
|
|
|
.ok()
|
|
|
|
.map(i => i.id)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return myUserInfo
|
|
|
|
.map(
|
|
|
|
mui =>
|
|
|
|
mui.community_blocks
|
|
|
|
.map(c => c.community.id)
|
|
|
|
.includes(pv.community.id) ||
|
|
|
|
mui.person_blocks.map(p => p.target.id).includes(pv.creator.id)
|
|
|
|
)
|
|
|
|
.unwrapOr(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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;
|
|
|
|
return (
|
|
|
|
!nsfw ||
|
|
|
|
(nsfw &&
|
|
|
|
myUserInfo
|
|
|
|
.map(m => m.local_user_view.local_user.show_nsfw)
|
|
|
|
.unwrapOr(false))
|
|
|
|
);
|
|
|
|
}
|