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