diff --git a/src/app/api/send/route.ts b/src/app/api/send/route.ts index 62212b5f..04876cd4 100644 --- a/src/app/api/send/route.ts +++ b/src/app/api/send/route.ts @@ -4,7 +4,7 @@ import { startOfHour, startOfMonth } from 'date-fns'; import clickhouse from '@/lib/clickhouse'; import { parseRequest } from '@/lib/request'; import { badRequest, json, forbidden, serverError } from '@/lib/response'; -import { fetchSession, fetchWebsite } from '@/lib/load'; +import { fetchWebsite } from '@/lib/load'; import { getClientInfo, hasBlockedIp } from '@/lib/detect'; import { createToken, parseToken } from '@/lib/jwt'; import { secret, uuid, hash } from '@/lib/crypto'; @@ -103,32 +103,24 @@ export async function POST(request: Request) { const sessionId = id ? uuid(websiteId, id) : uuid(websiteId, ip, userAgent, sessionSalt); - // Find session + // Create a session if not found if (!clickhouse.enabled && !cache?.sessionId) { - const session = await fetchSession(websiteId, sessionId); - - // Create a session if not found - if (!session) { - try { - await createSession({ - id: sessionId, - websiteId, - browser, - os, - device, - screen, - language, - country, - region, - city, - distinctId: id, - }); - } catch (e: any) { - if (!e.message.toLowerCase().includes('unique constraint')) { - return serverError(e); - } - } - } + await createSession( + { + id: sessionId, + websiteId, + browser, + os, + device, + screen, + language, + country, + region, + city, + distinctId: id, + }, + { skipDuplicates: true }, + ); } // Visit info diff --git a/src/lib/detect.ts b/src/lib/detect.ts index 00f42b42..2dc2f243 100644 --- a/src/lib/detect.ts +++ b/src/lib/detect.ts @@ -5,12 +5,13 @@ import ipaddr from 'ipaddr.js'; import maxmind from 'maxmind'; import { DESKTOP_OS, - MOBILE_OS, DESKTOP_SCREEN_WIDTH, - LAPTOP_SCREEN_WIDTH, - MOBILE_SCREEN_WIDTH, IP_ADDRESS_HEADERS, + LAPTOP_SCREEN_WIDTH, + MOBILE_OS, + MOBILE_SCREEN_WIDTH, } from './constants'; +import { safeDecodeURIComponent } from '@/lib/url'; const MAXMIND = 'maxmind'; @@ -148,9 +149,9 @@ export async function getClientInfo(request: Request, payload: Record