2020-10-31 13:33:59 -07:00
|
|
|
import { Router } from 'express';
|
|
|
|
import SQL from 'sql-template-strings';
|
|
|
|
import md5 from "js-md5";
|
|
|
|
import {ulid} from "ulid";
|
2020-11-02 12:12:15 -08:00
|
|
|
import avatar from "../avatar";
|
2021-06-09 05:47:08 -07:00
|
|
|
import {handleErrorAsync} from "../../src/helpers";
|
2021-07-02 16:15:44 -07:00
|
|
|
import { caches } from "../../src/cache";
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-01-01 12:11:37 -08:00
|
|
|
const normalise = s => s.trim().toLowerCase();
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
const calcAge = birthday => {
|
|
|
|
if (!birthday) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const now = new Date();
|
|
|
|
const birth = new Date(
|
|
|
|
parseInt(birthday.substring(0, 4)),
|
|
|
|
parseInt(birthday.substring(5, 7)) - 1,
|
|
|
|
parseInt(birthday.substring(8, 10))
|
|
|
|
);
|
|
|
|
|
|
|
|
const diff = now.getTime() - birth.getTime();
|
|
|
|
|
|
|
|
return parseInt(Math.floor(diff / 1000 / 60 / 60 / 24 / 365.25));
|
|
|
|
}
|
|
|
|
|
2021-06-16 07:08:38 -07:00
|
|
|
const fetchProfiles = async (db, username, self, isAdmin) => {
|
2020-10-31 13:33:59 -07:00
|
|
|
const profiles = await db.all(SQL`
|
2021-06-27 03:36:29 -07:00
|
|
|
SELECT profiles.*, users.id, users.username, users.email, users.avatarSource, users.bannedReason, users.roles FROM profiles LEFT JOIN users on users.id == profiles.userId
|
2021-01-01 12:11:37 -08:00
|
|
|
WHERE lower(trim(replace(replace(replace(replace(replace(replace(replace(replace(replace(username, 'Ą', 'ą'), 'Ć', 'ć'), 'Ę', 'ę'), 'Ł', 'ł'), 'Ń', 'ń'), 'Ó', 'ó'), 'Ś', 'ś'), 'Ż', 'ż'), 'Ź', 'ż'))) = ${normalise(username)}
|
2020-10-31 13:33:59 -07:00
|
|
|
AND profiles.active = 1
|
|
|
|
ORDER BY profiles.locale
|
|
|
|
`);
|
|
|
|
|
2020-11-02 12:12:15 -08:00
|
|
|
const p = {}
|
|
|
|
for (let profile of profiles) {
|
2021-06-16 07:08:38 -07:00
|
|
|
if (profile.bannedReason !== null && !isAdmin && !self) {
|
|
|
|
return {};
|
|
|
|
}
|
2020-11-02 12:12:15 -08:00
|
|
|
p[profile.locale] = {
|
|
|
|
id: profile.id,
|
|
|
|
userId: profile.userId,
|
|
|
|
username: profile.username,
|
|
|
|
emailHash: md5(profile.email),
|
|
|
|
names: JSON.parse(profile.names),
|
|
|
|
pronouns: JSON.parse(profile.pronouns),
|
|
|
|
description: profile.description,
|
|
|
|
age: calcAge(profile.birthday),
|
|
|
|
links: JSON.parse(profile.links),
|
|
|
|
flags: JSON.parse(profile.flags),
|
2021-04-05 08:03:13 -07:00
|
|
|
customFlags: JSON.parse(profile.customFlags),
|
2020-11-02 12:12:15 -08:00
|
|
|
words: JSON.parse(profile.words),
|
|
|
|
avatar: await avatar(db, profile),
|
|
|
|
birthday: self ? profile.birthday : undefined,
|
2021-01-12 11:06:59 -08:00
|
|
|
teamName: profile.teamName,
|
|
|
|
footerName: profile.footerName,
|
|
|
|
footerAreas: profile.footerAreas ? profile.footerAreas.split(',') : [],
|
2021-06-16 07:08:38 -07:00
|
|
|
bannedReason: profile.bannedReason,
|
2021-06-27 03:36:29 -07:00
|
|
|
team: !!profile.roles,
|
2020-11-02 12:12:15 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
return p;
|
2020-10-31 13:33:59 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const router = Router();
|
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.get('/profile/get/:username', handleErrorAsync(async (req, res) => {
|
2021-06-16 07:08:38 -07:00
|
|
|
return res.json(await fetchProfiles(req.db, req.params.username, req.user && req.user.username === req.params.username, req.isGranted('users')))
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/profile/save', handleErrorAsync(async (req, res) => {
|
2020-10-31 13:33:59 -07:00
|
|
|
if (!req.user) {
|
|
|
|
return res.status(401).json({error: 'Unauthorised'});
|
|
|
|
}
|
|
|
|
|
2021-06-03 01:28:53 -07:00
|
|
|
// TODO just make it a transaction...
|
2021-06-17 16:10:59 -07:00
|
|
|
const ids = (await req.db.all(SQL`SELECT * FROM profiles WHERE userId = ${req.user.id} AND locale = ${global.config.locale}`)).map(row => row.id);
|
2021-06-03 01:28:53 -07:00
|
|
|
if (ids.length) {
|
|
|
|
await req.db.get(SQL`UPDATE profiles
|
|
|
|
SET
|
|
|
|
names = ${JSON.stringify(req.body.names)},
|
|
|
|
pronouns = ${JSON.stringify(req.body.pronouns)},
|
|
|
|
description = ${req.body.description},
|
|
|
|
birthday = ${req.body.birthday || null},
|
|
|
|
links = ${JSON.stringify(req.body.links.filter(x => !!x))},
|
|
|
|
flags = ${JSON.stringify(req.body.flags)},
|
|
|
|
customFlags = ${JSON.stringify(req.body.customFlags)},
|
|
|
|
words = ${JSON.stringify(req.body.words)},
|
|
|
|
teamName = ${req.isGranted('users') ? req.body.teamName || null : ''},
|
|
|
|
footerName = ${req.isGranted('users') ? req.body.footerName || null : ''},
|
|
|
|
footerAreas = ${req.isGranted('users') ? req.body.footerAreas.join(',').toLowerCase() || null : ''}
|
|
|
|
WHERE id = ${ids[0]}
|
|
|
|
`);
|
|
|
|
} else {
|
|
|
|
await req.db.get(SQL`INSERT INTO profiles (id, userId, locale, names, pronouns, description, birthday, links, flags, customFlags, words, active, teamName, footerName, footerAreas)
|
2021-06-17 16:10:59 -07:00
|
|
|
VALUES (${ulid()}, ${req.user.id}, ${global.config.locale}, ${JSON.stringify(req.body.names)}, ${JSON.stringify(req.body.pronouns)},
|
2021-04-05 08:03:13 -07:00
|
|
|
${req.body.description}, ${req.body.birthday || null}, ${JSON.stringify(req.body.links.filter(x => !!x))}, ${JSON.stringify(req.body.flags)}, ${JSON.stringify(req.body.customFlags)},
|
2021-01-12 11:06:59 -08:00
|
|
|
${JSON.stringify(req.body.words)}, 1,
|
|
|
|
${req.isGranted('users') ? req.body.teamName || null : ''},
|
|
|
|
${req.isGranted('users') ? req.body.footerName || null : ''},
|
|
|
|
${req.isGranted('users') ? req.body.footerAreas.join(',').toLowerCase() || null : ''}
|
2021-06-03 01:28:53 -07:00
|
|
|
)`);
|
|
|
|
}
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-07-02 16:15:44 -07:00
|
|
|
if (req.body.teamName) {
|
|
|
|
await caches.admins.invalidate();
|
|
|
|
await caches.adminsFooter.invalidate();
|
|
|
|
}
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
return res.json(await fetchProfiles(req.db, req.user.username, true));
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
2021-06-09 05:47:08 -07:00
|
|
|
router.post('/profile/delete/:locale', handleErrorAsync(async (req, res) => {
|
2021-04-21 12:17:48 -07:00
|
|
|
await req.db.get(SQL`DELETE FROM profiles WHERE userId = ${req.user.id} AND locale = ${req.params.locale}`);
|
2020-10-31 13:33:59 -07:00
|
|
|
|
|
|
|
return res.json(await fetchProfiles(req.db, req.user.username, true));
|
2021-06-09 05:47:08 -07:00
|
|
|
}));
|
2020-10-31 13:33:59 -07:00
|
|
|
|
|
|
|
export default router;
|