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