umami/lib/session.ts

104 lines
2.4 KiB
TypeScript
Raw Normal View History

2022-11-08 23:50:34 +00:00
import clickhouse from 'lib/clickhouse';
2023-03-30 18:18:57 +00:00
import { secret, uuid } from 'lib/crypto';
2022-12-28 04:20:44 +00:00
import { getClientInfo, getJsonBody } from 'lib/detect';
2023-03-30 18:18:57 +00:00
import { parseToken } from 'next-basics';
import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send';
2023-04-02 00:38:35 +00:00
import { createSession } from 'queries';
2023-03-30 18:18:57 +00:00
import { validate } from 'uuid';
2023-04-02 00:38:35 +00:00
import { loadSession, loadWebsite } from './query';
2023-05-16 03:41:12 +00:00
import cache from './cache';
2023-03-30 18:18:57 +00:00
export async function findSession(req: NextApiRequestCollect) {
const { payload } = getJsonBody<CollectRequestBody>(req);
2020-08-09 06:48:43 +00:00
if (!payload) {
throw new Error('Invalid payload.');
2020-08-09 06:48:43 +00:00
}
// Check if cache token is passed
const cacheToken = req.headers['x-umami-cache'];
2020-10-03 03:33:46 +00:00
if (cacheToken) {
const result = await parseToken(cacheToken, secret());
2020-10-03 03:33:46 +00:00
if (result) {
2023-05-16 03:41:12 +00:00
await checkUserBlock(result?.ownerId);
2020-10-03 03:33:46 +00:00
return result;
}
}
// Verify payload
2022-11-01 06:42:37 +00:00
const { website: websiteId, hostname, screen, language } = payload;
2022-08-26 06:12:47 +00:00
2022-11-01 06:42:37 +00:00
if (!validate(websiteId)) {
throw new Error('Invalid website ID.');
2020-08-12 03:05:40 +00:00
}
// Find website
2023-04-02 00:38:35 +00:00
const website = await loadWebsite(websiteId);
2022-08-29 20:04:58 +00:00
2023-04-02 00:38:35 +00:00
if (!website) {
throw new Error(`Website not found: ${websiteId}.`);
2020-08-21 02:17:27 +00:00
}
2020-08-12 03:05:40 +00:00
2023-05-16 03:41:12 +00:00
await checkUserBlock(website.userId);
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
await getClientInfo(req, payload);
2022-11-01 06:42:37 +00:00
const sessionId = uuid(websiteId, hostname, ip, userAgent);
2020-08-12 03:05:40 +00:00
2022-11-09 01:11:08 +00:00
// Clickhouse does not require session lookup
if (clickhouse.enabled) {
return {
2022-11-08 23:50:34 +00:00
id: sessionId,
websiteId,
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
subdivision2,
city,
2022-11-08 23:50:34 +00:00
};
}
2022-11-09 01:11:08 +00:00
// Find session
2023-04-03 00:23:12 +00:00
let session = await loadSession(sessionId);
2022-11-09 01:11:08 +00:00
// Create a session if not found
if (!session) {
try {
session = await createSession({
id: sessionId,
websiteId,
hostname,
browser,
os,
device,
screen,
language,
country,
subdivision1,
subdivision2,
city,
2022-11-09 01:11:08 +00:00
});
2023-03-30 18:18:57 +00:00
} catch (e: any) {
2022-11-09 01:11:08 +00:00
if (!e.message.toLowerCase().includes('unique constraint')) {
throw e;
}
}
}
2023-05-16 03:41:12 +00:00
return { ...session, ownerId: website.userId };
}
async function checkUserBlock(userId: string) {
if (process.env.ENABLE_BLOCKER && (await cache.fetchUserBlock(userId))) {
throw new Error('Usage Limit.');
}
2020-08-05 05:45:05 +00:00
}