mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 04:37:11 +01:00
Updated queries to use cache.
This commit is contained in:
parent
74192cd695
commit
728e4cff5b
9 changed files with 59 additions and 42 deletions
|
|
@ -7,6 +7,7 @@ import { getTeamUser, getTeamUserById } from 'queries';
|
|||
import { getTeamWebsite, getTeamWebsiteByTeamMemberId } from 'queries/admin/teamWebsite';
|
||||
import { validate } from 'uuid';
|
||||
import { Auth } from './types';
|
||||
import { loadWebsite } from './query';
|
||||
|
||||
const log = debug('umami:auth');
|
||||
|
||||
|
|
@ -66,7 +67,7 @@ export async function canViewWebsite({ user, shareToken }: Auth, websiteId: stri
|
|||
return true;
|
||||
}
|
||||
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const website = await loadWebsite(websiteId);
|
||||
|
||||
if (website.userId) {
|
||||
return user.id === website.userId;
|
||||
|
|
@ -98,7 +99,7 @@ export async function canUpdateWebsite({ user }: Auth, websiteId: string) {
|
|||
return false;
|
||||
}
|
||||
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const website = await loadWebsite(websiteId);
|
||||
|
||||
if (website.userId) {
|
||||
return user.id === website.userId;
|
||||
|
|
@ -112,7 +113,7 @@ export async function canDeleteWebsite({ user }: Auth, websiteId: string) {
|
|||
return true;
|
||||
}
|
||||
|
||||
const website = await cache.fetchWebsite(websiteId);
|
||||
const website = await loadWebsite(websiteId);
|
||||
|
||||
if (website.userId) {
|
||||
return user.id === website.userId;
|
||||
|
|
|
|||
34
lib/query.ts
34
lib/query.ts
|
|
@ -1,5 +1,5 @@
|
|||
import cache from 'lib/cache';
|
||||
import { getWebsite } from 'queries';
|
||||
import { getWebsite, getSession, getUser } from 'queries';
|
||||
import { Website } from './types';
|
||||
|
||||
export async function loadWebsite(websiteId: string): Promise<Website> {
|
||||
|
|
@ -17,3 +17,35 @@ export async function loadWebsite(websiteId: string): Promise<Website> {
|
|||
|
||||
return website;
|
||||
}
|
||||
|
||||
export async function loadSession(sessionId: string): Promise<Website> {
|
||||
let session;
|
||||
|
||||
if (cache.enabled) {
|
||||
session = await cache.fetchSession(sessionId);
|
||||
} else {
|
||||
session = await getSession({ id: sessionId });
|
||||
}
|
||||
|
||||
if (!session) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return session;
|
||||
}
|
||||
|
||||
export async function loadUser(userId: string): Promise<Website> {
|
||||
let user;
|
||||
|
||||
if (cache.enabled) {
|
||||
user = await cache.fetchUser(userId);
|
||||
} else {
|
||||
user = await getUser({ id: userId });
|
||||
}
|
||||
|
||||
if (!user || user.deletedAt) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return user;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
import cache from 'lib/cache';
|
||||
import clickhouse from 'lib/clickhouse';
|
||||
import { secret, uuid } from 'lib/crypto';
|
||||
import { getClientInfo, getJsonBody } from 'lib/detect';
|
||||
import { parseToken } from 'next-basics';
|
||||
import { CollectRequestBody, NextApiRequestCollect } from 'pages/api/send';
|
||||
import { createSession, getSession, getWebsite } from 'queries';
|
||||
import { createSession } from 'queries';
|
||||
import { validate } from 'uuid';
|
||||
import { loadSession, loadWebsite } from './query';
|
||||
|
||||
export async function findSession(req: NextApiRequestCollect) {
|
||||
const { payload } = getJsonBody<CollectRequestBody>(req);
|
||||
|
|
@ -33,15 +33,9 @@ export async function findSession(req: NextApiRequestCollect) {
|
|||
}
|
||||
|
||||
// Find website
|
||||
let website;
|
||||
const website = await loadWebsite(websiteId);
|
||||
|
||||
if (cache.enabled) {
|
||||
website = await cache.fetchWebsite(websiteId);
|
||||
} else {
|
||||
website = await getWebsite({ id: websiteId });
|
||||
}
|
||||
|
||||
if (!website || website.deletedAt) {
|
||||
if (!website) {
|
||||
throw new Error(`Website not found: ${websiteId}`);
|
||||
}
|
||||
|
||||
|
|
@ -68,13 +62,7 @@ export async function findSession(req: NextApiRequestCollect) {
|
|||
}
|
||||
|
||||
// Find session
|
||||
let session;
|
||||
|
||||
if (cache.enabled) {
|
||||
session = await cache.fetchSession(sessionId);
|
||||
} else {
|
||||
session = await getSession({ id: sessionId });
|
||||
}
|
||||
let session = await loadSession(websiteId);
|
||||
|
||||
// Create a session if not found
|
||||
if (!session) {
|
||||
|
|
|
|||
|
|
@ -14,6 +14,7 @@ export type KafkaTopics = ObjectValues<typeof KAFKA_TOPIC>;
|
|||
export interface EventData {
|
||||
[key: string]: number | string | EventData | number[] | string[] | EventData[];
|
||||
}
|
||||
|
||||
export interface Auth {
|
||||
user?: {
|
||||
id: string;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue