]> Untitled Git - lemmy.git/blob - ui/src/utils.ts
Running prettier on code.
[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
10 import {
11   UserOperation,
12   Comment,
13   User,
14   SortType,
15   ListingType,
16   SearchType,
17 } from './interfaces';
18 import * as markdown_it from 'markdown-it';
19 import * as markdownitEmoji from 'markdown-it-emoji/light';
20 import * as markdown_it_container from 'markdown-it-container';
21 import * as twemoji from 'twemoji';
22 import * as emojiShortName from 'emoji-short-name';
23
24 export const repoUrl = 'https://github.com/dessalines/lemmy';
25 export const markdownHelpUrl = 'https://commonmark.org/help/';
26
27 export const postRefetchSeconds: number = 60 * 1000;
28 export const fetchLimit: number = 20;
29 export const mentionDropdownFetchLimit = 6;
30
31 export function randomStr() {
32   return Math.random()
33     .toString(36)
34     .replace(/[^a-z]+/g, '')
35     .substr(2, 10);
36 }
37
38 export function msgOp(msg: any): UserOperation {
39   let opStr: string = msg.op;
40   return UserOperation[opStr];
41 }
42
43 export const md = new markdown_it({
44   html: false,
45   linkify: true,
46   typographer: true,
47 })
48   .use(markdown_it_container, 'spoiler', {
49     validate: function(params: any) {
50       return params.trim().match(/^spoiler\s+(.*)$/);
51     },
52
53     render: function(tokens: any, idx: any) {
54       var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
55
56       if (tokens[idx].nesting === 1) {
57         // opening tag
58         return (
59           '<details><summary>' + md.utils.escapeHtml(m[1]) + '</summary>\n'
60         );
61       } else {
62         // closing tag
63         return '</details>\n';
64       }
65     },
66   })
67   .use(markdownitEmoji, {
68     defs: objectFlip(emojiShortName),
69   });
70
71 md.renderer.rules.emoji = function(token, idx) {
72   return twemoji.parse(token[idx].content);
73 };
74
75 export function hotRank(comment: Comment): number {
76   // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
77
78   let date: Date = new Date(comment.published + 'Z'); // Add Z to convert from UTC date
79   let now: Date = new Date();
80   let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
81
82   let rank =
83     (10000 * Math.log10(Math.max(1, 3 + comment.score))) /
84     Math.pow(hoursElapsed + 2, 1.8);
85
86   // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
87
88   return rank;
89 }
90
91 export function mdToHtml(text: string) {
92   return { __html: md.render(text) };
93 }
94
95 export function getUnixTime(text: string): number {
96   return text ? new Date(text).getTime() / 1000 : undefined;
97 }
98
99 export function addTypeInfo<T>(
100   arr: Array<T>,
101   name: string
102 ): Array<{ type_: string; data: T }> {
103   return arr.map(e => {
104     return { type_: name, data: e };
105   });
106 }
107
108 export function canMod(
109   user: User,
110   modIds: Array<number>,
111   creator_id: number,
112   onSelf: boolean = false
113 ): boolean {
114   // You can do moderator actions only on the mods added after you.
115   if (user) {
116     let yourIndex = modIds.findIndex(id => id == user.id);
117     if (yourIndex == -1) {
118       return false;
119     } else {
120       // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
121       modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
122       return !modIds.includes(creator_id);
123     }
124   } else {
125     return false;
126   }
127 }
128
129 export function isMod(modIds: Array<number>, creator_id: number): boolean {
130   return modIds.includes(creator_id);
131 }
132
133 var imageRegex = new RegExp(
134   `(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))`
135 );
136 var videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
137
138 export function isImage(url: string) {
139   return imageRegex.test(url);
140 }
141
142 export function isVideo(url: string) {
143   return videoRegex.test(url);
144 }
145
146 export function validURL(str: string) {
147   try {
148     return !!new URL(str);
149   } catch {
150     return false;
151   }
152 }
153
154 export function capitalizeFirstLetter(str: string): string {
155   return str.charAt(0).toUpperCase() + str.slice(1);
156 }
157
158 export function routeSortTypeToEnum(sort: string): SortType {
159   if (sort == 'new') {
160     return SortType.New;
161   } else if (sort == 'hot') {
162     return SortType.Hot;
163   } else if (sort == 'topday') {
164     return SortType.TopDay;
165   } else if (sort == 'topweek') {
166     return SortType.TopWeek;
167   } else if (sort == 'topmonth') {
168     return SortType.TopMonth;
169   } else if (sort == 'topall') {
170     return SortType.TopAll;
171   }
172 }
173
174 export function routeListingTypeToEnum(type: string): ListingType {
175   return ListingType[capitalizeFirstLetter(type)];
176 }
177
178 export function routeSearchTypeToEnum(type: string): SearchType {
179   return SearchType[capitalizeFirstLetter(type)];
180 }
181
182 export async function getPageTitle(url: string) {
183   let res = await fetch(`https://textance.herokuapp.com/title/${url}`);
184   let data = await res.text();
185   return data;
186 }
187
188 export function debounce(
189   func: any,
190   wait: number = 500,
191   immediate: boolean = false
192 ) {
193   // 'private' variable for instance
194   // The returned function will be able to reference this due to closure.
195   // Each call to the returned function will share this common timer.
196   let timeout: number;
197
198   // Calling debounce returns a new anonymous function
199   return function() {
200     // reference the context and args for the setTimeout function
201     var context = this,
202       args = arguments;
203
204     // Should the function be called now? If immediate is true
205     //   and not already in a timeout then the answer is: Yes
206     var callNow = immediate && !timeout;
207
208     // This is the basic debounce behaviour where you can call this
209     //   function several times, but it will only execute once
210     //   [before or after imposing a delay].
211     //   Each time the returned function is called, the timer starts over.
212     clearTimeout(timeout);
213
214     // Set the new timeout
215     timeout = setTimeout(function() {
216       // Inside the timeout function, clear the timeout variable
217       // which will let the next execution run when in 'immediate' mode
218       timeout = null;
219
220       // Check if the function already ran with the immediate flag
221       if (!immediate) {
222         // Call the original function with apply
223         // apply lets you define the 'this' object as well as the arguments
224         //    (both captured before setTimeout)
225         func.apply(context, args);
226       }
227     }, wait);
228
229     // Immediate mode and no wait timer? Execute the function..
230     if (callNow) func.apply(context, args);
231   };
232 }
233
234 export function getLanguage(): string {
235   return navigator.language || navigator.userLanguage;
236 }
237
238 export function objectFlip(obj: any) {
239   const ret = {};
240   Object.keys(obj).forEach(key => {
241     ret[obj[key]] = key;
242   });
243   return ret;
244 }
245
246 export function getMomentLanguage(): string {
247   let lang = getLanguage();
248   if (lang.startsWith('zh')) {
249     lang = 'zh-cn';
250   } else if (lang.startsWith('sv')) {
251     lang = 'sv';
252   } else if (lang.startsWith('fr')) {
253     lang = 'fr';
254   } else if (lang.startsWith('de')) {
255     lang = 'de';
256   } else if (lang.startsWith('ru')) {
257     lang = 'ru';
258   } else if (lang.startsWith('es')) {
259     lang = 'es';
260   } else if (lang.startsWith('eo')) {
261     lang = 'eo';
262   } else if (lang.startsWith('nl')) {
263     lang = 'nl';
264   } else {
265     lang = 'en';
266   }
267   return lang;
268 }
269
270 export const themes = [
271   'litera',
272   'minty',
273   'solar',
274   'united',
275   'cyborg',
276   'darkly',
277   'journal',
278   'sketchy',
279 ];
280
281 export function setTheme(theme: string = 'darkly') {
282   for (var i = 0; i < themes.length; i++) {
283     let styleSheet = document.getElementById(themes[i]);
284     if (themes[i] == theme) {
285       styleSheet.removeAttribute('disabled');
286     } else {
287       styleSheet.setAttribute('disabled', 'disabled');
288     }
289   }
290 }