]> Untitled Git - lemmy.git/blob - ui/src/utils.ts
Adding finnish to moment, dropdown.
[lemmy.git] / ui / src / utils.ts
1 import 'moment/locale/es';
2 import 'moment/locale/eo';
3 import 'moment/locale/de';
4 import 'moment/locale/zh-cn';
5 import 'moment/locale/fr';
6 import 'moment/locale/sv';
7 import 'moment/locale/ru';
8 import 'moment/locale/nl';
9 import 'moment/locale/it';
10 import 'moment/locale/fi';
11
12 import {
13   UserOperation,
14   Comment,
15   PrivateMessage,
16   User,
17   SortType,
18   ListingType,
19   SearchType,
20   WebSocketResponse,
21   WebSocketJsonResponse,
22   SearchForm,
23   SearchResponse,
24 } from './interfaces';
25 import { UserService, WebSocketService } from './services';
26
27 import Tribute from 'tributejs/src/Tribute.js';
28 import markdown_it from 'markdown-it';
29 import markdownitEmoji from 'markdown-it-emoji/light';
30 import markdown_it_container from 'markdown-it-container';
31 import twemoji from 'twemoji';
32 import emojiShortName from 'emoji-short-name';
33 import Toastify from 'toastify-js';
34
35 export const repoUrl = 'https://github.com/dessalines/lemmy';
36 export const markdownHelpUrl = 'https://commonmark.org/help/';
37 export const archiveUrl = 'https://archive.is';
38
39 export const postRefetchSeconds: number = 60 * 1000;
40 export const fetchLimit: number = 20;
41 export const mentionDropdownFetchLimit = 10;
42
43 export function randomStr() {
44   return Math.random()
45     .toString(36)
46     .replace(/[^a-z]+/g, '')
47     .substr(2, 10);
48 }
49
50 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
51   let opStr: string = msg.op;
52   return {
53     op: UserOperation[opStr],
54     data: msg.data,
55   };
56 }
57
58 export const md = new markdown_it({
59   html: false,
60   linkify: true,
61   typographer: true,
62 })
63   .use(markdown_it_container, 'spoiler', {
64     validate: function(params: any) {
65       return params.trim().match(/^spoiler\s+(.*)$/);
66     },
67
68     render: function(tokens: any, idx: any) {
69       var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
70
71       if (tokens[idx].nesting === 1) {
72         // opening tag
73         return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
74       } else {
75         // closing tag
76         return '</details>\n';
77       }
78     },
79   })
80   .use(markdownitEmoji, {
81     defs: objectFlip(emojiShortName),
82   });
83
84 md.renderer.rules.emoji = function(token, idx) {
85   return twemoji.parse(token[idx].content);
86 };
87
88 export function hotRank(comment: Comment): number {
89   // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
90
91   let date: Date = new Date(comment.published + 'Z'); // Add Z to convert from UTC date
92   let now: Date = new Date();
93   let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
94
95   let rank =
96     (10000 * Math.log10(Math.max(1, 3 + comment.score))) /
97     Math.pow(hoursElapsed + 2, 1.8);
98
99   // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
100
101   return rank;
102 }
103
104 export function mdToHtml(text: string) {
105   return { __html: md.render(text) };
106 }
107
108 export function getUnixTime(text: string): number {
109   return text ? new Date(text).getTime() / 1000 : undefined;
110 }
111
112 export function addTypeInfo<T>(
113   arr: Array<T>,
114   name: string
115 ): Array<{ type_: string; data: T }> {
116   return arr.map(e => {
117     return { type_: name, data: e };
118   });
119 }
120
121 export function canMod(
122   user: User,
123   modIds: Array<number>,
124   creator_id: number,
125   onSelf: boolean = false
126 ): boolean {
127   // You can do moderator actions only on the mods added after you.
128   if (user) {
129     let yourIndex = modIds.findIndex(id => id == user.id);
130     if (yourIndex == -1) {
131       return false;
132     } else {
133       // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
134       modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
135       return !modIds.includes(creator_id);
136     }
137   } else {
138     return false;
139   }
140 }
141
142 export function isMod(modIds: Array<number>, creator_id: number): boolean {
143   return modIds.includes(creator_id);
144 }
145
146 var imageRegex = new RegExp(
147   `(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))`
148 );
149 var videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
150
151 export function isImage(url: string) {
152   return imageRegex.test(url);
153 }
154
155 export function isVideo(url: string) {
156   return videoRegex.test(url);
157 }
158
159 export function validURL(str: string) {
160   try {
161     return !!new URL(str);
162   } catch {
163     return false;
164   }
165 }
166
167 export function validEmail(email: string) {
168   let re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
169   return re.test(String(email).toLowerCase());
170 }
171
172 export function capitalizeFirstLetter(str: string): string {
173   return str.charAt(0).toUpperCase() + str.slice(1);
174 }
175
176 export function routeSortTypeToEnum(sort: string): SortType {
177   if (sort == 'new') {
178     return SortType.New;
179   } else if (sort == 'hot') {
180     return SortType.Hot;
181   } else if (sort == 'topday') {
182     return SortType.TopDay;
183   } else if (sort == 'topweek') {
184     return SortType.TopWeek;
185   } else if (sort == 'topmonth') {
186     return SortType.TopMonth;
187   } else if (sort == 'topyear') {
188     return SortType.TopYear;
189   } else if (sort == 'topall') {
190     return SortType.TopAll;
191   }
192 }
193
194 export function routeListingTypeToEnum(type: string): ListingType {
195   return ListingType[capitalizeFirstLetter(type)];
196 }
197
198 export function routeSearchTypeToEnum(type: string): SearchType {
199   return SearchType[capitalizeFirstLetter(type)];
200 }
201
202 export async function getPageTitle(url: string) {
203   let res = await fetch(`https://textance.herokuapp.com/title/${url}`);
204   let data = await res.text();
205   return data;
206 }
207
208 export function debounce(
209   func: any,
210   wait: number = 1000,
211   immediate: boolean = false
212 ) {
213   // 'private' variable for instance
214   // The returned function will be able to reference this due to closure.
215   // Each call to the returned function will share this common timer.
216   let timeout: any;
217
218   // Calling debounce returns a new anonymous function
219   return function() {
220     // reference the context and args for the setTimeout function
221     var context = this,
222       args = arguments;
223
224     // Should the function be called now? If immediate is true
225     //   and not already in a timeout then the answer is: Yes
226     var callNow = immediate && !timeout;
227
228     // This is the basic debounce behaviour where you can call this
229     //   function several times, but it will only execute once
230     //   [before or after imposing a delay].
231     //   Each time the returned function is called, the timer starts over.
232     clearTimeout(timeout);
233
234     // Set the new timeout
235     timeout = setTimeout(function() {
236       // Inside the timeout function, clear the timeout variable
237       // which will let the next execution run when in 'immediate' mode
238       timeout = null;
239
240       // Check if the function already ran with the immediate flag
241       if (!immediate) {
242         // Call the original function with apply
243         // apply lets you define the 'this' object as well as the arguments
244         //    (both captured before setTimeout)
245         func.apply(context, args);
246       }
247     }, wait);
248
249     // Immediate mode and no wait timer? Execute the function..
250     if (callNow) func.apply(context, args);
251   };
252 }
253
254 export const languages = [
255   { code: 'en', name: 'English' },
256   { code: 'eo', name: 'Esperanto' },
257   { code: 'es', name: 'Español' },
258   { code: 'de', name: 'Deutsch' },
259   { code: 'zh', name: '中文' },
260   { code: 'fi', name: 'Suomi' },
261   { code: 'fr', name: 'Français' },
262   { code: 'sv', name: 'Svenska' },
263   { code: 'ru', name: 'Русский' },
264   { code: 'nl', name: 'Nederlands' },
265   { code: 'it', name: 'Italiano' },
266 ];
267
268 export function getLanguage(): string {
269   let user = UserService.Instance.user;
270   let lang = user && user.lang ? user.lang : 'browser';
271
272   if (lang == 'browser') {
273     return getBrowserLanguage();
274   } else {
275     return lang;
276   }
277 }
278
279 export function getBrowserLanguage(): string {
280   return navigator.language;
281 }
282
283 export function getMomentLanguage(): string {
284   let lang = getLanguage();
285   if (lang.startsWith('zh')) {
286     lang = 'zh-cn';
287   } else if (lang.startsWith('sv')) {
288     lang = 'sv';
289   } else if (lang.startsWith('fr')) {
290     lang = 'fr';
291   } else if (lang.startsWith('de')) {
292     lang = 'de';
293   } else if (lang.startsWith('ru')) {
294     lang = 'ru';
295   } else if (lang.startsWith('es')) {
296     lang = 'es';
297   } else if (lang.startsWith('eo')) {
298     lang = 'eo';
299   } else if (lang.startsWith('nl')) {
300     lang = 'nl';
301   } else if (lang.startsWith('it')) {
302     lang = 'it';
303   } else if (lang.startsWith('fi')) {
304     lang = 'fi';
305   } else {
306     lang = 'en';
307   }
308   return lang;
309 }
310
311 export const themes = [
312   'litera',
313   'minty',
314   'solar',
315   'united',
316   'cyborg',
317   'darkly',
318   'journal',
319   'sketchy',
320   'vaporwave',
321   'vaporwave-dark',
322 ];
323
324 export function setTheme(theme: string = 'darkly') {
325   // unload all the other themes
326   for (var i = 0; i < themes.length; i++) {
327     let styleSheet = document.getElementById(themes[i]);
328     if (styleSheet) {
329       styleSheet.setAttribute('disabled', 'disabled');
330     }
331   }
332
333   // Load the theme dynamically
334   if (!document.getElementById(theme)) {
335     var head = document.getElementsByTagName('head')[0];
336     var link = document.createElement('link');
337     link.id = theme;
338     link.rel = 'stylesheet';
339     link.type = 'text/css';
340     link.href = `/static/assets/css/themes/${theme}.min.css`;
341     link.media = 'all';
342     head.appendChild(link);
343   }
344   document.getElementById(theme).removeAttribute('disabled');
345 }
346
347 export function objectFlip(obj: any) {
348   const ret = {};
349   Object.keys(obj).forEach(key => {
350     ret[obj[key]] = key;
351   });
352   return ret;
353 }
354
355 export function pictshareAvatarThumbnail(src: string): string {
356   // sample url: http://localhost:8535/pictshare/gs7xuu.jpg
357   let split = src.split('pictshare');
358   let out = `${split[0]}pictshare/96x96${split[1]}`;
359   return out;
360 }
361
362 export function showAvatars(): boolean {
363   return (
364     (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
365     !UserService.Instance.user
366   );
367 }
368
369 /// Converts to image thumbnail (only supports pictshare currently)
370 export function imageThumbnailer(url: string): string {
371   let split = url.split('pictshare');
372   if (split.length > 1) {
373     let out = `${split[0]}pictshare/140x140${split[1]}`;
374     return out;
375   } else {
376     return url;
377   }
378 }
379
380 export function isCommentType(item: Comment | PrivateMessage): item is Comment {
381   return (item as Comment).community_id !== undefined;
382 }
383
384 export function toast(text: string, background: string = 'success') {
385   let backgroundColor = `var(--${background})`;
386   Toastify({
387     text: text,
388     backgroundColor: backgroundColor,
389     gravity: 'bottom',
390     position: 'left',
391   }).showToast();
392 }
393
394 export function setupTribute(): Tribute {
395   return new Tribute({
396     collection: [
397       // Emojis
398       {
399         trigger: ':',
400         menuItemTemplate: (item: any) => {
401           let emoji = `:${item.original.key}:`;
402           return `${item.original.val} ${emoji}`;
403         },
404         selectTemplate: (item: any) => {
405           return `:${item.original.key}:`;
406         },
407         values: Object.entries(emojiShortName).map(e => {
408           return { key: e[1], val: e[0] };
409         }),
410         allowSpaces: false,
411         autocompleteMode: true,
412         menuItemLimit: mentionDropdownFetchLimit,
413       },
414       // Users
415       {
416         trigger: '@',
417         selectTemplate: (item: any) => {
418           return `[/u/${item.original.key}](/u/${item.original.key})`;
419         },
420         values: (text: string, cb: any) => {
421           userSearch(text, (users: any) => cb(users));
422         },
423         allowSpaces: false,
424         autocompleteMode: true,
425         menuItemLimit: mentionDropdownFetchLimit,
426       },
427
428       // Communities
429       {
430         trigger: '#',
431         selectTemplate: (item: any) => {
432           return `[/c/${item.original.key}](/c/${item.original.key})`;
433         },
434         values: (text: string, cb: any) => {
435           communitySearch(text, (communities: any) => cb(communities));
436         },
437         allowSpaces: false,
438         autocompleteMode: true,
439         menuItemLimit: mentionDropdownFetchLimit,
440       },
441     ],
442   });
443 }
444
445 function userSearch(text: string, cb: any) {
446   if (text) {
447     let form: SearchForm = {
448       q: text,
449       type_: SearchType[SearchType.Users],
450       sort: SortType[SortType.TopAll],
451       page: 1,
452       limit: mentionDropdownFetchLimit,
453     };
454
455     WebSocketService.Instance.search(form);
456
457     this.userSub = WebSocketService.Instance.subject.subscribe(
458       msg => {
459         let res = wsJsonToRes(msg);
460         if (res.op == UserOperation.Search) {
461           let data = res.data as SearchResponse;
462           let users = data.users.map(u => {
463             return { key: u.name };
464           });
465           cb(users);
466           this.userSub.unsubscribe();
467         }
468       },
469       err => console.error(err),
470       () => console.log('complete')
471     );
472   } else {
473     cb([]);
474   }
475 }
476
477 function communitySearch(text: string, cb: any) {
478   if (text) {
479     let form: SearchForm = {
480       q: text,
481       type_: SearchType[SearchType.Communities],
482       sort: SortType[SortType.TopAll],
483       page: 1,
484       limit: mentionDropdownFetchLimit,
485     };
486
487     WebSocketService.Instance.search(form);
488
489     this.communitySub = WebSocketService.Instance.subject.subscribe(
490       msg => {
491         let res = wsJsonToRes(msg);
492         if (res.op == UserOperation.Search) {
493           let data = res.data as SearchResponse;
494           let communities = data.communities.map(u => {
495             return { key: u.name };
496           });
497           cb(communities);
498           this.communitySub.unsubscribe();
499         }
500       },
501       err => console.error(err),
502       () => console.log('complete')
503     );
504   } else {
505     cb([]);
506   }
507 }