-
Notifications
You must be signed in to change notification settings - Fork 0
/
avatar.ts
79 lines (71 loc) · 2.21 KB
/
avatar.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
import * as express from 'express';
import { param, validationResult } from 'express-validator';
import * as path from 'path';
import sql from 'sql-template-strings';
import { ApiResponse } from '../apiResponse';
import s3Client from '../aws/s3';
import db from '../db';
import ChatManager from '../managers/chatManager';
import UserManager from '../managers/userManager';
import allowedMethods from '../middlewares/allowedMethods';
const router = express.Router();
router.all('/:id.png', param('id').isString(), allowedMethods('GET'), async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return new ApiResponse(res).validationError(errors);
const id = req.params.id as string;
let avatar = null;
const user = await UserManager.getUserById(id);
res.header('Cache-Control', s3Client.cacheControlHeader);
if (user) {
avatar = await userAvatar(id);
if (avatar) {
const avatarUrl = await s3Client.getSingedUrl(avatar);
res.redirect(302, avatarUrl);
}
else {
// Default avatar based on user tag
res.sendFile(defaultAvatar(Number(user.tag) % 5));
}
}
else {
const chat = await ChatManager.getChat(id);
if (!chat) return new ApiResponse(res).notFound('User or chat not found');
avatar = await chatAvatar(id);
if (avatar) {
const avatarUrl = await s3Client.getSingedUrl(avatar);
res.redirect(302, avatarUrl);
}
else {
// Default avatar based on chat creator tag
const creator = await UserManager.getUserById(chat.creatorId);
res.sendFile(defaultAvatar(Number(creator.tag) % 5));
}
}
});
async function userAvatar(id: string): Promise<string | null> {
const query = await db.query(sql`
SELECT avatar
FROM users
WHERE id = $1
AND is_banned = FALSE -- don't show avatar of banned users
LIMIT 1;
`, [ id ]);
if (query.rowCount == 0) return null;
return await query.rows[0].avatar;
}
async function chatAvatar(id: string): Promise<string | null> {
const query = await db.query(sql`
SELECT
avatar
FROM
chats
WHERE id = $1
LIMIT 1;
`, [ id ]);
if (query.rowCount == 0) return null;
return query.rows[0].avatar;
}
function defaultAvatar(id: number): string {
return path.join(__dirname, `../public/img/avatars/${id}.png`);
}
export default router;