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';
33 CommentNode as CommentNodeI,
41 WebSocketJsonResponse,
47 } from 'lemmy-js-client';
49 import { httpUri } from './env';
51 import { CommentSortType, DataType, IsoData } from './interfaces';
52 import { UserService, WebSocketService } from './services';
56 Tribute = require('tributejs');
58 import markdown_it from 'markdown-it';
59 import markdown_it_sub from 'markdown-it-sub';
60 import markdown_it_sup from 'markdown-it-sup';
61 import markdownitEmoji from 'markdown-it-emoji/light';
62 import markdown_it_container from 'markdown-it-container';
63 import emojiShortName from 'emoji-short-name';
64 import Toastify from 'toastify-js';
65 import tippy from 'tippy.js';
66 import moment from 'moment';
67 import { Subscription } from 'rxjs';
68 import { retryWhen, delay, take } from 'rxjs/operators';
70 export const favIconUrl = '/static/assets/favicon.svg';
71 export const favIconPngUrl = '/static/assets/apple-touch-icon.png';
73 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
74 export const defaultFavIcon = 'test';
75 export const repoUrl = 'https://github.com/LemmyNet';
76 export const joinLemmyUrl = 'https://join.lemmy.ml';
77 export const helpGuideUrl = '/docs/about_guide.html';
78 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
79 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
80 export const archiveUrl = 'https://archive.is';
81 export const elementUrl = 'https://element.io/';
83 export const postRefetchSeconds: number = 60 * 1000;
84 export const fetchLimit: number = 20;
85 export const mentionDropdownFetchLimit = 10;
87 export const lemmyHttp = new LemmyHttp(httpUri);
89 export const languages = [
90 { code: 'ca', name: 'Català' },
91 { code: 'en', name: 'English' },
92 { code: 'el', name: 'Ελληνικά' },
93 { code: 'eu', name: 'Euskara' },
94 { code: 'eo', name: 'Esperanto' },
95 { code: 'es', name: 'Español' },
96 { code: 'de', name: 'Deutsch' },
97 { code: 'ga', name: 'Gaeilge' },
98 { code: 'gl', name: 'Galego' },
99 { code: 'hu', name: 'Magyar Nyelv' },
100 { code: 'ka', name: 'ქართული ენა' },
101 { code: 'ko', name: '한국어' },
102 { code: 'km', name: 'ភាសាខ្មែរ' },
103 { code: 'hi', name: 'मानक हिन्दी' },
104 { code: 'fa', name: 'فارسی' },
105 { code: 'ja', name: '日本語' },
106 { code: 'pl', name: 'Polski' },
107 { code: 'pt_BR', name: 'Português Brasileiro' },
108 { code: 'zh', name: '中文' },
109 { code: 'fi', name: 'Suomi' },
110 { code: 'fr', name: 'Français' },
111 { code: 'sv', name: 'Svenska' },
112 { code: 'sq', name: 'Shqip' },
113 { code: 'sr_Latn', name: 'srpski' },
114 { code: 'tr', name: 'Türkçe' },
115 { code: 'uk', name: 'Українська Mова' },
116 { code: 'ru', name: 'Русский' },
117 { code: 'nl', name: 'Nederlands' },
118 { code: 'it', name: 'Italiano' },
121 export const themes = [
137 const DEFAULT_ALPHABET =
138 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
140 function getRandomCharFromAlphabet(alphabet: string): string {
141 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
144 export function randomStr(
145 idDesiredLength: number = 20,
146 alphabet = DEFAULT_ALPHABET
149 * Create n-long array and map it to random chars from given alphabet.
150 * Then join individual chars as string
152 return Array.from({ length: idDesiredLength })
154 return getRandomCharFromAlphabet(alphabet);
159 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
160 let opStr: string = msg.op;
162 op: UserOperation[opStr],
167 export const md = new markdown_it({
172 .use(markdown_it_sub)
173 .use(markdown_it_sup)
174 .use(markdown_it_container, 'spoiler', {
175 validate: function (params: any) {
176 return params.trim().match(/^spoiler\s+(.*)$/);
179 render: function (tokens: any, idx: any) {
180 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
182 if (tokens[idx].nesting === 1) {
184 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
187 return '</details>\n';
191 .use(markdownitEmoji, {
192 defs: objectFlip(emojiShortName),
195 export function hotRankComment(comment: Comment): number {
196 return hotRank(comment.score, comment.published);
199 export function hotRankPost(post: Post): number {
200 return hotRank(post.score, post.newest_activity_time);
203 export function hotRank(score: number, timeStr: string): number {
204 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
205 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
206 let now: Date = new Date();
207 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
210 (10000 * Math.log10(Math.max(1, 3 + score))) /
211 Math.pow(hoursElapsed + 2, 1.8);
213 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
218 export function mdToHtml(text: string) {
219 return { __html: md.render(text) };
222 export function getUnixTime(text: string): number {
223 return text ? new Date(text).getTime() / 1000 : undefined;
226 export function addTypeInfo<T>(
229 ): { type_: string; data: T }[] {
230 return arr.map(e => {
231 return { type_: name, data: e };
235 export function canMod(
239 onSelf: boolean = false
241 // You can do moderator actions only on the mods added after you.
243 let yourIndex = modIds.findIndex(id => id == user.id);
244 if (yourIndex == -1) {
247 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
248 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
249 return !modIds.includes(creator_id);
256 export function isMod(modIds: number[], creator_id: number): boolean {
257 return modIds.includes(creator_id);
260 const imageRegex = new RegExp(
261 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
263 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
265 export function isImage(url: string) {
266 return imageRegex.test(url);
269 export function isVideo(url: string) {
270 return videoRegex.test(url);
274 export function validURL(str: string) {
276 return !!new URL(str);
282 export function communityRSSUrl(actorId: string, sort: string): string {
283 let url = new URL(actorId);
284 return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
287 export function validEmail(email: string) {
288 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
289 return re.test(String(email).toLowerCase());
292 export function capitalizeFirstLetter(str: string): string {
293 return str.charAt(0).toUpperCase() + str.slice(1);
296 export function routeSortTypeToEnum(sort: string): SortType {
297 return SortType[sort];
300 export function routeListingTypeToEnum(type: string): ListingType {
301 return ListingType[type];
304 export function routeDataTypeToEnum(type: string): DataType {
305 return DataType[capitalizeFirstLetter(type)];
308 export function routeSearchTypeToEnum(type: string): SearchType {
309 return SearchType[type];
312 export async function getPageTitle(url: string) {
313 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
314 let title = await res.title;
318 export function debounce(
321 immediate: boolean = false
323 // 'private' variable for instance
324 // The returned function will be able to reference this due to closure.
325 // Each call to the returned function will share this common timer.
328 // Calling debounce returns a new anonymous function
330 // reference the context and args for the setTimeout function
334 // Should the function be called now? If immediate is true
335 // and not already in a timeout then the answer is: Yes
336 var callNow = immediate && !timeout;
338 // This is the basic debounce behaviour where you can call this
339 // function several times, but it will only execute once
340 // [before or after imposing a delay].
341 // Each time the returned function is called, the timer starts over.
342 clearTimeout(timeout);
344 // Set the new timeout
345 timeout = setTimeout(function () {
346 // Inside the timeout function, clear the timeout variable
347 // which will let the next execution run when in 'immediate' mode
350 // Check if the function already ran with the immediate flag
352 // Call the original function with apply
353 // apply lets you define the 'this' object as well as the arguments
354 // (both captured before setTimeout)
355 func.apply(context, args);
359 // Immediate mode and no wait timer? Execute the function..
360 if (callNow) func.apply(context, args);
365 export function getLanguage(override?: string): string {
366 let user = UserService.Instance.user;
367 let lang = override || (user && user.lang ? user.lang : 'browser');
369 if (lang == 'browser' && isBrowser()) {
370 return getBrowserLanguage();
377 export function getBrowserLanguage(): string {
378 return navigator.language;
381 export function getMomentLanguage(): string {
382 let lang = getLanguage();
383 if (lang.startsWith('zh')) {
385 } else if (lang.startsWith('sv')) {
387 } else if (lang.startsWith('fr')) {
389 } else if (lang.startsWith('de')) {
391 } else if (lang.startsWith('ru')) {
393 } else if (lang.startsWith('es')) {
395 } else if (lang.startsWith('eo')) {
397 } else if (lang.startsWith('nl')) {
399 } else if (lang.startsWith('it')) {
401 } else if (lang.startsWith('fi')) {
403 } else if (lang.startsWith('ca')) {
405 } else if (lang.startsWith('fa')) {
407 } else if (lang.startsWith('pl')) {
409 } else if (lang.startsWith('pt')) {
411 } else if (lang.startsWith('ja')) {
413 } else if (lang.startsWith('ka')) {
415 } else if (lang.startsWith('hi')) {
417 } else if (lang.startsWith('el')) {
419 } else if (lang.startsWith('eu')) {
421 } else if (lang.startsWith('gl')) {
423 } else if (lang.startsWith('tr')) {
425 } else if (lang.startsWith('hu')) {
427 } else if (lang.startsWith('uk')) {
429 } else if (lang.startsWith('sq')) {
431 } else if (lang.startsWith('km')) {
433 } else if (lang.startsWith('ga')) {
435 } else if (lang.startsWith('sr')) {
437 } else if (lang.startsWith('ko')) {
445 export function setTheme(theme: string, forceReload: boolean = false) {
449 if (theme === 'browser' && !forceReload) {
452 // This is only run on a force reload
453 if (theme == 'browser') {
457 // Unload all the other themes
458 for (var i = 0; i < themes.length; i++) {
459 let styleSheet = document.getElementById(themes[i]);
461 styleSheet.setAttribute('disabled', 'disabled');
466 .getElementById('default-light')
467 ?.setAttribute('disabled', 'disabled');
468 document.getElementById('default-dark')?.setAttribute('disabled', 'disabled');
470 // Load the theme dynamically
471 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
472 loadCss(theme, cssLoc);
473 document.getElementById(theme).removeAttribute('disabled');
476 export function loadCss(id: string, loc: string) {
477 if (!document.getElementById(id)) {
478 var head = document.getElementsByTagName('head')[0];
479 var link = document.createElement('link');
481 link.rel = 'stylesheet';
482 link.type = 'text/css';
485 head.appendChild(link);
489 export function objectFlip(obj: any) {
491 Object.keys(obj).forEach(key => {
497 export function showAvatars(): boolean {
499 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
500 !UserService.Instance.user
504 export function isCakeDay(published: string): boolean {
505 // moment(undefined) or moment.utc(undefined) returns the current date/time
506 // moment(null) or moment.utc(null) returns null
507 const userCreationDate = moment.utc(published || null).local();
508 const currentDate = moment(new Date());
511 userCreationDate.date() === currentDate.date() &&
512 userCreationDate.month() === currentDate.month() &&
513 userCreationDate.year() !== currentDate.year()
517 export function isCommentType(
518 item: Comment | PrivateMessage | Post
521 (item as Comment).community_id !== undefined &&
522 (item as Comment).content !== undefined
526 export function isPostType(
527 item: Comment | PrivateMessage | Post
529 return (item as Post).stickied !== undefined;
532 export function toast(text: string, background: string = 'success') {
533 let backgroundColor = `var(--${background})`;
536 backgroundColor: backgroundColor,
542 export function pictrsDeleteToast(
543 clickToDeleteText: string,
544 deletePictureText: string,
547 let backgroundColor = `var(--light)`;
548 let toast = Toastify({
549 text: clickToDeleteText,
550 backgroundColor: backgroundColor,
556 window.location.replace(deleteUrl);
557 alert(deletePictureText);
565 interface NotifyInfo {
572 export function messageToastify(info: NotifyInfo, router: any) {
573 let htmlBody = info.body ? md.render(info.body) : '';
574 let backgroundColor = `var(--light)`;
576 let toast = Toastify({
577 text: `${htmlBody}<br />${info.name}`,
579 backgroundColor: backgroundColor,
580 className: 'text-dark',
588 router.history.push(info.link);
594 export function notifyPost(post: Post, router: any) {
595 let info: NotifyInfo = {
596 name: post.community_name,
597 icon: post.community_icon ? post.community_icon : defaultFavIcon,
598 link: `/post/${post.id}`,
601 notify(info, router);
604 export function notifyComment(comment: Comment, router: any) {
605 let info: NotifyInfo = {
606 name: comment.creator_name,
607 icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon,
608 link: `/post/${comment.post_id}/comment/${comment.id}`,
609 body: comment.content,
611 notify(info, router);
614 export function notifyPrivateMessage(pm: PrivateMessage, router: any) {
615 let info: NotifyInfo = {
616 name: pm.creator_name,
617 icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
621 notify(info, router);
624 function notify(info: NotifyInfo, router: any) {
625 messageToastify(info, router);
627 if (Notification.permission !== 'granted') Notification.requestPermission();
629 var notification = new Notification(info.name, {
634 notification.onclick = () => {
635 event.preventDefault();
636 router.history.push(info.link);
641 export function setupTribute() {
643 noMatchTemplate: function () {
650 menuItemTemplate: (item: any) => {
651 let shortName = `:${item.original.key}:`;
652 return `${item.original.val} ${shortName}`;
654 selectTemplate: (item: any) => {
655 return `:${item.original.key}:`;
657 values: Object.entries(emojiShortName).map(e => {
658 return { key: e[1], val: e[0] };
661 autocompleteMode: true,
663 // menuItemLimit: mentionDropdownFetchLimit,
664 menuShowMinLength: 2,
669 selectTemplate: (item: any) => {
670 let link = item.original.local
671 ? `[${item.original.key}](/u/${item.original.name})`
672 : `[${item.original.key}](/user/${item.original.id})`;
675 values: (text: string, cb: any) => {
676 userSearch(text, (users: any) => cb(users));
679 autocompleteMode: true,
681 // menuItemLimit: mentionDropdownFetchLimit,
682 menuShowMinLength: 2,
688 selectTemplate: (item: any) => {
689 let link = item.original.local
690 ? `[${item.original.key}](/c/${item.original.name})`
691 : `[${item.original.key}](/community/${item.original.id})`;
694 values: (text: string, cb: any) => {
695 communitySearch(text, (communities: any) => cb(communities));
698 autocompleteMode: true,
700 // menuItemLimit: mentionDropdownFetchLimit,
701 menuShowMinLength: 2,
709 tippyInstance = tippy('[data-tippy-content]');
712 export function setupTippy() {
714 tippyInstance.forEach(e => e.destroy());
715 tippyInstance = tippy('[data-tippy-content]', {
717 // Display on "long press"
718 touch: ['hold', 500],
723 function userSearch(text: string, cb: any) {
725 let form: SearchForm = {
727 type_: SearchType.Users,
728 sort: SortType.TopAll,
730 limit: mentionDropdownFetchLimit,
733 WebSocketService.Instance.search(form);
735 let userSub = WebSocketService.Instance.subject.subscribe(
737 let res = wsJsonToRes(msg);
738 if (res.op == UserOperation.Search) {
739 let data = res.data as SearchResponse;
740 let users = data.users.map(u => {
742 key: `@${u.name}@${hostname(u.actor_id)}`,
749 userSub.unsubscribe();
752 err => console.error(err),
753 () => console.log('complete')
760 function communitySearch(text: string, cb: any) {
762 let form: SearchForm = {
764 type_: SearchType.Communities,
765 sort: SortType.TopAll,
767 limit: mentionDropdownFetchLimit,
770 WebSocketService.Instance.search(form);
772 let communitySub = WebSocketService.Instance.subject.subscribe(
774 let res = wsJsonToRes(msg);
775 if (res.op == UserOperation.Search) {
776 let data = res.data as SearchResponse;
777 let communities = data.communities.map(c => {
779 key: `!${c.name}@${hostname(c.actor_id)}`,
786 communitySub.unsubscribe();
789 err => console.error(err),
790 () => console.log('complete')
797 export function getListingTypeFromProps(props: any): ListingType {
798 return props.match.params.listing_type
799 ? routeListingTypeToEnum(props.match.params.listing_type)
800 : UserService.Instance.user
801 ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
805 // TODO might need to add a user setting for this too
806 export function getDataTypeFromProps(props: any): DataType {
807 return props.match.params.data_type
808 ? routeDataTypeToEnum(props.match.params.data_type)
812 export function getSortTypeFromProps(props: any): SortType {
813 return props.match.params.sort
814 ? routeSortTypeToEnum(props.match.params.sort)
815 : UserService.Instance.user
816 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
820 export function getPageFromProps(props: any): number {
821 return props.match.params.page ? Number(props.match.params.page) : 1;
824 export function getRecipientIdFromProps(props: any): number {
825 return props.match.params.recipient_id
826 ? Number(props.match.params.recipient_id)
830 export function getIdFromProps(props: any): number {
831 return Number(props.match.params.id);
834 export function getCommentIdFromProps(props: any): number {
835 return Number(props.match.params.comment_id);
838 export function getUsernameFromProps(props: any): string {
839 return props.match.params.username;
842 export function editCommentRes(data: CommentResponse, comments: Comment[]) {
843 let found = comments.find(c => c.id == data.comment.id);
845 found.content = data.comment.content;
846 found.updated = data.comment.updated;
847 found.removed = data.comment.removed;
848 found.deleted = data.comment.deleted;
849 found.upvotes = data.comment.upvotes;
850 found.downvotes = data.comment.downvotes;
851 found.score = data.comment.score;
855 export function saveCommentRes(data: CommentResponse, comments: Comment[]) {
856 let found = comments.find(c => c.id == data.comment.id);
858 found.saved = data.comment.saved;
862 export function createCommentLikeRes(
863 data: CommentResponse,
866 let found: Comment = comments.find(c => c.id === data.comment.id);
868 found.score = data.comment.score;
869 found.upvotes = data.comment.upvotes;
870 found.downvotes = data.comment.downvotes;
871 if (data.comment.my_vote !== null) {
872 found.my_vote = data.comment.my_vote;
877 export function createPostLikeFindRes(data: PostResponse, posts: Post[]) {
878 let found = posts.find(c => c.id == data.post.id);
880 createPostLikeRes(data, found);
884 export function createPostLikeRes(data: PostResponse, post: Post) {
886 post.score = data.post.score;
887 post.upvotes = data.post.upvotes;
888 post.downvotes = data.post.downvotes;
889 if (data.post.my_vote !== null) {
890 post.my_vote = data.post.my_vote;
895 export function editPostFindRes(data: PostResponse, posts: Post[]) {
896 let found = posts.find(c => c.id == data.post.id);
898 editPostRes(data, found);
902 export function editPostRes(data: PostResponse, post: Post) {
904 post.url = data.post.url;
905 post.name = data.post.name;
906 post.nsfw = data.post.nsfw;
907 post.deleted = data.post.deleted;
908 post.removed = data.post.removed;
909 post.stickied = data.post.stickied;
910 post.body = data.post.body;
911 post.locked = data.post.locked;
912 post.saved = data.post.saved;
916 export function commentsToFlatNodes(comments: Comment[]): CommentNodeI[] {
917 let nodes: CommentNodeI[] = [];
918 for (let comment of comments) {
919 nodes.push({ comment: comment });
924 export function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
925 // First, put removed and deleted comments at the bottom, then do your other sorts
926 if (sort == CommentSortType.Top) {
929 +a.comment.removed - +b.comment.removed ||
930 +a.comment.deleted - +b.comment.deleted ||
931 b.comment.score - a.comment.score
933 } else if (sort == CommentSortType.New) {
936 +a.comment.removed - +b.comment.removed ||
937 +a.comment.deleted - +b.comment.deleted ||
938 b.comment.published.localeCompare(a.comment.published)
940 } else if (sort == CommentSortType.Old) {
943 +a.comment.removed - +b.comment.removed ||
944 +a.comment.deleted - +b.comment.deleted ||
945 a.comment.published.localeCompare(b.comment.published)
947 } else if (sort == CommentSortType.Hot) {
950 +a.comment.removed - +b.comment.removed ||
951 +a.comment.deleted - +b.comment.deleted ||
952 hotRankComment(b.comment) - hotRankComment(a.comment)
956 // Go through the children recursively
957 for (let node of tree) {
959 commentSort(node.children, sort);
964 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
965 commentSort(tree, convertCommentSortType(sort));
968 function convertCommentSortType(sort: SortType): CommentSortType {
970 sort == SortType.TopAll ||
971 sort == SortType.TopDay ||
972 sort == SortType.TopWeek ||
973 sort == SortType.TopMonth ||
974 sort == SortType.TopYear
976 return CommentSortType.Top;
977 } else if (sort == SortType.New) {
978 return CommentSortType.New;
979 } else if (sort == SortType.Hot || sort == SortType.Active) {
980 return CommentSortType.Hot;
982 return CommentSortType.Hot;
986 export function postSort(
989 communityType: boolean
991 // First, put removed and deleted comments at the bottom, then do your other sorts
993 sort == SortType.TopAll ||
994 sort == SortType.TopDay ||
995 sort == SortType.TopWeek ||
996 sort == SortType.TopMonth ||
997 sort == SortType.TopYear
1001 +a.removed - +b.removed ||
1002 +a.deleted - +b.deleted ||
1003 (communityType && +b.stickied - +a.stickied) ||
1006 } else if (sort == SortType.New) {
1009 +a.removed - +b.removed ||
1010 +a.deleted - +b.deleted ||
1011 (communityType && +b.stickied - +a.stickied) ||
1012 b.published.localeCompare(a.published)
1014 } else if (sort == SortType.Hot) {
1017 +a.removed - +b.removed ||
1018 +a.deleted - +b.deleted ||
1019 (communityType && +b.stickied - +a.stickied) ||
1020 b.hot_rank - a.hot_rank
1022 } else if (sort == SortType.Active) {
1025 +a.removed - +b.removed ||
1026 +a.deleted - +b.deleted ||
1027 (communityType && +b.stickied - +a.stickied) ||
1028 b.hot_rank_active - a.hot_rank_active
1033 export const colorList: string[] = [
1042 function hsl(num: number) {
1043 return `hsla(${num}, 35%, 50%, 1)`;
1046 // function randomHsl() {
1047 // return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
1050 export function previewLines(
1052 maxChars: number = 300,
1053 maxLines: number = 1
1059 // Use lines * 2 because markdown requires 2 lines
1060 .slice(0, maxLines * 2)
1065 export function hostname(url: string): string {
1066 let cUrl = new URL(url);
1067 return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1070 export function validTitle(title?: string): boolean {
1071 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1072 if (title === null || title.length < 3) return true;
1074 const regex = new RegExp(/.*\S.*/, 'g');
1076 return regex.test(title);
1079 export function siteBannerCss(banner: string): string {
1081 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1082 background-attachment: fixed; \
1083 background-position: top; \
1084 background-repeat: no-repeat; \
1085 background-size: 100% cover; \
1088 max-height: 100vh; \
1092 export function isBrowser() {
1093 return typeof window !== 'undefined';
1096 export function setAuth(obj: any, auth: string) {
1102 export function setIsoData(context: any): IsoData {
1103 let isoData: IsoData = isBrowser()
1105 : context.router.staticContext;
1109 export function wsSubscribe(parseMessage: any): Subscription {
1111 return WebSocketService.Instance.subject
1112 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1114 msg => parseMessage(msg),
1115 err => console.error(err),
1116 () => console.log('complete')
1123 moment.updateLocale('en', {