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