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,
44 } from 'lemmy-js-client';
50 CommentNode as CommentNodeI,
51 } 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 supportLemmyUrl = 'https://join.lemmy.ml/sponsors';
78 export const helpGuideUrl = '/docs/about/guide.html';
79 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
80 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
81 export const archiveUrl = 'https://archive.is';
82 export const elementUrl = 'https://element.io/';
84 export const postRefetchSeconds: number = 60 * 1000;
85 export const fetchLimit: number = 20;
86 export const mentionDropdownFetchLimit = 10;
88 export const languages = [
89 { code: 'ca', name: 'Català' },
90 { code: 'en', name: 'English' },
91 { code: 'el', name: 'Ελληνικά' },
92 { code: 'eu', name: 'Euskara' },
93 { code: 'eo', name: 'Esperanto' },
94 { code: 'es', name: 'Español' },
95 { code: 'da', name: 'Dansk' },
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<ResponseType>(
160 msg: WebSocketJsonResponse<ResponseType>
161 ): WebSocketResponse<ResponseType> {
168 export function wsUserOp(msg: any): UserOperation {
169 let opStr: string = msg.op;
170 return UserOperation[opStr];
173 export const md = new markdown_it({
178 .use(markdown_it_sub)
179 .use(markdown_it_sup)
180 .use(markdown_it_container, 'spoiler', {
181 validate: function (params: any) {
182 return params.trim().match(/^spoiler\s+(.*)$/);
185 render: function (tokens: any, idx: any) {
186 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
188 if (tokens[idx].nesting === 1) {
190 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
193 return '</details>\n';
197 .use(markdownitEmoji, {
198 defs: objectFlip(emojiShortName),
201 export function hotRankComment(comment_view: CommentView): number {
202 return hotRank(comment_view.counts.score, comment_view.comment.published);
205 export function hotRankActivePost(post_view: PostView): number {
206 return hotRank(post_view.counts.score, post_view.counts.newest_comment_time);
209 export function hotRankPost(post_view: PostView): number {
210 return hotRank(post_view.counts.score, post_view.post.published);
213 export function hotRank(score: number, timeStr: string): number {
214 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
215 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
216 let now: Date = new Date();
217 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
220 (10000 * Math.log10(Math.max(1, 3 + score))) /
221 Math.pow(hoursElapsed + 2, 1.8);
223 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
228 export function mdToHtml(text: string) {
229 return { __html: md.render(text) };
232 export function getUnixTime(text: string): number {
233 return text ? new Date(text).getTime() / 1000 : undefined;
236 export function canMod(
240 onSelf: boolean = false
242 // You can do moderator actions only on the mods added after you.
244 let yourIndex = modIds.findIndex(id => id == user.id);
245 if (yourIndex == -1) {
248 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
249 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
250 return !modIds.includes(creator_id);
257 export function isMod(modIds: number[], creator_id: number): boolean {
258 return modIds.includes(creator_id);
261 const imageRegex = new RegExp(
262 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
264 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
266 export function isImage(url: string) {
267 return imageRegex.test(url);
270 export function isVideo(url: string) {
271 return videoRegex.test(url);
274 export function validURL(str: string) {
275 return !!new URL(str);
278 export function communityRSSUrl(actorId: string, sort: string): string {
279 let url = new URL(actorId);
280 return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
283 export function validEmail(email: string) {
284 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
285 return re.test(String(email).toLowerCase());
288 export function capitalizeFirstLetter(str: string): string {
289 return str.charAt(0).toUpperCase() + str.slice(1);
292 export function routeSortTypeToEnum(sort: string): SortType {
293 return SortType[sort];
296 export function routeListingTypeToEnum(type: string): ListingType {
297 return ListingType[type];
300 export function routeDataTypeToEnum(type: string): DataType {
301 return DataType[capitalizeFirstLetter(type)];
304 export function routeSearchTypeToEnum(type: string): SearchType {
305 return SearchType[type];
308 export async function getPageTitle(url: string) {
309 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
310 let title = await res.title;
314 export function debounce(
317 immediate: boolean = false
319 // 'private' variable for instance
320 // The returned function will be able to reference this due to closure.
321 // Each call to the returned function will share this common timer.
324 // Calling debounce returns a new anonymous function
326 // reference the context and args for the setTimeout function
330 // Should the function be called now? If immediate is true
331 // and not already in a timeout then the answer is: Yes
332 var callNow = immediate && !timeout;
334 // This is the basic debounce behaviour where you can call this
335 // function several times, but it will only execute once
336 // [before or after imposing a delay].
337 // Each time the returned function is called, the timer starts over.
338 clearTimeout(timeout);
340 // Set the new timeout
341 timeout = setTimeout(function () {
342 // Inside the timeout function, clear the timeout variable
343 // which will let the next execution run when in 'immediate' mode
346 // Check if the function already ran with the immediate flag
348 // Call the original function with apply
349 // apply lets you define the 'this' object as well as the arguments
350 // (both captured before setTimeout)
351 func.apply(context, args);
355 // Immediate mode and no wait timer? Execute the function..
356 if (callNow) func.apply(context, args);
361 export function getLanguage(override?: string): string {
362 let user = UserService.Instance.user;
363 let lang = override || (user && user.lang ? user.lang : 'browser');
365 if (lang == 'browser' && isBrowser()) {
366 return getBrowserLanguage();
373 export function getBrowserLanguage(): string {
374 return navigator.language;
377 export function getMomentLanguage(): string {
378 let lang = getLanguage();
379 if (lang.startsWith('zh')) {
381 } else if (lang.startsWith('sv')) {
383 } else if (lang.startsWith('fr')) {
385 } else if (lang.startsWith('de')) {
387 } else if (lang.startsWith('ru')) {
389 } else if (lang.startsWith('es')) {
391 } else if (lang.startsWith('eo')) {
393 } else if (lang.startsWith('nl')) {
395 } else if (lang.startsWith('it')) {
397 } else if (lang.startsWith('fi')) {
399 } else if (lang.startsWith('ca')) {
401 } else if (lang.startsWith('fa')) {
403 } else if (lang.startsWith('pl')) {
405 } else if (lang.startsWith('pt')) {
407 } else if (lang.startsWith('ja')) {
409 } else if (lang.startsWith('ka')) {
411 } else if (lang.startsWith('hi')) {
413 } else if (lang.startsWith('el')) {
415 } else if (lang.startsWith('eu')) {
417 } else if (lang.startsWith('gl')) {
419 } else if (lang.startsWith('tr')) {
421 } else if (lang.startsWith('hu')) {
423 } else if (lang.startsWith('uk')) {
425 } else if (lang.startsWith('sq')) {
427 } else if (lang.startsWith('km')) {
429 } else if (lang.startsWith('ga')) {
431 } else if (lang.startsWith('sr')) {
433 } else if (lang.startsWith('ko')) {
435 } else if (lang.startsWith('da')) {
443 export function setTheme(theme: string, forceReload: boolean = false) {
447 if (theme === 'browser' && !forceReload) {
450 // This is only run on a force reload
451 if (theme == 'browser') {
455 // Unload all the other themes
456 for (var i = 0; i < themes.length; i++) {
457 let styleSheet = document.getElementById(themes[i]);
459 styleSheet.setAttribute('disabled', 'disabled');
464 .getElementById('default-light')
465 ?.setAttribute('disabled', 'disabled');
466 document.getElementById('default-dark')?.setAttribute('disabled', 'disabled');
468 // Load the theme dynamically
469 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
470 loadCss(theme, cssLoc);
471 document.getElementById(theme).removeAttribute('disabled');
474 export function loadCss(id: string, loc: string) {
475 if (!document.getElementById(id)) {
476 var head = document.getElementsByTagName('head')[0];
477 var link = document.createElement('link');
479 link.rel = 'stylesheet';
480 link.type = 'text/css';
483 head.appendChild(link);
487 export function objectFlip(obj: any) {
489 Object.keys(obj).forEach(key => {
495 export function showAvatars(): boolean {
497 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
498 !UserService.Instance.user
502 export function isCakeDay(published: string): boolean {
503 // moment(undefined) or moment.utc(undefined) returns the current date/time
504 // moment(null) or moment.utc(null) returns null
505 const userCreationDate = moment.utc(published || null).local();
506 const currentDate = moment(new Date());
509 userCreationDate.date() === currentDate.date() &&
510 userCreationDate.month() === currentDate.month() &&
511 userCreationDate.year() !== currentDate.year()
515 export function toast(text: string, background: string = 'success') {
517 let backgroundColor = `var(--${background})`;
520 backgroundColor: backgroundColor,
527 export function pictrsDeleteToast(
528 clickToDeleteText: string,
529 deletePictureText: string,
533 let backgroundColor = `var(--light)`;
534 let toast = Toastify({
535 text: clickToDeleteText,
536 backgroundColor: backgroundColor,
542 window.location.replace(deleteUrl);
543 alert(deletePictureText);
552 interface NotifyInfo {
559 export function messageToastify(info: NotifyInfo, router: any) {
561 let htmlBody = info.body ? md.render(info.body) : '';
562 let backgroundColor = `var(--light)`;
564 let toast = Toastify({
565 text: `${htmlBody}<br />${info.name}`,
567 backgroundColor: backgroundColor,
568 className: 'text-dark',
576 router.history.push(info.link);
583 export function notifyPost(post_view: PostView, router: any) {
584 let info: NotifyInfo = {
585 name: post_view.community.name,
586 icon: post_view.community.icon ? post_view.community.icon : defaultFavIcon,
587 link: `/post/${post_view.post.id}`,
588 body: post_view.post.name,
590 notify(info, router);
593 export function notifyComment(comment_view: CommentView, router: any) {
594 let info: NotifyInfo = {
595 name: comment_view.creator.name,
596 icon: comment_view.creator.avatar
597 ? comment_view.creator.avatar
599 link: `/post/${comment_view.post.id}/comment/${comment_view.comment.id}`,
600 body: comment_view.comment.content,
602 notify(info, router);
605 export function notifyPrivateMessage(pmv: PrivateMessageView, router: any) {
606 let info: NotifyInfo = {
607 name: pmv.creator.name,
608 icon: pmv.creator.avatar ? pmv.creator.avatar : defaultFavIcon,
610 body: pmv.private_message.content,
612 notify(info, router);
615 function notify(info: NotifyInfo, router: any) {
616 messageToastify(info, router);
618 if (Notification.permission !== 'granted') Notification.requestPermission();
620 var notification = new Notification(info.name, {
625 notification.onclick = () => {
626 event.preventDefault();
627 router.history.push(info.link);
632 export function setupTribute() {
634 noMatchTemplate: function () {
641 menuItemTemplate: (item: any) => {
642 let shortName = `:${item.original.key}:`;
643 return `${item.original.val} ${shortName}`;
645 selectTemplate: (item: any) => {
646 return `:${item.original.key}:`;
648 values: Object.entries(emojiShortName).map(e => {
649 return { key: e[1], val: e[0] };
652 autocompleteMode: true,
654 // menuItemLimit: mentionDropdownFetchLimit,
655 menuShowMinLength: 2,
660 selectTemplate: (item: any) => {
661 let link = item.original.local
662 ? `[${item.original.key}](/u/${item.original.name})`
663 : `[${item.original.key}](/user/${item.original.id})`;
666 values: (text: string, cb: any) => {
667 userSearch(text, (users: any) => cb(users));
670 autocompleteMode: true,
672 // menuItemLimit: mentionDropdownFetchLimit,
673 menuShowMinLength: 2,
679 selectTemplate: (item: any) => {
680 let link = item.original.local
681 ? `[${item.original.key}](/c/${item.original.name})`
682 : `[${item.original.key}](/community/${item.original.id})`;
685 values: (text: string, cb: any) => {
686 communitySearch(text, (communities: any) => cb(communities));
689 autocompleteMode: true,
691 // menuItemLimit: mentionDropdownFetchLimit,
692 menuShowMinLength: 2,
700 tippyInstance = tippy('[data-tippy-content]');
703 export function setupTippy() {
705 tippyInstance.forEach(e => e.destroy());
706 tippyInstance = tippy('[data-tippy-content]', {
708 // Display on "long press"
709 touch: ['hold', 500],
714 function userSearch(text: string, cb: any) {
718 type_: SearchType.Users,
719 sort: SortType.TopAll,
721 limit: mentionDropdownFetchLimit,
722 auth: UserService.Instance.authField(false),
725 WebSocketService.Instance.client.search(form);
727 let userSub = WebSocketService.Instance.subject.subscribe(
729 let res = wsJsonToRes(msg);
730 if (res.op == UserOperation.Search) {
731 let data = res.data as SearchResponse;
732 let users = data.users.map(uv => {
734 key: `@${uv.user.name}@${hostname(uv.user.actor_id)}`,
736 local: uv.user.local,
741 userSub.unsubscribe();
744 err => console.error(err),
745 () => console.log('complete')
752 function communitySearch(text: string, cb: any) {
756 type_: SearchType.Communities,
757 sort: SortType.TopAll,
759 limit: mentionDropdownFetchLimit,
760 auth: UserService.Instance.authField(false),
763 WebSocketService.Instance.client.search(form);
765 let communitySub = WebSocketService.Instance.subject.subscribe(
767 let res = wsJsonToRes(msg);
768 if (res.op == UserOperation.Search) {
769 let data = res.data as SearchResponse;
770 let communities = data.communities.map(cv => {
772 key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
773 name: cv.community.name,
774 local: cv.community.local,
779 communitySub.unsubscribe();
782 err => console.error(err),
783 () => console.log('complete')
790 export function getListingTypeFromProps(props: any): ListingType {
791 return props.match.params.listing_type
792 ? routeListingTypeToEnum(props.match.params.listing_type)
793 : UserService.Instance.user
794 ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
798 // TODO might need to add a user setting for this too
799 export function getDataTypeFromProps(props: any): DataType {
800 return props.match.params.data_type
801 ? routeDataTypeToEnum(props.match.params.data_type)
805 export function getSortTypeFromProps(props: any): SortType {
806 return props.match.params.sort
807 ? routeSortTypeToEnum(props.match.params.sort)
808 : UserService.Instance.user
809 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
813 export function getPageFromProps(props: any): number {
814 return props.match.params.page ? Number(props.match.params.page) : 1;
817 export function getRecipientIdFromProps(props: any): number {
818 return props.match.params.recipient_id
819 ? Number(props.match.params.recipient_id)
823 export function getIdFromProps(props: any): number {
824 return Number(props.match.params.id);
827 export function getCommentIdFromProps(props: any): number {
828 return Number(props.match.params.comment_id);
831 export function getUsernameFromProps(props: any): string {
832 return props.match.params.username;
835 export function editCommentRes(data: CommentView, comments: CommentView[]) {
836 let found = comments.find(c => c.comment.id == data.comment.id);
838 found.comment.content = data.comment.content;
839 found.comment.updated = data.comment.updated;
840 found.comment.removed = data.comment.removed;
841 found.comment.deleted = data.comment.deleted;
842 found.counts.upvotes = data.counts.upvotes;
843 found.counts.downvotes = data.counts.downvotes;
844 found.counts.score = data.counts.score;
848 export function saveCommentRes(data: CommentView, comments: CommentView[]) {
849 let found = comments.find(c => c.comment.id == data.comment.id);
851 found.saved = data.saved;
855 export function createCommentLikeRes(
857 comments: CommentView[]
859 let found = comments.find(c => c.comment.id === data.comment.id);
861 found.counts.score = data.counts.score;
862 found.counts.upvotes = data.counts.upvotes;
863 found.counts.downvotes = data.counts.downvotes;
864 if (data.my_vote !== null) {
865 found.my_vote = data.my_vote;
870 export function createPostLikeFindRes(data: PostView, posts: PostView[]) {
871 let found = posts.find(p => p.post.id == data.post.id);
873 createPostLikeRes(data, found);
877 export function createPostLikeRes(data: PostView, post_view: PostView) {
879 post_view.counts.score = data.counts.score;
880 post_view.counts.upvotes = data.counts.upvotes;
881 post_view.counts.downvotes = data.counts.downvotes;
882 if (data.my_vote !== null) {
883 post_view.my_vote = data.my_vote;
888 export function editPostFindRes(data: PostView, posts: PostView[]) {
889 let found = posts.find(p => p.post.id == data.post.id);
891 editPostRes(data, found);
895 export function editPostRes(data: PostView, post: PostView) {
897 post.post.url = data.post.url;
898 post.post.name = data.post.name;
899 post.post.nsfw = data.post.nsfw;
900 post.post.deleted = data.post.deleted;
901 post.post.removed = data.post.removed;
902 post.post.stickied = data.post.stickied;
903 post.post.body = data.post.body;
904 post.post.locked = data.post.locked;
905 post.saved = data.saved;
909 export function commentsToFlatNodes(comments: CommentView[]): CommentNodeI[] {
910 let nodes: CommentNodeI[] = [];
911 for (let comment of comments) {
912 nodes.push({ comment_view: comment });
917 export function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
918 // First, put removed and deleted comments at the bottom, then do your other sorts
919 if (sort == CommentSortType.Top) {
922 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
923 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
924 b.comment_view.counts.score - a.comment_view.counts.score
926 } else if (sort == CommentSortType.New) {
929 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
930 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
931 b.comment_view.comment.published.localeCompare(
932 a.comment_view.comment.published
935 } else if (sort == CommentSortType.Old) {
938 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
939 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
940 a.comment_view.comment.published.localeCompare(
941 b.comment_view.comment.published
944 } else if (sort == CommentSortType.Hot) {
947 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
948 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
949 hotRankComment(b.comment_view) - hotRankComment(a.comment_view)
953 // Go through the children recursively
954 for (let node of tree) {
956 commentSort(node.children, sort);
961 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
962 commentSort(tree, convertCommentSortType(sort));
965 function convertCommentSortType(sort: SortType): CommentSortType {
967 sort == SortType.TopAll ||
968 sort == SortType.TopDay ||
969 sort == SortType.TopWeek ||
970 sort == SortType.TopMonth ||
971 sort == SortType.TopYear
973 return CommentSortType.Top;
974 } else if (sort == SortType.New) {
975 return CommentSortType.New;
976 } else if (sort == SortType.Hot || sort == SortType.Active) {
977 return CommentSortType.Hot;
979 return CommentSortType.Hot;
983 export function postSort(
986 communityType: boolean
988 // First, put removed and deleted comments at the bottom, then do your other sorts
990 sort == SortType.TopAll ||
991 sort == SortType.TopDay ||
992 sort == SortType.TopWeek ||
993 sort == SortType.TopMonth ||
994 sort == SortType.TopYear
998 +a.post.removed - +b.post.removed ||
999 +a.post.deleted - +b.post.deleted ||
1000 (communityType && +b.post.stickied - +a.post.stickied) ||
1001 b.counts.score - a.counts.score
1003 } else if (sort == SortType.New) {
1006 +a.post.removed - +b.post.removed ||
1007 +a.post.deleted - +b.post.deleted ||
1008 (communityType && +b.post.stickied - +a.post.stickied) ||
1009 b.post.published.localeCompare(a.post.published)
1011 } else if (sort == SortType.Hot) {
1014 +a.post.removed - +b.post.removed ||
1015 +a.post.deleted - +b.post.deleted ||
1016 (communityType && +b.post.stickied - +a.post.stickied) ||
1017 hotRankPost(b) - hotRankPost(a)
1019 } else if (sort == SortType.Active) {
1022 +a.post.removed - +b.post.removed ||
1023 +a.post.deleted - +b.post.deleted ||
1024 (communityType && +b.post.stickied - +a.post.stickied) ||
1025 hotRankActivePost(b) - hotRankActivePost(a)
1030 export const colorList: string[] = [
1039 function hsl(num: number) {
1040 return `hsla(${num}, 35%, 50%, 1)`;
1043 // function randomHsl() {
1044 // return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
1047 export function previewLines(
1049 maxChars: number = 300,
1050 maxLines: number = 1
1056 // Use lines * 2 because markdown requires 2 lines
1057 .slice(0, maxLines * 2)
1062 export function hostname(url: string): string {
1063 let cUrl = new URL(url);
1064 return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1067 export function validTitle(title?: string): boolean {
1068 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1069 if (title === null || title.length < 3) return true;
1071 const regex = new RegExp(/.*\S.*/, 'g');
1073 return regex.test(title);
1076 export function siteBannerCss(banner: string): string {
1078 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1079 background-attachment: fixed; \
1080 background-position: top; \
1081 background-repeat: no-repeat; \
1082 background-size: 100% cover; \
1085 max-height: 100vh; \
1089 export function isBrowser() {
1090 return typeof window !== 'undefined';
1093 export function setIsoData(context: any): IsoData {
1094 let isoData: IsoData = isBrowser()
1096 : context.router.staticContext;
1100 export function wsSubscribe(parseMessage: any): Subscription {
1102 return WebSocketService.Instance.subject
1103 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1105 msg => parseMessage(msg),
1106 err => console.error(err),
1107 () => console.log('complete')
1114 moment.updateLocale('en', {