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