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 import 'moment/locale/ca';
12 import 'moment/locale/fa';
13 import 'moment/locale/pt-br';
14 import 'moment/locale/ja';
15 import 'moment/locale/ka';
20 CommentNode as CommentNodeI,
30 WebSocketJsonResponse,
35 } from './interfaces';
36 import { UserService, WebSocketService } from './services';
38 import Tribute from 'tributejs/src/Tribute.js';
39 import markdown_it from 'markdown-it';
40 import markdownitEmoji from 'markdown-it-emoji/light';
41 import markdown_it_container from 'markdown-it-container';
42 import twemoji from 'twemoji';
43 import emojiShortName from 'emoji-short-name';
44 import Toastify from 'toastify-js';
45 import tippy from 'tippy.js';
47 export const repoUrl = 'https://github.com/dessalines/lemmy';
48 export const helpGuideUrl = '/docs/about_guide.html';
49 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
50 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
51 export const archiveUrl = 'https://archive.is';
53 export const postRefetchSeconds: number = 60 * 1000;
54 export const fetchLimit: number = 20;
55 export const mentionDropdownFetchLimit = 10;
57 export const languages = [
58 { code: 'ca', name: 'Català' },
59 { code: 'en', name: 'English' },
60 { code: 'eo', name: 'Esperanto' },
61 { code: 'es', name: 'Español' },
62 { code: 'de', name: 'Deutsch' },
63 { code: 'ka', name: 'ქართული ენა' },
64 { code: 'fa', name: 'فارسی' },
65 { code: 'ja', name: '日本語' },
66 { code: 'pt_BR', name: 'Português Brasileiro' },
67 { code: 'zh', name: '中文' },
68 { code: 'fi', name: 'Suomi' },
69 { code: 'fr', name: 'Français' },
70 { code: 'sv', name: 'Svenska' },
71 { code: 'ru', name: 'Русский' },
72 { code: 'nl', name: 'Nederlands' },
73 { code: 'it', name: 'Italiano' },
76 export const themes = [
91 export function randomStr() {
94 .replace(/[^a-z]+/g, '')
98 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
99 let opStr: string = msg.op;
101 op: UserOperation[opStr],
106 export const md = new markdown_it({
111 .use(markdown_it_container, 'spoiler', {
112 validate: function(params: any) {
113 return params.trim().match(/^spoiler\s+(.*)$/);
116 render: function(tokens: any, idx: any) {
117 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
119 if (tokens[idx].nesting === 1) {
121 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
124 return '</details>\n';
128 .use(markdownitEmoji, {
129 defs: objectFlip(emojiShortName),
132 md.renderer.rules.emoji = function(token, idx) {
133 return twemoji.parse(token[idx].content);
136 export function hotRankComment(comment: Comment): number {
137 return hotRank(comment.score, comment.published);
140 export function hotRankPost(post: Post): number {
141 return hotRank(post.score, post.newest_activity_time);
144 export function hotRank(score: number, timeStr: string): number {
145 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
146 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
147 let now: Date = new Date();
148 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
151 (10000 * Math.log10(Math.max(1, 3 + score))) /
152 Math.pow(hoursElapsed + 2, 1.8);
154 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
159 export function mdToHtml(text: string) {
160 return { __html: md.render(text) };
163 export function getUnixTime(text: string): number {
164 return text ? new Date(text).getTime() / 1000 : undefined;
167 export function addTypeInfo<T>(
170 ): Array<{ type_: string; data: T }> {
171 return arr.map(e => {
172 return { type_: name, data: e };
176 export function canMod(
178 modIds: Array<number>,
180 onSelf: boolean = false
182 // You can do moderator actions only on the mods added after you.
184 let yourIndex = modIds.findIndex(id => id == user.id);
185 if (yourIndex == -1) {
188 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
189 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
190 return !modIds.includes(creator_id);
197 export function isMod(modIds: Array<number>, creator_id: number): boolean {
198 return modIds.includes(creator_id);
201 const imageRegex = new RegExp(
202 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg))/
204 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
206 export function isImage(url: string) {
207 return imageRegex.test(url);
210 export function isVideo(url: string) {
211 return videoRegex.test(url);
214 export function validURL(str: string) {
216 return !!new URL(str);
222 export function validEmail(email: string) {
223 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
224 return re.test(String(email).toLowerCase());
227 export function capitalizeFirstLetter(str: string): string {
228 return str.charAt(0).toUpperCase() + str.slice(1);
231 export function routeSortTypeToEnum(sort: string): SortType {
234 } else if (sort == 'hot') {
236 } else if (sort == 'topday') {
237 return SortType.TopDay;
238 } else if (sort == 'topweek') {
239 return SortType.TopWeek;
240 } else if (sort == 'topmonth') {
241 return SortType.TopMonth;
242 } else if (sort == 'topyear') {
243 return SortType.TopYear;
244 } else if (sort == 'topall') {
245 return SortType.TopAll;
249 export function routeListingTypeToEnum(type: string): ListingType {
250 return ListingType[capitalizeFirstLetter(type)];
253 export function routeDataTypeToEnum(type: string): DataType {
254 return DataType[capitalizeFirstLetter(type)];
257 export function routeSearchTypeToEnum(type: string): SearchType {
258 return SearchType[capitalizeFirstLetter(type)];
261 export async function getPageTitle(url: string) {
262 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
263 let title = await res.title;
267 export function debounce(
270 immediate: boolean = false
272 // 'private' variable for instance
273 // The returned function will be able to reference this due to closure.
274 // Each call to the returned function will share this common timer.
277 // Calling debounce returns a new anonymous function
279 // reference the context and args for the setTimeout function
283 // Should the function be called now? If immediate is true
284 // and not already in a timeout then the answer is: Yes
285 var callNow = immediate && !timeout;
287 // This is the basic debounce behaviour where you can call this
288 // function several times, but it will only execute once
289 // [before or after imposing a delay].
290 // Each time the returned function is called, the timer starts over.
291 clearTimeout(timeout);
293 // Set the new timeout
294 timeout = setTimeout(function() {
295 // Inside the timeout function, clear the timeout variable
296 // which will let the next execution run when in 'immediate' mode
299 // Check if the function already ran with the immediate flag
301 // Call the original function with apply
302 // apply lets you define the 'this' object as well as the arguments
303 // (both captured before setTimeout)
304 func.apply(context, args);
308 // Immediate mode and no wait timer? Execute the function..
309 if (callNow) func.apply(context, args);
313 export function getLanguage(): string {
314 let user = UserService.Instance.user;
315 let lang = user && user.lang ? user.lang : 'browser';
317 if (lang == 'browser') {
318 return getBrowserLanguage();
324 export function getBrowserLanguage(): string {
325 return navigator.language;
328 export function getMomentLanguage(): string {
329 let lang = getLanguage();
330 if (lang.startsWith('zh')) {
332 } else if (lang.startsWith('sv')) {
334 } else if (lang.startsWith('fr')) {
336 } else if (lang.startsWith('de')) {
338 } else if (lang.startsWith('ru')) {
340 } else if (lang.startsWith('es')) {
342 } else if (lang.startsWith('eo')) {
344 } else if (lang.startsWith('nl')) {
346 } else if (lang.startsWith('it')) {
348 } else if (lang.startsWith('fi')) {
350 } else if (lang.startsWith('ca')) {
352 } else if (lang.startsWith('fa')) {
354 } else if (lang.startsWith('pt')) {
356 } else if (lang.startsWith('ja')) {
358 } else if (lang.startsWith('ka')) {
366 export function setTheme(theme: string = 'darkly') {
367 // unload all the other themes
368 for (var i = 0; i < themes.length; i++) {
369 let styleSheet = document.getElementById(themes[i]);
371 styleSheet.setAttribute('disabled', 'disabled');
375 // Load the theme dynamically
376 if (!document.getElementById(theme)) {
377 var head = document.getElementsByTagName('head')[0];
378 var link = document.createElement('link');
380 link.rel = 'stylesheet';
381 link.type = 'text/css';
382 link.href = `/static/assets/css/themes/${theme}.min.css`;
384 head.appendChild(link);
386 document.getElementById(theme).removeAttribute('disabled');
389 export function objectFlip(obj: any) {
391 Object.keys(obj).forEach(key => {
397 export function pictshareAvatarThumbnail(src: string): string {
398 // sample url: http://localhost:8535/pictshare/gs7xuu.jpg
399 let split = src.split('pictshare');
400 let out = `${split[0]}pictshare/96${split[1]}`;
404 export function showAvatars(): boolean {
406 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
407 !UserService.Instance.user
411 // Converts to image thumbnail
412 export function pictshareImage(
414 thumbnail: boolean = false
416 let root = `/pictshare`;
418 // Necessary for other servers / domains
419 if (hash.includes('pictshare')) {
420 let split = hash.split('/pictshare/');
421 root = `${split[0]}/pictshare`;
425 let out = `${root}/${thumbnail ? '192/' : ''}${hash}`;
429 export function isCommentType(item: Comment | PrivateMessage): item is Comment {
430 return (item as Comment).community_id !== undefined;
433 export function toast(text: string, background: string = 'success') {
434 let backgroundColor = `var(--${background})`;
437 backgroundColor: backgroundColor,
443 export function messageToastify(
450 let backgroundColor = `var(--light)`;
452 let toast = Toastify({
453 text: `${body}<br />${creator}`,
455 backgroundColor: backgroundColor,
463 router.history.push(link);
469 export function setupTribute(): Tribute {
475 menuItemTemplate: (item: any) => {
476 let emoji = `:${item.original.key}:`;
477 return `${item.original.val} ${emoji}`;
479 selectTemplate: (item: any) => {
480 return `:${item.original.key}:`;
482 values: Object.entries(emojiShortName).map(e => {
483 return { key: e[1], val: e[0] };
486 autocompleteMode: true,
487 menuItemLimit: mentionDropdownFetchLimit,
488 menuShowMinLength: 2,
493 selectTemplate: (item: any) => {
494 return `[/u/${item.original.key}](/u/${item.original.key})`;
496 values: (text: string, cb: any) => {
497 userSearch(text, (users: any) => cb(users));
500 autocompleteMode: true,
501 menuItemLimit: mentionDropdownFetchLimit,
502 menuShowMinLength: 2,
508 selectTemplate: (item: any) => {
509 return `[/c/${item.original.key}](/c/${item.original.key})`;
511 values: (text: string, cb: any) => {
512 communitySearch(text, (communities: any) => cb(communities));
515 autocompleteMode: true,
516 menuItemLimit: mentionDropdownFetchLimit,
517 menuShowMinLength: 2,
523 let tippyInstance = tippy('[data-tippy-content]');
525 export function setupTippy() {
526 tippyInstance.forEach(e => e.destroy());
527 tippyInstance = tippy('[data-tippy-content]', {
529 // Display on "long press"
530 touch: ['hold', 500],
534 function userSearch(text: string, cb: any) {
536 let form: SearchForm = {
538 type_: SearchType[SearchType.Users],
539 sort: SortType[SortType.TopAll],
541 limit: mentionDropdownFetchLimit,
544 WebSocketService.Instance.search(form);
546 this.userSub = WebSocketService.Instance.subject.subscribe(
548 let res = wsJsonToRes(msg);
549 if (res.op == UserOperation.Search) {
550 let data = res.data as SearchResponse;
551 let users = data.users.map(u => {
552 return { key: u.name };
555 this.userSub.unsubscribe();
558 err => console.error(err),
559 () => console.log('complete')
566 function communitySearch(text: string, cb: any) {
568 let form: SearchForm = {
570 type_: SearchType[SearchType.Communities],
571 sort: SortType[SortType.TopAll],
573 limit: mentionDropdownFetchLimit,
576 WebSocketService.Instance.search(form);
578 this.communitySub = WebSocketService.Instance.subject.subscribe(
580 let res = wsJsonToRes(msg);
581 if (res.op == UserOperation.Search) {
582 let data = res.data as SearchResponse;
583 let communities = data.communities.map(u => {
584 return { key: u.name };
587 this.communitySub.unsubscribe();
590 err => console.error(err),
591 () => console.log('complete')
598 export function getListingTypeFromProps(props: any): ListingType {
599 return props.match.params.listing_type
600 ? routeListingTypeToEnum(props.match.params.listing_type)
601 : UserService.Instance.user
602 ? UserService.Instance.user.default_listing_type
606 // TODO might need to add a user setting for this too
607 export function getDataTypeFromProps(props: any): DataType {
608 return props.match.params.data_type
609 ? routeDataTypeToEnum(props.match.params.data_type)
613 export function getSortTypeFromProps(props: any): SortType {
614 return props.match.params.sort
615 ? routeSortTypeToEnum(props.match.params.sort)
616 : UserService.Instance.user
617 ? UserService.Instance.user.default_sort_type
621 export function getPageFromProps(props: any): number {
622 return props.match.params.page ? Number(props.match.params.page) : 1;
625 export function editCommentRes(
626 data: CommentResponse,
627 comments: Array<Comment>
629 let found = comments.find(c => c.id == data.comment.id);
631 found.content = data.comment.content;
632 found.updated = data.comment.updated;
633 found.removed = data.comment.removed;
634 found.deleted = data.comment.deleted;
635 found.upvotes = data.comment.upvotes;
636 found.downvotes = data.comment.downvotes;
637 found.score = data.comment.score;
641 export function saveCommentRes(
642 data: CommentResponse,
643 comments: Array<Comment>
645 let found = comments.find(c => c.id == data.comment.id);
647 found.saved = data.comment.saved;
651 export function createCommentLikeRes(
652 data: CommentResponse,
653 comments: Array<Comment>
655 let found: Comment = comments.find(c => c.id === data.comment.id);
657 found.score = data.comment.score;
658 found.upvotes = data.comment.upvotes;
659 found.downvotes = data.comment.downvotes;
660 if (data.comment.my_vote !== null) {
661 found.my_vote = data.comment.my_vote;
666 export function createPostLikeFindRes(data: PostResponse, posts: Array<Post>) {
667 let found = posts.find(c => c.id == data.post.id);
669 createPostLikeRes(data, found);
673 export function createPostLikeRes(data: PostResponse, post: Post) {
675 post.score = data.post.score;
676 post.upvotes = data.post.upvotes;
677 post.downvotes = data.post.downvotes;
678 if (data.post.my_vote !== null) {
679 post.my_vote = data.post.my_vote;
684 export function editPostFindRes(data: PostResponse, posts: Array<Post>) {
685 let found = posts.find(c => c.id == data.post.id);
687 editPostRes(data, found);
691 export function editPostRes(data: PostResponse, post: Post) {
693 post.url = data.post.url;
694 post.name = data.post.name;
695 post.nsfw = data.post.nsfw;
699 export function commentsToFlatNodes(
700 comments: Array<Comment>
701 ): Array<CommentNodeI> {
702 let nodes: Array<CommentNodeI> = [];
703 for (let comment of comments) {
704 nodes.push({ comment: comment });
709 export function commentSort(tree: Array<CommentNodeI>, sort: CommentSortType) {
710 // First, put removed and deleted comments at the bottom, then do your other sorts
711 if (sort == CommentSortType.Top) {
714 +a.comment.removed - +b.comment.removed ||
715 +a.comment.deleted - +b.comment.deleted ||
716 b.comment.score - a.comment.score
718 } else if (sort == CommentSortType.New) {
721 +a.comment.removed - +b.comment.removed ||
722 +a.comment.deleted - +b.comment.deleted ||
723 b.comment.published.localeCompare(a.comment.published)
725 } else if (sort == CommentSortType.Old) {
728 +a.comment.removed - +b.comment.removed ||
729 +a.comment.deleted - +b.comment.deleted ||
730 a.comment.published.localeCompare(b.comment.published)
732 } else if (sort == CommentSortType.Hot) {
735 +a.comment.removed - +b.comment.removed ||
736 +a.comment.deleted - +b.comment.deleted ||
737 hotRankComment(b.comment) - hotRankComment(a.comment)
741 // Go through the children recursively
742 for (let node of tree) {
744 commentSort(node.children, sort);
749 export function commentSortSortType(tree: Array<CommentNodeI>, sort: SortType) {
750 commentSort(tree, convertCommentSortType(sort));
753 function convertCommentSortType(sort: SortType): CommentSortType {
755 sort == SortType.TopAll ||
756 sort == SortType.TopDay ||
757 sort == SortType.TopWeek ||
758 sort == SortType.TopMonth ||
759 sort == SortType.TopYear
761 return CommentSortType.Top;
762 } else if (sort == SortType.New) {
763 return CommentSortType.New;
764 } else if (sort == SortType.Hot) {
765 return CommentSortType.Hot;
767 return CommentSortType.Hot;
771 export function postSort(
774 communityType: boolean
776 // First, put removed and deleted comments at the bottom, then do your other sorts
778 sort == SortType.TopAll ||
779 sort == SortType.TopDay ||
780 sort == SortType.TopWeek ||
781 sort == SortType.TopMonth ||
782 sort == SortType.TopYear
786 +a.removed - +b.removed ||
787 +a.deleted - +b.deleted ||
788 (communityType && +b.stickied - +a.stickied) ||
791 } else if (sort == SortType.New) {
794 +a.removed - +b.removed ||
795 +a.deleted - +b.deleted ||
796 (communityType && +b.stickied - +a.stickied) ||
797 b.published.localeCompare(a.published)
799 } else if (sort == SortType.Hot) {
802 +a.removed - +b.removed ||
803 +a.deleted - +b.deleted ||
804 (communityType && +b.stickied - +a.stickied) ||
805 hotRankPost(b) - hotRankPost(a)
810 export const colorList: Array<string> = [
819 function hsl(num: number) {
820 return `hsla(${num}, 35%, 50%, 1)`;
823 function randomHsl() {
824 return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;