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/lemmy';
76 export const helpGuideUrl = '/docs/about_guide.html';
77 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
78 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
79 export const archiveUrl = 'https://archive.is';
80 export const elementUrl = 'https://element.io/';
82 export const postRefetchSeconds: number = 60 * 1000;
83 export const fetchLimit: number = 20;
84 export const mentionDropdownFetchLimit = 10;
86 export const lemmyHttp = new LemmyHttp(httpUri);
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: 'de', name: 'Deutsch' },
96 { code: 'ga', name: 'Gaeilge' },
97 { code: 'gl', name: 'Galego' },
98 { code: 'hu', name: 'Magyar Nyelv' },
99 { code: 'ka', name: 'ქართული ენა' },
100 { code: 'ko', name: '한국어' },
101 { code: 'km', name: 'ភាសាខ្មែរ' },
102 { code: 'hi', name: 'मानक हिन्दी' },
103 { code: 'fa', name: 'فارسی' },
104 { code: 'ja', name: '日本語' },
105 { code: 'pl', name: 'Polski' },
106 { code: 'pt_BR', name: 'Português Brasileiro' },
107 { code: 'zh', name: '中文' },
108 { code: 'fi', name: 'Suomi' },
109 { code: 'fr', name: 'Français' },
110 { code: 'sv', name: 'Svenska' },
111 { code: 'sq', name: 'Shqip' },
112 { code: 'sr_Latn', name: 'srpski' },
113 { code: 'tr', name: 'Türkçe' },
114 { code: 'uk', name: 'Українська Mова' },
115 { code: 'ru', name: 'Русский' },
116 { code: 'nl', name: 'Nederlands' },
117 { code: 'it', name: 'Italiano' },
120 export const themes = [
136 const DEFAULT_ALPHABET =
137 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
139 function getRandomCharFromAlphabet(alphabet: string): string {
140 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
143 export function randomStr(
144 idDesiredLength: number = 20,
145 alphabet = DEFAULT_ALPHABET
148 * Create n-long array and map it to random chars from given alphabet.
149 * Then join individual chars as string
151 return Array.from({ length: idDesiredLength })
153 return getRandomCharFromAlphabet(alphabet);
158 export function wsJsonToRes(msg: WebSocketJsonResponse): WebSocketResponse {
159 let opStr: string = msg.op;
161 op: UserOperation[opStr],
166 export const md = new markdown_it({
171 .use(markdown_it_sub)
172 .use(markdown_it_sup)
173 .use(markdown_it_container, 'spoiler', {
174 validate: function (params: any) {
175 return params.trim().match(/^spoiler\s+(.*)$/);
178 render: function (tokens: any, idx: any) {
179 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
181 if (tokens[idx].nesting === 1) {
183 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
186 return '</details>\n';
190 .use(markdownitEmoji, {
191 defs: objectFlip(emojiShortName),
194 export function hotRankComment(comment: Comment): number {
195 return hotRank(comment.score, comment.published);
198 export function hotRankPost(post: Post): number {
199 return hotRank(post.score, post.newest_activity_time);
202 export function hotRank(score: number, timeStr: string): number {
203 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
204 let date: Date = new Date(timeStr + 'Z'); // Add Z to convert from UTC date
205 let now: Date = new Date();
206 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
209 (10000 * Math.log10(Math.max(1, 3 + score))) /
210 Math.pow(hoursElapsed + 2, 1.8);
212 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
217 export function mdToHtml(text: string) {
218 return { __html: md.render(text) };
221 export function getUnixTime(text: string): number {
222 return text ? new Date(text).getTime() / 1000 : undefined;
225 export function addTypeInfo<T>(
228 ): { type_: string; data: T }[] {
229 return arr.map(e => {
230 return { type_: name, data: e };
234 export function canMod(
238 onSelf: boolean = false
240 // You can do moderator actions only on the mods added after you.
242 let yourIndex = modIds.findIndex(id => id == user.id);
243 if (yourIndex == -1) {
246 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
247 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
248 return !modIds.includes(creator_id);
255 export function isMod(modIds: number[], creator_id: number): boolean {
256 return modIds.includes(creator_id);
259 const imageRegex = new RegExp(
260 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
262 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
264 export function isImage(url: string) {
265 return imageRegex.test(url);
268 export function isVideo(url: string) {
269 return videoRegex.test(url);
273 export function validURL(str: string) {
276 return !!new URL(str);
282 export function validEmail(email: string) {
283 let re = /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
284 return re.test(String(email).toLowerCase());
287 export function capitalizeFirstLetter(str: string): string {
288 return str.charAt(0).toUpperCase() + str.slice(1);
291 export function routeSortTypeToEnum(sort: string): SortType {
292 return SortType[sort];
295 export function routeListingTypeToEnum(type: string): ListingType {
296 return ListingType[type];
299 export function routeDataTypeToEnum(type: string): DataType {
300 return DataType[capitalizeFirstLetter(type)];
303 export function routeSearchTypeToEnum(type: string): SearchType {
304 return SearchType[capitalizeFirstLetter(type)];
307 export async function getPageTitle(url: string) {
308 let res = await fetch(`/iframely/oembed?url=${url}`).then(res => res.json());
309 let title = await res.title;
313 export function debounce(
316 immediate: boolean = false
318 // 'private' variable for instance
319 // The returned function will be able to reference this due to closure.
320 // Each call to the returned function will share this common timer.
323 // Calling debounce returns a new anonymous function
325 // reference the context and args for the setTimeout function
329 // Should the function be called now? If immediate is true
330 // and not already in a timeout then the answer is: Yes
331 var callNow = immediate && !timeout;
333 // This is the basic debounce behaviour where you can call this
334 // function several times, but it will only execute once
335 // [before or after imposing a delay].
336 // Each time the returned function is called, the timer starts over.
337 clearTimeout(timeout);
339 // Set the new timeout
340 timeout = setTimeout(function () {
341 // Inside the timeout function, clear the timeout variable
342 // which will let the next execution run when in 'immediate' mode
345 // Check if the function already ran with the immediate flag
347 // Call the original function with apply
348 // apply lets you define the 'this' object as well as the arguments
349 // (both captured before setTimeout)
350 func.apply(context, args);
354 // Immediate mode and no wait timer? Execute the function..
355 if (callNow) func.apply(context, args);
360 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') {
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')) {
442 export function setTheme(theme: string = 'darkly', loggedIn: boolean = false) {
443 // unload all the other themes
444 // for (var i = 0; i < themes.length; i++) {
445 // let styleSheet = document.getElementById(themes[i]);
447 // styleSheet.setAttribute('disabled', 'disabled');
450 // // if the user is not logged in, we load the default themes and let the browser decide
452 // document.getElementById('default-light').removeAttribute('disabled');
453 // document.getElementById('default-dark').removeAttribute('disabled');
456 // .getElementById('default-light')
457 // .setAttribute('disabled', 'disabled');
459 // .getElementById('default-dark')
460 // .setAttribute('disabled', 'disabled');
461 // // Load the theme dynamically
462 // let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
463 // loadCss(theme, cssLoc);
464 // document.getElementById(theme).removeAttribute('disabled');
468 // export function loadCss(id: string, loc: string) {
469 // if (!document.getElementById(id)) {
470 // var head = document.getElementsByTagName('head')[0];
471 // var link = document.createElement('link');
473 // link.rel = 'stylesheet';
474 // link.type = 'text/css';
476 // link.media = 'all';
477 // head.appendChild(link);
481 export function objectFlip(obj: any) {
483 Object.keys(obj).forEach(key => {
489 export function pictrsAvatarThumbnail(src: string): string {
490 // sample url: http://localhost:8535/pictrs/image/thumbnail256/gs7xuu.jpg
491 let split = src.split('/pictrs/image');
492 let out = `${split[0]}/pictrs/image/${
493 canUseWebP() ? 'webp/' : ''
494 }thumbnail96${split[1]}`;
498 export function showAvatars(): boolean {
500 (UserService.Instance.user && UserService.Instance.user.show_avatars) ||
501 !UserService.Instance.user
505 export function isCakeDay(published: string): boolean {
506 // moment(undefined) or moment.utc(undefined) returns the current date/time
507 // moment(null) or moment.utc(null) returns null
508 const userCreationDate = moment.utc(published || null).local();
509 const currentDate = moment(new Date());
512 userCreationDate.date() === currentDate.date() &&
513 userCreationDate.month() === currentDate.month() &&
514 userCreationDate.year() !== currentDate.year()
518 // Converts to image thumbnail
519 export function pictrsImage(hash: string, thumbnail: boolean = false): string {
520 let root = `/pictrs/image`;
522 // Necessary for other servers / domains
523 if (hash.includes('pictrs')) {
524 let split = hash.split('/pictrs/image/');
525 root = `${split[0]}/pictrs/image`;
529 let out = `${root}/${canUseWebP() ? 'webp/' : ''}${
530 thumbnail ? 'thumbnail256/' : ''
535 export function isCommentType(
536 item: Comment | PrivateMessage | Post
539 (item as Comment).community_id !== undefined &&
540 (item as Comment).content !== undefined
544 export function isPostType(
545 item: Comment | PrivateMessage | Post
547 return (item as Post).stickied !== undefined;
550 export function toast(text: string, background: string = 'success') {
551 let backgroundColor = `var(--${background})`;
554 backgroundColor: backgroundColor,
560 export function pictrsDeleteToast(
561 clickToDeleteText: string,
562 deletePictureText: string,
565 let backgroundColor = `var(--light)`;
566 let toast = Toastify({
567 text: clickToDeleteText,
568 backgroundColor: backgroundColor,
574 window.location.replace(deleteUrl);
575 alert(deletePictureText);
583 interface NotifyInfo {
590 export function messageToastify(info: NotifyInfo, router: any) {
591 let htmlBody = info.body ? md.render(info.body) : '';
592 let backgroundColor = `var(--light)`;
594 let toast = Toastify({
595 text: `${htmlBody}<br />${info.name}`,
597 backgroundColor: backgroundColor,
598 className: 'text-dark',
606 router.history.push(info.link);
612 export function notifyPost(post: Post, router: any) {
613 let info: NotifyInfo = {
614 name: post.community_name,
615 icon: post.community_icon ? post.community_icon : defaultFavIcon,
616 link: `/post/${post.id}`,
619 notify(info, router);
622 export function notifyComment(comment: Comment, router: any) {
623 let info: NotifyInfo = {
624 name: comment.creator_name,
625 icon: comment.creator_avatar ? comment.creator_avatar : defaultFavIcon,
626 link: `/post/${comment.post_id}/comment/${comment.id}`,
627 body: comment.content,
629 notify(info, router);
632 export function notifyPrivateMessage(pm: PrivateMessage, router: any) {
633 let info: NotifyInfo = {
634 name: pm.creator_name,
635 icon: pm.creator_avatar ? pm.creator_avatar : defaultFavIcon,
639 notify(info, router);
642 function notify(info: NotifyInfo, router: any) {
643 messageToastify(info, router);
645 if (Notification.permission !== 'granted') Notification.requestPermission();
647 var notification = new Notification(info.name, {
652 notification.onclick = () => {
653 event.preventDefault();
654 router.history.push(info.link);
659 export function setupTribute() {
661 noMatchTemplate: function () {
668 menuItemTemplate: (item: any) => {
669 let shortName = `:${item.original.key}:`;
670 return `${item.original.val} ${shortName}`;
672 selectTemplate: (item: any) => {
673 return `:${item.original.key}:`;
675 values: Object.entries(emojiShortName).map(e => {
676 return { key: e[1], val: e[0] };
679 autocompleteMode: true,
681 // menuItemLimit: mentionDropdownFetchLimit,
682 menuShowMinLength: 2,
687 selectTemplate: (item: any) => {
688 let link = item.original.local
689 ? `[${item.original.key}](/u/${item.original.name})`
690 : `[${item.original.key}](/user/${item.original.id})`;
693 values: (text: string, cb: any) => {
694 userSearch(text, (users: any) => cb(users));
697 autocompleteMode: true,
699 // menuItemLimit: mentionDropdownFetchLimit,
700 menuShowMinLength: 2,
706 selectTemplate: (item: any) => {
707 let link = item.original.local
708 ? `[${item.original.key}](/c/${item.original.name})`
709 : `[${item.original.key}](/community/${item.original.id})`;
712 values: (text: string, cb: any) => {
713 communitySearch(text, (communities: any) => cb(communities));
716 autocompleteMode: true,
718 // menuItemLimit: mentionDropdownFetchLimit,
719 menuShowMinLength: 2,
727 tippyInstance = tippy('[data-tippy-content]');
730 export function setupTippy() {
732 tippyInstance.forEach(e => e.destroy());
733 tippyInstance = tippy('[data-tippy-content]', {
735 // Display on "long press"
736 touch: ['hold', 500],
741 function userSearch(text: string, cb: any) {
743 let form: SearchForm = {
745 type_: SearchType.Users,
746 sort: SortType.TopAll,
748 limit: mentionDropdownFetchLimit,
751 WebSocketService.Instance.search(form);
753 let userSub = WebSocketService.Instance.subject.subscribe(
755 let res = wsJsonToRes(msg);
756 if (res.op == UserOperation.Search) {
757 let data = res.data as SearchResponse;
758 let users = data.users.map(u => {
760 key: `@${u.name}@${hostname(u.actor_id)}`,
767 userSub.unsubscribe();
770 err => console.error(err),
771 () => console.log('complete')
778 function communitySearch(text: string, cb: any) {
780 let form: SearchForm = {
782 type_: SearchType.Communities,
783 sort: SortType.TopAll,
785 limit: mentionDropdownFetchLimit,
788 WebSocketService.Instance.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 = data.communities.map(c => {
797 key: `!${c.name}@${hostname(c.actor_id)}`,
804 communitySub.unsubscribe();
807 err => console.error(err),
808 () => console.log('complete')
815 export function getListingTypeFromProps(props: any): ListingType {
816 return props.match.params.listing_type
817 ? routeListingTypeToEnum(props.match.params.listing_type)
818 : UserService.Instance.user
819 ? Object.values(ListingType)[UserService.Instance.user.default_listing_type]
823 // TODO might need to add a user setting for this too
824 export function getDataTypeFromProps(props: any): DataType {
825 return props.match.params.data_type
826 ? routeDataTypeToEnum(props.match.params.data_type)
830 export function getSortTypeFromProps(props: any): SortType {
831 return props.match.params.sort
832 ? routeSortTypeToEnum(props.match.params.sort)
833 : UserService.Instance.user
834 ? Object.values(SortType)[UserService.Instance.user.default_sort_type]
838 export function getPageFromProps(props: any): number {
839 return props.match.params.page ? Number(props.match.params.page) : 1;
842 export function getRecipientIdFromProps(props: any): number {
843 return props.match.params.recipient_id
844 ? Number(props.match.params.recipient_id)
848 export function getIdFromProps(props: any): number {
849 return Number(props.match.params.id);
852 export function getCommentIdFromProps(props: any): number {
853 return Number(props.match.params.comment_id);
856 export function getUsernameFromProps(props: any): string {
857 return props.match.params.username;
860 export function editCommentRes(data: CommentResponse, comments: Comment[]) {
861 let found = comments.find(c => c.id == data.comment.id);
863 found.content = data.comment.content;
864 found.updated = data.comment.updated;
865 found.removed = data.comment.removed;
866 found.deleted = data.comment.deleted;
867 found.upvotes = data.comment.upvotes;
868 found.downvotes = data.comment.downvotes;
869 found.score = data.comment.score;
873 export function saveCommentRes(data: CommentResponse, comments: Comment[]) {
874 let found = comments.find(c => c.id == data.comment.id);
876 found.saved = data.comment.saved;
880 export function createCommentLikeRes(
881 data: CommentResponse,
884 let found: Comment = comments.find(c => c.id === data.comment.id);
886 found.score = data.comment.score;
887 found.upvotes = data.comment.upvotes;
888 found.downvotes = data.comment.downvotes;
889 if (data.comment.my_vote !== null) {
890 found.my_vote = data.comment.my_vote;
895 export function createPostLikeFindRes(data: PostResponse, posts: Post[]) {
896 let found = posts.find(c => c.id == data.post.id);
898 createPostLikeRes(data, found);
902 export function createPostLikeRes(data: PostResponse, post: Post) {
904 post.score = data.post.score;
905 post.upvotes = data.post.upvotes;
906 post.downvotes = data.post.downvotes;
907 if (data.post.my_vote !== null) {
908 post.my_vote = data.post.my_vote;
913 export function editPostFindRes(data: PostResponse, posts: Post[]) {
914 let found = posts.find(c => c.id == data.post.id);
916 editPostRes(data, found);
920 export function editPostRes(data: PostResponse, post: Post) {
922 post.url = data.post.url;
923 post.name = data.post.name;
924 post.nsfw = data.post.nsfw;
925 post.deleted = data.post.deleted;
926 post.removed = data.post.removed;
927 post.stickied = data.post.stickied;
928 post.body = data.post.body;
929 post.locked = data.post.locked;
933 export function commentsToFlatNodes(comments: Comment[]): CommentNodeI[] {
934 let nodes: CommentNodeI[] = [];
935 for (let comment of comments) {
936 nodes.push({ comment: 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.removed - +b.comment.removed ||
947 +a.comment.deleted - +b.comment.deleted ||
948 b.comment.score - a.comment.score
950 } else if (sort == CommentSortType.New) {
953 +a.comment.removed - +b.comment.removed ||
954 +a.comment.deleted - +b.comment.deleted ||
955 b.comment.published.localeCompare(a.comment.published)
957 } else if (sort == CommentSortType.Old) {
960 +a.comment.removed - +b.comment.removed ||
961 +a.comment.deleted - +b.comment.deleted ||
962 a.comment.published.localeCompare(b.comment.published)
964 } else if (sort == CommentSortType.Hot) {
967 +a.comment.removed - +b.comment.removed ||
968 +a.comment.deleted - +b.comment.deleted ||
969 hotRankComment(b.comment) - hotRankComment(a.comment)
973 // Go through the children recursively
974 for (let node of tree) {
976 commentSort(node.children, sort);
981 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
982 commentSort(tree, convertCommentSortType(sort));
985 function convertCommentSortType(sort: SortType): CommentSortType {
987 sort == SortType.TopAll ||
988 sort == SortType.TopDay ||
989 sort == SortType.TopWeek ||
990 sort == SortType.TopMonth ||
991 sort == SortType.TopYear
993 return CommentSortType.Top;
994 } else if (sort == SortType.New) {
995 return CommentSortType.New;
996 } else if (sort == SortType.Hot || sort == SortType.Active) {
997 return CommentSortType.Hot;
999 return CommentSortType.Hot;
1003 export function postSort(
1006 communityType: boolean
1008 // First, put removed and deleted comments at the bottom, then do your other sorts
1010 sort == SortType.TopAll ||
1011 sort == SortType.TopDay ||
1012 sort == SortType.TopWeek ||
1013 sort == SortType.TopMonth ||
1014 sort == SortType.TopYear
1018 +a.removed - +b.removed ||
1019 +a.deleted - +b.deleted ||
1020 (communityType && +b.stickied - +a.stickied) ||
1023 } else if (sort == SortType.New) {
1026 +a.removed - +b.removed ||
1027 +a.deleted - +b.deleted ||
1028 (communityType && +b.stickied - +a.stickied) ||
1029 b.published.localeCompare(a.published)
1031 } else if (sort == SortType.Hot) {
1034 +a.removed - +b.removed ||
1035 +a.deleted - +b.deleted ||
1036 (communityType && +b.stickied - +a.stickied) ||
1037 b.hot_rank - a.hot_rank
1039 } else if (sort == SortType.Active) {
1042 +a.removed - +b.removed ||
1043 +a.deleted - +b.deleted ||
1044 (communityType && +b.stickied - +a.stickied) ||
1045 b.hot_rank_active - a.hot_rank_active
1050 export const colorList: string[] = [
1059 function hsl(num: number) {
1060 return `hsla(${num}, 35%, 50%, 1)`;
1063 function randomHsl() {
1064 return `hsla(${Math.random() * 360}, 100%, 50%, 1)`;
1067 export function previewLines(
1069 maxChars: number = 300,
1070 maxLines: number = 1
1076 // Use lines * 2 because markdown requires 2 lines
1077 .slice(0, maxLines * 2)
1082 export function hostname(url: string): string {
1083 let cUrl = new URL(url);
1085 return `${cUrl.hostname}:${cUrl.port}`;
1086 // return window.location.port
1087 // ? `${cUrl.hostname}:${cUrl.port}`
1088 // : `${cUrl.hostname}`;
1091 function canUseWebP() {
1092 // TODO pictshare might have a webp conversion bug, try disabling this
1095 // var elem = document.createElement('canvas');
1096 // if (!!(elem.getContext && elem.getContext('2d'))) {
1097 // var testString = !(window.mozInnerScreenX == null) ? 'png' : 'webp';
1098 // // was able or not to get WebP representation
1100 // elem.toDataURL('image/webp').startsWith('data:image/' + testString)
1104 // // very old browser like IE 8, canvas not supported
1108 export function validTitle(title?: string): boolean {
1109 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1110 if (title === null || title.length < 3) return true;
1112 const regex = new RegExp(/.*\S.*/, 'g');
1114 return regex.test(title);
1117 export function siteBannerCss(banner: string): string {
1119 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1120 background-attachment: fixed; \
1121 background-position: top; \
1122 background-repeat: no-repeat; \
1123 background-size: 100% cover; \
1126 max-height: 100vh; \
1130 export function isBrowser() {
1131 return typeof window !== 'undefined';
1134 export function setAuth(obj: any, auth: string) {
1140 export function setIsoData(context: any): IsoData {
1142 return window.isoData;
1144 return context.router.staticContext;
1148 export function wsSubscribe(parseMessage: any): Subscription {
1150 return WebSocketService.Instance.subject
1151 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1153 msg => parseMessage(msg),
1154 err => console.error(err),
1155 () => console.log('complete')