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';
21 WebSocketJsonResponse,
24 } from './interfaces';
25 import { UserService, WebSocketService } from './services';
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';
35 export const repoUrl = 'https://github.com/dessalines/lemmy';
36 export const markdownHelpUrl = 'https://commonmark.org/help/';
37 export const archiveUrl = 'https://archive.is';
39 export const postRefetchSeconds: number = 60 * 1000;
40 export const fetchLimit: number = 20;
41 export const mentionDropdownFetchLimit = 10;
43 export function randomStr() {
46 .replace(/[^a-z]+/g, '')
50 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
51 let opStr: string = msg.op;
53 op: UserOperation[opStr],
58 export const md = new markdown_it({
63 .use(markdown_it_container, 'spoiler', {
64 validate: function(params: any) {
65 return params.trim().match(/^spoiler\s+(.*)$/);
68 render: function(tokens: any, idx: any) {
69 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
71 if (tokens[idx].nesting === 1) {
73 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
76 return '</details>\n';
80 .use(markdownitEmoji, {
81 defs: objectFlip(emojiShortName),
84 md.renderer.rules.emoji = function(token, idx) {
85 return twemoji.parse(token[idx].content);
88 export function hotRank(comment: Comment): number {
89 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
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;
96 (10000 * Math.log10(Math.max(1, 3 + comment.score))) /
97 Math.pow(hoursElapsed + 2, 1.8);
99 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
104 export function mdToHtml(text: string) {
105 return { __html: md.render(text) };
108 export function getUnixTime(text: string): number {
109 return text ? new Date(text).getTime() / 1000 : undefined;
112 export function addTypeInfo<T>(
115 ): Array<{ type_: string; data: T }> {
116 return arr.map(e => {
117 return { type_: name, data: e };
121 export function canMod(
123 modIds: Array<number>,
125 onSelf: boolean = false
127 // You can do moderator actions only on the mods added after you.
129 let yourIndex = modIds.findIndex(id => id == user.id);
130 if (yourIndex == -1) {
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);
142 export function isMod(modIds: Array<number>, creator_id: number): boolean {
143 return modIds.includes(creator_id);
146 var imageRegex = new RegExp(
147 `(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))`
149 var videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
151 export function isImage(url: string) {
152 return imageRegex.test(url);
155 export function isVideo(url: string) {
156 return videoRegex.test(url);
159 export function validURL(str: string) {
161 return !!new URL(str);
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());
172 export function capitalizeFirstLetter(str: string): string {
173 return str.charAt(0).toUpperCase() + str.slice(1);
176 export function routeSortTypeToEnum(sort: string): SortType {
179 } else if (sort == '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;
194 export function routeListingTypeToEnum(type: string): ListingType {
195 return ListingType[capitalizeFirstLetter(type)];
198 export function routeSearchTypeToEnum(type: string): SearchType {
199 return SearchType[capitalizeFirstLetter(type)];
202 export async function getPageTitle(url: string) {
203 let res = await fetch(`https://textance.herokuapp.com/title/${url}`);
204 let data = await res.text();
208 export function debounce(
211 immediate: boolean = false
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.
218 // Calling debounce returns a new anonymous function
220 // reference the context and args for the setTimeout function
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;
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);
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
240 // Check if the function already ran with the immediate flag
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);
249 // Immediate mode and no wait timer? Execute the function..
250 if (callNow) func.apply(context, args);
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' },
268 export function getLanguage(): string {
269 let user = UserService.Instance.user;
270 let lang = user && user.lang ? user.lang : 'browser';
272 if (lang == 'browser') {
273 return getBrowserLanguage();
279 export function getBrowserLanguage(): string {
280 return navigator.language;
283 export function getMomentLanguage(): string {
284 let lang = getLanguage();
285 if (lang.startsWith('zh')) {
287 } else if (lang.startsWith('sv')) {
289 } else if (lang.startsWith('fr')) {
291 } else if (lang.startsWith('de')) {
293 } else if (lang.startsWith('ru')) {
295 } else if (lang.startsWith('es')) {
297 } else if (lang.startsWith('eo')) {
299 } else if (lang.startsWith('nl')) {
301 } else if (lang.startsWith('it')) {
303 } else if (lang.startsWith('fi')) {
311 export const themes = [
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]);
329 styleSheet.setAttribute('disabled', 'disabled');
333 // Load the theme dynamically
334 if (!document.getElementById(theme)) {
335 var head = document.getElementsByTagName('head')[0];
336 var link = document.createElement('link');
338 link.rel = 'stylesheet';
339 link.type = 'text/css';
340 link.href = `/static/assets/css/themes/${theme}.min.css`;
342 head.appendChild(link);
344 document.getElementById(theme).removeAttribute('disabled');
347 export function objectFlip(obj: any) {
349 Object.keys(obj).forEach(key => {
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]}`;
362 export function showAvatars(): boolean {
364 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
365 !UserService.Instance.user
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]}`;
380 export function isCommentType(item: Comment | PrivateMessage): item is Comment {
381 return (item as Comment).community_id !== undefined;
384 export function toast(text: string, background: string = 'success') {
385 let backgroundColor = `var(--${background})`;
388 backgroundColor: backgroundColor,
394 export function setupTribute(): Tribute {
400 menuItemTemplate: (item: any) => {
401 let emoji = `:${item.original.key}:`;
402 return `${item.original.val} ${emoji}`;
404 selectTemplate: (item: any) => {
405 return `:${item.original.key}:`;
407 values: Object.entries(emojiShortName).map(e => {
408 return { key: e[1], val: e[0] };
411 autocompleteMode: true,
412 menuItemLimit: mentionDropdownFetchLimit,
417 selectTemplate: (item: any) => {
418 return `[/u/${item.original.key}](/u/${item.original.key})`;
420 values: (text: string, cb: any) => {
421 userSearch(text, (users: any) => cb(users));
424 autocompleteMode: true,
425 menuItemLimit: mentionDropdownFetchLimit,
431 selectTemplate: (item: any) => {
432 return `[/c/${item.original.key}](/c/${item.original.key})`;
434 values: (text: string, cb: any) => {
435 communitySearch(text, (communities: any) => cb(communities));
438 autocompleteMode: true,
439 menuItemLimit: mentionDropdownFetchLimit,
445 function userSearch(text: string, cb: any) {
447 let form: SearchForm = {
449 type_: SearchType[SearchType.Users],
450 sort: SortType[SortType.TopAll],
452 limit: mentionDropdownFetchLimit,
455 WebSocketService.Instance.search(form);
457 this.userSub = WebSocketService.Instance.subject.subscribe(
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 };
466 this.userSub.unsubscribe();
469 err => console.error(err),
470 () => console.log('complete')
477 function communitySearch(text: string, cb: any) {
479 let form: SearchForm = {
481 type_: SearchType[SearchType.Communities],
482 sort: SortType[SortType.TopAll],
484 limit: mentionDropdownFetchLimit,
487 WebSocketService.Instance.search(form);
489 this.communitySub = WebSocketService.Instance.subject.subscribe(
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 };
498 this.communitySub.unsubscribe();
501 err => console.error(err),
502 () => console.log('complete')