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';
28 import 'moment/locale/ko';
29 import 'moment/locale/da';
39 WebSocketJsonResponse,
47 } from 'lemmy-js-client';
53 CommentNode as CommentNodeI,
54 } from './interfaces';
55 import { UserService, WebSocketService } from './services';
59 Tribute = require('tributejs');
61 import markdown_it from 'markdown-it';
62 import markdown_it_sub from 'markdown-it-sub';
63 import markdown_it_sup from 'markdown-it-sup';
64 import markdownitEmoji from 'markdown-it-emoji/light';
65 import markdown_it_container from 'markdown-it-container';
66 import emojiShortName from 'emoji-short-name';
67 import Toastify from 'toastify-js';
68 import tippy from 'tippy.js';
69 import moment from 'moment';
70 import { Subscription } from 'rxjs';
71 import { retryWhen, delay, take } from 'rxjs/operators';
72 import { i18n } from './i18next';
74 export const wsClient = new LemmyWebsocket();
76 export const favIconUrl = '/static/assets/favicon.svg';
77 export const favIconPngUrl = '/static/assets/apple-touch-icon.png';
79 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
80 export const repoUrl = 'https://github.com/LemmyNet';
81 export const joinLemmyUrl = 'https://join.lemmy.ml';
82 export const supportLemmyUrl = 'https://join.lemmy.ml/sponsors';
83 export const docsUrl = '/docs/en/index.html';
84 export const helpGuideUrl = '/docs/en/about/guide.html'; // TODO find a way to redirect to the non-en folder
85 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
86 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
87 export const archiveUrl = 'https://archive.is';
88 export const elementUrl = 'https://element.io/';
90 export const postRefetchSeconds: number = 60 * 1000;
91 export const fetchLimit: number = 20;
92 export const mentionDropdownFetchLimit = 10;
94 export const languages = [
95 { code: 'ca', name: 'Català' },
96 { code: 'en', name: 'English' },
97 { code: 'el', name: 'Ελληνικά' },
98 { code: 'eu', name: 'Euskara' },
99 { code: 'eo', name: 'Esperanto' },
100 { code: 'es', name: 'Español' },
101 { code: 'da', name: 'Dansk' },
102 { code: 'de', name: 'Deutsch' },
103 { code: 'ga', name: 'Gaeilge' },
104 { code: 'gl', name: 'Galego' },
105 { code: 'hu', name: 'Magyar Nyelv' },
106 { code: 'ka', name: 'ქართული ენა' },
107 { code: 'ko', name: '한국어' },
108 { code: 'km', name: 'ភាសាខ្មែរ' },
109 { code: 'hi', name: 'मानक हिन्दी' },
110 { code: 'fa', name: 'فارسی' },
111 { code: 'ja', name: '日本語' },
112 { code: 'oc', name: 'Occitan' },
113 { code: 'pl', name: 'Polski' },
114 { code: 'pt_BR', name: 'Português Brasileiro' },
115 { code: 'zh', name: '中文' },
116 { code: 'fi', name: 'Suomi' },
117 { code: 'fr', name: 'Français' },
118 { code: 'sv', name: 'Svenska' },
119 { code: 'sq', name: 'Shqip' },
120 { code: 'sr_Latn', name: 'srpski' },
121 { code: 'tr', name: 'Türkçe' },
122 { code: 'uk', name: 'Українська Mова' },
123 { code: 'ru', name: 'Русский' },
124 { code: 'nl', name: 'Nederlands' },
125 { code: 'it', name: 'Italiano' },
128 export const themes = [
144 const DEFAULT_ALPHABET =
145 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
147 function getRandomCharFromAlphabet(alphabet: string): string {
148 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
151 export function randomStr(
152 idDesiredLength: number = 20,
153 alphabet = DEFAULT_ALPHABET
156 * Create n-long array and map it to random chars from given alphabet.
157 * Then join individual chars as string
159 return Array.from({ length: idDesiredLength })
161 return getRandomCharFromAlphabet(alphabet);
166 export function wsJsonToRes<ResponseType>(
167 msg: WebSocketJsonResponse<ResponseType>
168 ): WebSocketResponse<ResponseType> {
175 export function wsUserOp(msg: any): UserOperation {
176 let opStr: string = msg.op;
177 return UserOperation[opStr];
180 export const md = new markdown_it({
185 .use(markdown_it_sub)
186 .use(markdown_it_sup)
187 .use(markdown_it_container, 'spoiler', {
188 validate: function (params: any) {
189 return params.trim().match(/^spoiler\s+(.*)$/);
192 render: function (tokens: any, idx: any) {
193 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
195 if (tokens[idx].nesting === 1) {
197 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
200 return '</details>\n';
204 .use(markdownitEmoji, {
205 defs: objectFlip(emojiShortName),
208 export function hotRankComment(comment_view: CommentView): number {
209 return hotRank(comment_view.counts.score, comment_view.comment.published);
212 export function hotRankActivePost(post_view: PostView): number {
213 return hotRank(post_view.counts.score, post_view.counts.newest_comment_time);
216 export function hotRankPost(post_view: PostView): number {
217 return hotRank(post_view.counts.score, post_view.post.published);
220 export function hotRank(score: number, timeStr: string): number {
221 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
222 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
223 let now: Date = new Date();
224 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
227 (10000 * Math.log10(Math.max(1, 3 + score))) /
228 Math.pow(hoursElapsed + 2, 1.8);
230 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
235 export function mdToHtml(text: string) {
236 return { __html: md.render(text) };
239 export function getUnixTime(text: string): number {
240 return text ? new Date(text).getTime() / 1000 : undefined;
243 export function canMod(
244 user: UserSafeSettings,
247 onSelf: boolean = false
249 // You can do moderator actions only on the mods added after you.
251 let yourIndex = modIds.findIndex(id => id == user.id);
252 if (yourIndex == -1) {
255 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
256 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
257 return !modIds.includes(creator_id);
264 export function isMod(modIds: number[], creator_id: number): boolean {
265 return modIds.includes(creator_id);
268 const imageRegex = new RegExp(
269 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
271 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
273 export function isImage(url: string) {
274 return imageRegex.test(url);
277 export function isVideo(url: string) {
278 return videoRegex.test(url);
281 export function validURL(str: string) {
282 return !!new URL(str);
285 export function communityRSSUrl(actorId: string, sort: string): string {
286 let url = new URL(actorId);
287 return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
290 export function validEmail(email: string) {
291 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
292 return re.test(String(email).toLowerCase());
295 export function capitalizeFirstLetter(str: string): string {
296 return str.charAt(0).toUpperCase() + str.slice(1);
299 export function routeSortTypeToEnum(sort: string): SortType {
300 return SortType[sort];
303 export function listingTypeFromNum(type_: number): ListingType {
304 return Object.values(ListingType)[type_];
307 export function sortTypeFromNum(type_: number): SortType {
308 return Object.values(SortType)[type_];
311 export function routeListingTypeToEnum(type: string): ListingType {
312 return ListingType[type];
315 export function routeDataTypeToEnum(type: string): DataType {
316 return DataType[capitalizeFirstLetter(type)];
319 export function routeSearchTypeToEnum(type: string): SearchType {
320 return SearchType[type];
323 export async function getPageTitle(url: string) {
324 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
325 let title = await res.title;
329 export function debounce(
332 immediate: boolean = false
334 // 'private' variable for instance
335 // The returned function will be able to reference this due to closure.
336 // Each call to the returned function will share this common timer.
339 // Calling debounce returns a new anonymous function
341 // reference the context and args for the setTimeout function
345 // Should the function be called now? If immediate is true
346 // and not already in a timeout then the answer is: Yes
347 var callNow = immediate && !timeout;
349 // This is the basic debounce behaviour where you can call this
350 // function several times, but it will only execute once
351 // [before or after imposing a delay].
352 // Each time the returned function is called, the timer starts over.
353 clearTimeout(timeout);
355 // Set the new timeout
356 timeout = setTimeout(function () {
357 // Inside the timeout function, clear the timeout variable
358 // which will let the next execution run when in 'immediate' mode
361 // Check if the function already ran with the immediate flag
363 // Call the original function with apply
364 // apply lets you define the 'this' object as well as the arguments
365 // (both captured before setTimeout)
366 func.apply(context, args);
370 // Immediate mode and no wait timer? Execute the function..
371 if (callNow) func.apply(context, args);
376 export function getLanguage(override?: string): string {
377 let user = UserService.Instance.user;
378 let lang = override || (user && user.lang ? user.lang : 'browser');
380 if (lang == 'browser' && isBrowser()) {
381 return getBrowserLanguage();
388 export function getBrowserLanguage(): string {
389 return navigator.language;
392 export function getMomentLanguage(): string {
393 let lang = getLanguage();
394 if (lang.startsWith('zh')) {
396 } else if (lang.startsWith('sv')) {
398 } else if (lang.startsWith('fr')) {
400 } else if (lang.startsWith('de')) {
402 } else if (lang.startsWith('ru')) {
404 } else if (lang.startsWith('es')) {
406 } else if (lang.startsWith('eo')) {
408 } else if (lang.startsWith('nl')) {
410 } else if (lang.startsWith('it')) {
412 } else if (lang.startsWith('fi')) {
414 } else if (lang.startsWith('ca')) {
416 } else if (lang.startsWith('fa')) {
418 } else if (lang.startsWith('pl')) {
420 } else if (lang.startsWith('pt')) {
422 } else if (lang.startsWith('ja')) {
424 } else if (lang.startsWith('ka')) {
426 } else if (lang.startsWith('hi')) {
428 } else if (lang.startsWith('el')) {
430 } else if (lang.startsWith('eu')) {
432 } else if (lang.startsWith('gl')) {
434 } else if (lang.startsWith('tr')) {
436 } else if (lang.startsWith('hu')) {
438 } else if (lang.startsWith('uk')) {
440 } else if (lang.startsWith('sq')) {
442 } else if (lang.startsWith('km')) {
444 } else if (lang.startsWith('ga')) {
446 } else if (lang.startsWith('sr')) {
448 } else if (lang.startsWith('ko')) {
450 } else if (lang.startsWith('da')) {
452 } else if (lang.startsWith('oc')) {
460 export function setTheme(theme: string, forceReload: boolean = false) {
464 if (theme === 'browser' && !forceReload) {
467 // This is only run on a force reload
468 if (theme == 'browser') {
472 // Unload all the other themes
473 for (var i = 0; i < themes.length; i++) {
474 let styleSheet = document.getElementById(themes[i]);
476 styleSheet.setAttribute('disabled', 'disabled');
481 .getElementById('default-light')
482 ?.setAttribute('disabled', 'disabled');
483 document.getElementById('default-dark')?.setAttribute('disabled', 'disabled');
485 // Load the theme dynamically
486 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
487 loadCss(theme, cssLoc);
488 document.getElementById(theme).removeAttribute('disabled');
491 export function loadCss(id: string, loc: string) {
492 if (!document.getElementById(id)) {
493 var head = document.getElementsByTagName('head')[0];
494 var link = document.createElement('link');
496 link.rel = 'stylesheet';
497 link.type = 'text/css';
500 head.appendChild(link);
504 export function objectFlip(obj: any) {
506 Object.keys(obj).forEach(key => {
512 export function showAvatars(): boolean {
514 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
515 !UserService.Instance.user
519 export function isCakeDay(published: string): boolean {
520 // moment(undefined) or moment.utc(undefined) returns the current date/time
521 // moment(null) or moment.utc(null) returns null
522 const userCreationDate = moment.utc(published || null).local();
523 const currentDate = moment(new Date());
526 userCreationDate.date() === currentDate.date() &&
527 userCreationDate.month() === currentDate.month() &&
528 userCreationDate.year() !== currentDate.year()
532 export function toast(text: string, background: string = 'success') {
534 let backgroundColor = `var(--${background})`;
537 backgroundColor: backgroundColor,
544 export function pictrsDeleteToast(
545 clickToDeleteText: string,
546 deletePictureText: string,
550 let backgroundColor = `var(--light)`;
551 let toast = Toastify({
552 text: clickToDeleteText,
553 backgroundColor: backgroundColor,
559 window.location.replace(deleteUrl);
560 alert(deletePictureText);
569 interface NotifyInfo {
576 export function messageToastify(info: NotifyInfo, router: any) {
578 let htmlBody = info.body ? md.render(info.body) : '';
579 let backgroundColor = `var(--light)`;
581 let toast = Toastify({
582 text: `${htmlBody}<br />${info.name}`,
583 avatar: info.icon ? info.icon : null,
584 backgroundColor: backgroundColor,
585 className: 'text-dark',
593 router.history.push(info.link);
600 export function notifyPost(post_view: PostView, router: any) {
601 let info: NotifyInfo = {
602 name: post_view.community.name,
603 icon: post_view.community.icon,
604 link: `/post/${post_view.post.id}`,
605 body: post_view.post.name,
607 notify(info, router);
610 export function notifyComment(comment_view: CommentView, router: any) {
611 let info: NotifyInfo = {
612 name: comment_view.creator.name,
613 icon: comment_view.creator.avatar,
614 link: `/post/${comment_view.post.id}/comment/${comment_view.comment.id}`,
615 body: comment_view.comment.content,
617 notify(info, router);
620 export function notifyPrivateMessage(pmv: PrivateMessageView, router: any) {
621 let info: NotifyInfo = {
622 name: pmv.creator.name,
623 icon: pmv.creator.avatar,
625 body: pmv.private_message.content,
627 notify(info, router);
630 function notify(info: NotifyInfo, router: any) {
631 messageToastify(info, router);
633 if (Notification.permission !== 'granted') Notification.requestPermission();
635 var notification = new Notification(info.name, {
640 notification.onclick = (ev: Event): any => {
642 router.history.push(info.link);
647 export function setupTribute() {
649 noMatchTemplate: function () {
656 menuItemTemplate: (item: any) => {
657 let shortName = `:${item.original.key}:`;
658 return `${item.original.val} ${shortName}`;
660 selectTemplate: (item: any) => {
661 return `:${item.original.key}:`;
663 values: Object.entries(emojiShortName).map(e => {
664 return { key: e[1], val: e[0] };
667 autocompleteMode: true,
669 // menuItemLimit: mentionDropdownFetchLimit,
670 menuShowMinLength: 2,
675 selectTemplate: (item: any) => {
676 let it: UserTribute = item.original;
677 return `[${it.key}](${it.view.user.actor_id})`;
679 values: (text: string, cb: (users: UserTribute[]) => any) => {
680 userSearch(text, (users: UserTribute[]) => cb(users));
683 autocompleteMode: true,
685 // menuItemLimit: mentionDropdownFetchLimit,
686 menuShowMinLength: 2,
692 selectTemplate: (item: any) => {
693 let it: CommunityTribute = item.original;
694 return `[${it.key}](${it.view.community.actor_id})`;
696 values: (text: string, cb: any) => {
697 communitySearch(text, (communities: CommunityTribute[]) =>
702 autocompleteMode: true,
704 // menuItemLimit: mentionDropdownFetchLimit,
705 menuShowMinLength: 2,
711 var tippyInstance: any;
713 tippyInstance = tippy('[data-tippy-content]');
716 export function setupTippy() {
718 tippyInstance.forEach((e: any) => e.destroy());
719 tippyInstance = tippy('[data-tippy-content]', {
721 // Display on "long press"
722 touch: ['hold', 500],
727 interface UserTribute {
732 function userSearch(text: string, cb: (users: UserTribute[]) => any) {
736 type_: SearchType.Users,
737 sort: SortType.TopAll,
739 limit: mentionDropdownFetchLimit,
740 auth: authField(false),
743 WebSocketService.Instance.send(wsClient.search(form));
745 let userSub = WebSocketService.Instance.subject.subscribe(
747 let res = wsJsonToRes(msg);
748 if (res.op == UserOperation.Search) {
749 let data = res.data as SearchResponse;
750 let users: UserTribute[] = data.users.map(uv => {
751 let tribute: UserTribute = {
752 key: `@${uv.user.name}@${hostname(uv.user.actor_id)}`,
758 userSub.unsubscribe();
761 err => console.error(err),
762 () => console.log('complete')
769 interface CommunityTribute {
774 function communitySearch(
776 cb: (communities: CommunityTribute[]) => any
781 type_: SearchType.Communities,
782 sort: SortType.TopAll,
784 limit: mentionDropdownFetchLimit,
785 auth: authField(false),
788 WebSocketService.Instance.send(wsClient.search(form));
790 let communitySub = WebSocketService.Instance.subject.subscribe(
792 let res = wsJsonToRes(msg);
793 if (res.op == UserOperation.Search) {
794 let data = res.data as SearchResponse;
795 let communities: CommunityTribute[] = data.communities.map(cv => {
796 let tribute: CommunityTribute = {
797 key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
803 communitySub.unsubscribe();
806 err => console.error(err),
807 () => console.log('complete')
814 export function getListingTypeFromProps(props: any): ListingType {
815 return props.match.params.listing_type
816 ? routeListingTypeToEnum(props.match.params.listing_type)
817 : UserService.Instance.user
818 ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
822 // TODO might need to add a user setting for this too
823 export function getDataTypeFromProps(props: any): DataType {
824 return props.match.params.data_type
825 ? routeDataTypeToEnum(props.match.params.data_type)
829 export function getSortTypeFromProps(props: any): SortType {
830 return props.match.params.sort
831 ? routeSortTypeToEnum(props.match.params.sort)
832 : UserService.Instance.user
833 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
837 export function getPageFromProps(props: any): number {
838 return props.match.params.page ? Number(props.match.params.page) : 1;
841 export function getRecipientIdFromProps(props: any): number {
842 return props.match.params.recipient_id
843 ? Number(props.match.params.recipient_id)
847 export function getIdFromProps(props: any): number {
848 return Number(props.match.params.id);
851 export function getCommentIdFromProps(props: any): number {
852 return Number(props.match.params.comment_id);
855 export function getUsernameFromProps(props: any): string {
856 return props.match.params.username;
859 export function editCommentRes(data: CommentView, comments: CommentView[]) {
860 let found = comments.find(c => c.comment.id == data.comment.id);
862 found.comment.content = data.comment.content;
863 found.comment.updated = data.comment.updated;
864 found.comment.removed = data.comment.removed;
865 found.comment.deleted = data.comment.deleted;
866 found.counts.upvotes = data.counts.upvotes;
867 found.counts.downvotes = data.counts.downvotes;
868 found.counts.score = data.counts.score;
872 export function saveCommentRes(data: CommentView, comments: CommentView[]) {
873 let found = comments.find(c => c.comment.id == data.comment.id);
875 found.saved = data.saved;
879 export function createCommentLikeRes(
881 comments: CommentView[]
883 let found = comments.find(c => c.comment.id === data.comment.id);
885 found.counts.score = data.counts.score;
886 found.counts.upvotes = data.counts.upvotes;
887 found.counts.downvotes = data.counts.downvotes;
888 if (data.my_vote !== null) {
889 found.my_vote = data.my_vote;
894 export function createPostLikeFindRes(data: PostView, posts: PostView[]) {
895 let found = posts.find(p => p.post.id == data.post.id);
897 createPostLikeRes(data, found);
901 export function createPostLikeRes(data: PostView, post_view: PostView) {
903 post_view.counts.score = data.counts.score;
904 post_view.counts.upvotes = data.counts.upvotes;
905 post_view.counts.downvotes = data.counts.downvotes;
906 if (data.my_vote !== null) {
907 post_view.my_vote = data.my_vote;
912 export function editPostFindRes(data: PostView, posts: PostView[]) {
913 let found = posts.find(p => p.post.id == data.post.id);
915 editPostRes(data, found);
919 export function editPostRes(data: PostView, post: PostView) {
921 post.post.url = data.post.url;
922 post.post.name = data.post.name;
923 post.post.nsfw = data.post.nsfw;
924 post.post.deleted = data.post.deleted;
925 post.post.removed = data.post.removed;
926 post.post.stickied = data.post.stickied;
927 post.post.body = data.post.body;
928 post.post.locked = data.post.locked;
929 post.saved = data.saved;
933 export function commentsToFlatNodes(comments: CommentView[]): CommentNodeI[] {
934 let nodes: CommentNodeI[] = [];
935 for (let comment of comments) {
936 nodes.push({ comment_view: comment });
941 export function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
942 // First, put removed and deleted comments at the bottom, then do your other sorts
943 if (sort == CommentSortType.Top) {
946 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
947 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
948 b.comment_view.counts.score - a.comment_view.counts.score
950 } else if (sort == CommentSortType.New) {
953 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
954 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
955 b.comment_view.comment.published.localeCompare(
956 a.comment_view.comment.published
959 } else if (sort == CommentSortType.Old) {
962 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
963 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
964 a.comment_view.comment.published.localeCompare(
965 b.comment_view.comment.published
968 } else if (sort == CommentSortType.Hot) {
971 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
972 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
973 hotRankComment(b.comment_view) - hotRankComment(a.comment_view)
977 // Go through the children recursively
978 for (let node of tree) {
980 commentSort(node.children, sort);
985 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
986 commentSort(tree, convertCommentSortType(sort));
989 function convertCommentSortType(sort: SortType): CommentSortType {
991 sort == SortType.TopAll ||
992 sort == SortType.TopDay ||
993 sort == SortType.TopWeek ||
994 sort == SortType.TopMonth ||
995 sort == SortType.TopYear
997 return CommentSortType.Top;
998 } else if (sort == SortType.New) {
999 return CommentSortType.New;
1000 } else if (sort == SortType.Hot || sort == SortType.Active) {
1001 return CommentSortType.Hot;
1003 return CommentSortType.Hot;
1007 export function postSort(
1010 communityType: boolean
1012 // First, put removed and deleted comments at the bottom, then do your other sorts
1014 sort == SortType.TopAll ||
1015 sort == SortType.TopDay ||
1016 sort == SortType.TopWeek ||
1017 sort == SortType.TopMonth ||
1018 sort == SortType.TopYear
1022 +a.post.removed - +b.post.removed ||
1023 +a.post.deleted - +b.post.deleted ||
1024 (communityType && +b.post.stickied - +a.post.stickied) ||
1025 b.counts.score - a.counts.score
1027 } else if (sort == SortType.New) {
1030 +a.post.removed - +b.post.removed ||
1031 +a.post.deleted - +b.post.deleted ||
1032 (communityType && +b.post.stickied - +a.post.stickied) ||
1033 b.post.published.localeCompare(a.post.published)
1035 } else if (sort == SortType.Hot) {
1038 +a.post.removed - +b.post.removed ||
1039 +a.post.deleted - +b.post.deleted ||
1040 (communityType && +b.post.stickied - +a.post.stickied) ||
1041 hotRankPost(b) - hotRankPost(a)
1043 } else if (sort == SortType.Active) {
1046 +a.post.removed - +b.post.removed ||
1047 +a.post.deleted - +b.post.deleted ||
1048 (communityType && +b.post.stickied - +a.post.stickied) ||
1049 hotRankActivePost(b) - hotRankActivePost(a)
1054 export const colorList: string[] = [
1063 function hsl(num: number) {
1064 return `hsla(${num}, 35%, 50%, 1)`;
1067 // function randomHsl() {
1068 // return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
1071 export function previewLines(
1073 maxChars: number = 300,
1074 maxLines: number = 1
1080 // Use lines * 2 because markdown requires 2 lines
1081 .slice(0, maxLines * 2)
1086 export function hostname(url: string): string {
1087 let cUrl = new URL(url);
1088 return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1091 export function validTitle(title?: string): boolean {
1092 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1093 if (title === null || title.length < 3) return true;
1095 const regex = new RegExp(/.*\S.*/, 'g');
1097 return regex.test(title);
1100 export function siteBannerCss(banner: string): string {
1102 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1103 background-attachment: fixed; \
1104 background-position: top; \
1105 background-repeat: no-repeat; \
1106 background-size: 100% cover; \
1109 max-height: 100vh; \
1113 export function isBrowser() {
1114 return typeof window !== 'undefined';
1117 export function setIsoData(context: any): IsoData {
1118 let isoData: IsoData = isBrowser()
1120 : context.router.staticContext;
1124 export function wsSubscribe(parseMessage: any): Subscription {
1126 return WebSocketService.Instance.subject
1127 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1129 msg => parseMessage(msg),
1130 err => console.error(err),
1131 () => console.log('complete')
1138 export function setOptionalAuth(obj: any, auth = UserService.Instance.auth) {
1144 export function authField(
1145 throwErr: boolean = true,
1146 auth = UserService.Instance.auth
1148 if (auth == null && throwErr) {
1149 toast(i18n.t('not_logged_in'), 'danger');
1150 throw 'Not logged in';
1156 moment.updateLocale('en', {
1177 export function saveScrollPosition(context: any) {
1178 let path: string = context.router.route.location.pathname;
1179 let y = window.scrollY;
1180 sessionStorage.setItem(`scrollPosition_${path}`, y.toString());
1183 export function restoreScrollPosition(context: any) {
1184 let path: string = context.router.route.location.pathname;
1185 let y = Number(sessionStorage.getItem(`scrollPosition_${path}`));
1186 window.scrollTo(0, y);