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