1 import 'moment/locale/es';
2 import 'moment/locale/el';
3 import 'moment/locale/eu';
4 import 'moment/locale/eo';
5 import 'moment/locale/de';
6 import 'moment/locale/zh-cn';
7 import 'moment/locale/fr';
8 import 'moment/locale/sv';
9 import 'moment/locale/ru';
10 import 'moment/locale/nl';
11 import 'moment/locale/it';
12 import 'moment/locale/fi';
13 import 'moment/locale/ca';
14 import 'moment/locale/fa';
15 import 'moment/locale/pt-br';
16 import 'moment/locale/ja';
17 import 'moment/locale/ka';
18 import 'moment/locale/hi';
23 CommentNode as CommentNodeI,
33 WebSocketJsonResponse,
38 } from './interfaces';
39 import { UserService, WebSocketService } from './services';
41 import Tribute from 'tributejs/src/Tribute.js';
42 import markdown_it from 'markdown-it';
43 import markdownitEmoji from 'markdown-it-emoji/light';
44 import markdown_it_container from 'markdown-it-container';
45 import twemoji from 'twemoji';
46 import emojiShortName from 'emoji-short-name';
47 import Toastify from 'toastify-js';
48 import tippy from 'tippy.js';
49 import EmojiButton from '@joeattardi/emoji-button';
51 export const repoUrl = 'https://github.com/LemmyNet/lemmy';
52 export const helpGuideUrl = '/docs/about_guide.html';
53 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
54 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
55 export const archiveUrl = 'https://archive.is';
57 export const postRefetchSeconds: number = 60 * 1000;
58 export const fetchLimit: number = 20;
59 export const mentionDropdownFetchLimit = 10;
61 export const languages = [
62 { code: 'ca', name: 'Català' },
63 { code: 'en', name: 'English' },
64 { code: 'el', name: 'Ελληνικά' },
65 { code: 'eu', name: 'Euskara' },
66 { code: 'eo', name: 'Esperanto' },
67 { code: 'es', name: 'Español' },
68 { code: 'de', name: 'Deutsch' },
69 { code: 'ka', name: 'ქართული ენა' },
70 { code: 'hi', name: 'मानक हिन्दी' },
71 { code: 'fa', name: 'فارسی' },
72 { code: 'ja', name: '日本語' },
73 { code: 'pt_BR', name: 'Português Brasileiro' },
74 { code: 'zh', name: '中文' },
75 { code: 'fi', name: 'Suomi' },
76 { code: 'fr', name: 'Français' },
77 { code: 'sv', name: 'Svenska' },
78 { code: 'ru', name: 'Русский' },
79 { code: 'nl', name: 'Nederlands' },
80 { code: 'it', name: 'Italiano' },
83 export const themes = [
98 export const emojiPicker = new EmojiButton({
99 // Use the emojiShortName from native
102 position: 'auto-start',
106 export function randomStr() {
109 .replace(/[^a-z]+/g, '')
113 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
114 let opStr: string = msg.op;
116 op: UserOperation[opStr],
121 export const md = new markdown_it({
126 .use(markdown_it_container, 'spoiler', {
127 validate: function (params: any) {
128 return params.trim().match(/^spoiler\s+(.*)$/);
131 render: function (tokens: any, idx: any) {
132 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
134 if (tokens[idx].nesting === 1) {
136 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
139 return '</details>\n';
143 .use(markdownitEmoji, {
144 defs: objectFlip(emojiShortName),
147 md.renderer.rules.emoji = function (token, idx) {
148 return twemoji.parse(token[idx].content);
151 export function hotRankComment(comment: Comment): number {
152 return hotRank(comment.score, comment.published);
155 export function hotRankPost(post: Post): number {
156 return hotRank(post.score, post.newest_activity_time);
159 export function hotRank(score: number, timeStr: string): number {
160 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
161 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
162 let now: Date = new Date();
163 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
166 (10000 * Math.log10(Math.max(1, 3 + score))) /
167 Math.pow(hoursElapsed + 2, 1.8);
169 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
174 export function mdToHtml(text: string) {
175 return { __html: md.render(text) };
178 export function getUnixTime(text: string): number {
179 return text ? new Date(text).getTime() / 1000 : undefined;
182 export function addTypeInfo<T>(
185 ): Array<{ type_: string; data: T }> {
186 return arr.map(e => {
187 return { type_: name, data: e };
191 export function canMod(
193 modIds: Array<number>,
195 onSelf: boolean = false
197 // You can do moderator actions only on the mods added after you.
199 let yourIndex = modIds.findIndex(id => id == user.id);
200 if (yourIndex == -1) {
203 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
204 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
205 return !modIds.includes(creator_id);
212 export function isMod(modIds: Array<number>, creator_id: number): boolean {
213 return modIds.includes(creator_id);
216 const imageRegex = new RegExp(
217 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
219 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
221 export function isImage(url: string) {
222 return imageRegex.test(url);
225 export function isVideo(url: string) {
226 return videoRegex.test(url);
229 export function validURL(str: string) {
231 return !!new URL(str);
237 export function validEmail(email: string) {
238 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
239 return re.test(String(email).toLowerCase());
242 export function capitalizeFirstLetter(str: string): string {
243 return str.charAt(0).toUpperCase() + str.slice(1);
246 export function routeSortTypeToEnum(sort: string): SortType {
249 } else if (sort == 'hot') {
251 } else if (sort == 'topday') {
252 return SortType.TopDay;
253 } else if (sort == 'topweek') {
254 return SortType.TopWeek;
255 } else if (sort == 'topmonth') {
256 return SortType.TopMonth;
257 } else if (sort == 'topyear') {
258 return SortType.TopYear;
259 } else if (sort == 'topall') {
260 return SortType.TopAll;
264 export function routeListingTypeToEnum(type: string): ListingType {
265 return ListingType[capitalizeFirstLetter(type)];
268 export function routeDataTypeToEnum(type: string): DataType {
269 return DataType[capitalizeFirstLetter(type)];
272 export function routeSearchTypeToEnum(type: string): SearchType {
273 return SearchType[capitalizeFirstLetter(type)];
276 export async function getPageTitle(url: string) {
277 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
278 let title = await res.title;
282 export function debounce(
285 immediate: boolean = false
287 // 'private' variable for instance
288 // The returned function will be able to reference this due to closure.
289 // Each call to the returned function will share this common timer.
292 // Calling debounce returns a new anonymous function
294 // reference the context and args for the setTimeout function
298 // Should the function be called now? If immediate is true
299 // and not already in a timeout then the answer is: Yes
300 var callNow = immediate && !timeout;
302 // This is the basic debounce behaviour where you can call this
303 // function several times, but it will only execute once
304 // [before or after imposing a delay].
305 // Each time the returned function is called, the timer starts over.
306 clearTimeout(timeout);
308 // Set the new timeout
309 timeout = setTimeout(function () {
310 // Inside the timeout function, clear the timeout variable
311 // which will let the next execution run when in 'immediate' mode
314 // Check if the function already ran with the immediate flag
316 // Call the original function with apply
317 // apply lets you define the 'this' object as well as the arguments
318 // (both captured before setTimeout)
319 func.apply(context, args);
323 // Immediate mode and no wait timer? Execute the function..
324 if (callNow) func.apply(context, args);
328 export function getLanguage(): string {
329 let user = UserService.Instance.user;
330 let lang = user && user.lang ? user.lang : 'browser';
332 if (lang == 'browser') {
333 return getBrowserLanguage();
339 export function getBrowserLanguage(): string {
340 return navigator.language;
343 export function getMomentLanguage(): string {
344 let lang = getLanguage();
345 if (lang.startsWith('zh')) {
347 } else if (lang.startsWith('sv')) {
349 } else if (lang.startsWith('fr')) {
351 } else if (lang.startsWith('de')) {
353 } else if (lang.startsWith('ru')) {
355 } else if (lang.startsWith('es')) {
357 } else if (lang.startsWith('eo')) {
359 } else if (lang.startsWith('nl')) {
361 } else if (lang.startsWith('it')) {
363 } else if (lang.startsWith('fi')) {
365 } else if (lang.startsWith('ca')) {
367 } else if (lang.startsWith('fa')) {
369 } else if (lang.startsWith('pt')) {
371 } else if (lang.startsWith('ja')) {
373 } else if (lang.startsWith('ka')) {
375 } else if (lang.startsWith('hi')) {
377 } else if (lang.startsWith('el')) {
379 } else if (lang.startsWith('eu')) {
387 export function setTheme(theme: string = 'darkly') {
388 // unload all the other themes
389 for (var i = 0; i < themes.length; i++) {
390 let styleSheet = document.getElementById(themes[i]);
392 styleSheet.setAttribute('disabled', 'disabled');
396 // Load the theme dynamically
397 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
398 loadCss(theme, cssLoc);
399 document.getElementById(theme).removeAttribute('disabled');
402 export function loadCss(id: string, loc: string) {
403 if (!document.getElementById(id)) {
404 var head = document.getElementsByTagName('head')[0];
405 var link = document.createElement('link');
407 link.rel = 'stylesheet';
408 link.type = 'text/css';
411 head.appendChild(link);
415 export function objectFlip(obj: any) {
417 Object.keys(obj).forEach(key => {
423 export function pictshareAvatarThumbnail(src: string): string {
424 // sample url: http://localhost:8535/pictshare/gs7xuu.jpg
425 let split = src.split('pictshare');
426 let out = `${split[0]}pictshare/webp/96${split[1]}`;
430 export function showAvatars(): boolean {
432 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
433 !UserService.Instance.user
437 // Converts to image thumbnail
438 export function pictshareImage(
440 thumbnail: boolean = false
442 let root = `/pictshare`;
444 // Necessary for other servers / domains
445 if (hash.includes('pictshare')) {
446 let split = hash.split('/pictshare/');
447 root = `${split[0]}/pictshare`;
451 let out = `${root}/webp/${thumbnail ? '192/' : ''}${hash}`;
455 export function isCommentType(item: Comment | PrivateMessage): item is Comment {
456 return (item as Comment).community_id !== undefined;
459 export function toast(text: string, background: string = 'success') {
460 let backgroundColor = `var(--${background})`;
463 backgroundColor: backgroundColor,
469 export function messageToastify(
476 let backgroundColor = `var(--light)`;
478 let toast = Toastify({
479 text: `${body}<br />${creator}`,
481 backgroundColor: backgroundColor,
482 className: 'text-body',
490 router.history.push(link);
496 export function setupTribute(): Tribute {
502 menuItemTemplate: (item: any) => {
503 let shortName = `:${item.original.key}:`;
504 let twemojiIcon = twemoji.parse(item.original.val);
505 return `${twemojiIcon} ${shortName}`;
507 selectTemplate: (item: any) => {
508 return `:${item.original.key}:`;
510 values: Object.entries(emojiShortName).map(e => {
511 return { key: e[1], val: e[0] };
514 autocompleteMode: true,
515 menuItemLimit: mentionDropdownFetchLimit,
516 menuShowMinLength: 2,
521 selectTemplate: (item: any) => {
522 return `[/u/${item.original.key}](/u/${item.original.key})`;
524 values: (text: string, cb: any) => {
525 userSearch(text, (users: any) => cb(users));
528 autocompleteMode: true,
529 menuItemLimit: mentionDropdownFetchLimit,
530 menuShowMinLength: 2,
536 selectTemplate: (item: any) => {
537 return `[/c/${item.original.key}](/c/${item.original.key})`;
539 values: (text: string, cb: any) => {
540 communitySearch(text, (communities: any) => cb(communities));
543 autocompleteMode: true,
544 menuItemLimit: mentionDropdownFetchLimit,
545 menuShowMinLength: 2,
551 let tippyInstance = tippy('[data-tippy-content]');
553 export function setupTippy() {
554 tippyInstance.forEach(e => e.destroy());
555 tippyInstance = tippy('[data-tippy-content]', {
557 // Display on "long press"
558 touch: ['hold', 500],
562 function userSearch(text: string, cb: any) {
564 let form: SearchForm = {
566 type_: SearchType[SearchType.Users],
567 sort: SortType[SortType.TopAll],
569 limit: mentionDropdownFetchLimit,
572 WebSocketService.Instance.search(form);
574 this.userSub = WebSocketService.Instance.subject.subscribe(
576 let res = wsJsonToRes(msg);
577 if (res.op == UserOperation.Search) {
578 let data = res.data as SearchResponse;
579 let users = data.users.map(u => {
580 return { key: u.name };
583 this.userSub.unsubscribe();
586 err => console.error(err),
587 () => console.log('complete')
594 function communitySearch(text: string, cb: any) {
596 let form: SearchForm = {
598 type_: SearchType[SearchType.Communities],
599 sort: SortType[SortType.TopAll],
601 limit: mentionDropdownFetchLimit,
604 WebSocketService.Instance.search(form);
606 this.communitySub = WebSocketService.Instance.subject.subscribe(
608 let res = wsJsonToRes(msg);
609 if (res.op == UserOperation.Search) {
610 let data = res.data as SearchResponse;
611 let communities = data.communities.map(u => {
612 return { key: u.name };
615 this.communitySub.unsubscribe();
618 err => console.error(err),
619 () => console.log('complete')
626 export function getListingTypeFromProps(props: any): ListingType {
627 return props.match.params.listing_type
628 ? routeListingTypeToEnum(props.match.params.listing_type)
629 : UserService.Instance.user
630 ? UserService.Instance.user.default_listing_type
634 // TODO might need to add a user setting for this too
635 export function getDataTypeFromProps(props: any): DataType {
636 return props.match.params.data_type
637 ? routeDataTypeToEnum(props.match.params.data_type)
641 export function getSortTypeFromProps(props: any): SortType {
642 return props.match.params.sort
643 ? routeSortTypeToEnum(props.match.params.sort)
644 : UserService.Instance.user
645 ? UserService.Instance.user.default_sort_type
649 export function getPageFromProps(props: any): number {
650 return props.match.params.page ? Number(props.match.params.page) : 1;
653 export function editCommentRes(
654 data: CommentResponse,
655 comments: Array<Comment>
657 let found = comments.find(c => c.id == data.comment.id);
659 found.content = data.comment.content;
660 found.updated = data.comment.updated;
661 found.removed = data.comment.removed;
662 found.deleted = data.comment.deleted;
663 found.upvotes = data.comment.upvotes;
664 found.downvotes = data.comment.downvotes;
665 found.score = data.comment.score;
669 export function saveCommentRes(
670 data: CommentResponse,
671 comments: Array<Comment>
673 let found = comments.find(c => c.id == data.comment.id);
675 found.saved = data.comment.saved;
679 export function createCommentLikeRes(
680 data: CommentResponse,
681 comments: Array<Comment>
683 let found: Comment = comments.find(c => c.id === data.comment.id);
685 found.score = data.comment.score;
686 found.upvotes = data.comment.upvotes;
687 found.downvotes = data.comment.downvotes;
688 if (data.comment.my_vote !== null) {
689 found.my_vote = data.comment.my_vote;
694 export function createPostLikeFindRes(data: PostResponse, posts: Array<Post>) {
695 let found = posts.find(c => c.id == data.post.id);
697 createPostLikeRes(data, found);
701 export function createPostLikeRes(data: PostResponse, post: Post) {
703 post.score = data.post.score;
704 post.upvotes = data.post.upvotes;
705 post.downvotes = data.post.downvotes;
706 if (data.post.my_vote !== null) {
707 post.my_vote = data.post.my_vote;
712 export function editPostFindRes(data: PostResponse, posts: Array<Post>) {
713 let found = posts.find(c => c.id == data.post.id);
715 editPostRes(data, found);
719 export function editPostRes(data: PostResponse, post: Post) {
721 post.url = data.post.url;
722 post.name = data.post.name;
723 post.nsfw = data.post.nsfw;
727 export function commentsToFlatNodes(
728 comments: Array<Comment>
729 ): Array<CommentNodeI> {
730 let nodes: Array<CommentNodeI> = [];
731 for (let comment of comments) {
732 nodes.push({ comment: comment });
737 export function commentSort(tree: Array<CommentNodeI>, sort: CommentSortType) {
738 // First, put removed and deleted comments at the bottom, then do your other sorts
739 if (sort == CommentSortType.Top) {
742 +a.comment.removed - +b.comment.removed ||
743 +a.comment.deleted - +b.comment.deleted ||
744 b.comment.score - a.comment.score
746 } else if (sort == CommentSortType.New) {
749 +a.comment.removed - +b.comment.removed ||
750 +a.comment.deleted - +b.comment.deleted ||
751 b.comment.published.localeCompare(a.comment.published)
753 } else if (sort == CommentSortType.Old) {
756 +a.comment.removed - +b.comment.removed ||
757 +a.comment.deleted - +b.comment.deleted ||
758 a.comment.published.localeCompare(b.comment.published)
760 } else if (sort == CommentSortType.Hot) {
763 +a.comment.removed - +b.comment.removed ||
764 +a.comment.deleted - +b.comment.deleted ||
765 hotRankComment(b.comment) - hotRankComment(a.comment)
769 // Go through the children recursively
770 for (let node of tree) {
772 commentSort(node.children, sort);
777 export function commentSortSortType(tree: Array<CommentNodeI>, sort: SortType) {
778 commentSort(tree, convertCommentSortType(sort));
781 function convertCommentSortType(sort: SortType): CommentSortType {
783 sort == SortType.TopAll ||
784 sort == SortType.TopDay ||
785 sort == SortType.TopWeek ||
786 sort == SortType.TopMonth ||
787 sort == SortType.TopYear
789 return CommentSortType.Top;
790 } else if (sort == SortType.New) {
791 return CommentSortType.New;
792 } else if (sort == SortType.Hot) {
793 return CommentSortType.Hot;
795 return CommentSortType.Hot;
799 export function postSort(
802 communityType: boolean
804 // First, put removed and deleted comments at the bottom, then do your other sorts
806 sort == SortType.TopAll ||
807 sort == SortType.TopDay ||
808 sort == SortType.TopWeek ||
809 sort == SortType.TopMonth ||
810 sort == SortType.TopYear
814 +a.removed - +b.removed ||
815 +a.deleted - +b.deleted ||
816 (communityType && +b.stickied - +a.stickied) ||
819 } else if (sort == SortType.New) {
822 +a.removed - +b.removed ||
823 +a.deleted - +b.deleted ||
824 (communityType && +b.stickied - +a.stickied) ||
825 b.published.localeCompare(a.published)
827 } else if (sort == SortType.Hot) {
830 +a.removed - +b.removed ||
831 +a.deleted - +b.deleted ||
832 (communityType && +b.stickied - +a.stickied) ||
833 hotRankPost(b) - hotRankPost(a)
838 export const colorList: Array<string> = [
847 function hsl(num: number) {
848 return `hsla(${num}, 35%, 50%, 1)`;
851 function randomHsl() {
852 return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
855 export function previewLines(text: string, lines: number = 3): string {
856 // Use lines * 2 because markdown requires 2 lines