]> Untitled Git - lemmy.git/blob - ui/src/utils.ts
Adding better community and main page routing, with sorting and type in url.
[lemmy.git] / ui / src / utils.ts
1 import { UserOperation, Comment, User, SortType, ListingType } from './interfaces';
2 import * as markdown_it from 'markdown-it';
3
4 export let repoUrl = 'https://github.com/dessalines/lemmy';
5
6 export function msgOp(msg: any): UserOperation {
7   let opStr: string = msg.op;
8   return UserOperation[opStr];
9 }
10
11 var md = new markdown_it({
12   html: true,
13   linkify: true,
14   typographer: true
15 });
16
17 export function hotRank(comment: Comment): number {
18   // Rank = ScaleFactor * sign(Score) * log(1 + abs(Score)) / (Time + 2)^Gravity
19
20   let date: Date = new Date(comment.published + 'Z'); // Add Z to convert from UTC date
21   let now: Date = new Date();
22   let hoursElapsed: number = (now.getTime() - date.getTime()) / 36e5;
23
24   let rank = (10000 *  Math.log10(Math.max(1, 3 + comment.score))) / Math.pow(hoursElapsed + 2, 1.8);
25
26   // console.log(`Comment: ${comment.content}\nRank: ${rank}\nScore: ${comment.score}\nHours: ${hoursElapsed}`);
27
28   return rank;
29 }
30
31 export function mdToHtml(text: string) {
32   return {__html: md.render(text)};
33 }
34
35 export function getUnixTime(text: string): number { 
36   return text ? new Date(text).getTime()/1000 : undefined;
37 }
38
39 export function addTypeInfo<T>(arr: Array<T>, name: string): Array<{type_: string, data: T}> {  
40   return arr.map(e => {return {type_: name, data: e}});
41 }
42
43 export function canMod(user: User, modIds: Array<number>, creator_id: number): boolean {
44   // You can do moderator actions only on the mods added after you.
45   if (user) {
46     let yourIndex = modIds.findIndex(id => id == user.id);
47     if (yourIndex == -1) {
48       return false;
49     } else { 
50       modIds = modIds.slice(0, yourIndex+1); // +1 cause you cant mod yourself
51       return !modIds.includes(creator_id);
52     }
53   } else {
54     return false;
55   }
56 }
57
58 export function isMod(modIds: Array<number>, creator_id: number): boolean {
59   return modIds.includes(creator_id);
60 }
61
62
63 var imageRegex = new RegExp(`(http)?s?:?(\/\/[^"']*\.(?:png|jpg|jpeg|gif|png|svg))`);
64
65 export function isImage(url: string) {
66   return imageRegex.test(url);
67 }
68
69 export let fetchLimit: number = 20;
70
71 export function capitalizeFirstLetter(str: string): string {
72   return str.charAt(0).toUpperCase() + str.slice(1);
73 }
74
75
76 export function routeSortTypeToEnum(sort: string): SortType {
77   if (sort == 'new') {
78     return SortType.New;
79   } else if (sort == 'hot') {
80     return SortType.Hot;
81   } else if (sort == 'topday') {
82     return SortType.TopDay;
83   } else if (sort == 'topweek') {
84     return SortType.TopWeek;
85   } else if (sort == 'topmonth') {
86     return SortType.TopMonth;
87   } else if (sort == 'topall') {
88     return SortType.TopAll;
89   }
90 }
91
92 export function routeListingTypeToEnum(type: string): ListingType {
93   return ListingType[capitalizeFirstLetter(type)];
94 }
95