mirror of
https://github.com/umami-software/umami.git
synced 2025-12-08 05:12:36 +01:00
fix send message. look at redis on session create
This commit is contained in:
parent
818f8721e9
commit
54ade11a98
5 changed files with 38 additions and 40 deletions
|
|
@ -1,8 +1,7 @@
|
|||
import { getWebsiteByUuid, getSessionByUuid, createSession } from 'queries';
|
||||
import { getJsonBody, getClientInfo } from 'lib/request';
|
||||
import { uuid, isValidUuid, parseToken } from 'lib/crypto';
|
||||
import { runAnalyticsQuery } from 'lib/db';
|
||||
import { RELATIONAL, CLICKHOUSE } from 'lib/constants';
|
||||
import { isValidUuid, parseToken, uuid } from 'lib/crypto';
|
||||
import redis from 'lib/redis';
|
||||
import { getClientInfo, getJsonBody } from 'lib/request';
|
||||
import { createSession, getSessionByUuid, getWebsiteByUuid } from 'queries';
|
||||
|
||||
export async function getSession(req) {
|
||||
const { payload } = getJsonBody(req);
|
||||
|
|
@ -27,28 +26,42 @@ export async function getSession(req) {
|
|||
throw new Error(`Invalid website: ${website_uuid}`);
|
||||
}
|
||||
|
||||
const website = await getWebsiteByUuid(website_uuid);
|
||||
let websiteId = null;
|
||||
|
||||
if (!website) {
|
||||
// Check if website exists
|
||||
if (process.env.REDIS_URL) {
|
||||
websiteId = await redis.get(`website:${website_uuid}`);
|
||||
} else {
|
||||
const { website_id } = await getWebsiteByUuid(website_uuid);
|
||||
websiteId = website_id;
|
||||
}
|
||||
|
||||
if (!websiteId) {
|
||||
throw new Error(`Website not found: ${website_uuid}`);
|
||||
}
|
||||
|
||||
const { userAgent, browser, os, ip, country, device } = await getClientInfo(req, payload);
|
||||
|
||||
const { website_id } = website;
|
||||
const session_uuid = uuid(website_id, hostname, ip, userAgent);
|
||||
const session_uuid = uuid(websiteId, hostname, ip, userAgent);
|
||||
|
||||
// logic placeholder for redis
|
||||
runAnalyticsQuery({
|
||||
[RELATIONAL]: () => {},
|
||||
[CLICKHOUSE]: () => {},
|
||||
});
|
||||
let sessionCreated = false;
|
||||
let sessionId = null;
|
||||
let session = null;
|
||||
|
||||
let session = await getSessionByUuid(session_uuid);
|
||||
// Check if session exists
|
||||
if (process.env.REDIS_URL) {
|
||||
sessionCreated = (await redis.get(`session:${session_uuid}`)) !== null;
|
||||
} else {
|
||||
console.log('test');
|
||||
session = await getSessionByUuid(session_uuid);
|
||||
sessionCreated = !!session;
|
||||
sessionId = session ? session.session_id : null;
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
if (!sessionCreated) {
|
||||
try {
|
||||
session = await createSession(website_id, {
|
||||
console.log('test2');
|
||||
session = await createSession(websiteId, {
|
||||
session_uuid,
|
||||
hostname,
|
||||
browser,
|
||||
|
|
@ -58,6 +71,8 @@ export async function getSession(req) {
|
|||
country,
|
||||
device,
|
||||
});
|
||||
|
||||
sessionId = session ? session.session_id : null;
|
||||
} catch (e) {
|
||||
if (!e.message.toLowerCase().includes('unique constraint')) {
|
||||
throw e;
|
||||
|
|
@ -65,15 +80,9 @@ export async function getSession(req) {
|
|||
}
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const { session_id } = session;
|
||||
|
||||
return {
|
||||
website_id,
|
||||
session_id,
|
||||
website_id: websiteId,
|
||||
session_id: sessionId,
|
||||
session_uuid,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue