Merge remote-tracking branch 'origin/dev' into dev

This commit is contained in:
Mike Cao 2023-08-23 12:50:30 -07:00
commit 3e71994767
5 changed files with 57 additions and 14 deletions

View File

@ -4,7 +4,7 @@ import debug from 'debug';
import { PERMISSIONS, ROLE_PERMISSIONS, SHARE_TOKEN_HEADER } from 'lib/constants';
import { secret } from 'lib/crypto';
import { createSecureToken, ensureArray, getRandomChars, parseToken } from 'next-basics';
import { findTeamWebsiteByUserId, getTeamUser, getTeamWebsite, getWebsitesByUserId } from 'queries';
import { findTeamWebsiteByUserId, getTeamUser, getTeamWebsite } from 'queries';
import { loadWebsite } from './load';
import { Auth } from './types';
@ -60,11 +60,11 @@ export async function canViewWebsite({ user, shareToken }: Auth, websiteId: stri
export async function canCreateWebsite({ user, grant }: Auth) {
if (cloudMode) {
if (grant.find(a => a === PERMISSIONS.websiteCreate)) {
if (grant?.find(a => a === PERMISSIONS.websiteCreate)) {
return true;
}
return (await getWebsitesByUserId(user.id)).count < Number(process.env.WEBSITE_LIMIT);
return false;
}
if (user.isAdmin) {
@ -120,7 +120,7 @@ export async function canDeleteReport(auth: Auth, report: Report) {
export async function canCreateTeam({ user, grant }: Auth) {
if (cloudMode) {
if (grant.find(a => a === PERMISSIONS.teamCreate)) {
if (grant?.find(a => a === PERMISSIONS.teamCreate)) {
return true;
}

View File

@ -2,17 +2,20 @@ import { User, Website } from '@prisma/client';
import redis from '@umami/redis-client';
import { getSession, getUserById, getWebsiteById } from '../queries';
const { fetchObject, storeObject, deleteObject } = redis;
const { fetchObject, storeObject, deleteObject, expire } = redis;
async function fetchWebsite(id): Promise<Website> {
return fetchObject(`website:${id}`, () => getWebsiteById(id));
return fetchObject(`website:${id}`, () => getWebsiteById(id), 86400);
}
async function storeWebsite(data) {
const { id } = data;
const key = `website:${id}`;
return storeObject(key, data);
const obj = await storeObject(key, data);
await expire(key, 86400);
return obj;
}
async function deleteWebsite(id) {
@ -20,14 +23,17 @@ async function deleteWebsite(id) {
}
async function fetchUser(id): Promise<User> {
return fetchObject(`user:${id}`, () => getUserById(id, { includePassword: true }));
return fetchObject(`user:${id}`, () => getUserById(id, { includePassword: true }), 86400);
}
async function storeUser(data) {
const { id } = data;
const key = `user:${id}`;
return storeObject(key, data);
const obj = await storeObject(key, data);
await expire(key, 86400);
return obj;
}
async function deleteUser(id) {
@ -35,14 +41,17 @@ async function deleteUser(id) {
}
async function fetchSession(id) {
return fetchObject(`session:${id}`, () => getSession(id));
return fetchObject(`session:${id}`, () => getSession(id), 86400);
}
async function storeSession(data) {
const { id } = data;
const key = `session:${id}`;
return storeObject(key, data);
const obj = await storeObject(key, data);
await expire(key, 86400);
return obj;
}
async function deleteSession(id) {

View File

@ -1,12 +1,27 @@
import { secret, uuid, isUuid } from 'lib/crypto';
import { isUuid, 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 } from 'queries';
import cache from './cache';
import clickhouse from './clickhouse';
import { loadSession, loadWebsite } from './load';
export async function findSession(req: NextApiRequestCollect) {
export async function findSession(req: NextApiRequestCollect): Promise<{
id: any;
websiteId: string;
hostname: string;
browser: string;
os: any;
device: string;
screen: string;
language: string;
country: any;
subdivision1: any;
subdivision2: any;
city: any;
ownerId: string;
}> {
const { payload } = getJsonBody<CollectRequestBody>(req);
if (!payload) {
@ -53,6 +68,25 @@ export async function findSession(req: NextApiRequestCollect) {
const sessionId = uuid(websiteId, hostname, ip, userAgent);
// Clickhouse does not require session lookup
if (clickhouse.enabled) {
return {
id: sessionId,
websiteId,
hostname,
browser,
os: os as any,
device,
screen,
language,
country,
subdivision1,
subdivision2,
city,
ownerId: website.userId,
};
}
// Find session
let session = await loadSession(sessionId);

View File

@ -23,6 +23,6 @@
"jsx": "preserve",
"incremental": false
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"include": ["**/*.ts", "**/*.tsx", "**/*.js", "**/*.jsx", "next-env.d.ts"],
"exclude": ["node_modules"]
}