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';
16 import 'moment/locale/hi';
21 CommentNode as CommentNodeI,
31 WebSocketJsonResponse,
36 } from './interfaces';
37 import { UserService, WebSocketService } from './services';
39 import Tribute from 'tributejs/src/Tribute.js';
40 import markdown_it from 'markdown-it';
41 import markdownitEmoji from 'markdown-it-emoji/light';
42 import markdown_it_container from 'markdown-it-container';
43 import twemoji from 'twemoji';
44 import emojiShortName from 'emoji-short-name';
45 import Toastify from 'toastify-js';
46 import tippy from 'tippy.js';
47 import EmojiButton from '@joeattardi/emoji-button';
49 export const repoUrl = 'https://github.com/LemmyNet/lemmy';
50 export const helpGuideUrl = '/docs/about_guide.html';
51 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
52 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
53 export const archiveUrl = 'https://archive.is';
55 export const postRefetchSeconds: number = 60 * 1000;
56 export const fetchLimit: number = 20;
57 export const mentionDropdownFetchLimit = 10;
59 export const languages = [
60 { code: 'ca', name: 'Català' },
61 { code: 'en', name: 'English' },
62 { code: 'eo', name: 'Esperanto' },
63 { code: 'es', name: 'Español' },
64 { code: 'de', name: 'Deutsch' },
65 { code: 'ka', name: 'ქართული ენა' },
66 { code: 'hi', name: 'मानक हिन्दी' },
67 { code: 'fa', name: 'فارسی' },
68 { code: 'ja', name: '日本語' },
69 { code: 'pt_BR', name: 'Português Brasileiro' },
70 { code: 'zh', name: '中文' },
71 { code: 'fi', name: 'Suomi' },
72 { code: 'fr', name: 'Français' },
73 { code: 'sv', name: 'Svenska' },
74 { code: 'ru', name: 'Русский' },
75 { code: 'nl', name: 'Nederlands' },
76 { code: 'it', name: 'Italiano' },
79 export const themes = [
94 export const emojiPicker = new EmojiButton({
95 // Use the emojiShortName from native
98 position: 'auto-start',
102 export function randomStr() {
105 .replace(/[^a-z]+/g, '')
109 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
110 let opStr: string = msg.op;
112 op: UserOperation[opStr],
117 export const md = new markdown_it({
122 .use(markdown_it_container, 'spoiler', {
123 validate: function (params: any) {
124 return params.trim().match(/^spoiler\s+(.*)$/);
127 render: function (tokens: any, idx: any) {
128 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
130 if (tokens[idx].nesting === 1) {
132 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
135 return '</details>\n';
139 .use(markdownitEmoji, {
140 defs: objectFlip(emojiShortName),
143 md.renderer.rules.emoji = function (token, idx) {
144 return twemoji.parse(token[idx].content);
147 export function hotRankComment(comment: Comment): number {
148 return hotRank(comment.score, comment.published);
151 export function hotRankPost(post: Post): number {
152 return hotRank(post.score, post.newest_activity_time);
155 export function hotRank(score: number, timeStr: string): number {
156 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
157 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
158 let now: Date = new Date();
159 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
162 (10000 * Math.log10(Math.max(1, 3 + score))) /
163 Math.pow(hoursElapsed + 2, 1.8);
165 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
170 export function mdToHtml(text: string) {
171 return { __html: md.render(text) };
174 export function getUnixTime(text: string): number {
175 return text ? new Date(text).getTime() / 1000 : undefined;
178 export function addTypeInfo<T>(
181 ): Array<{ type_: string; data: T }> {
182 return arr.map(e => {
183 return { type_: name, data: e };
187 export function canMod(
189 modIds: Array<number>,
191 onSelf: boolean = false
193 // You can do moderator actions only on the mods added after you.
195 let yourIndex = modIds.findIndex(id => id == user.id);
196 if (yourIndex == -1) {
199 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
200 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
201 return !modIds.includes(creator_id);
208 export function isMod(modIds: Array<number>, creator_id: number): boolean {
209 return modIds.includes(creator_id);
212 const imageRegex = new RegExp(
213 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg))/
215 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
217 export function isImage(url: string) {
218 return imageRegex.test(url);
221 export function isVideo(url: string) {
222 return videoRegex.test(url);
225 export function validURL(str: string) {
227 return !!new URL(str);
233 export function validEmail(email: string) {
234 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
235 return re.test(String(email).toLowerCase());
238 export function capitalizeFirstLetter(str: string): string {
239 return str.charAt(0).toUpperCase() + str.slice(1);
242 export function routeSortTypeToEnum(sort: string): SortType {
245 } else if (sort == 'hot') {
247 } else if (sort == 'topday') {
248 return SortType.TopDay;
249 } else if (sort == 'topweek') {
250 return SortType.TopWeek;
251 } else if (sort == 'topmonth') {
252 return SortType.TopMonth;
253 } else if (sort == 'topyear') {
254 return SortType.TopYear;
255 } else if (sort == 'topall') {
256 return SortType.TopAll;
260 export function routeListingTypeToEnum(type: string): ListingType {
261 return ListingType[capitalizeFirstLetter(type)];
264 export function routeDataTypeToEnum(type: string): DataType {
265 return DataType[capitalizeFirstLetter(type)];
268 export function routeSearchTypeToEnum(type: string): SearchType {
269 return SearchType[capitalizeFirstLetter(type)];
272 export async function getPageTitle(url: string) {
273 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
274 let title = await res.title;
278 export function debounce(
281 immediate: boolean = false
283 // 'private' variable for instance
284 // The returned function will be able to reference this due to closure.
285 // Each call to the returned function will share this common timer.
288 // Calling debounce returns a new anonymous function
290 // reference the context and args for the setTimeout function
294 // Should the function be called now? If immediate is true
295 // and not already in a timeout then the answer is: Yes
296 var callNow = immediate && !timeout;
298 // This is the basic debounce behaviour where you can call this
299 // function several times, but it will only execute once
300 // [before or after imposing a delay].
301 // Each time the returned function is called, the timer starts over.
302 clearTimeout(timeout);
304 // Set the new timeout
305 timeout = setTimeout(function () {
306 // Inside the timeout function, clear the timeout variable
307 // which will let the next execution run when in 'immediate' mode
310 // Check if the function already ran with the immediate flag
312 // Call the original function with apply
313 // apply lets you define the 'this' object as well as the arguments
314 // (both captured before setTimeout)
315 func.apply(context, args);
319 // Immediate mode and no wait timer? Execute the function..
320 if (callNow) func.apply(context, args);
324 export function getLanguage(): string {
325 let user = UserService.Instance.user;
326 let lang = user && user.lang ? user.lang : 'browser';
328 if (lang == 'browser') {
329 return getBrowserLanguage();
335 export function getBrowserLanguage(): string {
336 return navigator.language;
339 export function getMomentLanguage(): string {
340 let lang = getLanguage();
341 if (lang.startsWith('zh')) {
343 } else if (lang.startsWith('sv')) {
345 } else if (lang.startsWith('fr')) {
347 } else if (lang.startsWith('de')) {
349 } else if (lang.startsWith('ru')) {
351 } else if (lang.startsWith('es')) {
353 } else if (lang.startsWith('eo')) {
355 } else if (lang.startsWith('nl')) {
357 } else if (lang.startsWith('it')) {
359 } else if (lang.startsWith('fi')) {
361 } else if (lang.startsWith('ca')) {
363 } else if (lang.startsWith('fa')) {
365 } else if (lang.startsWith('pt')) {
367 } else if (lang.startsWith('ja')) {
369 } else if (lang.startsWith('ka')) {
371 } else if (lang.startsWith('hi')) {
379 export function setTheme(theme: string = 'darkly') {
380 // unload all the other themes
381 for (var i = 0; i < themes.length; i++) {
382 let styleSheet = document.getElementById(themes[i]);
384 styleSheet.setAttribute('disabled', 'disabled');
388 // Load the theme dynamically
389 if (!document.getElementById(theme)) {
390 var head = document.getElementsByTagName('head')[0];
391 var link = document.createElement('link');
393 link.rel = 'stylesheet';
394 link.type = 'text/css';
395 link.href = `/static/assets/css/themes/${theme}.min.css`;
397 head.appendChild(link);
399 document.getElementById(theme).removeAttribute('disabled');
402 export function objectFlip(obj: any) {
404 Object.keys(obj).forEach(key => {
410 export function pictshareAvatarThumbnail(src: string): string {
411 // sample url: http://localhost:8535/pictshare/gs7xuu.jpg
412 let split = src.split('pictshare');
413 let out = `${split[0]}pictshare/96${split[1]}`;
417 export function showAvatars(): boolean {
419 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
420 !UserService.Instance.user
424 // Converts to image thumbnail
425 export function pictshareImage(
427 thumbnail: boolean = false
429 let root = `/pictshare`;
431 // Necessary for other servers / domains
432 if (hash.includes('pictshare')) {
433 let split = hash.split('/pictshare/');
434 root = `${split[0]}/pictshare`;
438 let out = `${root}/${thumbnail ? '192/' : ''}${hash}`;
442 export function isCommentType(item: Comment | PrivateMessage): item is Comment {
443 return (item as Comment).community_id !== undefined;
446 export function toast(text: string, background: string = 'success') {
447 let backgroundColor = `var(--${background})`;
450 backgroundColor: backgroundColor,
456 export function messageToastify(
463 let backgroundColor = `var(--light)`;
465 let toast = Toastify({
466 text: `${body}<br />${creator}`,
468 backgroundColor: backgroundColor,
476 router.history.push(link);
482 export function setupTribute(): Tribute {
488 menuItemTemplate: (item: any) => {
489 let shortName = `:${item.original.key}:`;
490 let twemojiIcon = twemoji.parse(item.original.val);
491 return `${twemojiIcon} ${shortName}`;
493 selectTemplate: (item: any) => {
494 return `:${item.original.key}:`;
496 values: Object.entries(emojiShortName).map(e => {
497 return { key: e[1], val: e[0] };
500 autocompleteMode: true,
501 menuItemLimit: mentionDropdownFetchLimit,
502 menuShowMinLength: 2,
507 selectTemplate: (item: any) => {
508 return `[/u/${item.original.key}](/u/${item.original.key})`;
510 values: (text: string, cb: any) => {
511 userSearch(text, (users: any) => cb(users));
514 autocompleteMode: true,
515 menuItemLimit: mentionDropdownFetchLimit,
516 menuShowMinLength: 2,
522 selectTemplate: (item: any) => {
523 return `[/c/${item.original.key}](/c/${item.original.key})`;
525 values: (text: string, cb: any) => {
526 communitySearch(text, (communities: any) => cb(communities));
529 autocompleteMode: true,
530 menuItemLimit: mentionDropdownFetchLimit,
531 menuShowMinLength: 2,
537 let tippyInstance = tippy('[data-tippy-content]');
539 export function setupTippy() {
540 tippyInstance.forEach(e => e.destroy());
541 tippyInstance = tippy('[data-tippy-content]', {
543 // Display on "long press"
544 touch: ['hold', 500],
548 function userSearch(text: string, cb: any) {
550 let form: SearchForm = {
552 type_: SearchType[SearchType.Users],
553 sort: SortType[SortType.TopAll],
555 limit: mentionDropdownFetchLimit,
558 WebSocketService.Instance.search(form);
560 this.userSub = WebSocketService.Instance.subject.subscribe(
562 let res = wsJsonToRes(msg);
563 if (res.op == UserOperation.Search) {
564 let data = res.data as SearchResponse;
565 let users = data.users.map(u => {
566 return { key: u.name };
569 this.userSub.unsubscribe();
572 err => console.error(err),
573 () => console.log('complete')
580 function communitySearch(text: string, cb: any) {
582 let form: SearchForm = {
584 type_: SearchType[SearchType.Communities],
585 sort: SortType[SortType.TopAll],
587 limit: mentionDropdownFetchLimit,
590 WebSocketService.Instance.search(form);
592 this.communitySub = WebSocketService.Instance.subject.subscribe(
594 let res = wsJsonToRes(msg);
595 if (res.op == UserOperation.Search) {
596 let data = res.data as SearchResponse;
597 let communities = data.communities.map(u => {
598 return { key: u.name };
601 this.communitySub.unsubscribe();
604 err => console.error(err),
605 () => console.log('complete')
612 export function getListingTypeFromProps(props: any): ListingType {
613 return props.match.params.listing_type
614 ? routeListingTypeToEnum(props.match.params.listing_type)
615 : UserService.Instance.user
616 ? UserService.Instance.user.default_listing_type
620 // TODO might need to add a user setting for this too
621 export function getDataTypeFromProps(props: any): DataType {
622 return props.match.params.data_type
623 ? routeDataTypeToEnum(props.match.params.data_type)
627 export function getSortTypeFromProps(props: any): SortType {
628 return props.match.params.sort
629 ? routeSortTypeToEnum(props.match.params.sort)
630 : UserService.Instance.user
631 ? UserService.Instance.user.default_sort_type
635 export function getPageFromProps(props: any): number {
636 return props.match.params.page ? Number(props.match.params.page) : 1;
639 export function editCommentRes(
640 data: CommentResponse,
641 comments: Array<Comment>
643 let found = comments.find(c => c.id == data.comment.id);
645 found.content = data.comment.content;
646 found.updated = data.comment.updated;
647 found.removed = data.comment.removed;
648 found.deleted = data.comment.deleted;
649 found.upvotes = data.comment.upvotes;
650 found.downvotes = data.comment.downvotes;
651 found.score = data.comment.score;
655 export function saveCommentRes(
656 data: CommentResponse,
657 comments: Array<Comment>
659 let found = comments.find(c => c.id == data.comment.id);
661 found.saved = data.comment.saved;
665 export function createCommentLikeRes(
666 data: CommentResponse,
667 comments: Array<Comment>
669 let found: Comment = comments.find(c => c.id === data.comment.id);
671 found.score = data.comment.score;
672 found.upvotes = data.comment.upvotes;
673 found.downvotes = data.comment.downvotes;
674 if (data.comment.my_vote !== null) {
675 found.my_vote = data.comment.my_vote;
680 export function createPostLikeFindRes(data: PostResponse, posts: Array<Post>) {
681 let found = posts.find(c => c.id == data.post.id);
683 createPostLikeRes(data, found);
687 export function createPostLikeRes(data: PostResponse, post: Post) {
689 post.score = data.post.score;
690 post.upvotes = data.post.upvotes;
691 post.downvotes = data.post.downvotes;
692 if (data.post.my_vote !== null) {
693 post.my_vote = data.post.my_vote;
698 export function editPostFindRes(data: PostResponse, posts: Array<Post>) {
699 let found = posts.find(c => c.id == data.post.id);
701 editPostRes(data, found);
705 export function editPostRes(data: PostResponse, post: Post) {
707 post.url = data.post.url;
708 post.name = data.post.name;
709 post.nsfw = data.post.nsfw;
713 export function commentsToFlatNodes(
714 comments: Array<Comment>
715 ): Array<CommentNodeI> {
716 let nodes: Array<CommentNodeI> = [];
717 for (let comment of comments) {
718 nodes.push({ comment: comment });
723 export function commentSort(tree: Array<CommentNodeI>, sort: CommentSortType) {
724 // First, put removed and deleted comments at the bottom, then do your other sorts
725 if (sort == CommentSortType.Top) {
728 +a.comment.removed - +b.comment.removed ||
729 +a.comment.deleted - +b.comment.deleted ||
730 b.comment.score - a.comment.score
732 } else if (sort == CommentSortType.New) {
735 +a.comment.removed - +b.comment.removed ||
736 +a.comment.deleted - +b.comment.deleted ||
737 b.comment.published.localeCompare(a.comment.published)
739 } else if (sort == CommentSortType.Old) {
742 +a.comment.removed - +b.comment.removed ||
743 +a.comment.deleted - +b.comment.deleted ||
744 a.comment.published.localeCompare(b.comment.published)
746 } else if (sort == CommentSortType.Hot) {
749 +a.comment.removed - +b.comment.removed ||
750 +a.comment.deleted - +b.comment.deleted ||
751 hotRankComment(b.comment) - hotRankComment(a.comment)
755 // Go through the children recursively
756 for (let node of tree) {
758 commentSort(node.children, sort);
763 export function commentSortSortType(tree: Array<CommentNodeI>, sort: SortType) {
764 commentSort(tree, convertCommentSortType(sort));
767 function convertCommentSortType(sort: SortType): CommentSortType {
769 sort == SortType.TopAll ||
770 sort == SortType.TopDay ||
771 sort == SortType.TopWeek ||
772 sort == SortType.TopMonth ||
773 sort == SortType.TopYear
775 return CommentSortType.Top;
776 } else if (sort == SortType.New) {
777 return CommentSortType.New;
778 } else if (sort == SortType.Hot) {
779 return CommentSortType.Hot;
781 return CommentSortType.Hot;
785 export function postSort(
788 communityType: boolean
790 // First, put removed and deleted comments at the bottom, then do your other sorts
792 sort == SortType.TopAll ||
793 sort == SortType.TopDay ||
794 sort == SortType.TopWeek ||
795 sort == SortType.TopMonth ||
796 sort == SortType.TopYear
800 +a.removed - +b.removed ||
801 +a.deleted - +b.deleted ||
802 (communityType && +b.stickied - +a.stickied) ||
805 } else if (sort == SortType.New) {
808 +a.removed - +b.removed ||
809 +a.deleted - +b.deleted ||
810 (communityType && +b.stickied - +a.stickied) ||
811 b.published.localeCompare(a.published)
813 } else if (sort == SortType.Hot) {
816 +a.removed - +b.removed ||
817 +a.deleted - +b.deleted ||
818 (communityType && +b.stickied - +a.stickied) ||
819 hotRankPost(b) - hotRankPost(a)
824 export const colorList: Array<string> = [
833 function hsl(num: number) {
834 return `hsla(${num}, 35%, 50%, 1)`;
837 function randomHsl() {
838 return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
841 export function previewLines(text: string, lines: number = 3): string {
842 // Use lines * 2 because markdown requires 2 lines