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';
10 import { UserOperation, Comment, User, SortType, ListingType, SearchType } from './interfaces';
11 import * as markdown_it from 'markdown-it';
12 import * as markdownitEmoji from 'markdown-it-emoji/light';
13 import * as markdown_it_container from 'markdown-it-container';
14 import * as twemoji from 'twemoji';
15 import * as emojiShortName from 'emoji-short-name';
17 export const repoUrl = 'https://github.com/dessalines/lemmy';
18 export const markdownHelpUrl = 'https://commonmark.org/help/';
20 export const postRefetchSeconds: number = 60*1000;
21 export const fetchLimit: number = 20;
22 export const mentionDropdownFetchLimit = 6;
24 export function randomStr() {return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10)}
26 export function msgOp(msg: any): UserOperation {
27 let opStr: string = msg.op;
28 return UserOperation[opStr];
31 export const md = new markdown_it({
35 }).use(markdown_it_container, 'spoiler', {
36 validate: function(params: any) {
37 return params.trim().match(/^spoiler\s+(.*)$/);
40 render: function (tokens: any, idx: any) {
41 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
43 if (tokens[idx].nesting === 1) {
45 return '<details><summary>' + md.utils.escapeHtml(m[1]) + '</summary>\n';
49 return '</details>\n';
52 }).use(markdownitEmoji, {
53 defs: objectFlip(emojiShortName)
56 md.renderer.rules.emoji = function(token, idx) {
57 return twemoji.parse(token[idx].content);
60 export function hotRank(comment: Comment): number {
61 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
63 let date: Date = new Date(comment.published + 'Z'); // Add Z to convert from UTC date
64 let now: Date = new Date();
65 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
67 let rank = (10000 * Math.log10(Math.max(1, 3 + comment.score))) / Math.pow(hoursElapsed + 2, 1.8);
69 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
74 export function mdToHtml(text: string) {
75 return {__html: md.render(text)};
78 export function getUnixTime(text: string): number {
79 return text ? new Date(text).getTime()/1000 : undefined;
82 export function addTypeInfo<T>(arr: Array<T>, name: string): Array<{type_: string, data: T}> {
83 return arr.map(e => {return {type_: name, data: e}});
86 export function canMod(user: User, modIds: Array<number>, creator_id: number, onSelf: boolean = false): boolean {
87 // You can do moderator actions only on the mods added after you.
89 let yourIndex = modIds.findIndex(id => id == user.id);
90 if (yourIndex == -1) {
93 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
94 modIds = modIds.slice(0, yourIndex+(onSelf ? 0 : 1));
95 return !modIds.includes(creator_id);
102 export function isMod(modIds: Array<number>, creator_id: number): boolean {
103 return modIds.includes(creator_id);
107 var imageRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))`);
108 var videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
110 export function isImage(url: string) {
111 return imageRegex.test(url);
114 export function isVideo(url: string) {
115 return videoRegex.test(url);
118 export function validURL(str: string) {
119 var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol
120 '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name
121 '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address
122 '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path
123 '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string
124 '(\\#[-a-z\\d_]*)?$','i'); // fragment locator
125 return !!pattern.test(str);
128 export function capitalizeFirstLetter(str: string): string {
129 return str.charAt(0).toUpperCase() + str.slice(1);
133 export function routeSortTypeToEnum(sort: string): SortType {
136 } else if (sort == 'hot') {
138 } else if (sort == 'topday') {
139 return SortType.TopDay;
140 } else if (sort == 'topweek') {
141 return SortType.TopWeek;
142 } else if (sort == 'topmonth') {
143 return SortType.TopMonth;
144 } else if (sort == 'topall') {
145 return SortType.TopAll;
149 export function routeListingTypeToEnum(type: string): ListingType {
150 return ListingType[capitalizeFirstLetter(type)];
153 export function routeSearchTypeToEnum(type: string): SearchType {
154 return SearchType[capitalizeFirstLetter(type)];
157 export async function getPageTitle(url: string) {
158 let res = await fetch(`https://textance.herokuapp.com/title/${url}`);
159 let data = await res.text();
163 export function debounce(func: any, wait: number = 500, immediate: boolean = false) {
164 // 'private' variable for instance
165 // The returned function will be able to reference this due to closure.
166 // Each call to the returned function will share this common timer.
169 // Calling debounce returns a new anonymous function
171 // reference the context and args for the setTimeout function
175 // Should the function be called now? If immediate is true
176 // and not already in a timeout then the answer is: Yes
177 var callNow = immediate && !timeout;
179 // This is the basic debounce behaviour where you can call this
180 // function several times, but it will only execute once
181 // [before or after imposing a delay].
182 // Each time the returned function is called, the timer starts over.
183 clearTimeout(timeout);
185 // Set the new timeout
186 timeout = setTimeout(function() {
188 // Inside the timeout function, clear the timeout variable
189 // which will let the next execution run when in 'immediate' mode
192 // Check if the function already ran with the immediate flag
194 // Call the original function with apply
195 // apply lets you define the 'this' object as well as the arguments
196 // (both captured before setTimeout)
197 func.apply(context, args);
201 // Immediate mode and no wait timer? Execute the function..
202 if (callNow) func.apply(context, args);
206 export function getLanguage(): string {
207 return (navigator.language || navigator.userLanguage);
210 export function objectFlip(obj: any) {
212 Object.keys(obj).forEach((key) => {
218 export function getMomentLanguage(): string {
219 let lang = getLanguage();
220 if (lang.startsWith('zh')) {
222 } else if (lang.startsWith('sv')) {
224 } else if (lang.startsWith('fr')) {
226 } else if (lang.startsWith('de')) {
228 } else if (lang.startsWith('ru')) {
230 } else if (lang.startsWith('es')) {
232 } else if (lang.startsWith('eo')) {
234 } else if (lang.startsWith('nl')) {
242 export const themes = ['litera', 'minty', 'solar', 'united', 'cyborg','darkly', 'journal', 'sketchy'];
244 export function setTheme(theme: string = 'darkly') {
245 for (var i=0; i < themes.length; i++) {
247 let styleSheet = document.getElementById(themes[i]);
248 if (themes[i] == theme) {
249 styleSheet.removeAttribute("disabled");
251 styleSheet.setAttribute("disabled", "disabled");