2022-11-09 00:50:34 +01:00
|
|
|
import clickhouse from 'lib/clickhouse';
|
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-04-02 02:38:35 +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) {
|
2022-11-08 07:35:51 +01:00
|
|
|
return null;
|
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) {
|
|
|
|
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)) {
|
2022-08-29 05:20:54 +02:00
|
|
|
return null;
|
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) {
|
2022-11-01 07:42:37 +01: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-02-20 18:04:20 +01:00
|
|
|
const { userAgent, browser, os, ip, country, subdivision1, subdivision2, city, device } =
|
|
|
|
await getClientInfo(req, payload);
|
2022-11-01 07:42:37 +01:00
|
|
|
const sessionId = uuid(websiteId, hostname, ip, userAgent);
|
2020-08-12 05:05:40 +02:00
|
|
|
|
2022-11-09 02:11:08 +01:00
|
|
|
// Clickhouse does not require session lookup
|
|
|
|
if (clickhouse.enabled) {
|
|
|
|
return {
|
2022-11-09 00:50:34 +01:00
|
|
|
id: sessionId,
|
|
|
|
websiteId,
|
|
|
|
hostname,
|
|
|
|
browser,
|
|
|
|
os,
|
|
|
|
device,
|
|
|
|
screen,
|
|
|
|
language,
|
|
|
|
country,
|
2023-02-20 18:04:20 +01:00
|
|
|
subdivision1,
|
|
|
|
subdivision2,
|
|
|
|
city,
|
2022-11-09 00:50:34 +01:00
|
|
|
};
|
2022-10-06 22:13:44 +02:00
|
|
|
}
|
2020-08-08 02:19:42 +02:00
|
|
|
|
2022-11-09 02:11:08 +01:00
|
|
|
// Find session
|
2023-04-02 02:38:35 +02:00
|
|
|
let session = await loadSession(websiteId);
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-08 07:35:51 +01:00
|
|
|
return session;
|
2020-08-05 07:45:05 +02:00
|
|
|
}
|