]> Untitled Git - lemmy-ui.git/blob - src/shared/utils.ts
Adding the support / donate page to the navbar. Fixes #86
[lemmy-ui.git] / src / shared / utils.ts
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
30 import {
31   UserOperation,
32   Comment,
33   CommentNode as CommentNodeI,
34   Post,
35   PrivateMessage,
36   User,
37   SortType,
38   ListingType,
39   SearchType,
40   WebSocketResponse,
41   WebSocketJsonResponse,
42   SearchForm,
43   SearchResponse,
44   CommentResponse,
45   PostResponse,
46 } from 'lemmy-js-client';
47
48 import { CommentSortType, DataType, IsoData } from './interfaces';
49 import { UserService, WebSocketService } from './services';
50
51 var Tribute;
52 if (isBrowser()) {
53   Tribute = require('tributejs');
54 }
55 import markdown_it from 'markdown-it';
56 import markdown_it_sub from 'markdown-it-sub';
57 import markdown_it_sup from 'markdown-it-sup';
58 import markdownitEmoji from 'markdown-it-emoji/light';
59 import markdown_it_container from 'markdown-it-container';
60 import emojiShortName from 'emoji-short-name';
61 import Toastify from 'toastify-js';
62 import tippy from 'tippy.js';
63 import moment from 'moment';
64 import { Subscription } from 'rxjs';
65 import { retryWhen, delay, take } from 'rxjs/operators';
66
67 export const favIconUrl = '/static/assets/favicon.svg';
68 export const favIconPngUrl = '/static/assets/apple-touch-icon.png';
69 // TODO
70 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
71 export const defaultFavIcon = 'test';
72 export const repoUrl = 'https://github.com/LemmyNet';
73 export const joinLemmyUrl = 'https://join.lemmy.ml';
74 export const supportLemmyUrl = 'https://join.lemmy.ml/sponsors';
75 export const helpGuideUrl = '/docs/about_guide.html';
76 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
77 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
78 export const archiveUrl = 'https://archive.is';
79 export const elementUrl = 'https://element.io/';
80
81 export const postRefetchSeconds: number = 60 * 1000;
82 export const fetchLimit: number = 20;
83 export const mentionDropdownFetchLimit = 10;
84
85 export const languages = [
86   { code: 'ca', name: 'Català' },
87   { code: 'en', name: 'English' },
88   { code: 'el', name: 'Ελληνικά' },
89   { code: 'eu', name: 'Euskara' },
90   { code: 'eo', name: 'Esperanto' },
91   { code: 'es', name: 'Español' },
92   { code: 'de', name: 'Deutsch' },
93   { code: 'ga', name: 'Gaeilge' },
94   { code: 'gl', name: 'Galego' },
95   { code: 'hu', name: 'Magyar Nyelv' },
96   { code: 'ka', name: 'ქართული ენა' },
97   { code: 'ko', name: '한국어' },
98   { code: 'km', name: 'ភាសាខ្មែរ' },
99   { code: 'hi', name: 'मानक हिन्दी' },
100   { code: 'fa', name: 'فارسی' },
101   { code: 'ja', name: '日本語' },
102   { code: 'pl', name: 'Polski' },
103   { code: 'pt_BR', name: 'Português Brasileiro' },
104   { code: 'zh', name: '中文' },
105   { code: 'fi', name: 'Suomi' },
106   { code: 'fr', name: 'Français' },
107   { code: 'sv', name: 'Svenska' },
108   { code: 'sq', name: 'Shqip' },
109   { code: 'sr_Latn', name: 'srpski' },
110   { code: 'tr', name: 'Türkçe' },
111   { code: 'uk', name: 'Українська Mова' },
112   { code: 'ru', name: 'Русский' },
113   { code: 'nl', name: 'Nederlands' },
114   { code: 'it', name: 'Italiano' },
115 ];
116
117 export const themes = [
118   'litera',
119   'materia',
120   'minty',
121   'solar',
122   'united',
123   'cyborg',
124   'darkly',
125   'journal',
126   'sketchy',
127   'vaporwave',
128   'vaporwave-dark',
129   'i386',
130   'litely',
131 ];
132
133 const DEFAULT_ALPHABET =
134   'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
135
136 function getRandomCharFromAlphabet(alphabet: string): string {
137   return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
138 }
139
140 export function randomStr(
141   idDesiredLength: number = 20,
142   alphabet = DEFAULT_ALPHABET
143 ): string {
144   /**
145    * Create n-long array and map it to random chars from given alphabet.
146    * Then join individual chars as string
147    */
148   return Array.from({ length: idDesiredLength })
149     .map(() => {
150       return getRandomCharFromAlphabet(alphabet);
151     })
152     .join('');
153 }
154
155 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
156   let opStr: string = msg.op;
157   return {
158     op: UserOperation[opStr],
159     data: msg.data,
160   };
161 }
162
163 export const md = new markdown_it({
164   html: false,
165   linkify: true,
166   typographer: true,
167 })
168   .use(markdown_it_sub)
169   .use(markdown_it_sup)
170   .use(markdown_it_container, 'spoiler', {
171     validate: function (params: any) {
172       return params.trim().match(/^spoiler\s+(.*)$/);
173     },
174
175     render: function (tokens: any, idx: any) {
176       var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
177
178       if (tokens[idx].nesting === 1) {
179         // opening tag
180         return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
181       } else {
182         // closing tag
183         return '</details>\n';
184       }
185     },
186   })
187   .use(markdownitEmoji, {
188     defs: objectFlip(emojiShortName),
189   });
190
191 export function hotRankComment(comment: Comment): number {
192   return hotRank(comment.score, comment.published);
193 }
194
195 export function hotRankPost(post: Post): number {
196   return hotRank(post.score, post.newest_activity_time);
197 }
198
199 export function hotRank(score: number, timeStr: string): number {
200   // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
201   let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
202   let now: Date = new Date();
203   let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
204
205   let rank =
206     (10000 * Math.log10(Math.max(1, 3 + score))) /
207     Math.pow(hoursElapsed + 2, 1.8);
208
209   // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
210
211   return rank;
212 }
213
214 export function mdToHtml(text: string) {
215   return { __html: md.render(text) };
216 }
217
218 export function getUnixTime(text: string): number {
219   return text ? new Date(text).getTime() / 1000 : undefined;
220 }
221
222 export function addTypeInfo<T>(
223   arr: T[],
224   name: string
225 ): { type_: string; data: T }[] {
226   return arr.map(e => {
227     return { type_: name, data: e };
228   });
229 }
230
231 export function canMod(
232   user: User,
233   modIds: number[],
234   creator_id: number,
235   onSelf: boolean = false
236 ): boolean {
237   // You can do moderator actions only on the mods added after you.
238   if (user) {
239     let yourIndex = modIds.findIndex(id => id == user.id);
240     if (yourIndex == -1) {
241       return false;
242     } else {
243       // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
244       modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
245       return !modIds.includes(creator_id);
246     }
247   } else {
248     return false;
249   }
250 }
251
252 export function isMod(modIds: number[], creator_id: number): boolean {
253   return modIds.includes(creator_id);
254 }
255
256 const imageRegex = new RegExp(
257   /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
258 );
259 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
260
261 export function isImage(url: string) {
262   return imageRegex.test(url);
263 }
264
265 export function isVideo(url: string) {
266   return videoRegex.test(url);
267 }
268
269 export function validURL(str: string) {
270   return !!new URL(str);
271 }
272
273 export function communityRSSUrl(actorId: string, sort: string): string {
274   let url = new URL(actorId);
275   return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
276 }
277
278 export function validEmail(email: string) {
279   let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
280   return re.test(String(email).toLowerCase());
281 }
282
283 export function capitalizeFirstLetter(str: string): string {
284   return str.charAt(0).toUpperCase() + str.slice(1);
285 }
286
287 export function routeSortTypeToEnum(sort: string): SortType {
288   return SortType[sort];
289 }
290
291 export function routeListingTypeToEnum(type: string): ListingType {
292   return ListingType[type];
293 }
294
295 export function routeDataTypeToEnum(type: string): DataType {
296   return DataType[capitalizeFirstLetter(type)];
297 }
298
299 export function routeSearchTypeToEnum(type: string): SearchType {
300   return SearchType[type];
301 }
302
303 export async function getPageTitle(url: string) {
304   let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
305   let title = await res.title;
306   return title;
307 }
308
309 export function debounce(
310   func: any,
311   wait: number = 1000,
312   immediate: boolean = false
313 ) {
314   // 'private' variable for instance
315   // The returned function will be able to reference this due to closure.
316   // Each call to the returned function will share this common timer.
317   let timeout: any;
318
319   // Calling debounce returns a new anonymous function
320   return function () {
321     // reference the context and args for the setTimeout function
322     var context = this,
323       args = arguments;
324
325     // Should the function be called now? If immediate is true
326     //   and not already in a timeout then the answer is: Yes
327     var callNow = immediate && !timeout;
328
329     // This is the basic debounce behaviour where you can call this
330     //   function several times, but it will only execute once
331     //   [before or after imposing a delay].
332     //   Each time the returned function is called, the timer starts over.
333     clearTimeout(timeout);
334
335     // Set the new timeout
336     timeout = setTimeout(function () {
337       // Inside the timeout function, clear the timeout variable
338       // which will let the next execution run when in 'immediate' mode
339       timeout = null;
340
341       // Check if the function already ran with the immediate flag
342       if (!immediate) {
343         // Call the original function with apply
344         // apply lets you define the 'this' object as well as the arguments
345         //    (both captured before setTimeout)
346         func.apply(context, args);
347       }
348     }, wait);
349
350     // Immediate mode and no wait timer? Execute the function..
351     if (callNow) func.apply(context, args);
352   };
353 }
354
355 // TODO
356 export function getLanguage(override?: string): string {
357   let user = UserService.Instance.user;
358   let lang = override || (user && user.lang ? user.lang : 'browser');
359
360   if (lang == 'browser' && isBrowser()) {
361     return getBrowserLanguage();
362   } else {
363     return lang;
364   }
365 }
366
367 // TODO
368 export function getBrowserLanguage(): string {
369   return navigator.language;
370 }
371
372 export function getMomentLanguage(): string {
373   let lang = getLanguage();
374   if (lang.startsWith('zh')) {
375     lang = 'zh-cn';
376   } else if (lang.startsWith('sv')) {
377     lang = 'sv';
378   } else if (lang.startsWith('fr')) {
379     lang = 'fr';
380   } else if (lang.startsWith('de')) {
381     lang = 'de';
382   } else if (lang.startsWith('ru')) {
383     lang = 'ru';
384   } else if (lang.startsWith('es')) {
385     lang = 'es';
386   } else if (lang.startsWith('eo')) {
387     lang = 'eo';
388   } else if (lang.startsWith('nl')) {
389     lang = 'nl';
390   } else if (lang.startsWith('it')) {
391     lang = 'it';
392   } else if (lang.startsWith('fi')) {
393     lang = 'fi';
394   } else if (lang.startsWith('ca')) {
395     lang = 'ca';
396   } else if (lang.startsWith('fa')) {
397     lang = 'fa';
398   } else if (lang.startsWith('pl')) {
399     lang = 'pl';
400   } else if (lang.startsWith('pt')) {
401     lang = 'pt-br';
402   } else if (lang.startsWith('ja')) {
403     lang = 'ja';
404   } else if (lang.startsWith('ka')) {
405     lang = 'ka';
406   } else if (lang.startsWith('hi')) {
407     lang = 'hi';
408   } else if (lang.startsWith('el')) {
409     lang = 'el';
410   } else if (lang.startsWith('eu')) {
411     lang = 'eu';
412   } else if (lang.startsWith('gl')) {
413     lang = 'gl';
414   } else if (lang.startsWith('tr')) {
415     lang = 'tr';
416   } else if (lang.startsWith('hu')) {
417     lang = 'hu';
418   } else if (lang.startsWith('uk')) {
419     lang = 'uk';
420   } else if (lang.startsWith('sq')) {
421     lang = 'sq';
422   } else if (lang.startsWith('km')) {
423     lang = 'km';
424   } else if (lang.startsWith('ga')) {
425     lang = 'ga';
426   } else if (lang.startsWith('sr')) {
427     lang = 'sr';
428   } else if (lang.startsWith('ko')) {
429     lang = 'ko';
430   } else {
431     lang = 'en';
432   }
433   return lang;
434 }
435
436 export function setTheme(theme: string, forceReload: boolean = false) {
437   if (!isBrowser()) {
438     return;
439   }
440   if (theme === 'browser' && !forceReload) {
441     return;
442   }
443   // This is only run on a force reload
444   if (theme == 'browser') {
445     theme = 'darkly';
446   }
447
448   // Unload all the other themes
449   for (var i = 0; i < themes.length; i++) {
450     let styleSheet = document.getElementById(themes[i]);
451     if (styleSheet) {
452       styleSheet.setAttribute('disabled', 'disabled');
453     }
454   }
455
456   document
457     .getElementById('default-light')
458     ?.setAttribute('disabled', 'disabled');
459   document.getElementById('default-dark')?.setAttribute('disabled', 'disabled');
460
461   // Load the theme dynamically
462   let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
463   loadCss(theme, cssLoc);
464   document.getElementById(theme).removeAttribute('disabled');
465 }
466
467 export function loadCss(id: string, loc: string) {
468   if (!document.getElementById(id)) {
469     var head = document.getElementsByTagName('head')[0];
470     var link = document.createElement('link');
471     link.id = id;
472     link.rel = 'stylesheet';
473     link.type = 'text/css';
474     link.href = loc;
475     link.media = 'all';
476     head.appendChild(link);
477   }
478 }
479
480 export function objectFlip(obj: any) {
481   const ret = {};
482   Object.keys(obj).forEach(key => {
483     ret[obj[key]] = key;
484   });
485   return ret;
486 }
487
488 export function showAvatars(): boolean {
489   return (
490     (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
491     !UserService.Instance.user
492   );
493 }
494
495 export function isCakeDay(published: string): boolean {
496   // moment(undefined) or moment.utc(undefined) returns the current date/time
497   // moment(null) or moment.utc(null) returns null
498   const userCreationDate = moment.utc(published || null).local();
499   const currentDate = moment(new Date());
500
501   return (
502     userCreationDate.date() === currentDate.date() &&
503     userCreationDate.month() === currentDate.month() &&
504     userCreationDate.year() !== currentDate.year()
505   );
506 }
507
508 export function isCommentType(
509   item: Comment | PrivateMessage | Post
510 ): item is Comment {
511   return (
512     (item as Comment).community_id !== undefined &&
513     (item as Comment).content !== undefined
514   );
515 }
516
517 export function isPostType(
518   item: Comment | PrivateMessage | Post
519 ): item is Post {
520   return (item as Post).stickied !== undefined;
521 }
522
523 export function toast(text: string, background: string = 'success') {
524   let backgroundColor = `var(--${background})`;
525   Toastify({
526     text: text,
527     backgroundColor: backgroundColor,
528     gravity: 'bottom',
529     position: 'left',
530   }).showToast();
531 }
532
533 export function pictrsDeleteToast(
534   clickToDeleteText: string,
535   deletePictureText: string,
536   deleteUrl: string
537 ) {
538   let backgroundColor = `var(--light)`;
539   let toast = Toastify({
540     text: clickToDeleteText,
541     backgroundColor: backgroundColor,
542     gravity: 'top',
543     position: 'right',
544     duration: 10000,
545     onClick: () => {
546       if (toast) {
547         window.location.replace(deleteUrl);
548         alert(deletePictureText);
549         toast.hideToast();
550       }
551     },
552     close: true,
553   }).showToast();
554 }
555
556 interface NotifyInfo {
557   name: string;
558   icon: string;
559   link: string;
560   body: string;
561 }
562
563 export function messageToastify(info: NotifyInfo, router: any) {
564   let htmlBody = info.body ? md.render(info.body) : '';
565   let backgroundColor = `var(--light)`;
566
567   let toast = Toastify({
568     text: `${htmlBody}<br />${info.name}`,
569     avatar: info.icon,
570     backgroundColor: backgroundColor,
571     className: 'text-dark',
572     close: true,
573     gravity: 'top',
574     position: 'right',
575     duration: 5000,
576     onClick: () => {
577       if (toast) {
578         toast.hideToast();
579         router.history.push(info.link);
580       }
581     },
582   }).showToast();
583 }
584
585 export function notifyPost(post: Post, router: any) {
586   let info: NotifyInfo = {
587     name: post.community_name,
588     icon: post.community_icon ? post.community_icon : defaultFavIcon,
589     link: `/post/${post.id}`,
590     body: post.name,
591   };
592   notify(info, router);
593 }
594
595 export function notifyComment(comment: Comment, router: any) {
596   let info: NotifyInfo = {
597     name: comment.creator_name,
598     icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon,
599     link: `/post/${comment.post_id}/comment/${comment.id}`,
600     body: comment.content,
601   };
602   notify(info, router);
603 }
604
605 export function notifyPrivateMessage(pm: PrivateMessage, router: any) {
606   let info: NotifyInfo = {
607     name: pm.creator_name,
608     icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
609     link: `/inbox`,
610     body: pm.content,
611   };
612   notify(info, router);
613 }
614
615 function notify(info: NotifyInfo, router: any) {
616   messageToastify(info, router);
617
618   if (Notification.permission !== 'granted') Notification.requestPermission();
619   else {
620     var notification = new Notification(info.name, {
621       icon: info.icon,
622       body: info.body,
623     });
624
625     notification.onclick = () => {
626       event.preventDefault();
627       router.history.push(info.link);
628     };
629   }
630 }
631
632 export function setupTribute() {
633   return new Tribute({
634     noMatchTemplate: function () {
635       return '';
636     },
637     collection: [
638       // Emojis
639       {
640         trigger: ':',
641         menuItemTemplate: (item: any) => {
642           let shortName = `:${item.original.key}:`;
643           return `${item.original.val} ${shortName}`;
644         },
645         selectTemplate: (item: any) => {
646           return `:${item.original.key}:`;
647         },
648         values: Object.entries(emojiShortName).map(e => {
649           return { key: e[1], val: e[0] };
650         }),
651         allowSpaces: false,
652         autocompleteMode: true,
653         // TODO
654         // menuItemLimit: mentionDropdownFetchLimit,
655         menuShowMinLength: 2,
656       },
657       // Users
658       {
659         trigger: '@',
660         selectTemplate: (item: any) => {
661           let link = item.original.local
662             ? `[${item.original.key}](/u/${item.original.name})`
663             : `[${item.original.key}](/user/${item.original.id})`;
664           return link;
665         },
666         values: (text: string, cb: any) => {
667           userSearch(text, (users: any) => cb(users));
668         },
669         allowSpaces: false,
670         autocompleteMode: true,
671         // TODO
672         // menuItemLimit: mentionDropdownFetchLimit,
673         menuShowMinLength: 2,
674       },
675
676       // Communities
677       {
678         trigger: '!',
679         selectTemplate: (item: any) => {
680           let link = item.original.local
681             ? `[${item.original.key}](/c/${item.original.name})`
682             : `[${item.original.key}](/community/${item.original.id})`;
683           return link;
684         },
685         values: (text: string, cb: any) => {
686           communitySearch(text, (communities: any) => cb(communities));
687         },
688         allowSpaces: false,
689         autocompleteMode: true,
690         // TODO
691         // menuItemLimit: mentionDropdownFetchLimit,
692         menuShowMinLength: 2,
693       },
694     ],
695   });
696 }
697
698 var tippyInstance;
699 if (isBrowser()) {
700   tippyInstance = tippy('[data-tippy-content]');
701 }
702
703 export function setupTippy() {
704   if (isBrowser()) {
705     tippyInstance.forEach(e => e.destroy());
706     tippyInstance = tippy('[data-tippy-content]', {
707       delay: [500, 0],
708       // Display on "long press"
709       touch: ['hold', 500],
710     });
711   }
712 }
713
714 function userSearch(text: string, cb: any) {
715   if (text) {
716     let form: SearchForm = {
717       q: text,
718       type_: SearchType.Users,
719       sort: SortType.TopAll,
720       page: 1,
721       limit: mentionDropdownFetchLimit,
722     };
723
724     WebSocketService.Instance.search(form);
725
726     let userSub = WebSocketService.Instance.subject.subscribe(
727       msg => {
728         let res = wsJsonToRes(msg);
729         if (res.op == UserOperation.Search) {
730           let data = res.data as SearchResponse;
731           let users = data.users.map(u => {
732             return {
733               key: `@${u.name}@${hostname(u.actor_id)}`,
734               name: u.name,
735               local: u.local,
736               id: u.id,
737             };
738           });
739           cb(users);
740           userSub.unsubscribe();
741         }
742       },
743       err => console.error(err),
744       () => console.log('complete')
745     );
746   } else {
747     cb([]);
748   }
749 }
750
751 function communitySearch(text: string, cb: any) {
752   if (text) {
753     let form: SearchForm = {
754       q: text,
755       type_: SearchType.Communities,
756       sort: SortType.TopAll,
757       page: 1,
758       limit: mentionDropdownFetchLimit,
759     };
760
761     WebSocketService.Instance.search(form);
762
763     let communitySub = WebSocketService.Instance.subject.subscribe(
764       msg => {
765         let res = wsJsonToRes(msg);
766         if (res.op == UserOperation.Search) {
767           let data = res.data as SearchResponse;
768           let communities = data.communities.map(c => {
769             return {
770               key: `!${c.name}@${hostname(c.actor_id)}`,
771               name: c.name,
772               local: c.local,
773               id: c.id,
774             };
775           });
776           cb(communities);
777           communitySub.unsubscribe();
778         }
779       },
780       err => console.error(err),
781       () => console.log('complete')
782     );
783   } else {
784     cb([]);
785   }
786 }
787
788 export function getListingTypeFromProps(props: any): ListingType {
789   return props.match.params.listing_type
790     ? routeListingTypeToEnum(props.match.params.listing_type)
791     : UserService.Instance.user
792     ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
793     : ListingType.Local;
794 }
795
796 // TODO might need to add a user setting for this too
797 export function getDataTypeFromProps(props: any): DataType {
798   return props.match.params.data_type
799     ? routeDataTypeToEnum(props.match.params.data_type)
800     : DataType.Post;
801 }
802
803 export function getSortTypeFromProps(props: any): SortType {
804   return props.match.params.sort
805     ? routeSortTypeToEnum(props.match.params.sort)
806     : UserService.Instance.user
807     ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
808     : SortType.Active;
809 }
810
811 export function getPageFromProps(props: any): number {
812   return props.match.params.page ? Number(props.match.params.page) : 1;
813 }
814
815 export function getRecipientIdFromProps(props: any): number {
816   return props.match.params.recipient_id
817     ? Number(props.match.params.recipient_id)
818     : 1;
819 }
820
821 export function getIdFromProps(props: any): number {
822   return Number(props.match.params.id);
823 }
824
825 export function getCommentIdFromProps(props: any): number {
826   return Number(props.match.params.comment_id);
827 }
828
829 export function getUsernameFromProps(props: any): string {
830   return props.match.params.username;
831 }
832
833 export function editCommentRes(data: CommentResponse, comments: Comment[]) {
834   let found = comments.find(c => c.id == data.comment.id);
835   if (found) {
836     found.content = data.comment.content;
837     found.updated = data.comment.updated;
838     found.removed = data.comment.removed;
839     found.deleted = data.comment.deleted;
840     found.upvotes = data.comment.upvotes;
841     found.downvotes = data.comment.downvotes;
842     found.score = data.comment.score;
843   }
844 }
845
846 export function saveCommentRes(data: CommentResponse, comments: Comment[]) {
847   let found = comments.find(c => c.id == data.comment.id);
848   if (found) {
849     found.saved = data.comment.saved;
850   }
851 }
852
853 export function createCommentLikeRes(
854   data: CommentResponse,
855   comments: Comment[]
856 ) {
857   let found: Comment = comments.find(c => c.id === data.comment.id);
858   if (found) {
859     found.score = data.comment.score;
860     found.upvotes = data.comment.upvotes;
861     found.downvotes = data.comment.downvotes;
862     if (data.comment.my_vote !== null) {
863       found.my_vote = data.comment.my_vote;
864     }
865   }
866 }
867
868 export function createPostLikeFindRes(data: PostResponse, posts: Post[]) {
869   let found = posts.find(c => c.id == data.post.id);
870   if (found) {
871     createPostLikeRes(data, found);
872   }
873 }
874
875 export function createPostLikeRes(data: PostResponse, post: Post) {
876   if (post) {
877     post.score = data.post.score;
878     post.upvotes = data.post.upvotes;
879     post.downvotes = data.post.downvotes;
880     if (data.post.my_vote !== null) {
881       post.my_vote = data.post.my_vote;
882     }
883   }
884 }
885
886 export function editPostFindRes(data: PostResponse, posts: Post[]) {
887   let found = posts.find(c => c.id == data.post.id);
888   if (found) {
889     editPostRes(data, found);
890   }
891 }
892
893 export function editPostRes(data: PostResponse, post: Post) {
894   if (post) {
895     post.url = data.post.url;
896     post.name = data.post.name;
897     post.nsfw = data.post.nsfw;
898     post.deleted = data.post.deleted;
899     post.removed = data.post.removed;
900     post.stickied = data.post.stickied;
901     post.body = data.post.body;
902     post.locked = data.post.locked;
903     post.saved = data.post.saved;
904   }
905 }
906
907 export function commentsToFlatNodes(comments: Comment[]): CommentNodeI[] {
908   let nodes: CommentNodeI[] = [];
909   for (let comment of comments) {
910     nodes.push({ comment: comment });
911   }
912   return nodes;
913 }
914
915 export function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
916   // First, put removed and deleted comments at the bottom, then do your other sorts
917   if (sort == CommentSortType.Top) {
918     tree.sort(
919       (a, b) =>
920         +a.comment.removed - +b.comment.removed ||
921         +a.comment.deleted - +b.comment.deleted ||
922         b.comment.score - a.comment.score
923     );
924   } else if (sort == CommentSortType.New) {
925     tree.sort(
926       (a, b) =>
927         +a.comment.removed - +b.comment.removed ||
928         +a.comment.deleted - +b.comment.deleted ||
929         b.comment.published.localeCompare(a.comment.published)
930     );
931   } else if (sort == CommentSortType.Old) {
932     tree.sort(
933       (a, b) =>
934         +a.comment.removed - +b.comment.removed ||
935         +a.comment.deleted - +b.comment.deleted ||
936         a.comment.published.localeCompare(b.comment.published)
937     );
938   } else if (sort == CommentSortType.Hot) {
939     tree.sort(
940       (a, b) =>
941         +a.comment.removed - +b.comment.removed ||
942         +a.comment.deleted - +b.comment.deleted ||
943         hotRankComment(b.comment) - hotRankComment(a.comment)
944     );
945   }
946
947   // Go through the children recursively
948   for (let node of tree) {
949     if (node.children) {
950       commentSort(node.children, sort);
951     }
952   }
953 }
954
955 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
956   commentSort(tree, convertCommentSortType(sort));
957 }
958
959 function convertCommentSortType(sort: SortType): CommentSortType {
960   if (
961     sort == SortType.TopAll ||
962     sort == SortType.TopDay ||
963     sort == SortType.TopWeek ||
964     sort == SortType.TopMonth ||
965     sort == SortType.TopYear
966   ) {
967     return CommentSortType.Top;
968   } else if (sort == SortType.New) {
969     return CommentSortType.New;
970   } else if (sort == SortType.Hot || sort == SortType.Active) {
971     return CommentSortType.Hot;
972   } else {
973     return CommentSortType.Hot;
974   }
975 }
976
977 export function postSort(
978   posts: Post[],
979   sort: SortType,
980   communityType: boolean
981 ) {
982   // First, put removed and deleted comments at the bottom, then do your other sorts
983   if (
984     sort == SortType.TopAll ||
985     sort == SortType.TopDay ||
986     sort == SortType.TopWeek ||
987     sort == SortType.TopMonth ||
988     sort == SortType.TopYear
989   ) {
990     posts.sort(
991       (a, b) =>
992         +a.removed - +b.removed ||
993         +a.deleted - +b.deleted ||
994         (communityType && +b.stickied - +a.stickied) ||
995         b.score - a.score
996     );
997   } else if (sort == SortType.New) {
998     posts.sort(
999       (a, b) =>
1000         +a.removed - +b.removed ||
1001         +a.deleted - +b.deleted ||
1002         (communityType && +b.stickied - +a.stickied) ||
1003         b.published.localeCompare(a.published)
1004     );
1005   } else if (sort == SortType.Hot) {
1006     posts.sort(
1007       (a, b) =>
1008         +a.removed - +b.removed ||
1009         +a.deleted - +b.deleted ||
1010         (communityType && +b.stickied - +a.stickied) ||
1011         b.hot_rank - a.hot_rank
1012     );
1013   } else if (sort == SortType.Active) {
1014     posts.sort(
1015       (a, b) =>
1016         +a.removed - +b.removed ||
1017         +a.deleted - +b.deleted ||
1018         (communityType && +b.stickied - +a.stickied) ||
1019         b.hot_rank_active - a.hot_rank_active
1020     );
1021   }
1022 }
1023
1024 export const colorList: string[] = [
1025   hsl(0),
1026   hsl(100),
1027   hsl(150),
1028   hsl(200),
1029   hsl(250),
1030   hsl(300),
1031 ];
1032
1033 function hsl(num: number) {
1034   return `hsla(${num}, 35%, 50%, 1)`;
1035 }
1036
1037 // function randomHsl() {
1038 //   return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
1039 // }
1040
1041 export function previewLines(
1042   text: string,
1043   maxChars: number = 300,
1044   maxLines: number = 1
1045 ): string {
1046   return (
1047     text
1048       .slice(0, maxChars)
1049       .split('\n')
1050       // Use lines * 2 because markdown requires 2 lines
1051       .slice(0, maxLines * 2)
1052       .join('\n') + '...'
1053   );
1054 }
1055
1056 export function hostname(url: string): string {
1057   let cUrl = new URL(url);
1058   return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1059 }
1060
1061 export function validTitle(title?: string): boolean {
1062   // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1063   if (title === null || title.length < 3) return true;
1064
1065   const regex = new RegExp(/.*\S.*/, 'g');
1066
1067   return regex.test(title);
1068 }
1069
1070 export function siteBannerCss(banner: string): string {
1071   return ` \
1072     background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1073     background-attachment: fixed; \
1074     background-position: top; \
1075     background-repeat: no-repeat; \
1076     background-size: 100% cover; \
1077
1078     width: 100%; \
1079     max-height: 100vh; \
1080     `;
1081 }
1082
1083 export function isBrowser() {
1084   return typeof window !== 'undefined';
1085 }
1086
1087 export function setAuth(obj: any, auth: string) {
1088   if (auth) {
1089     obj.auth = auth;
1090   }
1091 }
1092
1093 export function setIsoData(context: any): IsoData {
1094   let isoData: IsoData = isBrowser()
1095     ? window.isoData
1096     : context.router.staticContext;
1097   return isoData;
1098 }
1099
1100 export function wsSubscribe(parseMessage: any): Subscription {
1101   if (isBrowser()) {
1102     return WebSocketService.Instance.subject
1103       .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1104       .subscribe(
1105         msg => parseMessage(msg),
1106         err => console.error(err),
1107         () => console.log('complete')
1108       );
1109   } else {
1110     return null;
1111   }
1112 }
1113
1114 moment.updateLocale('en', {
1115   relativeTime: {
1116     future: 'in %s',
1117     past: '%s ago',
1118     s: '<1m',
1119     ss: '%ds',
1120     m: '1m',
1121     mm: '%dm',
1122     h: '1h',
1123     hh: '%dh',
1124     d: '1d',
1125     dd: '%dd',
1126     w: '1w',
1127     ww: '%dw',
1128     M: '1M',
1129     MM: '%dM',
1130     y: '1Y',
1131     yy: '%dY',
1132   },
1133 });