-
Notifications
You must be signed in to change notification settings - Fork 0
/
services.ts
157 lines (140 loc) · 4.05 KB
/
services.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { Request, Response } from "express";
import { Item } from "./models";
import {
banned,
generateShortString,
getContext,
isAdmin,
getHasDeleteAccess,
isValidDesiredLink,
} from "./utils";
// Checks if a token is valid
// Returns user info or 400
export const checkToken = async (req: Request, res: Response) => {
const { user } = getContext();
if (!user) return res.sendStatus(400);
return res.status(200).json(user);
};
export const createLink = async (req: Request, res: Response) => {
const { url, mandate, expires } = req.body as {
url: string;
mandate?: string;
expires?: number;
};
// force lowercase on short name
const desired = req.body.desired?.toLowerCase();
// Check blacklist
const host = new URL(url).host;
if (
banned[host] ||
banned[`www.${host}`] ||
banned[host.replace(/www[.]/, "")]
) {
return res.status(400).json({
errors: [
{
msg: `"${host}" is blacklisted`,
param: "url",
},
],
});
}
const { user } = getContext();
const data: {
expires: number | null;
user: string;
short?: string;
url?: string;
mandate?: string;
} = {
url,
user: user.user,
expires: expires ?? null,
};
if (mandate) {
data["mandate"] = mandate;
}
// If specified a desired short url
if (desired) {
data["short"] = desired;
const desiredExists = await Item.findOne({ short: desired });
if (!isValidDesiredLink(desired)) {
return res.status(400).json({
errors: [
{
msg: `"${desired}" is invalid, valid characters are [a-z0-9], '-' and '_'`,
param: "",
},
],
});
}
if (desiredExists !== null) {
return res.status(400).json({
errors: [
{
msg: `"${desired}" already exists`,
param: "",
},
],
});
}
// No desired url specified
} else {
try {
data["short"] = await generateShortString();
} catch (err) {
console.log(err);
return res.status(500).send(err);
}
}
Item.create(data)
.then((item) => {
res.status(201).json(item);
})
.catch((err) => {
console.log(err);
res.status(500).send(err);
});
};
export const getAllLinks = async (req: Request, res: Response) => {
let query;
const { user } = getContext();
if (isAdmin(user)) {
// Find everything
query = Item.find({});
} else {
// Find links belonging to the user or the users' mandates or the users' groups
query = Item.find({
$or: [
{ user: user.user },
...user.mandates.map((m) => ({ mandate: m.identifier })),
...user.groups.map((g) => ({ mandate: g.identifier })),
],
});
}
query
.then((data) => res.json(data))
.catch((err) => res.status(500).send(err));
};
export const getLink = async (req: Request, res: Response) => {
const { code } = req.params;
const item = await Item.findOneAndUpdate(
{ short: code.toLowerCase() },
{ $inc: { clicks: 1 } }
);
if (!item) return res.sendStatus(404);
return res.status(200).json(item);
};
export const deleteLink = async (req: Request, res: Response) => {
const { code } = req.params;
const item = await Item.findOne({ short: code });
if (!item) return res.sendStatus(404);
const { user } = getContext();
const hasAccess = getHasDeleteAccess(user, item);
if (hasAccess) {
await Item.deleteOne({ short: code });
return res.sendStatus(200);
} else {
return res.sendStatus(403);
}
};