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';
40 WebSocketJsonResponse,
48 } from 'lemmy-js-client';
54 CommentNode as CommentNodeI,
55 } from './interfaces';
56 import { UserService, WebSocketService } from './services';
59 Tribute = require('tributejs');
61 import markdown_it from 'markdown-it';
62 import markdown_it_sub from 'markdown-it-sub';
63 import markdown_it_sup from 'markdown-it-sup';
64 import markdown_it_container from 'markdown-it-container';
65 import emojiShortName from 'emoji-short-name';
66 import Toastify from 'toastify-js';
67 import tippy from 'tippy.js';
68 import moment from 'moment';
69 import { Subscription } from 'rxjs';
70 import { retryWhen, delay, take } from 'rxjs/operators';
71 import { i18n } from './i18next';
73 export const wsClient = new LemmyWebsocket();
75 export const favIconUrl = '/static/assets/favicon.svg';
76 export const favIconPngUrl = '/static/assets/apple-touch-icon.png';
78 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
79 export const repoUrl = 'https://github.com/LemmyNet';
80 export const joinLemmyUrl = 'https://join.lemmy.ml';
81 export const supportLemmyUrl = 'https://join.lemmy.ml/sponsors';
82 export const docsUrl = 'https://join.lemmy.ml/docs/en/index.html';
83 export const helpGuideUrl = 'https://join.lemmy.ml/docs/en/about/guide.html'; // TODO find a way to redirect to the non-en folder
84 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
85 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
86 export const archiveUrl = 'https://archive.is';
87 export const elementUrl = 'https://element.io/';
89 export const postRefetchSeconds: number = 60 * 1000;
90 export const fetchLimit: number = 20;
91 export const mentionDropdownFetchLimit = 10;
93 export const languages = [
94 { code: 'ca', name: 'Català' },
95 { code: 'en', name: 'English' },
96 { code: 'el', name: 'Ελληνικά' },
97 { code: 'eu', name: 'Euskara' },
98 { code: 'eo', name: 'Esperanto' },
99 { code: 'es', name: 'Español' },
100 { code: 'da', name: 'Dansk' },
101 { code: 'de', name: 'Deutsch' },
102 { code: 'ga', name: 'Gaeilge' },
103 { code: 'gl', name: 'Galego' },
104 { code: 'hr', name: 'hrvatski' },
105 { code: 'hu', name: 'Magyar Nyelv' },
106 { code: 'ka', name: 'ქართული ენა' },
107 { code: 'ko', name: '한국어' },
108 { code: 'km', name: 'ភាសាខ្មែរ' },
109 { code: 'hi', name: 'मानक हिन्दी' },
110 { code: 'fa', name: 'فارسی' },
111 { code: 'ja', name: '日本語' },
112 { code: 'oc', name: 'Occitan' },
113 { code: 'pl', name: 'Polski' },
114 { code: 'pt_BR', name: 'Português Brasileiro' },
115 { code: 'zh', name: '中文' },
116 { code: 'fi', name: 'Suomi' },
117 { code: 'fr', name: 'Français' },
118 { code: 'sv', name: 'Svenska' },
119 { code: 'sq', name: 'Shqip' },
120 { code: 'sr_Latn', name: 'srpski' },
121 { code: 'tr', name: 'Türkçe' },
122 { code: 'uk', name: 'Українська Mова' },
123 { code: 'ru', name: 'Русский' },
124 { code: 'nl', name: 'Nederlands' },
125 { code: 'it', name: 'Italiano' },
128 export const themes = [
144 const DEFAULT_ALPHABET =
145 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
147 function getRandomCharFromAlphabet(alphabet: string): string {
148 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
151 export function randomStr(
152 idDesiredLength: number = 20,
153 alphabet = DEFAULT_ALPHABET
156 * Create n-long array and map it to random chars from given alphabet.
157 * Then join individual chars as string
159 return Array.from({ length: idDesiredLength })
161 return getRandomCharFromAlphabet(alphabet);
166 export function wsJsonToRes<ResponseType>(
167 msg: WebSocketJsonResponse<ResponseType>
168 ): WebSocketResponse<ResponseType> {
175 export function wsUserOp(msg: any): UserOperation {
176 let opStr: string = msg.op;
177 return UserOperation[opStr];
180 export const md = new markdown_it({
185 .use(markdown_it_sub)
186 .use(markdown_it_sup)
187 .use(markdown_it_container, 'spoiler', {
188 validate: function (params: any) {
189 return params.trim().match(/^spoiler\s+(.*)$/);
192 render: function (tokens: any, idx: any) {
193 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
195 if (tokens[idx].nesting === 1) {
197 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
200 return '</details>\n';
205 export function hotRankComment(comment_view: CommentView): number {
206 return hotRank(comment_view.counts.score, comment_view.comment.published);
209 export function hotRankActivePost(post_view: PostView): number {
210 return hotRank(post_view.counts.score, post_view.counts.newest_comment_time);
213 export function hotRankPost(post_view: PostView): number {
214 return hotRank(post_view.counts.score, post_view.post.published);
217 export function hotRank(score: number, timeStr: string): number {
218 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
219 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
220 let now: Date = new Date();
221 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
224 (10000 * Math.log10(Math.max(1, 3 + score))) /
225 Math.pow(hoursElapsed + 2, 1.8);
227 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
232 export function mdToHtml(text: string) {
233 return { __html: md.render(text) };
236 export function getUnixTime(text: string): number {
237 return text ? new Date(text).getTime() / 1000 : undefined;
240 export function canMod(
241 user: UserSafeSettings,
244 onSelf: boolean = false
246 // You can do moderator actions only on the mods added after you.
248 let yourIndex = modIds.findIndex(id => id == user.id);
249 if (yourIndex == -1) {
252 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
253 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
254 return !modIds.includes(creator_id);
261 export function isMod(modIds: number[], creator_id: number): boolean {
262 return modIds.includes(creator_id);
265 const imageRegex = new RegExp(
266 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
268 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
270 export function isImage(url: string) {
271 return imageRegex.test(url);
274 export function isVideo(url: string) {
275 return videoRegex.test(url);
278 export function validURL(str: string) {
279 return !!new URL(str);
282 export function communityRSSUrl(actorId: string, sort: string): string {
283 let url = new URL(actorId);
284 return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
287 export function validEmail(email: string) {
288 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
289 return re.test(String(email).toLowerCase());
292 export function capitalizeFirstLetter(str: string): string {
293 return str.charAt(0).toUpperCase() + str.slice(1);
296 export function routeSortTypeToEnum(sort: string): SortType {
297 return SortType[sort];
300 export function listingTypeFromNum(type_: number): ListingType {
301 return Object.values(ListingType)[type_];
304 export function sortTypeFromNum(type_: number): SortType {
305 return Object.values(SortType)[type_];
308 export function routeListingTypeToEnum(type: string): ListingType {
309 return ListingType[type];
312 export function routeDataTypeToEnum(type: string): DataType {
313 return DataType[capitalizeFirstLetter(type)];
316 export function routeSearchTypeToEnum(type: string): SearchType {
317 return SearchType[type];
320 export async function getPageTitle(url: string) {
321 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
322 let title = await res.title;
326 export function debounce(
329 immediate: boolean = false
331 // 'private' variable for instance
332 // The returned function will be able to reference this due to closure.
333 // Each call to the returned function will share this common timer.
336 // Calling debounce returns a new anonymous function
338 // reference the context and args for the setTimeout function
342 // Should the function be called now? If immediate is true
343 // and not already in a timeout then the answer is: Yes
344 var callNow = immediate && !timeout;
346 // This is the basic debounce behaviour where you can call this
347 // function several times, but it will only execute once
348 // [before or after imposing a delay].
349 // Each time the returned function is called, the timer starts over.
350 clearTimeout(timeout);
352 // Set the new timeout
353 timeout = setTimeout(function () {
354 // Inside the timeout function, clear the timeout variable
355 // which will let the next execution run when in 'immediate' mode
358 // Check if the function already ran with the immediate flag
360 // Call the original function with apply
361 // apply lets you define the 'this' object as well as the arguments
362 // (both captured before setTimeout)
363 func.apply(context, args);
367 // Immediate mode and no wait timer? Execute the function..
368 if (callNow) func.apply(context, args);
373 export function getLanguage(override?: string): string {
374 let user = UserService.Instance.user;
375 let lang = override || (user && user.lang ? user.lang : 'browser');
377 if (lang == 'browser' && isBrowser()) {
378 return getBrowserLanguage();
385 export function getBrowserLanguage(): string {
386 return navigator.language;
389 export function getMomentLanguage(): string {
390 let lang = getLanguage();
391 if (lang.startsWith('zh')) {
393 } else if (lang.startsWith('sv')) {
395 } else if (lang.startsWith('fr')) {
397 } else if (lang.startsWith('de')) {
399 } else if (lang.startsWith('ru')) {
401 } else if (lang.startsWith('es')) {
403 } else if (lang.startsWith('eo')) {
405 } else if (lang.startsWith('nl')) {
407 } else if (lang.startsWith('it')) {
409 } else if (lang.startsWith('fi')) {
411 } else if (lang.startsWith('ca')) {
413 } else if (lang.startsWith('fa')) {
415 } else if (lang.startsWith('pl')) {
417 } else if (lang.startsWith('pt')) {
419 } else if (lang.startsWith('ja')) {
421 } else if (lang.startsWith('ka')) {
423 } else if (lang.startsWith('hi')) {
425 } else if (lang.startsWith('el')) {
427 } else if (lang.startsWith('eu')) {
429 } else if (lang.startsWith('gl')) {
431 } else if (lang.startsWith('tr')) {
433 } else if (lang.startsWith('hu')) {
435 } else if (lang.startsWith('uk')) {
437 } else if (lang.startsWith('sq')) {
439 } else if (lang.startsWith('km')) {
441 } else if (lang.startsWith('ga')) {
443 } else if (lang.startsWith('sr')) {
445 } else if (lang.startsWith('ko')) {
447 } else if (lang.startsWith('da')) {
449 } else if (lang.startsWith('oc')) {
451 } else if (lang.startsWith('hr')) {
459 export function setTheme(theme: string, forceReload: boolean = false) {
463 if (theme === 'browser' && !forceReload) {
466 // This is only run on a force reload
467 if (theme == 'browser') {
471 // Unload all the other themes
472 for (var i = 0; i < themes.length; i++) {
473 let styleSheet = document.getElementById(themes[i]);
475 styleSheet.setAttribute('disabled', 'disabled');
480 .getElementById('default-light')
481 ?.setAttribute('disabled', 'disabled');
482 document.getElementById('default-dark')?.setAttribute('disabled', 'disabled');
484 // Load the theme dynamically
485 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
486 loadCss(theme, cssLoc);
487 document.getElementById(theme).removeAttribute('disabled');
490 export function loadCss(id: string, loc: string) {
491 if (!document.getElementById(id)) {
492 var head = document.getElementsByTagName('head')[0];
493 var link = document.createElement('link');
495 link.rel = 'stylesheet';
496 link.type = 'text/css';
499 head.appendChild(link);
503 export function objectFlip(obj: any) {
505 Object.keys(obj).forEach(key => {
511 export function showAvatars(): boolean {
513 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
514 !UserService.Instance.user
518 export function isCakeDay(published: string): boolean {
519 // moment(undefined) or moment.utc(undefined) returns the current date/time
520 // moment(null) or moment.utc(null) returns null
521 const userCreationDate = moment.utc(published || null).local();
522 const currentDate = moment(new Date());
525 userCreationDate.date() === currentDate.date() &&
526 userCreationDate.month() === currentDate.month() &&
527 userCreationDate.year() !== currentDate.year()
531 export function toast(text: string, background: string = 'success') {
533 let backgroundColor = `var(--${background})`;
536 backgroundColor: backgroundColor,
543 export function pictrsDeleteToast(
544 clickToDeleteText: string,
545 deletePictureText: string,
549 let backgroundColor = `var(--light)`;
550 let toast = Toastify({
551 text: clickToDeleteText,
552 backgroundColor: backgroundColor,
558 window.location.replace(deleteUrl);
559 alert(deletePictureText);
568 interface NotifyInfo {
575 export function messageToastify(info: NotifyInfo, router: any) {
577 let htmlBody = info.body ? md.render(info.body) : '';
578 let backgroundColor = `var(--light)`;
580 let toast = Toastify({
581 text: `${htmlBody}<br />${info.name}`,
582 avatar: info.icon ? info.icon : null,
583 backgroundColor: backgroundColor,
584 className: 'text-dark',
592 router.history.push(info.link);
599 export function notifyPost(post_view: PostView, router: any) {
600 let info: NotifyInfo = {
601 name: post_view.community.name,
602 icon: post_view.community.icon,
603 link: `/post/${post_view.post.id}`,
604 body: post_view.post.name,
606 notify(info, router);
609 export function notifyComment(comment_view: CommentView, router: any) {
610 let info: NotifyInfo = {
611 name: comment_view.creator.name,
612 icon: comment_view.creator.avatar,
613 link: `/post/${comment_view.post.id}/comment/${comment_view.comment.id}`,
614 body: comment_view.comment.content,
616 notify(info, router);
619 export function notifyPrivateMessage(pmv: PrivateMessageView, router: any) {
620 let info: NotifyInfo = {
621 name: pmv.creator.name,
622 icon: pmv.creator.avatar,
624 body: pmv.private_message.content,
626 notify(info, router);
629 function notify(info: NotifyInfo, router: any) {
630 messageToastify(info, router);
632 if (Notification.permission !== 'granted') Notification.requestPermission();
634 var notification = new Notification(info.name, {
639 notification.onclick = (ev: Event): any => {
641 router.history.push(info.link);
646 export function setupTribute() {
648 noMatchTemplate: function () {
655 menuItemTemplate: (item: any) => {
656 let shortName = `:${item.original.key}:`;
657 return `${item.original.val} ${shortName}`;
659 selectTemplate: (item: any) => {
660 return `${item.original.val}`;
662 values: Object.entries(emojiShortName).map(e => {
663 return { key: e[1], val: e[0] };
666 autocompleteMode: true,
668 // menuItemLimit: mentionDropdownFetchLimit,
669 menuShowMinLength: 2,
674 selectTemplate: (item: any) => {
675 let it: UserTribute = item.original;
676 return `[${it.key}](${it.view.user.actor_id})`;
678 values: (text: string, cb: (users: UserTribute[]) => any) => {
679 userSearch(text, (users: UserTribute[]) => cb(users));
682 autocompleteMode: true,
684 // menuItemLimit: mentionDropdownFetchLimit,
685 menuShowMinLength: 2,
691 selectTemplate: (item: any) => {
692 let it: CommunityTribute = item.original;
693 return `[${it.key}](${it.view.community.actor_id})`;
695 values: (text: string, cb: any) => {
696 communitySearch(text, (communities: CommunityTribute[]) =>
701 autocompleteMode: true,
703 // menuItemLimit: mentionDropdownFetchLimit,
704 menuShowMinLength: 2,
710 var tippyInstance: any;
712 tippyInstance = tippy('[data-tippy-content]');
715 export function setupTippy() {
717 tippyInstance.forEach((e: any) => e.destroy());
718 tippyInstance = tippy('[data-tippy-content]', {
720 // Display on "long press"
721 touch: ['hold', 500],
726 interface UserTribute {
731 function userSearch(text: string, cb: (users: UserTribute[]) => any) {
735 type_: SearchType.Users,
736 sort: SortType.TopAll,
738 limit: mentionDropdownFetchLimit,
739 auth: authField(false),
742 WebSocketService.Instance.send(wsClient.search(form));
744 let userSub = WebSocketService.Instance.subject.subscribe(
746 let res = wsJsonToRes(msg);
747 if (res.op == UserOperation.Search) {
748 let data = res.data as SearchResponse;
749 let users: UserTribute[] = data.users.map(uv => {
750 let tribute: UserTribute = {
751 key: `@${uv.user.name}@${hostname(uv.user.actor_id)}`,
757 userSub.unsubscribe();
760 err => console.error(err),
761 () => console.log('complete')
768 interface CommunityTribute {
773 function communitySearch(
775 cb: (communities: CommunityTribute[]) => any
780 type_: SearchType.Communities,
781 sort: SortType.TopAll,
783 limit: mentionDropdownFetchLimit,
784 auth: authField(false),
787 WebSocketService.Instance.send(wsClient.search(form));
789 let communitySub = WebSocketService.Instance.subject.subscribe(
791 let res = wsJsonToRes(msg);
792 if (res.op == UserOperation.Search) {
793 let data = res.data as SearchResponse;
794 let communities: CommunityTribute[] = data.communities.map(cv => {
795 let tribute: CommunityTribute = {
796 key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
802 communitySub.unsubscribe();
805 err => console.error(err),
806 () => console.log('complete')
813 export function getListingTypeFromProps(props: any): ListingType {
814 return props.match.params.listing_type
815 ? routeListingTypeToEnum(props.match.params.listing_type)
816 : UserService.Instance.user
817 ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
821 // TODO might need to add a user setting for this too
822 export function getDataTypeFromProps(props: any): DataType {
823 return props.match.params.data_type
824 ? routeDataTypeToEnum(props.match.params.data_type)
828 export function getSortTypeFromProps(props: any): SortType {
829 return props.match.params.sort
830 ? routeSortTypeToEnum(props.match.params.sort)
831 : UserService.Instance.user
832 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
836 export function getPageFromProps(props: any): number {
837 return props.match.params.page ? Number(props.match.params.page) : 1;
840 export function getRecipientIdFromProps(props: any): number {
841 return props.match.params.recipient_id
842 ? Number(props.match.params.recipient_id)
846 export function getIdFromProps(props: any): number {
847 return Number(props.match.params.id);
850 export function getCommentIdFromProps(props: any): number {
851 return Number(props.match.params.comment_id);
854 export function getUsernameFromProps(props: any): string {
855 return props.match.params.username;
858 export function editCommentRes(data: CommentView, comments: CommentView[]) {
859 let found = comments.find(c => c.comment.id == data.comment.id);
861 found.comment.content = data.comment.content;
862 found.comment.updated = data.comment.updated;
863 found.comment.removed = data.comment.removed;
864 found.comment.deleted = data.comment.deleted;
865 found.counts.upvotes = data.counts.upvotes;
866 found.counts.downvotes = data.counts.downvotes;
867 found.counts.score = data.counts.score;
871 export function saveCommentRes(data: CommentView, comments: CommentView[]) {
872 let found = comments.find(c => c.comment.id == data.comment.id);
874 found.saved = data.saved;
878 export function createCommentLikeRes(
880 comments: CommentView[]
882 let found = comments.find(c => c.comment.id === data.comment.id);
884 found.counts.score = data.counts.score;
885 found.counts.upvotes = data.counts.upvotes;
886 found.counts.downvotes = data.counts.downvotes;
887 if (data.my_vote !== null) {
888 found.my_vote = data.my_vote;
893 export function createPostLikeFindRes(data: PostView, posts: PostView[]) {
894 let found = posts.find(p => p.post.id == data.post.id);
896 createPostLikeRes(data, found);
900 export function createPostLikeRes(data: PostView, post_view: PostView) {
902 post_view.counts.score = data.counts.score;
903 post_view.counts.upvotes = data.counts.upvotes;
904 post_view.counts.downvotes = data.counts.downvotes;
905 if (data.my_vote !== null) {
906 post_view.my_vote = data.my_vote;
911 export function editPostFindRes(data: PostView, posts: PostView[]) {
912 let found = posts.find(p => p.post.id == data.post.id);
914 editPostRes(data, found);
918 export function editPostRes(data: PostView, post: PostView) {
920 post.post.url = data.post.url;
921 post.post.name = data.post.name;
922 post.post.nsfw = data.post.nsfw;
923 post.post.deleted = data.post.deleted;
924 post.post.removed = data.post.removed;
925 post.post.stickied = data.post.stickied;
926 post.post.body = data.post.body;
927 post.post.locked = data.post.locked;
928 post.saved = data.saved;
932 export function commentsToFlatNodes(comments: CommentView[]): CommentNodeI[] {
933 let nodes: CommentNodeI[] = [];
934 for (let comment of comments) {
935 nodes.push({ comment_view: comment });
940 function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
941 // First, put removed and deleted comments at the bottom, then do your other sorts
942 if (sort == CommentSortType.Top) {
945 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
946 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
947 b.comment_view.counts.score - a.comment_view.counts.score
949 } else if (sort == CommentSortType.New) {
952 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
953 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
954 b.comment_view.comment.published.localeCompare(
955 a.comment_view.comment.published
958 } else if (sort == CommentSortType.Old) {
961 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
962 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
963 a.comment_view.comment.published.localeCompare(
964 b.comment_view.comment.published
967 } else if (sort == CommentSortType.Hot) {
970 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
971 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
972 hotRankComment(b.comment_view) - hotRankComment(a.comment_view)
976 // Go through the children recursively
977 for (let node of tree) {
979 commentSort(node.children, sort);
984 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
985 commentSort(tree, convertCommentSortType(sort));
988 function convertCommentSortType(sort: SortType): CommentSortType {
990 sort == SortType.TopAll ||
991 sort == SortType.TopDay ||
992 sort == SortType.TopWeek ||
993 sort == SortType.TopMonth ||
994 sort == SortType.TopYear
996 return CommentSortType.Top;
997 } else if (sort == SortType.New) {
998 return CommentSortType.New;
999 } else if (sort == SortType.Hot || sort == SortType.Active) {
1000 return CommentSortType.Hot;
1002 return CommentSortType.Hot;
1006 export function buildCommentsTree(
1007 comments: CommentView[],
1008 commentSortType: CommentSortType
1010 let map = new Map<number, CommentNodeI>();
1011 for (let comment_view of comments) {
1012 let node: CommentNodeI = {
1013 comment_view: comment_view,
1016 map.set(comment_view.comment.id, { ...node });
1018 let tree: CommentNodeI[] = [];
1019 for (let comment_view of comments) {
1020 let child = map.get(comment_view.comment.id);
1021 if (comment_view.comment.parent_id) {
1022 let parent_ = map.get(comment_view.comment.parent_id);
1023 parent_.children.push(child);
1031 commentSort(tree, commentSortType);
1036 function setDepth(node: CommentNodeI, i: number = 0) {
1037 for (let child of node.children) {
1039 setDepth(child, i + 1);
1043 export function insertCommentIntoTree(tree: CommentNodeI[], cv: CommentView) {
1044 // Building a fake node to be used for later
1045 let node: CommentNodeI = {
1051 if (cv.comment.parent_id) {
1052 let parentComment = searchCommentTree(tree, cv.comment.parent_id);
1053 if (parentComment) {
1054 node.depth = parentComment.depth + 1;
1055 parentComment.children.unshift(node);
1062 export function searchCommentTree(
1063 tree: CommentNodeI[],
1066 for (let node of tree) {
1067 if (node.comment_view.comment.id === id) {
1071 for (const child of node.children) {
1072 const res = searchCommentTree([child], id);
1082 export const colorList: string[] = [
1091 function hsl(num: number) {
1092 return `hsla(${num}, 35%, 50%, 1)`;
1095 export function previewLines(
1097 maxChars: number = 300,
1098 maxLines: number = 1
1104 // Use lines * 2 because markdown requires 2 lines
1105 .slice(0, maxLines * 2)
1110 export function hostname(url: string): string {
1111 let cUrl = new URL(url);
1112 return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1115 export function validTitle(title?: string): boolean {
1116 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1117 if (title === null || title.length < 3) return true;
1119 const regex = new RegExp(/.*\S.*/, 'g');
1121 return regex.test(title);
1124 export function siteBannerCss(banner: string): string {
1126 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1127 background-attachment: fixed; \
1128 background-position: top; \
1129 background-repeat: no-repeat; \
1130 background-size: 100% cover; \
1133 max-height: 100vh; \
1137 export function isBrowser() {
1138 return typeof window !== 'undefined';
1141 export function setIsoData(context: any): IsoData {
1142 let isoData: IsoData = isBrowser()
1144 : context.router.staticContext;
1148 export function wsSubscribe(parseMessage: any): Subscription {
1150 return WebSocketService.Instance.subject
1151 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1153 msg => parseMessage(msg),
1154 err => console.error(err),
1155 () => console.log('complete')
1162 export function setOptionalAuth(obj: any, auth = UserService.Instance.auth) {
1168 export function authField(
1169 throwErr: boolean = true,
1170 auth = UserService.Instance.auth
1172 if (auth == null && throwErr) {
1173 toast(i18n.t('not_logged_in'), 'danger');
1174 throw 'Not logged in';
1180 moment.updateLocale('en', {
1201 export function saveScrollPosition(context: any) {
1202 let path: string = context.router.route.location.pathname;
1203 let y = window.scrollY;
1204 sessionStorage.setItem(`scrollPosition_${path}`, y.toString());
1207 export function restoreScrollPosition(context: any) {
1208 let path: string = context.router.route.location.pathname;
1209 let y = Number(sessionStorage.getItem(`scrollPosition_${path}`));
1210 window.scrollTo(0, y);