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