1 import emojiShortName from "emoji-short-name";
3 BlockCommunityResponse,
23 WebSocketJsonResponse,
25 } from "lemmy-js-client";
26 import markdown_it from "markdown-it";
27 import markdown_it_container from "markdown-it-container";
28 import markdown_it_sub from "markdown-it-sub";
29 import markdown_it_sup from "markdown-it-sup";
30 import moment from "moment";
31 import "moment/locale/bg";
32 import "moment/locale/ca";
33 import "moment/locale/cy";
34 import "moment/locale/da";
35 import "moment/locale/de";
36 import "moment/locale/el";
37 import "moment/locale/eo";
38 import "moment/locale/es";
39 import "moment/locale/eu";
40 import "moment/locale/fa";
41 import "moment/locale/fi";
42 import "moment/locale/fr";
43 import "moment/locale/ga";
44 import "moment/locale/gl";
45 import "moment/locale/hi";
46 import "moment/locale/hr";
47 import "moment/locale/hu";
48 import "moment/locale/id";
49 import "moment/locale/it";
50 import "moment/locale/ja";
51 import "moment/locale/ka";
52 import "moment/locale/km";
53 import "moment/locale/ko";
54 import "moment/locale/nb";
55 import "moment/locale/nl";
56 import "moment/locale/pl";
57 import "moment/locale/pt-br";
58 import "moment/locale/ru";
59 import "moment/locale/sk";
60 import "moment/locale/sq";
61 import "moment/locale/sr";
62 import "moment/locale/sv";
63 import "moment/locale/tr";
64 import "moment/locale/uk";
65 import "moment/locale/vi";
66 import "moment/locale/zh-cn";
67 import { Subscription } from "rxjs";
68 import { delay, retryWhen, take } from "rxjs/operators";
69 import tippy from "tippy.js";
70 import Toastify from "toastify-js";
71 import { httpBase } from "./env";
72 import { i18n } from "./i18next";
74 CommentNode as CommentNodeI,
78 } from "./interfaces";
79 import { UserService, WebSocketService } from "./services";
83 Tribute = require("tributejs");
86 export const wsClient = new LemmyWebsocket();
88 export const favIconUrl = "/static/assets/icons/favicon.svg";
89 export const favIconPngUrl = "/static/assets/icons/apple-touch-icon.png";
91 // export const defaultFavIcon = `${window.location.protocol}//${window.location.host}${favIconPngUrl}`;
92 export const repoUrl = "https://github.com/LemmyNet";
93 export const joinLemmyUrl = "https://join-lemmy.org";
94 export const supportLemmyUrl = `${joinLemmyUrl}/support`;
95 export const docsUrl = `${joinLemmyUrl}/docs/en/index.html`;
96 export const helpGuideUrl = `${joinLemmyUrl}/docs/en/about/guide.html`; // TODO find a way to redirect to the non-en folder
97 export const markdownHelpUrl = `${helpGuideUrl}#markdown-guide`;
98 export const sortingHelpUrl = `${helpGuideUrl}#sorting`;
99 export const archiveUrl = "https://archive.is";
100 export const elementUrl = "https://element.io/";
102 export const postRefetchSeconds: number = 60 * 1000;
103 export const fetchLimit = 20;
104 export const mentionDropdownFetchLimit = 10;
106 export const languages = [
152 export const themes = [
168 const DEFAULT_ALPHABET =
169 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
171 function getRandomCharFromAlphabet(alphabet: string): string {
172 return alphabet.charAt(Math.floor(Math.random() * alphabet.length));
175 export function randomStr(
176 idDesiredLength = 20,
177 alphabet = DEFAULT_ALPHABET
180 * Create n-long array and map it to random chars from given alphabet.
181 * Then join individual chars as string
183 return Array.from({ length: idDesiredLength })
185 return getRandomCharFromAlphabet(alphabet);
190 export function wsJsonToRes<ResponseType>(
191 msg: WebSocketJsonResponse<ResponseType>
192 ): WebSocketResponse<ResponseType> {
199 export function wsUserOp(msg: any): UserOperation {
200 let opStr: string = msg.op;
201 return UserOperation[opStr];
204 export const md = new markdown_it({
209 .use(markdown_it_sub)
210 .use(markdown_it_sup)
211 .use(markdown_it_container, "spoiler", {
212 validate: function (params: any) {
213 return params.trim().match(/^spoiler\s+(.*)$/);
216 render: function (tokens: any, idx: any) {
217 var m = tokens[idx].info.trim().match(/^spoiler\s+(.*)$/);
219 if (tokens[idx].nesting === 1) {
221 return `<details><summary> ${md.utils.escapeHtml(m[1])} </summary>\n`;
224 return "</details>\n";
229 export function hotRankComment(comment_view: CommentView): number {
230 return hotRank(comment_view.counts.score, comment_view.comment.published);
233 export function hotRankActivePost(post_view: PostView): number {
234 return hotRank(post_view.counts.score, post_view.counts.newest_comment_time);
237 export function hotRankPost(post_view: PostView): number {
238 return hotRank(post_view.counts.score, post_view.post.published);
241 export function hotRank(score: number, timeStr: string): number {
242 // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
243 let date: Date = new Date(timeStr + "Z"); // Add Z to convert from UTC date
244 let now: Date = new Date();
245 let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
248 (10000 * Math.log10(Math.max(1, 3 + score))) /
249 Math.pow(hoursElapsed + 2, 1.8);
251 // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
256 export function mdToHtml(text: string) {
257 return { __html: md.render(text) };
260 export function getUnixTime(text: string): number {
261 return text ? new Date(text).getTime() / 1000 : undefined;
264 export function canMod(
265 myUserInfo: MyUserInfo,
270 // You can do moderator actions only on the mods added after you.
272 let yourIndex = modIds.findIndex(
273 id => id == myUserInfo.local_user_view.person.id
275 if (yourIndex == -1) {
278 // onSelf +1 on mod actions not for yourself, IE ban, remove, etc
279 modIds = modIds.slice(0, yourIndex + (onSelf ? 0 : 1));
280 return !modIds.includes(creator_id);
287 export function isMod(modIds: number[], creator_id: number): boolean {
288 return modIds.includes(creator_id);
291 const imageRegex = new RegExp(
292 /(http)?s?:?(\/\/[^"']*\.(?:jpg|jpeg|gif|png|svg|webp))/
294 const videoRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:mp4))`);
296 export function isImage(url: string) {
297 return imageRegex.test(url);
300 export function isVideo(url: string) {
301 return videoRegex.test(url);
304 export function validURL(str: string) {
305 return !!new URL(str);
308 export function communityRSSUrl(actorId: string, sort: string): string {
309 let url = new URL(actorId);
310 return `${url.origin}/feeds${url.pathname}.xml?sort=${sort}`;
313 export function validEmail(email: string) {
315 /^(([^\s"(),.:;<>@[\\\]]+(\.[^\s"(),.:;<>@[\\\]]+)*)|(".+"))@((\[(?:\d{1,3}\.){3}\d{1,3}])|(([\dA-Za-z\-]+\.)+[A-Za-z]{2,}))$/;
316 return re.test(String(email).toLowerCase());
319 export function capitalizeFirstLetter(str: string): string {
320 return str.charAt(0).toUpperCase() + str.slice(1);
323 export function routeSortTypeToEnum(sort: string): SortType {
324 return SortType[sort];
327 export function listingTypeFromNum(type_: number): ListingType {
328 return Object.values(ListingType)[type_];
331 export function sortTypeFromNum(type_: number): SortType {
332 return Object.values(SortType)[type_];
335 export function routeListingTypeToEnum(type: string): ListingType {
336 return ListingType[type];
339 export function routeDataTypeToEnum(type: string): DataType {
340 return DataType[capitalizeFirstLetter(type)];
343 export function routeSearchTypeToEnum(type: string): SearchType {
344 return SearchType[type];
347 export async function getSiteMetadata(url: string) {
348 let form: GetSiteMetadata = {
351 let client = new LemmyHttp(httpBase);
352 return client.getSiteMetadata(form);
355 export function debounce(func: any, wait = 1000, immediate = false) {
356 // 'private' variable for instance
357 // The returned function will be able to reference this due to closure.
358 // Each call to the returned function will share this common timer.
361 // Calling debounce returns a new anonymous function
363 // reference the context and args for the setTimeout function
364 var args = arguments;
366 // Should the function be called now? If immediate is true
367 // and not already in a timeout then the answer is: Yes
368 var callNow = immediate && !timeout;
370 // This is the basic debounce behaviour where you can call this
371 // function several times, but it will only execute once
372 // [before or after imposing a delay].
373 // Each time the returned function is called, the timer starts over.
374 clearTimeout(timeout);
376 // Set the new timeout
377 timeout = setTimeout(function () {
378 // Inside the timeout function, clear the timeout variable
379 // which will let the next execution run when in 'immediate' mode
382 // Check if the function already ran with the immediate flag
384 // Call the original function with apply
385 // apply lets you define the 'this' object as well as the arguments
386 // (both captured before setTimeout)
387 func.apply(this, args);
391 // Immediate mode and no wait timer? Execute the function..
392 if (callNow) func.apply(this, args);
397 export function getLanguage(override?: string): string {
398 let myUserInfo = UserService.Instance.myUserInfo;
401 (myUserInfo?.local_user_view.local_user.lang
402 ? myUserInfo.local_user_view.local_user.lang
405 if (lang == "browser" && isBrowser()) {
406 return getBrowserLanguage();
412 export function getBrowserLanguage(): string {
413 // Intersect lemmy's langs, with the browser langs
414 let langs = languages ? languages.map(l => l.code) : ["en"];
416 // NOTE, mobile browsers seem to be missing this list, so append en
417 let allowedLangs = navigator.languages
419 .filter(v => langs.includes(v));
420 return allowedLangs[0];
423 export function getMomentLanguage(): string {
424 let lang = getLanguage();
425 if (lang.startsWith("zh")) {
427 } else if (lang.startsWith("sv")) {
429 } else if (lang.startsWith("fr")) {
431 } else if (lang.startsWith("de")) {
433 } else if (lang.startsWith("ru")) {
435 } else if (lang.startsWith("es")) {
437 } else if (lang.startsWith("eo")) {
439 } else if (lang.startsWith("nl")) {
441 } else if (lang.startsWith("it")) {
443 } else if (lang.startsWith("fi")) {
445 } else if (lang.startsWith("ca")) {
447 } else if (lang.startsWith("fa")) {
449 } else if (lang.startsWith("pl")) {
451 } else if (lang.startsWith("pt_BR")) {
453 } else if (lang.startsWith("ja")) {
455 } else if (lang.startsWith("ka")) {
457 } else if (lang.startsWith("hi")) {
459 } else if (lang.startsWith("el")) {
461 } else if (lang.startsWith("eu")) {
463 } else if (lang.startsWith("gl")) {
465 } else if (lang.startsWith("tr")) {
467 } else if (lang.startsWith("hu")) {
469 } else if (lang.startsWith("uk")) {
471 } else if (lang.startsWith("sq")) {
473 } else if (lang.startsWith("km")) {
475 } else if (lang.startsWith("ga")) {
477 } else if (lang.startsWith("sr")) {
479 } else if (lang.startsWith("ko")) {
481 } else if (lang.startsWith("da")) {
483 } else if (lang.startsWith("oc")) {
485 } else if (lang.startsWith("hr")) {
487 } else if (lang.startsWith("th")) {
489 } else if (lang.startsWith("bg")) {
491 } else if (lang.startsWith("id")) {
493 } else if (lang.startsWith("nb")) {
495 } else if (lang.startsWith("cy")) {
497 } else if (lang.startsWith("sk")) {
499 } else if (lang.startsWith("vi")) {
501 } else if (lang.startsWith("pt")) {
503 } else if (lang.startsWith("ar")) {
511 export function setTheme(theme: string, forceReload = false) {
515 if (theme === "browser" && !forceReload) {
518 // This is only run on a force reload
519 if (theme == "browser") {
523 // Unload all the other themes
524 for (var i = 0; i < themes.length; i++) {
525 let styleSheet = document.getElementById(themes[i]);
527 styleSheet.setAttribute("disabled", "disabled");
532 .getElementById("default-light")
533 ?.setAttribute("disabled", "disabled");
534 document.getElementById("default-dark")?.setAttribute("disabled", "disabled");
536 // Load the theme dynamically
537 let cssLoc = `/static/assets/css/themes/${theme}.min.css`;
538 loadCss(theme, cssLoc);
539 document.getElementById(theme).removeAttribute("disabled");
542 export function loadCss(id: string, loc: string) {
543 if (!document.getElementById(id)) {
544 var head = document.getElementsByTagName("head")[0];
545 var link = document.createElement("link");
547 link.rel = "stylesheet";
548 link.type = "text/css";
551 head.appendChild(link);
555 export function objectFlip(obj: any) {
557 Object.keys(obj).forEach(key => {
563 export function showAvatars(): boolean {
565 UserService.Instance.myUserInfo?.local_user_view.local_user.show_avatars ||
566 !UserService.Instance.myUserInfo
570 export function showScores(): boolean {
572 UserService.Instance.myUserInfo?.local_user_view.local_user.show_scores ||
573 !UserService.Instance.myUserInfo
577 export function isCakeDay(published: string): boolean {
578 // moment(undefined) or moment.utc(undefined) returns the current date/time
579 // moment(null) or moment.utc(null) returns null
580 const createDate = moment.utc(published || null).local();
581 const currentDate = moment(new Date());
584 createDate.date() === currentDate.date() &&
585 createDate.month() === currentDate.month() &&
586 createDate.year() !== currentDate.year()
590 export function toast(text: string, background = "success") {
592 let backgroundColor = `var(--${background})`;
595 backgroundColor: backgroundColor,
602 export function pictrsDeleteToast(
603 clickToDeleteText: string,
604 deletePictureText: string,
608 let backgroundColor = `var(--light)`;
609 let toast = Toastify({
610 text: clickToDeleteText,
611 backgroundColor: backgroundColor,
617 window.location.replace(deleteUrl);
618 alert(deletePictureText);
627 interface NotifyInfo {
634 export function messageToastify(info: NotifyInfo, router: any) {
636 let htmlBody = info.body ? md.render(info.body) : "";
637 let backgroundColor = `var(--light)`;
639 let toast = Toastify({
640 text: `${htmlBody}<br />${info.name}`,
641 avatar: info.icon ? info.icon : null,
642 backgroundColor: backgroundColor,
643 className: "text-dark",
652 router.history.push(info.link);
659 export function notifyPost(post_view: PostView, router: any) {
660 let info: NotifyInfo = {
661 name: post_view.community.name,
662 icon: post_view.community.icon,
663 link: `/post/${post_view.post.id}`,
664 body: post_view.post.name,
666 notify(info, router);
669 export function notifyComment(comment_view: CommentView, router: any) {
670 let info: NotifyInfo = {
671 name: comment_view.creator.name,
672 icon: comment_view.creator.avatar,
673 link: `/post/${comment_view.post.id}/comment/${comment_view.comment.id}`,
674 body: comment_view.comment.content,
676 notify(info, router);
679 export function notifyPrivateMessage(pmv: PrivateMessageView, router: any) {
680 let info: NotifyInfo = {
681 name: pmv.creator.name,
682 icon: pmv.creator.avatar,
684 body: pmv.private_message.content,
686 notify(info, router);
689 function notify(info: NotifyInfo, router: any) {
690 messageToastify(info, router);
692 // TODO absolute nightmare bug, but notifs are currently broken.
693 // Notification.new will try to do a browser fetch ???
695 // if (Notification.permission !== "granted") Notification.requestPermission();
697 // var notification = new Notification(info.name, {
702 // notification.onclick = (ev: Event): any => {
703 // ev.preventDefault();
704 // router.history.push(info.link);
709 export function setupTribute() {
711 noMatchTemplate: function () {
718 menuItemTemplate: (item: any) => {
719 let shortName = `:${item.original.key}:`;
720 return `${item.original.val} ${shortName}`;
722 selectTemplate: (item: any) => {
723 return `${item.original.val}`;
725 values: Object.entries(emojiShortName).map(e => {
726 return { key: e[1], val: e[0] };
729 autocompleteMode: true,
731 // menuItemLimit: mentionDropdownFetchLimit,
732 menuShowMinLength: 2,
737 selectTemplate: (item: any) => {
738 let it: PersonTribute = item.original;
739 return `[${it.key}](${it.view.person.actor_id})`;
741 values: (text: string, cb: (persons: PersonTribute[]) => any) => {
742 personSearch(text, (persons: PersonTribute[]) => cb(persons));
745 autocompleteMode: true,
747 // menuItemLimit: mentionDropdownFetchLimit,
748 menuShowMinLength: 2,
754 selectTemplate: (item: any) => {
755 let it: CommunityTribute = item.original;
756 return `[${it.key}](${it.view.community.actor_id})`;
758 values: (text: string, cb: any) => {
759 communitySearch(text, (communities: CommunityTribute[]) =>
764 autocompleteMode: true,
766 // menuItemLimit: mentionDropdownFetchLimit,
767 menuShowMinLength: 2,
773 var tippyInstance: any;
775 tippyInstance = tippy("[data-tippy-content]");
778 export function setupTippy() {
780 tippyInstance.forEach((e: any) => e.destroy());
781 tippyInstance = tippy("[data-tippy-content]", {
783 // Display on "long press"
784 touch: ["hold", 500],
789 interface PersonTribute {
791 view: PersonViewSafe;
794 function personSearch(text: string, cb: (persons: PersonTribute[]) => any) {
798 type_: SearchType.Users,
799 sort: SortType.TopAll,
800 listing_type: ListingType.All,
802 limit: mentionDropdownFetchLimit,
803 auth: authField(false),
806 WebSocketService.Instance.send(wsClient.search(form));
808 let personSub = WebSocketService.Instance.subject.subscribe(
810 let res = wsJsonToRes(msg);
811 if (res.op == UserOperation.Search) {
812 let data = res.data as SearchResponse;
813 let persons: PersonTribute[] = data.users.map(pv => {
814 let tribute: PersonTribute = {
815 key: `@${pv.person.name}@${hostname(pv.person.actor_id)}`,
821 personSub.unsubscribe();
824 err => console.error(err),
825 () => console.log("complete")
832 interface CommunityTribute {
837 function communitySearch(
839 cb: (communities: CommunityTribute[]) => any
844 type_: SearchType.Communities,
845 sort: SortType.TopAll,
846 listing_type: ListingType.All,
848 limit: mentionDropdownFetchLimit,
849 auth: authField(false),
852 WebSocketService.Instance.send(wsClient.search(form));
854 let communitySub = WebSocketService.Instance.subject.subscribe(
856 let res = wsJsonToRes(msg);
857 if (res.op == UserOperation.Search) {
858 let data = res.data as SearchResponse;
859 let communities: CommunityTribute[] = data.communities.map(cv => {
860 let tribute: CommunityTribute = {
861 key: `!${cv.community.name}@${hostname(cv.community.actor_id)}`,
867 communitySub.unsubscribe();
870 err => console.error(err),
871 () => console.log("complete")
878 export function getListingTypeFromProps(props: any): ListingType {
879 return props.match.params.listing_type
880 ? routeListingTypeToEnum(props.match.params.listing_type)
881 : UserService.Instance.myUserInfo
882 ? Object.values(ListingType)[
883 UserService.Instance.myUserInfo.local_user_view.local_user
884 .default_listing_type
889 export function getListingTypeFromPropsNoDefault(props: any): ListingType {
890 return props.match.params.listing_type
891 ? routeListingTypeToEnum(props.match.params.listing_type)
895 // TODO might need to add a user setting for this too
896 export function getDataTypeFromProps(props: any): DataType {
897 return props.match.params.data_type
898 ? routeDataTypeToEnum(props.match.params.data_type)
902 export function getSortTypeFromProps(props: any): SortType {
903 return props.match.params.sort
904 ? routeSortTypeToEnum(props.match.params.sort)
905 : UserService.Instance.myUserInfo
906 ? Object.values(SortType)[
907 UserService.Instance.myUserInfo.local_user_view.local_user
913 export function getPageFromProps(props: any): number {
914 return props.match.params.page ? Number(props.match.params.page) : 1;
917 export function getRecipientIdFromProps(props: any): number {
918 return props.match.params.recipient_id
919 ? Number(props.match.params.recipient_id)
923 export function getIdFromProps(props: any): number {
924 return Number(props.match.params.id);
927 export function getCommentIdFromProps(props: any): number {
928 return Number(props.match.params.comment_id);
931 export function getUsernameFromProps(props: any): string {
932 return props.match.params.username;
935 export function editCommentRes(data: CommentView, comments: CommentView[]) {
936 let found = comments.find(c => c.comment.id == data.comment.id);
938 found.comment.content = data.comment.content;
939 found.comment.updated = data.comment.updated;
940 found.comment.removed = data.comment.removed;
941 found.comment.deleted = data.comment.deleted;
942 found.counts.upvotes = data.counts.upvotes;
943 found.counts.downvotes = data.counts.downvotes;
944 found.counts.score = data.counts.score;
948 export function saveCommentRes(data: CommentView, comments: CommentView[]) {
949 let found = comments.find(c => c.comment.id == data.comment.id);
951 found.saved = data.saved;
955 export function updatePersonBlock(
956 data: BlockPersonResponse
957 ): PersonBlockView[] {
959 UserService.Instance.myUserInfo.person_blocks.push({
960 person: UserService.Instance.myUserInfo.local_user_view.person,
961 target: data.person_view.person,
963 toast(`${i18n.t("blocked")} ${data.person_view.person.name}`);
965 UserService.Instance.myUserInfo.person_blocks =
966 UserService.Instance.myUserInfo.person_blocks.filter(
967 i => i.target.id != data.person_view.person.id
969 toast(`${i18n.t("unblocked")} ${data.person_view.person.name}`);
971 return UserService.Instance.myUserInfo.person_blocks;
974 export function updateCommunityBlock(
975 data: BlockCommunityResponse
976 ): CommunityBlockView[] {
978 UserService.Instance.myUserInfo.community_blocks.push({
979 person: UserService.Instance.myUserInfo.local_user_view.person,
980 community: data.community_view.community,
982 toast(`${i18n.t("blocked")} ${data.community_view.community.name}`);
984 UserService.Instance.myUserInfo.community_blocks =
985 UserService.Instance.myUserInfo.community_blocks.filter(
986 i => i.community.id != data.community_view.community.id
988 toast(`${i18n.t("unblocked")} ${data.community_view.community.name}`);
990 return UserService.Instance.myUserInfo.community_blocks;
993 export function createCommentLikeRes(
995 comments: CommentView[]
997 let found = comments.find(c => c.comment.id === data.comment.id);
999 found.counts.score = data.counts.score;
1000 found.counts.upvotes = data.counts.upvotes;
1001 found.counts.downvotes = data.counts.downvotes;
1002 if (data.my_vote !== null) {
1003 found.my_vote = data.my_vote;
1008 export function createPostLikeFindRes(data: PostView, posts: PostView[]) {
1009 let found = posts.find(p => p.post.id == data.post.id);
1011 createPostLikeRes(data, found);
1015 export function createPostLikeRes(data: PostView, post_view: PostView) {
1017 post_view.counts.score = data.counts.score;
1018 post_view.counts.upvotes = data.counts.upvotes;
1019 post_view.counts.downvotes = data.counts.downvotes;
1020 if (data.my_vote !== null) {
1021 post_view.my_vote = data.my_vote;
1026 export function editPostFindRes(data: PostView, posts: PostView[]) {
1027 let found = posts.find(p => p.post.id == data.post.id);
1029 editPostRes(data, found);
1033 export function editPostRes(data: PostView, post: PostView) {
1035 post.post.url = data.post.url;
1036 post.post.name = data.post.name;
1037 post.post.nsfw = data.post.nsfw;
1038 post.post.deleted = data.post.deleted;
1039 post.post.removed = data.post.removed;
1040 post.post.stickied = data.post.stickied;
1041 post.post.body = data.post.body;
1042 post.post.locked = data.post.locked;
1043 post.saved = data.saved;
1047 export function commentsToFlatNodes(comments: CommentView[]): CommentNodeI[] {
1048 let nodes: CommentNodeI[] = [];
1049 for (let comment of comments) {
1050 nodes.push({ comment_view: comment });
1055 function commentSort(tree: CommentNodeI[], sort: CommentSortType) {
1056 // First, put removed and deleted comments at the bottom, then do your other sorts
1057 if (sort == CommentSortType.Top) {
1060 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
1061 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
1062 b.comment_view.counts.score - a.comment_view.counts.score
1064 } else if (sort == CommentSortType.New) {
1067 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
1068 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
1069 b.comment_view.comment.published.localeCompare(
1070 a.comment_view.comment.published
1073 } else if (sort == CommentSortType.Old) {
1076 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
1077 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
1078 a.comment_view.comment.published.localeCompare(
1079 b.comment_view.comment.published
1082 } else if (sort == CommentSortType.Hot) {
1085 +a.comment_view.comment.removed - +b.comment_view.comment.removed ||
1086 +a.comment_view.comment.deleted - +b.comment_view.comment.deleted ||
1087 hotRankComment(b.comment_view) - hotRankComment(a.comment_view)
1091 // Go through the children recursively
1092 for (let node of tree) {
1093 if (node.children) {
1094 commentSort(node.children, sort);
1099 export function commentSortSortType(tree: CommentNodeI[], sort: SortType) {
1100 commentSort(tree, convertCommentSortType(sort));
1103 function convertCommentSortType(sort: SortType): CommentSortType {
1105 sort == SortType.TopAll ||
1106 sort == SortType.TopDay ||
1107 sort == SortType.TopWeek ||
1108 sort == SortType.TopMonth ||
1109 sort == SortType.TopYear
1111 return CommentSortType.Top;
1112 } else if (sort == SortType.New) {
1113 return CommentSortType.New;
1114 } else if (sort == SortType.Hot || sort == SortType.Active) {
1115 return CommentSortType.Hot;
1117 return CommentSortType.Hot;
1121 export function buildCommentsTree(
1122 comments: CommentView[],
1123 commentSortType: CommentSortType
1125 let map = new Map<number, CommentNodeI>();
1126 for (let comment_view of comments) {
1127 let node: CommentNodeI = {
1128 comment_view: comment_view,
1131 map.set(comment_view.comment.id, { ...node });
1133 let tree: CommentNodeI[] = [];
1134 for (let comment_view of comments) {
1135 let child = map.get(comment_view.comment.id);
1136 let parent_id = comment_view.comment.parent_id;
1138 let parent = map.get(parent_id);
1139 // Necessary because blocked comment might not exist
1141 parent.children.push(child);
1150 commentSort(tree, commentSortType);
1155 function setDepth(node: CommentNodeI, i = 0) {
1156 for (let child of node.children) {
1158 setDepth(child, i + 1);
1162 export function insertCommentIntoTree(tree: CommentNodeI[], cv: CommentView) {
1163 // Building a fake node to be used for later
1164 let node: CommentNodeI = {
1170 if (cv.comment.parent_id) {
1171 let parentComment = searchCommentTree(tree, cv.comment.parent_id);
1172 if (parentComment) {
1173 node.depth = parentComment.depth + 1;
1174 parentComment.children.unshift(node);
1181 export function searchCommentTree(
1182 tree: CommentNodeI[],
1185 for (let node of tree) {
1186 if (node.comment_view.comment.id === id) {
1190 for (const child of node.children) {
1191 const res = searchCommentTree([child], id);
1201 export const colorList: string[] = [
1210 function hsl(num: number) {
1211 return `hsla(${num}, 35%, 50%, 1)`;
1214 export function previewLines(
1223 // Use lines * 2 because markdown requires 2 lines
1224 .slice(0, maxLines * 2)
1229 export function hostname(url: string): string {
1230 let cUrl = new URL(url);
1231 return cUrl.port ? `${cUrl.hostname}:${cUrl.port}` : `${cUrl.hostname}`;
1234 export function validTitle(title?: string): boolean {
1235 // Initial title is null, minimum length is taken care of by textarea's minLength={3}
1236 if (title === null || title.length < 3) return true;
1238 const regex = new RegExp(/.*\S.*/, "g");
1240 return regex.test(title);
1243 export function siteBannerCss(banner: string): string {
1245 background-image: linear-gradient( rgba(0, 0, 0, 0.8), rgba(0, 0, 0, 0.8) ) ,url("${banner}"); \
1246 background-attachment: fixed; \
1247 background-position: top; \
1248 background-repeat: no-repeat; \
1249 background-size: 100% cover; \
1252 max-height: 100vh; \
1256 export function isBrowser() {
1257 return typeof window !== "undefined";
1260 export function setIsoData(context: any): IsoData {
1261 let isoData: IsoData = isBrowser()
1263 : context.router.staticContext;
1267 export function wsSubscribe(parseMessage: any): Subscription {
1269 return WebSocketService.Instance.subject
1270 .pipe(retryWhen(errors => errors.pipe(delay(3000), take(10))))
1272 msg => parseMessage(msg),
1273 err => console.error(err),
1274 () => console.log("complete")
1281 export function setOptionalAuth(obj: any, auth = UserService.Instance.auth) {
1287 export function authField(
1289 auth = UserService.Instance.auth
1291 if (auth == null && throwErr) {
1292 toast(i18n.t("not_logged_in"), "danger");
1293 throw "Not logged in";
1299 moment.updateLocale("en", {
1320 export function saveScrollPosition(context: any) {
1321 let path: string = context.router.route.location.pathname;
1322 let y = window.scrollY;
1323 sessionStorage.setItem(`scrollPosition_${path}`, y.toString());
1326 export function restoreScrollPosition(context: any) {
1327 let path: string = context.router.route.location.pathname;
1328 let y = Number(sessionStorage.getItem(`scrollPosition_${path}`));
1329 window.scrollTo(0, y);
1332 export function showLocal(isoData: IsoData): boolean {
1333 return isoData.site_res.federated_instances?.linked.length > 0;
1336 interface ChoicesValue {
1341 export function communityToChoice(cv: CommunityView): ChoicesValue {
1342 let choice: ChoicesValue = {
1343 value: cv.community.id.toString(),
1344 label: communitySelectName(cv),
1349 export function personToChoice(pvs: PersonViewSafe): ChoicesValue {
1350 let choice: ChoicesValue = {
1351 value: pvs.person.id.toString(),
1352 label: personSelectName(pvs),
1357 export async function fetchCommunities(q: string) {
1358 let form: Search = {
1360 type_: SearchType.Communities,
1361 sort: SortType.TopAll,
1362 listing_type: ListingType.All,
1365 auth: authField(false),
1367 let client = new LemmyHttp(httpBase);
1368 return client.search(form);
1371 export async function fetchUsers(q: string) {
1372 let form: Search = {
1374 type_: SearchType.Users,
1375 sort: SortType.TopAll,
1376 listing_type: ListingType.All,
1379 auth: authField(false),
1381 let client = new LemmyHttp(httpBase);
1382 return client.search(form);
1385 export const choicesConfig = {
1387 searchResultLimit: fetchLimit,
1389 containerOuter: "choices",
1390 containerInner: "choices__inner bg-secondary border-0",
1391 input: "form-control",
1392 inputCloned: "choices__input--cloned",
1393 list: "choices__list",
1394 listItems: "choices__list--multiple",
1395 listSingle: "choices__list--single",
1396 listDropdown: "choices__list--dropdown",
1397 item: "choices__item bg-secondary",
1398 itemSelectable: "choices__item--selectable",
1399 itemDisabled: "choices__item--disabled",
1400 itemChoice: "choices__item--choice",
1401 placeholder: "choices__placeholder",
1402 group: "choices__group",
1403 groupHeading: "choices__heading",
1404 button: "choices__button",
1405 activeState: "is-active",
1406 focusState: "is-focused",
1407 openState: "is-open",
1408 disabledState: "is-disabled",
1409 highlightedState: "text-info",
1410 selectedState: "text-info",
1411 flippedState: "is-flipped",
1412 loadingState: "is-loading",
1413 noResults: "has-no-results",
1414 noChoices: "has-no-choices",
1418 export function communitySelectName(cv: CommunityView): string {
1419 return cv.community.local
1421 : `${hostname(cv.community.actor_id)}/${cv.community.name}`;
1424 export function personSelectName(pvs: PersonViewSafe): string {
1425 return pvs.person.local
1427 : `${hostname(pvs.person.actor_id)}/${pvs.person.name}`;
1430 export function initializeSite(site: GetSiteResponse) {
1431 UserService.Instance.myUserInfo = site.my_user;
1432 i18n.changeLanguage(getLanguage());
1435 const SHORTNUM_SI_FORMAT = new Intl.NumberFormat("en-US", {
1436 maximumSignificantDigits: 3,
1438 notation: "compact",
1439 compactDisplay: "short",
1442 export function numToSI(value: number): string {
1443 return SHORTNUM_SI_FORMAT.format(value);