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/pl';
16 import 'moment/locale/pt-br';
17 import 'moment/locale/ja';
18 import 'moment/locale/ka';
19 import 'moment/locale/hi';
20 import 'moment/locale/gl';
21 import 'moment/locale/tr';
22 import 'moment/locale/hu';
23 import 'moment/locale/uk';
24 import 'moment/locale/sq';
25 import 'moment/locale/km';
26 import 'moment/locale/ga';
27 import 'moment/locale/sr';
32 CommentNode as CommentNodeI,
42 WebSocketJsonResponse,
47 } from './interfaces';
48 import { UserService, WebSocketService } from './services';
50 import Tribute from 'tributejs/src/Tribute.js';
51 import markdown_it from 'markdown-it';
52 import markdownitEmoji from 'markdown-it-emoji/light';
53 import markdown_it_container from 'markdown-it-container';
54 import twemoji from 'twemoji';
55 import emojiShortName from 'emoji-short-name';
56 import Toastify from 'toastify-js';
57 import tippy from 'tippy.js';
58 import EmojiButton from '@joeattardi/emoji-button';
59 import moment from 'moment';
61 export const repoUrl = 'https://github.com/LemmyNet/lemmy';
62 export const helpGuideUrl = '/docs/about_guide.html';
63 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
64 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
65 export const archiveUrl = 'https://archive.is';
67 export const postRefetchSeconds: number = 60 * 1000;
68 export const fetchLimit: number = 20;
69 export const mentionDropdownFetchLimit = 10;
71 export const languages = [
72 { code: 'ca', name: 'Català' },
73 { code: 'en', name: 'English' },
74 { code: 'el', name: 'Ελληνικά' },
75 { code: 'eu', name: 'Euskara' },
76 { code: 'eo', name: 'Esperanto' },
77 { code: 'es', name: 'Español' },
78 { code: 'de', name: 'Deutsch' },
79 { code: 'ga', name: 'Gaeilge' },
80 { code: 'gl', name: 'Galego' },
81 { code: 'hu', name: 'Magyar Nyelv' },
82 { code: 'ka', name: 'ქართული ენა' },
83 { code: 'km', name: 'ភាសាខ្មែរ' },
84 { code: 'hi', name: 'मानक हिन्दी' },
85 { code: 'fa', name: 'فارسی' },
86 { code: 'ja', name: '日本語' },
87 { code: 'pl', name: 'Polski' },
88 { code: 'pt_BR', name: 'Português Brasileiro' },
89 { code: 'zh', name: '中文' },
90 { code: 'fi', name: 'Suomi' },
91 { code: 'fr', name: 'Français' },
92 { code: 'sv', name: 'Svenska' },
93 { code: 'sq', name: 'Shqip' },
94 { code: 'sr_Latn', name: 'srpski' },
95 { code: 'tr', name: 'Türkçe' },
96 { code: 'uk', name: 'Українська Mова' },
97 { code: 'ru', name: 'Русский' },
98 { code: 'nl', name: 'Nederlands' },
99 { code: 'it', name: 'Italiano' },
102 export const themes = [
118 export const emojiPicker = new EmojiButton({
119 // Use the emojiShortName from native
122 position: 'auto-start',
126 const DEFAULT_ALPHABET =
127 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
129 function getRandomCharFromAlphabet(alphabet: string): string {
130 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
133 export function randomStr(
134 idDesiredLength: number = 20,
135 alphabet = DEFAULT_ALPHABET
138 * Create n-long array and map it to random chars from given alphabet.
139 * Then join individual chars as string
141 return Array.from({ length: idDesiredLength })
143 return getRandomCharFromAlphabet(alphabet);
148 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
149 let opStr: string = msg.op;
151 op: UserOperation[opStr],
156 export const md = new markdown_it({
161 .use(markdown_it_container, 'spoiler', {
162 validate: function (params: any) {
163 return params.trim().match(/^spoiler\s+(.*)$/);
166 render: function (tokens: any, idx: any) {
167 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
169 if (tokens[idx].nesting === 1) {
171 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
174 return '</details>\n';
178 .use(markdownitEmoji, {
179 defs: objectFlip(emojiShortName),
182 md.renderer.rules.emoji = function (token, idx) {
183 return twemoji.parse(token[idx].content);
186 export function hotRankComment(comment: Comment): number {
187 return hotRank(comment.score, comment.published);
190 export function hotRankPost(post: Post): number {
191 return hotRank(post.score, post.newest_activity_time);
194 export function hotRank(score: number, timeStr: string): number {
195 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
196 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
197 let now: Date = new Date();
198 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
201 (10000 * Math.log10(Math.max(1, 3 + score))) /
202 Math.pow(hoursElapsed + 2, 1.8);
204 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
209 export function mdToHtml(text: string) {
210 return { __html: md.render(text) };
213 export function getUnixTime(text: string): number {
214 return text ? new Date(text).getTime() / 1000 : undefined;
217 export function addTypeInfo<T>(
220 ): Array<{ type_: string; data: T }> {
221 return arr.map(e => {
222 return { type_: name, data: e };
226 export function canMod(
228 modIds: Array<number>,
230 onSelf: boolean = false
232 // You can do moderator actions only on the mods added after you.
234 let yourIndex = modIds.findIndex(id => id == user.id);
235 if (yourIndex == -1) {
238 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
239 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
240 return !modIds.includes(creator_id);
247 export function isMod(modIds: Array<number>, creator_id: number): boolean {
248 return modIds.includes(creator_id);
251 const imageRegex = new RegExp(
252 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
254 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
256 export function isImage(url: string) {
257 return imageRegex.test(url);
260 export function isVideo(url: string) {
261 return videoRegex.test(url);
264 export function validURL(str: string) {
266 return !!new URL(str);
272 export function validEmail(email: string) {
273 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
274 return re.test(String(email).toLowerCase());
277 export function capitalizeFirstLetter(str: string): string {
278 return str.charAt(0).toUpperCase() + str.slice(1);
281 export function routeSortTypeToEnum(sort: string): SortType {
284 } else if (sort == 'hot') {
286 } else if (sort == 'topday') {
287 return SortType.TopDay;
288 } else if (sort == 'topweek') {
289 return SortType.TopWeek;
290 } else if (sort == 'topmonth') {
291 return SortType.TopMonth;
292 } else if (sort == 'topyear') {
293 return SortType.TopYear;
294 } else if (sort == 'topall') {
295 return SortType.TopAll;
299 export function routeListingTypeToEnum(type: string): ListingType {
300 return ListingType[capitalizeFirstLetter(type)];
303 export function routeDataTypeToEnum(type: string): DataType {
304 return DataType[capitalizeFirstLetter(type)];
307 export function routeSearchTypeToEnum(type: string): SearchType {
308 return SearchType[capitalizeFirstLetter(type)];
311 export async function getPageTitle(url: string) {
312 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
313 let title = await res.title;
317 export function debounce(
320 immediate: boolean = false
322 // 'private' variable for instance
323 // The returned function will be able to reference this due to closure.
324 // Each call to the returned function will share this common timer.
327 // Calling debounce returns a new anonymous function
329 // reference the context and args for the setTimeout function
333 // Should the function be called now? If immediate is true
334 // and not already in a timeout then the answer is: Yes
335 var callNow = immediate && !timeout;
337 // This is the basic debounce behaviour where you can call this
338 // function several times, but it will only execute once
339 // [before or after imposing a delay].
340 // Each time the returned function is called, the timer starts over.
341 clearTimeout(timeout);
343 // Set the new timeout
344 timeout = setTimeout(function () {
345 // Inside the timeout function, clear the timeout variable
346 // which will let the next execution run when in 'immediate' mode
349 // Check if the function already ran with the immediate flag
351 // Call the original function with apply
352 // apply lets you define the 'this' object as well as the arguments
353 // (both captured before setTimeout)
354 func.apply(context, args);
358 // Immediate mode and no wait timer? Execute the function..
359 if (callNow) func.apply(context, args);
363 export function getLanguage(): string {
364 let user = UserService.Instance.user;
365 let lang = user && user.lang ? user.lang : 'browser';
367 if (lang == 'browser') {
368 return getBrowserLanguage();
374 export function getBrowserLanguage(): string {
375 return navigator.language;
378 export function getMomentLanguage(): string {
379 let lang = getLanguage();
380 if (lang.startsWith('zh')) {
382 } else if (lang.startsWith('sv')) {
384 } else if (lang.startsWith('fr')) {
386 } else if (lang.startsWith('de')) {
388 } else if (lang.startsWith('ru')) {
390 } else if (lang.startsWith('es')) {
392 } else if (lang.startsWith('eo')) {
394 } else if (lang.startsWith('nl')) {
396 } else if (lang.startsWith('it')) {
398 } else if (lang.startsWith('fi')) {
400 } else if (lang.startsWith('ca')) {
402 } else if (lang.startsWith('fa')) {
404 } else if (lang.startsWith('pl')) {
406 } else if (lang.startsWith('pt')) {
408 } else if (lang.startsWith('ja')) {
410 } else if (lang.startsWith('ka')) {
412 } else if (lang.startsWith('hi')) {
414 } else if (lang.startsWith('el')) {
416 } else if (lang.startsWith('eu')) {
418 } else if (lang.startsWith('gl')) {
420 } else if (lang.startsWith('tr')) {
422 } else if (lang.startsWith('hu')) {
424 } else if (lang.startsWith('uk')) {
426 } else if (lang.startsWith('sq')) {
428 } else if (lang.startsWith('km')) {
430 } else if (lang.startsWith('ga')) {
432 } else if (lang.startsWith('sr')) {
440 export function setTheme(theme: string = 'darkly', loggedIn: boolean = false) {
441 // unload all the other themes
442 for (var i = 0; i < themes.length; i++) {
443 let styleSheet = document.getElementById(themes[i]);
445 styleSheet.setAttribute('disabled', 'disabled');
449 // if the user is not logged in, we load the default themes and let the browser decide
451 document.getElementById('default-light').removeAttribute('disabled');
452 document.getElementById('default-dark').removeAttribute('disabled');
455 .getElementById('default-light')
456 .setAttribute('disabled', 'disabled');
458 .getElementById('default-dark')
459 .setAttribute('disabled', 'disabled');
461 // Load the theme dynamically
462 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
463 loadCss(theme, cssLoc);
464 document.getElementById(theme).removeAttribute('disabled');
468 export function loadCss(id: string, loc: string) {
469 if (!document.getElementById(id)) {
470 var head = document.getElementsByTagName('head')[0];
471 var link = document.createElement('link');
473 link.rel = 'stylesheet';
474 link.type = 'text/css';
477 head.appendChild(link);
481 export function objectFlip(obj: any) {
483 Object.keys(obj).forEach(key => {
489 export function pictrsAvatarThumbnail(src: string): string {
490 // sample url: http://localhost:8535/pictrs/image/thumbnail256/gs7xuu.jpg
491 let split = src.split('/pictrs/image');
492 let out = `${split[0]}/pictrs/image/${
493 canUseWebP() ? 'webp/' : ''
494 }thumbnail96${split[1]}`;
498 export function showAvatars(): boolean {
500 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
501 !UserService.Instance.user
505 export function isCakeDay(creator_published: string): boolean {
506 // moment(undefined) or moment.utc(undefined) returns the current date/time
507 // moment(null) or moment.utc(null) returns null
508 const userCreationDate = moment.utc(creator_published || null).local();
509 const currentDate = moment(new Date());
512 userCreationDate.date() === currentDate.date() &&
513 userCreationDate.month() === currentDate.month()
517 // Converts to image thumbnail
518 export function pictrsImage(hash: string, thumbnail: boolean = false): string {
519 let root = `/pictrs/image`;
521 // Necessary for other servers / domains
522 if (hash.includes('pictrs')) {
523 let split = hash.split('/pictrs/image/');
524 root = `${split[0]}/pictrs/image`;
528 let out = `${root}/${canUseWebP() ? 'webp/' : ''}${
529 thumbnail ? 'thumbnail256/' : ''
534 export function isCommentType(item: Comment | PrivateMessage): item is Comment {
535 return (item as Comment).community_id !== undefined;
538 export function toast(text: string, background: string = 'success') {
539 let backgroundColor = `var(--${background})`;
542 backgroundColor: backgroundColor,
548 export function pictrsDeleteToast(
549 clickToDeleteText: string,
550 deletePictureText: string,
553 let backgroundColor = `var(--light)`;
554 let toast = Toastify({
555 text: clickToDeleteText,
556 backgroundColor: backgroundColor,
562 window.location.replace(deleteUrl);
563 alert(deletePictureText);
571 export function messageToastify(
578 let backgroundColor = `var(--light)`;
580 let toast = Toastify({
581 text: `${body}<br />${creator}`,
583 backgroundColor: backgroundColor,
584 className: 'text-dark',
592 router.history.push(link);
598 export function setupTribute(): Tribute {
604 menuItemTemplate: (item: any) => {
605 let shortName = `:${item.original.key}:`;
606 let twemojiIcon = twemoji.parse(item.original.val);
607 return `${twemojiIcon} ${shortName}`;
609 selectTemplate: (item: any) => {
610 return `:${item.original.key}:`;
612 values: Object.entries(emojiShortName).map(e => {
613 return { key: e[1], val: e[0] };
616 autocompleteMode: true,
617 menuItemLimit: mentionDropdownFetchLimit,
618 menuShowMinLength: 2,
623 selectTemplate: (item: any) => {
624 let link = item.original.local
625 ? `[${item.original.key}](/u/${item.original.name})`
626 : `[${item.original.key}](/user/${item.original.id})`;
629 values: (text: string, cb: any) => {
630 userSearch(text, (users: any) => cb(users));
633 autocompleteMode: true,
634 menuItemLimit: mentionDropdownFetchLimit,
635 menuShowMinLength: 2,
641 selectTemplate: (item: any) => {
642 let link = item.original.local
643 ? `[${item.original.key}](/c/${item.original.name})`
644 : `[${item.original.key}](/community/${item.original.id})`;
647 values: (text: string, cb: any) => {
648 communitySearch(text, (communities: any) => cb(communities));
651 autocompleteMode: true,
652 menuItemLimit: mentionDropdownFetchLimit,
653 menuShowMinLength: 2,
659 let tippyInstance = tippy('[data-tippy-content]');
661 export function setupTippy() {
662 tippyInstance.forEach(e => e.destroy());
663 tippyInstance = tippy('[data-tippy-content]', {
665 // Display on "long press"
666 touch: ['hold', 500],
670 function userSearch(text: string, cb: any) {
672 let form: SearchForm = {
674 type_: SearchType[SearchType.Users],
675 sort: SortType[SortType.TopAll],
677 limit: mentionDropdownFetchLimit,
680 WebSocketService.Instance.search(form);
682 this.userSub = WebSocketService.Instance.subject.subscribe(
684 let res = wsJsonToRes(msg);
685 if (res.op == UserOperation.Search) {
686 let data = res.data as SearchResponse;
687 let users = data.users.map(u => {
689 key: `@${u.name}@${hostname(u.actor_id)}`,
696 this.userSub.unsubscribe();
699 err => console.error(err),
700 () => console.log('complete')
707 function communitySearch(text: string, cb: any) {
709 let form: SearchForm = {
711 type_: SearchType[SearchType.Communities],
712 sort: SortType[SortType.TopAll],
714 limit: mentionDropdownFetchLimit,
717 WebSocketService.Instance.search(form);
719 this.communitySub = WebSocketService.Instance.subject.subscribe(
721 let res = wsJsonToRes(msg);
722 if (res.op == UserOperation.Search) {
723 let data = res.data as SearchResponse;
724 let communities = data.communities.map(c => {
726 key: `!${c.name}@${hostname(c.actor_id)}`,
733 this.communitySub.unsubscribe();
736 err => console.error(err),
737 () => console.log('complete')
744 export function getListingTypeFromProps(props: any): ListingType {
745 return props.match.params.listing_type
746 ? routeListingTypeToEnum(props.match.params.listing_type)
747 : UserService.Instance.user
748 ? UserService.Instance.user.default_listing_type
752 // TODO might need to add a user setting for this too
753 export function getDataTypeFromProps(props: any): DataType {
754 return props.match.params.data_type
755 ? routeDataTypeToEnum(props.match.params.data_type)
759 export function getSortTypeFromProps(props: any): SortType {
760 return props.match.params.sort
761 ? routeSortTypeToEnum(props.match.params.sort)
762 : UserService.Instance.user
763 ? UserService.Instance.user.default_sort_type
767 export function getPageFromProps(props: any): number {
768 return props.match.params.page ? Number(props.match.params.page) : 1;
771 export function editCommentRes(
772 data: CommentResponse,
773 comments: Array<Comment>
775 let found = comments.find(c => c.id == data.comment.id);
777 found.content = data.comment.content;
778 found.updated = data.comment.updated;
779 found.removed = data.comment.removed;
780 found.deleted = data.comment.deleted;
781 found.upvotes = data.comment.upvotes;
782 found.downvotes = data.comment.downvotes;
783 found.score = data.comment.score;
787 export function saveCommentRes(
788 data: CommentResponse,
789 comments: Array<Comment>
791 let found = comments.find(c => c.id == data.comment.id);
793 found.saved = data.comment.saved;
797 export function createCommentLikeRes(
798 data: CommentResponse,
799 comments: Array<Comment>
801 let found: Comment = comments.find(c => c.id === data.comment.id);
803 found.score = data.comment.score;
804 found.upvotes = data.comment.upvotes;
805 found.downvotes = data.comment.downvotes;
806 if (data.comment.my_vote !== null) {
807 found.my_vote = data.comment.my_vote;
812 export function createPostLikeFindRes(data: PostResponse, posts: Array<Post>) {
813 let found = posts.find(c => c.id == data.post.id);
815 createPostLikeRes(data, found);
819 export function createPostLikeRes(data: PostResponse, post: Post) {
821 post.score = data.post.score;
822 post.upvotes = data.post.upvotes;
823 post.downvotes = data.post.downvotes;
824 if (data.post.my_vote !== null) {
825 post.my_vote = data.post.my_vote;
830 export function editPostFindRes(data: PostResponse, posts: Array<Post>) {
831 let found = posts.find(c => c.id == data.post.id);
833 editPostRes(data, found);
837 export function editPostRes(data: PostResponse, post: Post) {
839 post.url = data.post.url;
840 post.name = data.post.name;
841 post.nsfw = data.post.nsfw;
845 export function commentsToFlatNodes(
846 comments: Array<Comment>
847 ): Array<CommentNodeI> {
848 let nodes: Array<CommentNodeI> = [];
849 for (let comment of comments) {
850 nodes.push({ comment: comment });
855 export function commentSort(tree: Array<CommentNodeI>, sort: CommentSortType) {
856 // First, put removed and deleted comments at the bottom, then do your other sorts
857 if (sort == CommentSortType.Top) {
860 +a.comment.removed - +b.comment.removed ||
861 +a.comment.deleted - +b.comment.deleted ||
862 b.comment.score - a.comment.score
864 } else if (sort == CommentSortType.New) {
867 +a.comment.removed - +b.comment.removed ||
868 +a.comment.deleted - +b.comment.deleted ||
869 b.comment.published.localeCompare(a.comment.published)
871 } else if (sort == CommentSortType.Old) {
874 +a.comment.removed - +b.comment.removed ||
875 +a.comment.deleted - +b.comment.deleted ||
876 a.comment.published.localeCompare(b.comment.published)
878 } else if (sort == CommentSortType.Hot) {
881 +a.comment.removed - +b.comment.removed ||
882 +a.comment.deleted - +b.comment.deleted ||
883 hotRankComment(b.comment) - hotRankComment(a.comment)
887 // Go through the children recursively
888 for (let node of tree) {
890 commentSort(node.children, sort);
895 export function commentSortSortType(tree: Array<CommentNodeI>, sort: SortType) {
896 commentSort(tree, convertCommentSortType(sort));
899 function convertCommentSortType(sort: SortType): CommentSortType {
901 sort == SortType.TopAll ||
902 sort == SortType.TopDay ||
903 sort == SortType.TopWeek ||
904 sort == SortType.TopMonth ||
905 sort == SortType.TopYear
907 return CommentSortType.Top;
908 } else if (sort == SortType.New) {
909 return CommentSortType.New;
910 } else if (sort == SortType.Hot) {
911 return CommentSortType.Hot;
913 return CommentSortType.Hot;
917 export function postSort(
920 communityType: boolean
922 // First, put removed and deleted comments at the bottom, then do your other sorts
924 sort == SortType.TopAll ||
925 sort == SortType.TopDay ||
926 sort == SortType.TopWeek ||
927 sort == SortType.TopMonth ||
928 sort == SortType.TopYear
932 +a.removed - +b.removed ||
933 +a.deleted - +b.deleted ||
934 (communityType && +b.stickied - +a.stickied) ||
937 } else if (sort == SortType.New) {
940 +a.removed - +b.removed ||
941 +a.deleted - +b.deleted ||
942 (communityType && +b.stickied - +a.stickied) ||
943 b.published.localeCompare(a.published)
945 } else if (sort == SortType.Hot) {
948 +a.removed - +b.removed ||
949 +a.deleted - +b.deleted ||
950 (communityType && +b.stickied - +a.stickied) ||
951 b.hot_rank - a.hot_rank
956 export const colorList: Array<string> = [
965 function hsl(num: number) {
966 return `hsla(${num}, 35%, 50%, 1)`;
969 function randomHsl() {
970 return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
973 export function previewLines(text: string, lines: number = 3): string {
974 // Use lines * 2 because markdown requires 2 lines
981 export function hostname(url: string): string {
982 let cUrl = new URL(url);
983 return window.location.port
984 ? `${cUrl.hostname}:${cUrl.port}`
985 : `${cUrl.hostname}`;
988 function canUseWebP() {
989 // TODO pictshare might have a webp conversion bug, try disabling this
992 // var elem = document.createElement('canvas');
993 // if (!!(elem.getContext && elem.getContext('2d'))) {
994 // var testString = !(window.mozInnerScreenX == null) ? 'png' : 'webp';
995 // // was able or not to get WebP representation
997 // elem.toDataURL('image/webp').startsWith('data:image/' + testString)
1001 // // very old browser like IE 8, canvas not supported