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';
18 } from './interfaces';
19 import { UserService } from './services/UserService';
20 import markdown_it from 'markdown-it';
21 import markdownitEmoji from 'markdown-it-emoji/light';
22 import markdown_it_container from 'markdown-it-container';
23 import * as twemoji from 'twemoji';
24 import * as emojiShortName from 'emoji-short-name';
26 export const repoUrl = 'https://github.com/dessalines/lemmy';
27 export const markdownHelpUrl = 'https://commonmark.org/help/';
28 export const archiveUrl = 'https://archive.is';
30 export const postRefetchSeconds: number = 60 * 1000;
31 export const fetchLimit: number = 20;
32 export const mentionDropdownFetchLimit = 6;
34 export function randomStr() {
37 .replace(/[^a-z]+/g, '')
41 export function msgOp(msg: any): UserOperation {
42 let opStr: string = msg.op;
43 return UserOperation[opStr];
46 export const md = new markdown_it({
51 .use(markdown_it_container, 'spoiler', {
52 validate: function(params: any) {
53 return params.trim().match(/^spoiler\s+(.*)$/);
56 render: function(tokens: any, idx: any) {
57 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
59 if (tokens[idx].nesting === 1) {
61 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
64 return '</details>\n';
68 .use(markdownitEmoji, {
69 defs: objectFlip(emojiShortName),
72 md.renderer.rules.emoji = function(token, idx) {
73 return twemoji.parse(token[idx].content);
76 export function hotRank(comment: Comment): number {
77 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
79 let date: Date = new Date(comment.published + 'Z'); // Add Z to convert from UTC date
80 let now: Date = new Date();
81 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
84 (10000 * Math.log10(Math.max(1, 3 + comment.score))) /
85 Math.pow(hoursElapsed + 2, 1.8);
87 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
92 export function mdToHtml(text: string) {
93 return { __html: md.render(text) };
96 export function getUnixTime(text: string): number {
97 return text ? new Date(text).getTime() / 1000 : undefined;
100 export function addTypeInfo<T>(
103 ): Array<{ type_: string; data: T }> {
104 return arr.map(e => {
105 return { type_: name, data: e };
109 export function canMod(
111 modIds: Array<number>,
113 onSelf: boolean = false
115 // You can do moderator actions only on the mods added after you.
117 let yourIndex = modIds.findIndex(id => id == user.id);
118 if (yourIndex == -1) {
121 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
122 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
123 return !modIds.includes(creator_id);
130 export function isMod(modIds: Array<number>, creator_id: number): boolean {
131 return modIds.includes(creator_id);
134 var imageRegex = new RegExp(
135 `(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))`
137 var videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
139 export function isImage(url: string) {
140 return imageRegex.test(url);
143 export function isVideo(url: string) {
144 return videoRegex.test(url);
147 export function validURL(str: string) {
149 return !!new URL(str);
155 export function validEmail(email: string) {
156 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,}))$/;
157 return re.test(String(email).toLowerCase());
160 export function capitalizeFirstLetter(str: string): string {
161 return str.charAt(0).toUpperCase() + str.slice(1);
164 export function routeSortTypeToEnum(sort: string): SortType {
167 } else if (sort == 'hot') {
169 } else if (sort == 'topday') {
170 return SortType.TopDay;
171 } else if (sort == 'topweek') {
172 return SortType.TopWeek;
173 } else if (sort == 'topmonth') {
174 return SortType.TopMonth;
175 } else if (sort == 'topyear') {
176 return SortType.TopYear;
177 } else if (sort == 'topall') {
178 return SortType.TopAll;
182 export function routeListingTypeToEnum(type: string): ListingType {
183 return ListingType[capitalizeFirstLetter(type)];
186 export function routeSearchTypeToEnum(type: string): SearchType {
187 return SearchType[capitalizeFirstLetter(type)];
190 export async function getPageTitle(url: string) {
191 let res = await fetch(`https://textance.herokuapp.com/title/${url}`);
192 let data = await res.text();
196 export function debounce(
199 immediate: boolean = false
201 // 'private' variable for instance
202 // The returned function will be able to reference this due to closure.
203 // Each call to the returned function will share this common timer.
206 // Calling debounce returns a new anonymous function
208 // reference the context and args for the setTimeout function
212 // Should the function be called now? If immediate is true
213 // and not already in a timeout then the answer is: Yes
214 var callNow = immediate && !timeout;
216 // This is the basic debounce behaviour where you can call this
217 // function several times, but it will only execute once
218 // [before or after imposing a delay].
219 // Each time the returned function is called, the timer starts over.
220 clearTimeout(timeout);
222 // Set the new timeout
223 timeout = setTimeout(function() {
224 // Inside the timeout function, clear the timeout variable
225 // which will let the next execution run when in 'immediate' mode
228 // Check if the function already ran with the immediate flag
230 // Call the original function with apply
231 // apply lets you define the 'this' object as well as the arguments
232 // (both captured before setTimeout)
233 func.apply(context, args);
237 // Immediate mode and no wait timer? Execute the function..
238 if (callNow) func.apply(context, args);
242 export const languages = [
243 { code: 'en', name: 'English' },
244 { code: 'eo', name: 'Esperanto' },
245 { code: 'es', name: 'Español' },
246 { code: 'de', name: 'Deutsch' },
247 { code: 'zh', name: '中文' },
248 { code: 'fr', name: 'Français' },
249 { code: 'sv', name: 'Svenska' },
250 { code: 'ru', name: 'Русский' },
251 { code: 'nl', name: 'Nederlands' },
252 { code: 'it', name: 'Italiano' },
255 export function getLanguage(): string {
256 let user = UserService.Instance.user;
257 let lang = user && user.lang ? user.lang : 'browser';
259 if (lang == 'browser') {
260 return getBrowserLanguage();
266 export function getBrowserLanguage(): string {
267 return navigator.language;
270 export function getMomentLanguage(): string {
271 let lang = getLanguage();
272 if (lang.startsWith('zh')) {
274 } else if (lang.startsWith('sv')) {
276 } else if (lang.startsWith('fr')) {
278 } else if (lang.startsWith('de')) {
280 } else if (lang.startsWith('ru')) {
282 } else if (lang.startsWith('es')) {
284 } else if (lang.startsWith('eo')) {
286 } else if (lang.startsWith('nl')) {
288 } else if (lang.startsWith('it')) {
296 export const themes = [
309 export function setTheme(theme: string = 'darkly') {
310 // unload all the other themes
311 for (var i = 0; i < themes.length; i++) {
312 let styleSheet = document.getElementById(themes[i]);
314 styleSheet.setAttribute('disabled', 'disabled');
318 // Load the theme dynamically
319 if (!document.getElementById(theme)) {
320 var head = document.getElementsByTagName('head')[0];
321 var link = document.createElement('link');
323 link.rel = 'stylesheet';
324 link.type = 'text/css';
325 link.href = `/static/assets/css/themes/${theme}.min.css`;
327 head.appendChild(link);
329 document.getElementById(theme).removeAttribute('disabled');
332 export function objectFlip(obj: any) {
334 Object.keys(obj).forEach(key => {
340 export function pictshareAvatarThumbnail(src: string): string {
341 // sample url: http://localhost:8535/pictshare/gs7xuu.jpg
342 let split = src.split('pictshare');
343 let out = `${split[0]}pictshare/96x96${split[1]}`;
347 export function showAvatars(): boolean {
349 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
350 !UserService.Instance.user
354 /// Converts to image thumbnail (only supports pictshare currently)
355 export function imageThumbnailer(url: string): string {
356 let split = url.split('pictshare');
357 if (split.length > 1) {
358 let out = `${split[0]}pictshare/140x140${split[1]}`;