1 import "moment/locale/es";
2 import "moment/locale/el";
3 import "moment/locale/eu";
4 import "moment/locale/eo";
5 import "moment/locale/de";
6 import "moment/locale/zh-cn";
7 import "moment/locale/fr";
8 import "moment/locale/sv";
9 import "moment/locale/ru";
10 import "moment/locale/nl";
11 import "moment/locale/it";
12 import "moment/locale/fi";
13 import "moment/locale/ca";
14 import "moment/locale/fa";
15 import "moment/locale/pl";
16 import "moment/locale/pt-br";
17 import "moment/locale/ja";
18 import "moment/locale/ka";
19 import "moment/locale/hi";
20 import "moment/locale/gl";
21 import "moment/locale/tr";
22 import "moment/locale/hu";
23 import "moment/locale/uk";
24 import "moment/locale/sq";
25 import "moment/locale/km";
26 import "moment/locale/ga";
27 import "moment/locale/sr";
28 import "moment/locale/ko";
29 import "moment/locale/da";
30 import "moment/locale/hr";
31 import "moment/locale/bg";
32 import "moment/locale/id";
33 import "moment/locale/nb";
38 LocalUserSettingsView,
43 WebSocketJsonResponse,
52 } from "lemmy-js-client";
58 CommentNode as CommentNodeI,
59 } from "./interfaces";
60 import { UserService, WebSocketService } from "./services";
63 Tribute = require("tributejs");
65 import markdown_it from "markdown-it";
66 import markdown_it_sub from "markdown-it-sub";
67 import markdown_it_sup from "markdown-it-sup";
68 import markdown_it_container from "markdown-it-container";
69 import emojiShortName from "emoji-short-name";
70 import Toastify from "toastify-js";
71 import tippy from "tippy.js";
72 import moment from "moment";
73 import { Subscription } from "rxjs";
74 import { retryWhen, delay, take } from "rxjs/operators";
75 import { i18n } from "./i18next";
76 import { httpBase } from "./env";
78 export const wsClient = new LemmyWebsocket();
80 export const favIconUrl = "/static/assets/icons/favicon.svg";
81 export const favIconPngUrl = "/static/assets/icons/apple-touch-icon.png";
83 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
84 export const repoUrl = "https://github.com/LemmyNet";
85 export const joinLemmyUrl = "https://join-lemmy.org";
86 export const supportLemmyUrl = `${joinLemmyUrl}/support`;
87 export const docsUrl = `${joinLemmyUrl}/docs/en/index.html`;
88 export const helpGuideUrl = `${joinLemmyUrl}/docs/en/about/guide.html`; // TODO find a way to redirect to the non-en folder
89 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
90 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
91 export const archiveUrl = "https://archive.is";
92 export const elementUrl = "https://element.io/";
94 export const postRefetchSeconds: number = 60 * 1000;
95 export const fetchLimit = 20;
96 export const mentionDropdownFetchLimit = 10;
98 export const languages = [
138 export const themes = [
154 const DEFAULT_ALPHABET =
155 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
157 function getRandomCharFromAlphabet(alphabet: string): string {
158 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
161 export function randomStr(
162 idDesiredLength = 20,
163 alphabet = DEFAULT_ALPHABET
166 * Create n-long array and map it to random chars from given alphabet.
167 * Then join individual chars as string
169 return Array.from({ length: idDesiredLength })
171 return getRandomCharFromAlphabet(alphabet);
176 export function wsJsonToRes<ResponseType>(
177 msg: WebSocketJsonResponse<ResponseType>
178 ): WebSocketResponse<ResponseType> {
185 export function wsUserOp(msg: any): UserOperation {
186 let opStr: string = msg.op;
187 return UserOperation[opStr];
190 export const md = new markdown_it({
195 .use(markdown_it_sub)
196 .use(markdown_it_sup)
197 .use(markdown_it_container, "spoiler", {
198 validate: function (params: any) {
199 return params.trim().match(/^spoiler\s+(.*)$/);
202 render: function (tokens: any, idx: any) {
203 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
205 if (tokens[idx].nesting === 1) {
207 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
210 return "</details>\n";
215 export function hotRankComment(comment_view: CommentView): number {
216 return hotRank(comment_view.counts.score, comment_view.comment.published);
219 export function hotRankActivePost(post_view: PostView): number {
220 return hotRank(post_view.counts.score, post_view.counts.newest_comment_time);
223 export function hotRankPost(post_view: PostView): number {
224 return hotRank(post_view.counts.score, post_view.post.published);
227 export function hotRank(score: number, timeStr: string): number {
228 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
229 let date: Date = new Date(timeStr + "Z"); // Add Z to convert from UTC date
230 let now: Date = new Date();
231 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
234 (10000 * Math.log10(Math.max(1, 3 + score))) /
235 Math.pow(hoursElapsed + 2, 1.8);
237 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
242 export function mdToHtml(text: string) {
243 return { __html: md.render(text) };
246 export function getUnixTime(text: string): number {
247 return text ? new Date(text).getTime() / 1000 : undefined;
250 export function canMod(
251 localUserView: LocalUserSettingsView,
256 // You can do moderator actions only on the mods added after you.
258 let yourIndex = modIds.findIndex(id => id == localUserView.person.id);
259 if (yourIndex == -1) {
262 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
263 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
264 return !modIds.includes(creator_id);
271 export function isMod(modIds: number[], creator_id: number): boolean {
272 return modIds.includes(creator_id);
275 const imageRegex = new RegExp(
276 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
278 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
280 export function isImage(url: string) {
281 return imageRegex.test(url);
284 export function isVideo(url: string) {
285 return videoRegex.test(url);
288 export function validURL(str: string) {
289 return !!new URL(str);
292 export function communityRSSUrl(actorId: string, sort: string): string {
293 let url = new URL(actorId);
294 return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
297 export function validEmail(email: string) {
299 /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
300 return re.test(String(email).toLowerCase());
303 export function capitalizeFirstLetter(str: string): string {
304 return str.charAt(0).toUpperCase() + str.slice(1);
307 export function routeSortTypeToEnum(sort: string): SortType {
308 return SortType[sort];
311 export function listingTypeFromNum(type_: number): ListingType {
312 return Object.values(ListingType)[type_];
315 export function sortTypeFromNum(type_: number): SortType {
316 return Object.values(SortType)[type_];
319 export function routeListingTypeToEnum(type: string): ListingType {
320 return ListingType[type];
323 export function routeDataTypeToEnum(type: string): DataType {
324 return DataType[capitalizeFirstLetter(type)];
327 export function routeSearchTypeToEnum(type: string): SearchType {
328 return SearchType[type];
331 export async function getPageTitle(url: string) {
332 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
333 let title = await res.title;
337 export function debounce(func: any, wait = 1000, immediate = false) {
338 // 'private' variable for instance
339 // The returned function will be able to reference this due to closure.
340 // Each call to the returned function will share this common timer.
343 // Calling debounce returns a new anonymous function
345 // reference the context and args for the setTimeout function
346 var args = arguments;
348 // Should the function be called now? If immediate is true
349 // and not already in a timeout then the answer is: Yes
350 var callNow = immediate && !timeout;
352 // This is the basic debounce behaviour where you can call this
353 // function several times, but it will only execute once
354 // [before or after imposing a delay].
355 // Each time the returned function is called, the timer starts over.
356 clearTimeout(timeout);
358 // Set the new timeout
359 timeout = setTimeout(function () {
360 // Inside the timeout function, clear the timeout variable
361 // which will let the next execution run when in 'immediate' mode
364 // Check if the function already ran with the immediate flag
366 // Call the original function with apply
367 // apply lets you define the 'this' object as well as the arguments
368 // (both captured before setTimeout)
369 func.apply(this, args);
373 // Immediate mode and no wait timer? Execute the function..
374 if (callNow) func.apply(this, args);
379 export function getLanguage(override?: string): string {
380 let localUserView = UserService.Instance.localUserView;
383 (localUserView?.local_user.lang
384 ? localUserView.local_user.lang
387 if (lang == "browser" && isBrowser()) {
388 return getBrowserLanguage();
394 export function getBrowserLanguage(): string {
395 // Intersect lemmy's langs, with the browser langs
396 let langs = languages ? languages.map(l => l.code) : ["en"];
397 let allowedLangs = navigator.languages.filter(v => langs.includes(v)) || [
400 return allowedLangs[0];
403 export function getMomentLanguage(): string {
404 let lang = getLanguage();
405 if (lang.startsWith("zh")) {
407 } else if (lang.startsWith("sv")) {
409 } else if (lang.startsWith("fr")) {
411 } else if (lang.startsWith("de")) {
413 } else if (lang.startsWith("ru")) {
415 } else if (lang.startsWith("es")) {
417 } else if (lang.startsWith("eo")) {
419 } else if (lang.startsWith("nl")) {
421 } else if (lang.startsWith("it")) {
423 } else if (lang.startsWith("fi")) {
425 } else if (lang.startsWith("ca")) {
427 } else if (lang.startsWith("fa")) {
429 } else if (lang.startsWith("pl")) {
431 } else if (lang.startsWith("pt")) {
433 } else if (lang.startsWith("ja")) {
435 } else if (lang.startsWith("ka")) {
437 } else if (lang.startsWith("hi")) {
439 } else if (lang.startsWith("el")) {
441 } else if (lang.startsWith("eu")) {
443 } else if (lang.startsWith("gl")) {
445 } else if (lang.startsWith("tr")) {
447 } else if (lang.startsWith("hu")) {
449 } else if (lang.startsWith("uk")) {
451 } else if (lang.startsWith("sq")) {
453 } else if (lang.startsWith("km")) {
455 } else if (lang.startsWith("ga")) {
457 } else if (lang.startsWith("sr")) {
459 } else if (lang.startsWith("ko")) {
461 } else if (lang.startsWith("da")) {
463 } else if (lang.startsWith("oc")) {
465 } else if (lang.startsWith("hr")) {
467 } else if (lang.startsWith("th")) {
469 } else if (lang.startsWith("bg")) {
471 } else if (lang.startsWith("id")) {
473 } else if (lang.startsWith("nb")) {
481 export function setTheme(theme: string, forceReload = false) {
485 if (theme === "browser" && !forceReload) {
488 // This is only run on a force reload
489 if (theme == "browser") {
493 // Unload all the other themes
494 for (var i = 0; i < themes.length; i++) {
495 let styleSheet = document.getElementById(themes[i]);
497 styleSheet.setAttribute("disabled", "disabled");
502 .getElementById("default-light")
503 ?.setAttribute("disabled", "disabled");
504 document.getElementById("default-dark")?.setAttribute("disabled", "disabled");
506 // Load the theme dynamically
507 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
508 loadCss(theme, cssLoc);
509 document.getElementById(theme).removeAttribute("disabled");
512 export function loadCss(id: string, loc: string) {
513 if (!document.getElementById(id)) {
514 var head = document.getElementsByTagName("head")[0];
515 var link = document.createElement("link");
517 link.rel = "stylesheet";
518 link.type = "text/css";
521 head.appendChild(link);
525 export function objectFlip(obj: any) {
527 Object.keys(obj).forEach(key => {
533 export function showAvatars(): boolean {
535 UserService.Instance.localUserView?.local_user.show_avatars ||
536 !UserService.Instance.localUserView
540 export function showScores(): boolean {
542 UserService.Instance.localUserView?.local_user.show_scores ||
543 !UserService.Instance.localUserView
547 export function isCakeDay(published: string): boolean {
548 // moment(undefined) or moment.utc(undefined) returns the current date/time
549 // moment(null) or moment.utc(null) returns null
550 const createDate = moment.utc(published || null).local();
551 const currentDate = moment(new Date());
554 createDate.date() === currentDate.date() &&
555 createDate.month() === currentDate.month() &&
556 createDate.year() !== currentDate.year()
560 export function toast(text: string, background = "success") {
562 let backgroundColor = `var(--${background})`;
565 backgroundColor: backgroundColor,
572 export function pictrsDeleteToast(
573 clickToDeleteText: string,
574 deletePictureText: string,
578 let backgroundColor = `var(--light)`;
579 let toast = Toastify({
580 text: clickToDeleteText,
581 backgroundColor: backgroundColor,
587 window.location.replace(deleteUrl);
588 alert(deletePictureText);
597 interface NotifyInfo {
604 export function messageToastify(info: NotifyInfo, router: any) {
606 let htmlBody = info.body ? md.render(info.body) : "";
607 let backgroundColor = `var(--light)`;
609 let toast = Toastify({
610 text: `${htmlBody}<br />${info.name}`,
611 avatar: info.icon ? info.icon : null,
612 backgroundColor: backgroundColor,
613 className: "text-dark",
622 router.history.push(info.link);
629 export function notifyPost(post_view: PostView, router: any) {
630 let info: NotifyInfo = {
631 name: post_view.community.name,
632 icon: post_view.community.icon,
633 link: `/post/${post_view.post.id}`,
634 body: post_view.post.name,
636 notify(info, router);
639 export function notifyComment(comment_view: CommentView, router: any) {
640 let info: NotifyInfo = {
641 name: comment_view.creator.name,
642 icon: comment_view.creator.avatar,
643 link: `/post/${comment_view.post.id}/comment/${comment_view.comment.id}`,
644 body: comment_view.comment.content,
646 notify(info, router);
649 export function notifyPrivateMessage(pmv: PrivateMessageView, router: any) {
650 let info: NotifyInfo = {
651 name: pmv.creator.name,
652 icon: pmv.creator.avatar,
654 body: pmv.private_message.content,
656 notify(info, router);
659 function notify(info: NotifyInfo, router: any) {
660 messageToastify(info, router);
662 if (Notification.permission !== "granted") Notification.requestPermission();
664 var notification = new Notification(info.name, {
669 notification.onclick = (ev: Event): any => {
671 router.history.push(info.link);
676 export function setupTribute() {
678 noMatchTemplate: function () {
685 menuItemTemplate: (item: any) => {
686 let shortName = `:${item.original.key}:`;
687 return `${item.original.val} ${shortName}`;
689 selectTemplate: (item: any) => {
690 return `${item.original.val}`;
692 values: Object.entries(emojiShortName).map(e => {
693 return { key: e[1], val: e[0] };
696 autocompleteMode: true,
698 // menuItemLimit: mentionDropdownFetchLimit,
699 menuShowMinLength: 2,
704 selectTemplate: (item: any) => {
705 let it: PersonTribute = item.original;
706 return `[${it.key}](${it.view.person.actor_id})`;
708 values: (text: string, cb: (persons: PersonTribute[]) => any) => {
709 personSearch(text, (persons: PersonTribute[]) => cb(persons));
712 autocompleteMode: true,
714 // menuItemLimit: mentionDropdownFetchLimit,
715 menuShowMinLength: 2,
721 selectTemplate: (item: any) => {
722 let it: CommunityTribute = item.original;
723 return `[${it.key}](${it.view.community.actor_id})`;
725 values: (text: string, cb: any) => {
726 communitySearch(text, (communities: CommunityTribute[]) =>
731 autocompleteMode: true,
733 // menuItemLimit: mentionDropdownFetchLimit,
734 menuShowMinLength: 2,
740 var tippyInstance: any;
742 tippyInstance = tippy("[data-tippy-content]");
745 export function setupTippy() {
747 tippyInstance.forEach((e: any) => e.destroy());
748 tippyInstance = tippy("[data-tippy-content]", {
750 // Display on "long press"
751 touch: ["hold", 500],
756 interface PersonTribute {
758 view: PersonViewSafe;
761 function personSearch(text: string, cb: (persons: PersonTribute[]) => any) {
765 type_: SearchType.Users,
766 sort: SortType.TopAll,
767 listing_type: ListingType.All,
769 limit: mentionDropdownFetchLimit,
770 auth: authField(false),
773 WebSocketService.Instance.send(wsClient.search(form));
775 let personSub = WebSocketService.Instance.subject.subscribe(
777 let res = wsJsonToRes(msg);
778 if (res.op == UserOperation.Search) {
779 let data = res.data as SearchResponse;
780 let persons: PersonTribute[] = data.users.map(pv => {
781 let tribute: PersonTribute = {
782 key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
788 personSub.unsubscribe();
791 err => console.error(err),
792 () => console.log("complete")
799 interface CommunityTribute {
804 function communitySearch(
806 cb: (communities: CommunityTribute[]) => any
811 type_: SearchType.Communities,
812 sort: SortType.TopAll,
813 listing_type: ListingType.All,
815 limit: mentionDropdownFetchLimit,
816 auth: authField(false),
819 WebSocketService.Instance.send(wsClient.search(form));
821 let communitySub = WebSocketService.Instance.subject.subscribe(
823 let res = wsJsonToRes(msg);
824 if (res.op == UserOperation.Search) {
825 let data = res.data as SearchResponse;
826 let communities: CommunityTribute[] = data.communities.map(cv => {
827 let tribute: CommunityTribute = {
828 key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
834 communitySub.unsubscribe();
837 err => console.error(err),
838 () => console.log("complete")
845 export function getListingTypeFromProps(props: any): ListingType {
846 return props.match.params.listing_type
847 ? routeListingTypeToEnum(props.match.params.listing_type)
848 : UserService.Instance.localUserView
849 ? Object.values(ListingType)[
850 UserService.Instance.localUserView.local_user.default_listing_type
855 // TODO might need to add a user setting for this too
856 export function getDataTypeFromProps(props: any): DataType {
857 return props.match.params.data_type
858 ? routeDataTypeToEnum(props.match.params.data_type)
862 export function getSortTypeFromProps(props: any): SortType {
863 return props.match.params.sort
864 ? routeSortTypeToEnum(props.match.params.sort)
865 : UserService.Instance.localUserView
866 ? Object.values(SortType)[
867 UserService.Instance.localUserView.local_user.default_sort_type
872 export function getPageFromProps(props: any): number {
873 return props.match.params.page ? Number(props.match.params.page) : 1;
876 export function getRecipientIdFromProps(props: any): number {
877 return props.match.params.recipient_id
878 ? Number(props.match.params.recipient_id)
882 export function getIdFromProps(props: any): number {
883 return Number(props.match.params.id);
886 export function getCommentIdFromProps(props: any): number {
887 return Number(props.match.params.comment_id);
890 export function getUsernameFromProps(props: any): string {
891 return props.match.params.username;
894 export function editCommentRes(data: CommentView, comments: CommentView[]) {
895 let found = comments.find(c => c.comment.id == data.comment.id);
897 found.comment.content = data.comment.content;
898 found.comment.updated = data.comment.updated;
899 found.comment.removed = data.comment.removed;
900 found.comment.deleted = data.comment.deleted;
901 found.counts.upvotes = data.counts.upvotes;
902 found.counts.downvotes = data.counts.downvotes;
903 found.counts.score = data.counts.score;
907 export function saveCommentRes(data: CommentView, comments: CommentView[]) {
908 let found = comments.find(c => c.comment.id == data.comment.id);
910 found.saved = data.saved;
914 export function createCommentLikeRes(
916 comments: CommentView[]
918 let found = comments.find(c => c.comment.id === data.comment.id);
920 found.counts.score = data.counts.score;
921 found.counts.upvotes = data.counts.upvotes;
922 found.counts.downvotes = data.counts.downvotes;
923 if (data.my_vote !== null) {
924 found.my_vote = data.my_vote;
929 export function createPostLikeFindRes(data: PostView, posts: PostView[]) {
930 let found = posts.find(p => p.post.id == data.post.id);
932 createPostLikeRes(data, found);
936 export function createPostLikeRes(data: PostView, post_view: PostView) {
938 post_view.counts.score = data.counts.score;
939 post_view.counts.upvotes = data.counts.upvotes;
940 post_view.counts.downvotes = data.counts.downvotes;
941 if (data.my_vote !== null) {
942 post_view.my_vote = data.my_vote;
947 export function editPostFindRes(data: PostView, posts: PostView[]) {
948 let found = posts.find(p => p.post.id == data.post.id);
950 editPostRes(data, found);
954 export function editPostRes(data: PostView, post: PostView) {
956 post.post.url = data.post.url;
957 post.post.name = data.post.name;
958 post.post.nsfw = data.post.nsfw;
959 post.post.deleted = data.post.deleted;
960 post.post.removed = data.post.removed;
961 post.post.stickied = data.post.stickied;
962 post.post.body = data.post.body;
963 post.post.locked = data.post.locked;
964 post.saved = data.saved;
968 export function commentsToFlatNodes(comments: CommentView[]): CommentNodeI[] {
969 let nodes: CommentNodeI[] = [];
970 for (let comment of comments) {
971 nodes.push({ comment_view: comment });
976 function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
977 // First, put removed and deleted comments at the bottom, then do your other sorts
978 if (sort == CommentSortType.Top) {
981 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
982 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
983 b.comment_view.counts.score - a.comment_view.counts.score
985 } else if (sort == CommentSortType.New) {
988 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
989 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
990 b.comment_view.comment.published.localeCompare(
991 a.comment_view.comment.published
994 } else if (sort == CommentSortType.Old) {
997 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
998 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
999 a.comment_view.comment.published.localeCompare(
1000 b.comment_view.comment.published
1003 } else if (sort == CommentSortType.Hot) {
1006 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
1007 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
1008 hotRankComment(b.comment_view) - hotRankComment(a.comment_view)
1012 // Go through the children recursively
1013 for (let node of tree) {
1014 if (node.children) {
1015 commentSort(node.children, sort);
1020 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
1021 commentSort(tree, convertCommentSortType(sort));
1024 function convertCommentSortType(sort: SortType): CommentSortType {
1026 sort == SortType.TopAll ||
1027 sort == SortType.TopDay ||
1028 sort == SortType.TopWeek ||
1029 sort == SortType.TopMonth ||
1030 sort == SortType.TopYear
1032 return CommentSortType.Top;
1033 } else if (sort == SortType.New) {
1034 return CommentSortType.New;
1035 } else if (sort == SortType.Hot || sort == SortType.Active) {
1036 return CommentSortType.Hot;
1038 return CommentSortType.Hot;
1042 export function buildCommentsTree(
1043 comments: CommentView[],
1044 commentSortType: CommentSortType
1046 let map = new Map<number, CommentNodeI>();
1047 for (let comment_view of comments) {
1048 let node: CommentNodeI = {
1049 comment_view: comment_view,
1052 map.set(comment_view.comment.id, { ...node });
1054 let tree: CommentNodeI[] = [];
1055 for (let comment_view of comments) {
1056 let child = map.get(comment_view.comment.id);
1057 if (comment_view.comment.parent_id) {
1058 let parent_ = map.get(comment_view.comment.parent_id);
1059 parent_.children.push(child);
1067 commentSort(tree, commentSortType);
1072 function setDepth(node: CommentNodeI, i = 0) {
1073 for (let child of node.children) {
1075 setDepth(child, i + 1);
1079 export function insertCommentIntoTree(tree: CommentNodeI[], cv: CommentView) {
1080 // Building a fake node to be used for later
1081 let node: CommentNodeI = {
1087 if (cv.comment.parent_id) {
1088 let parentComment = searchCommentTree(tree, cv.comment.parent_id);
1089 if (parentComment) {
1090 node.depth = parentComment.depth + 1;
1091 parentComment.children.unshift(node);
1098 export function searchCommentTree(
1099 tree: CommentNodeI[],
1102 for (let node of tree) {
1103 if (node.comment_view.comment.id === id) {
1107 for (const child of node.children) {
1108 const res = searchCommentTree([child], id);
1118 export const colorList: string[] = [
1127 function hsl(num: number) {
1128 return `hsla(${num}, 35%, 50%, 1)`;
1131 export function previewLines(
1140 // Use lines * 2 because markdown requires 2 lines
1141 .slice(0, maxLines * 2)
1146 export function hostname(url: string): string {
1147 let cUrl = new URL(url);
1148 return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1151 export function validTitle(title?: string): boolean {
1152 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1153 if (title === null || title.length < 3) return true;
1155 const regex = new RegExp(/.*\S.*/, "g");
1157 return regex.test(title);
1160 export function siteBannerCss(banner: string): string {
1162 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1163 background-attachment: fixed; \
1164 background-position: top; \
1165 background-repeat: no-repeat; \
1166 background-size: 100% cover; \
1169 max-height: 100vh; \
1173 export function isBrowser() {
1174 return typeof window !== "undefined";
1177 export function setIsoData(context: any): IsoData {
1178 let isoData: IsoData = isBrowser()
1180 : context.router.staticContext;
1184 export function wsSubscribe(parseMessage: any): Subscription {
1186 return WebSocketService.Instance.subject
1187 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1189 msg => parseMessage(msg),
1190 err => console.error(err),
1191 () => console.log("complete")
1198 export function setOptionalAuth(obj: any, auth = UserService.Instance.auth) {
1204 export function authField(
1206 auth = UserService.Instance.auth
1208 if (auth == null && throwErr) {
1209 toast(i18n.t("not_logged_in"), "danger");
1210 throw "Not logged in";
1216 moment.updateLocale("en", {
1237 export function saveScrollPosition(context: any) {
1238 let path: string = context.router.route.location.pathname;
1239 let y = window.scrollY;
1240 sessionStorage.setItem(`scrollPosition_${path}`, y.toString());
1243 export function restoreScrollPosition(context: any) {
1244 let path: string = context.router.route.location.pathname;
1245 let y = Number(sessionStorage.getItem(`scrollPosition_${path}`));
1246 window.scrollTo(0, y);
1249 export function showLocal(isoData: IsoData): boolean {
1250 return isoData.site_res.federated_instances?.linked.length > 0;
1253 interface ChoicesValue {
1258 export function communityToChoice(cv: CommunityView): ChoicesValue {
1259 let choice: ChoicesValue = {
1260 value: cv.community.id.toString(),
1261 label: communitySelectName(cv),
1266 export function personToChoice(pvs: PersonViewSafe): ChoicesValue {
1267 let choice: ChoicesValue = {
1268 value: pvs.person.id.toString(),
1269 label: personSelectName(pvs),
1274 export async function fetchCommunities(q: string) {
1275 let form: Search = {
1277 type_: SearchType.Communities,
1278 sort: SortType.TopAll,
1279 listing_type: ListingType.All,
1282 auth: authField(false),
1284 let client = new LemmyHttp(httpBase);
1285 return client.search(form);
1288 export async function fetchUsers(q: string) {
1289 let form: Search = {
1291 type_: SearchType.Users,
1292 sort: SortType.TopAll,
1293 listing_type: ListingType.All,
1296 auth: authField(false),
1298 let client = new LemmyHttp(httpBase);
1299 return client.search(form);
1302 export const choicesConfig = {
1304 searchResultLimit: fetchLimit,
1306 containerOuter: "choices",
1307 containerInner: "choices__inner bg-light border-0",
1308 input: "form-control",
1309 inputCloned: "choices__input--cloned",
1310 list: "choices__list",
1311 listItems: "choices__list--multiple",
1312 listSingle: "choices__list--single",
1313 listDropdown: "choices__list--dropdown",
1314 item: "choices__item bg-light",
1315 itemSelectable: "choices__item--selectable",
1316 itemDisabled: "choices__item--disabled",
1317 itemChoice: "choices__item--choice",
1318 placeholder: "choices__placeholder",
1319 group: "choices__group",
1320 groupHeading: "choices__heading",
1321 button: "choices__button",
1322 activeState: "is-active",
1323 focusState: "is-focused",
1324 openState: "is-open",
1325 disabledState: "is-disabled",
1326 highlightedState: "text-info",
1327 selectedState: "text-info",
1328 flippedState: "is-flipped",
1329 loadingState: "is-loading",
1330 noResults: "has-no-results",
1331 noChoices: "has-no-choices",
1335 export function communitySelectName(cv: CommunityView): string {
1336 return cv.community.local
1338 : `${hostname(cv.community.actor_id)}/${cv.community.name}`;
1341 export function personSelectName(pvs: PersonViewSafe): string {
1342 return pvs.person.local
1344 : `${hostname(pvs.person.actor_id)}/${pvs.person.name}`;