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';
28 CommentNode as CommentNodeI,
38 WebSocketJsonResponse,
43 } from './interfaces';
44 import { UserService, WebSocketService } from './services';
46 import Tribute from 'tributejs/src/Tribute.js';
47 import markdown_it from 'markdown-it';
48 import markdownitEmoji from 'markdown-it-emoji/light';
49 import markdown_it_container from 'markdown-it-container';
50 import twemoji from 'twemoji';
51 import emojiShortName from 'emoji-short-name';
52 import Toastify from 'toastify-js';
53 import tippy from 'tippy.js';
54 import EmojiButton from '@joeattardi/emoji-button';
56 export const repoUrl = 'https://github.com/LemmyNet/lemmy';
57 export const helpGuideUrl = '/docs/about_guide.html';
58 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
59 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
60 export const archiveUrl = 'https://archive.is';
62 export const postRefetchSeconds: number = 60 * 1000;
63 export const fetchLimit: number = 20;
64 export const mentionDropdownFetchLimit = 10;
66 export const languages = [
67 { code: 'ca', name: 'Català' },
68 { code: 'en', name: 'English' },
69 { code: 'el', name: 'Ελληνικά' },
70 { code: 'eu', name: 'Euskara' },
71 { code: 'eo', name: 'Esperanto' },
72 { code: 'es', name: 'Español' },
73 { code: 'de', name: 'Deutsch' },
74 { code: 'gl', name: 'Galego' },
75 { code: 'hu', name: 'Magyar Nyelv' },
76 { code: 'ka', name: 'ქართული ენა' },
77 { code: 'hi', name: 'मानक हिन्दी' },
78 { code: 'fa', name: 'فارسی' },
79 { code: 'ja', name: '日本語' },
80 { code: 'pl', name: 'Polski' },
81 { code: 'pt_BR', name: 'Português Brasileiro' },
82 { code: 'zh', name: '中文' },
83 { code: 'fi', name: 'Suomi' },
84 { code: 'fr', name: 'Français' },
85 { code: 'sv', name: 'Svenska' },
86 { code: 'tr', name: 'Türkçe' },
87 { code: 'uk', name: 'українська мова' },
88 { code: 'ru', name: 'Русский' },
89 { code: 'nl', name: 'Nederlands' },
90 { code: 'it', name: 'Italiano' },
93 export const themes = [
108 export const emojiPicker = new EmojiButton({
109 // Use the emojiShortName from native
112 position: 'auto-start',
116 export function randomStr() {
119 .replace(/[^a-z]+/g, '')
123 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
124 let opStr: string = msg.op;
126 op: UserOperation[opStr],
131 export const md = new markdown_it({
136 .use(markdown_it_container, 'spoiler', {
137 validate: function (params: any) {
138 return params.trim().match(/^spoiler\s+(.*)$/);
141 render: function (tokens: any, idx: any) {
142 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
144 if (tokens[idx].nesting === 1) {
146 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
149 return '</details>\n';
153 .use(markdownitEmoji, {
154 defs: objectFlip(emojiShortName),
157 md.renderer.rules.emoji = function (token, idx) {
158 return twemoji.parse(token[idx].content);
161 export function hotRankComment(comment: Comment): number {
162 return hotRank(comment.score, comment.published);
165 export function hotRankPost(post: Post): number {
166 return hotRank(post.score, post.newest_activity_time);
169 export function hotRank(score: number, timeStr: string): number {
170 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
171 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
172 let now: Date = new Date();
173 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
176 (10000 * Math.log10(Math.max(1, 3 + score))) /
177 Math.pow(hoursElapsed + 2, 1.8);
179 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
184 export function mdToHtml(text: string) {
185 return { __html: md.render(text) };
188 export function getUnixTime(text: string): number {
189 return text ? new Date(text).getTime() / 1000 : undefined;
192 export function addTypeInfo<T>(
195 ): Array<{ type_: string; data: T }> {
196 return arr.map(e => {
197 return { type_: name, data: e };
201 export function canMod(
203 modIds: Array<number>,
205 onSelf: boolean = false
207 // You can do moderator actions only on the mods added after you.
209 let yourIndex = modIds.findIndex(id => id == user.id);
210 if (yourIndex == -1) {
213 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
214 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
215 return !modIds.includes(creator_id);
222 export function isMod(modIds: Array<number>, creator_id: number): boolean {
223 return modIds.includes(creator_id);
226 const imageRegex = new RegExp(
227 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
229 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
231 export function isImage(url: string) {
232 return imageRegex.test(url);
235 export function isVideo(url: string) {
236 return videoRegex.test(url);
239 export function validURL(str: string) {
241 return !!new URL(str);
247 export function validEmail(email: string) {
248 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
249 return re.test(String(email).toLowerCase());
252 export function capitalizeFirstLetter(str: string): string {
253 return str.charAt(0).toUpperCase() + str.slice(1);
256 export function routeSortTypeToEnum(sort: string): SortType {
259 } else if (sort == 'hot') {
261 } else if (sort == 'topday') {
262 return SortType.TopDay;
263 } else if (sort == 'topweek') {
264 return SortType.TopWeek;
265 } else if (sort == 'topmonth') {
266 return SortType.TopMonth;
267 } else if (sort == 'topyear') {
268 return SortType.TopYear;
269 } else if (sort == 'topall') {
270 return SortType.TopAll;
274 export function routeListingTypeToEnum(type: string): ListingType {
275 return ListingType[capitalizeFirstLetter(type)];
278 export function routeDataTypeToEnum(type: string): DataType {
279 return DataType[capitalizeFirstLetter(type)];
282 export function routeSearchTypeToEnum(type: string): SearchType {
283 return SearchType[capitalizeFirstLetter(type)];
286 export async function getPageTitle(url: string) {
287 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
288 let title = await res.title;
292 export function debounce(
295 immediate: boolean = false
297 // 'private' variable for instance
298 // The returned function will be able to reference this due to closure.
299 // Each call to the returned function will share this common timer.
302 // Calling debounce returns a new anonymous function
304 // reference the context and args for the setTimeout function
308 // Should the function be called now? If immediate is true
309 // and not already in a timeout then the answer is: Yes
310 var callNow = immediate && !timeout;
312 // This is the basic debounce behaviour where you can call this
313 // function several times, but it will only execute once
314 // [before or after imposing a delay].
315 // Each time the returned function is called, the timer starts over.
316 clearTimeout(timeout);
318 // Set the new timeout
319 timeout = setTimeout(function () {
320 // Inside the timeout function, clear the timeout variable
321 // which will let the next execution run when in 'immediate' mode
324 // Check if the function already ran with the immediate flag
326 // Call the original function with apply
327 // apply lets you define the 'this' object as well as the arguments
328 // (both captured before setTimeout)
329 func.apply(context, args);
333 // Immediate mode and no wait timer? Execute the function..
334 if (callNow) func.apply(context, args);
338 export function getLanguage(): string {
339 let user = UserService.Instance.user;
340 let lang = user && user.lang ? user.lang : 'browser';
342 if (lang == 'browser') {
343 return getBrowserLanguage();
349 export function getBrowserLanguage(): string {
350 return navigator.language;
353 export function getMomentLanguage(): string {
354 let lang = getLanguage();
355 if (lang.startsWith('zh')) {
357 } else if (lang.startsWith('sv')) {
359 } else if (lang.startsWith('fr')) {
361 } else if (lang.startsWith('de')) {
363 } else if (lang.startsWith('ru')) {
365 } else if (lang.startsWith('es')) {
367 } else if (lang.startsWith('eo')) {
369 } else if (lang.startsWith('nl')) {
371 } else if (lang.startsWith('it')) {
373 } else if (lang.startsWith('fi')) {
375 } else if (lang.startsWith('ca')) {
377 } else if (lang.startsWith('fa')) {
379 } else if (lang.startsWith('pl')) {
381 } else if (lang.startsWith('pt')) {
383 } else if (lang.startsWith('ja')) {
385 } else if (lang.startsWith('ka')) {
387 } else if (lang.startsWith('hi')) {
389 } else if (lang.startsWith('el')) {
391 } else if (lang.startsWith('eu')) {
393 } else if (lang.startsWith('gl')) {
395 } else if (lang.startsWith('tr')) {
397 } else if (lang.startsWith('hu')) {
399 } else if (lang.startsWith('uk')) {
407 export function setTheme(theme: string = 'darkly', loggedIn: boolean = false) {
408 // unload all the other themes
409 for (var i = 0; i < themes.length; i++) {
410 let styleSheet = document.getElementById(themes[i]);
412 styleSheet.setAttribute('disabled', 'disabled');
416 // if the user is not logged in, we load the default themes and let the browser decide
418 document.getElementById('default-light').removeAttribute('disabled');
419 document.getElementById('default-dark').removeAttribute('disabled');
422 .getElementById('default-light')
423 .setAttribute('disabled', 'disabled');
425 .getElementById('default-dark')
426 .setAttribute('disabled', 'disabled');
428 // Load the theme dynamically
429 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
430 loadCss(theme, cssLoc);
431 document.getElementById(theme).removeAttribute('disabled');
435 export function loadCss(id: string, loc: string) {
436 if (!document.getElementById(id)) {
437 var head = document.getElementsByTagName('head')[0];
438 var link = document.createElement('link');
440 link.rel = 'stylesheet';
441 link.type = 'text/css';
444 head.appendChild(link);
448 export function objectFlip(obj: any) {
450 Object.keys(obj).forEach(key => {
456 export function pictrsAvatarThumbnail(src: string): string {
457 // sample url: http://localhost:8535/pictrs/image/thumbnail256/gs7xuu.jpg
458 let split = src.split('/pictrs/image');
459 let out = `${split[0]}/pictrs/image/${
460 canUseWebP() ? 'webp/' : ''
461 }thumbnail96${split[1]}`;
465 export function showAvatars(): boolean {
467 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
468 !UserService.Instance.user
472 // Converts to image thumbnail
473 export function pictrsImage(hash: string, thumbnail: boolean = false): string {
474 let root = `/pictrs/image`;
476 // Necessary for other servers / domains
477 if (hash.includes('pictrs')) {
478 let split = hash.split('/pictrs/image/');
479 root = `${split[0]}/pictrs/image`;
483 let out = `${root}/${canUseWebP() ? 'webp/' : ''}${
484 thumbnail ? 'thumbnail192/' : ''
489 export function isCommentType(item: Comment | PrivateMessage): item is Comment {
490 return (item as Comment).community_id !== undefined;
493 export function toast(text: string, background: string = 'success') {
494 let backgroundColor = `var(--${background})`;
497 backgroundColor: backgroundColor,
503 export function pictrsDeleteToast(
504 clickToDeleteText: string,
505 deletePictureText: string,
508 let backgroundColor = `var(--light)`;
509 let toast = Toastify({
510 text: clickToDeleteText,
511 backgroundColor: backgroundColor,
517 window.location.replace(deleteUrl);
518 alert(deletePictureText);
526 export function messageToastify(
533 let backgroundColor = `var(--light)`;
535 let toast = Toastify({
536 text: `${body}<br />${creator}`,
538 backgroundColor: backgroundColor,
539 className: 'text-dark',
547 router.history.push(link);
553 export function setupTribute(): Tribute {
559 menuItemTemplate: (item: any) => {
560 let shortName = `:${item.original.key}:`;
561 let twemojiIcon = twemoji.parse(item.original.val);
562 return `${twemojiIcon} ${shortName}`;
564 selectTemplate: (item: any) => {
565 return `:${item.original.key}:`;
567 values: Object.entries(emojiShortName).map(e => {
568 return { key: e[1], val: e[0] };
571 autocompleteMode: true,
572 menuItemLimit: mentionDropdownFetchLimit,
573 menuShowMinLength: 2,
578 selectTemplate: (item: any) => {
579 return `[/u/${item.original.key}](/u/${item.original.key})`;
581 values: (text: string, cb: any) => {
582 userSearch(text, (users: any) => cb(users));
585 autocompleteMode: true,
586 menuItemLimit: mentionDropdownFetchLimit,
587 menuShowMinLength: 2,
593 selectTemplate: (item: any) => {
594 return `[/c/${item.original.key}](/c/${item.original.key})`;
596 values: (text: string, cb: any) => {
597 communitySearch(text, (communities: any) => cb(communities));
600 autocompleteMode: true,
601 menuItemLimit: mentionDropdownFetchLimit,
602 menuShowMinLength: 2,
608 let tippyInstance = tippy('[data-tippy-content]');
610 export function setupTippy() {
611 tippyInstance.forEach(e => e.destroy());
612 tippyInstance = tippy('[data-tippy-content]', {
614 // Display on "long press"
615 touch: ['hold', 500],
619 function userSearch(text: string, cb: any) {
621 let form: SearchForm = {
623 type_: SearchType[SearchType.Users],
624 sort: SortType[SortType.TopAll],
626 limit: mentionDropdownFetchLimit,
629 WebSocketService.Instance.search(form);
631 this.userSub = WebSocketService.Instance.subject.subscribe(
633 let res = wsJsonToRes(msg);
634 if (res.op == UserOperation.Search) {
635 let data = res.data as SearchResponse;
636 let users = data.users.map(u => {
637 return { key: u.name };
640 this.userSub.unsubscribe();
643 err => console.error(err),
644 () => console.log('complete')
651 function communitySearch(text: string, cb: any) {
653 let form: SearchForm = {
655 type_: SearchType[SearchType.Communities],
656 sort: SortType[SortType.TopAll],
658 limit: mentionDropdownFetchLimit,
661 WebSocketService.Instance.search(form);
663 this.communitySub = WebSocketService.Instance.subject.subscribe(
665 let res = wsJsonToRes(msg);
666 if (res.op == UserOperation.Search) {
667 let data = res.data as SearchResponse;
668 let communities = data.communities.map(u => {
669 return { key: u.name };
672 this.communitySub.unsubscribe();
675 err => console.error(err),
676 () => console.log('complete')
683 export function getListingTypeFromProps(props: any): ListingType {
684 return props.match.params.listing_type
685 ? routeListingTypeToEnum(props.match.params.listing_type)
686 : UserService.Instance.user
687 ? UserService.Instance.user.default_listing_type
691 // TODO might need to add a user setting for this too
692 export function getDataTypeFromProps(props: any): DataType {
693 return props.match.params.data_type
694 ? routeDataTypeToEnum(props.match.params.data_type)
698 export function getSortTypeFromProps(props: any): SortType {
699 return props.match.params.sort
700 ? routeSortTypeToEnum(props.match.params.sort)
701 : UserService.Instance.user
702 ? UserService.Instance.user.default_sort_type
706 export function getPageFromProps(props: any): number {
707 return props.match.params.page ? Number(props.match.params.page) : 1;
710 export function editCommentRes(
711 data: CommentResponse,
712 comments: Array<Comment>
714 let found = comments.find(c => c.id == data.comment.id);
716 found.content = data.comment.content;
717 found.updated = data.comment.updated;
718 found.removed = data.comment.removed;
719 found.deleted = data.comment.deleted;
720 found.upvotes = data.comment.upvotes;
721 found.downvotes = data.comment.downvotes;
722 found.score = data.comment.score;
726 export function saveCommentRes(
727 data: CommentResponse,
728 comments: Array<Comment>
730 let found = comments.find(c => c.id == data.comment.id);
732 found.saved = data.comment.saved;
736 export function createCommentLikeRes(
737 data: CommentResponse,
738 comments: Array<Comment>
740 let found: Comment = comments.find(c => c.id === data.comment.id);
742 found.score = data.comment.score;
743 found.upvotes = data.comment.upvotes;
744 found.downvotes = data.comment.downvotes;
745 if (data.comment.my_vote !== null) {
746 found.my_vote = data.comment.my_vote;
751 export function createPostLikeFindRes(data: PostResponse, posts: Array<Post>) {
752 let found = posts.find(c => c.id == data.post.id);
754 createPostLikeRes(data, found);
758 export function createPostLikeRes(data: PostResponse, post: Post) {
760 post.score = data.post.score;
761 post.upvotes = data.post.upvotes;
762 post.downvotes = data.post.downvotes;
763 if (data.post.my_vote !== null) {
764 post.my_vote = data.post.my_vote;
769 export function editPostFindRes(data: PostResponse, posts: Array<Post>) {
770 let found = posts.find(c => c.id == data.post.id);
772 editPostRes(data, found);
776 export function editPostRes(data: PostResponse, post: Post) {
778 post.url = data.post.url;
779 post.name = data.post.name;
780 post.nsfw = data.post.nsfw;
784 export function commentsToFlatNodes(
785 comments: Array<Comment>
786 ): Array<CommentNodeI> {
787 let nodes: Array<CommentNodeI> = [];
788 for (let comment of comments) {
789 nodes.push({ comment: comment });
794 export function commentSort(tree: Array<CommentNodeI>, sort: CommentSortType) {
795 // First, put removed and deleted comments at the bottom, then do your other sorts
796 if (sort == CommentSortType.Top) {
799 +a.comment.removed - +b.comment.removed ||
800 +a.comment.deleted - +b.comment.deleted ||
801 b.comment.score - a.comment.score
803 } else if (sort == CommentSortType.New) {
806 +a.comment.removed - +b.comment.removed ||
807 +a.comment.deleted - +b.comment.deleted ||
808 b.comment.published.localeCompare(a.comment.published)
810 } else if (sort == CommentSortType.Old) {
813 +a.comment.removed - +b.comment.removed ||
814 +a.comment.deleted - +b.comment.deleted ||
815 a.comment.published.localeCompare(b.comment.published)
817 } else if (sort == CommentSortType.Hot) {
820 +a.comment.removed - +b.comment.removed ||
821 +a.comment.deleted - +b.comment.deleted ||
822 hotRankComment(b.comment) - hotRankComment(a.comment)
826 // Go through the children recursively
827 for (let node of tree) {
829 commentSort(node.children, sort);
834 export function commentSortSortType(tree: Array<CommentNodeI>, sort: SortType) {
835 commentSort(tree, convertCommentSortType(sort));
838 function convertCommentSortType(sort: SortType): CommentSortType {
840 sort == SortType.TopAll ||
841 sort == SortType.TopDay ||
842 sort == SortType.TopWeek ||
843 sort == SortType.TopMonth ||
844 sort == SortType.TopYear
846 return CommentSortType.Top;
847 } else if (sort == SortType.New) {
848 return CommentSortType.New;
849 } else if (sort == SortType.Hot) {
850 return CommentSortType.Hot;
852 return CommentSortType.Hot;
856 export function postSort(
859 communityType: boolean
861 // First, put removed and deleted comments at the bottom, then do your other sorts
863 sort == SortType.TopAll ||
864 sort == SortType.TopDay ||
865 sort == SortType.TopWeek ||
866 sort == SortType.TopMonth ||
867 sort == SortType.TopYear
871 +a.removed - +b.removed ||
872 +a.deleted - +b.deleted ||
873 (communityType && +b.stickied - +a.stickied) ||
876 } else if (sort == SortType.New) {
879 +a.removed - +b.removed ||
880 +a.deleted - +b.deleted ||
881 (communityType && +b.stickied - +a.stickied) ||
882 b.published.localeCompare(a.published)
884 } else if (sort == SortType.Hot) {
887 +a.removed - +b.removed ||
888 +a.deleted - +b.deleted ||
889 (communityType && +b.stickied - +a.stickied) ||
890 hotRankPost(b) - hotRankPost(a)
895 export const colorList: Array<string> = [
904 function hsl(num: number) {
905 return `hsla(${num}, 35%, 50%, 1)`;
908 function randomHsl() {
909 return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
912 export function previewLines(text: string, lines: number = 3): string {
913 // Use lines * 2 because markdown requires 2 lines
920 function canUseWebP() {
921 // TODO pictshare might have a webp conversion bug, try disabling this
924 // var elem = document.createElement('canvas');
925 // if (!!(elem.getContext && elem.getContext('2d'))) {
926 // var testString = !(window.mozInnerScreenX == null) ? 'png' : 'webp';
927 // // was able or not to get WebP representation
929 // elem.toDataURL('image/webp').startsWith('data:image/' + testString)
933 // // very old browser like IE 8, canvas not supported