📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
community edition: project share API
Blue Mouse4ec662aa304d
diff --git a/backend/apps/community/src/common/utils.ts b/backend/apps/community/src/common/utils.tsindex 749b96a42..817e5c38b 100644--- a/backend/apps/community/src/common/utils.ts+++ b/backend/apps/community/src/common/utils.ts@@ -72,6 +72,7 @@ const ALLOWED_KEYS = []const ALLOWED_FUNNEL_KEYS = ['name', 'steps']+const ALLOWED_SHARE_KEYS = ['role', 'confirmed']const getRateLimitHash = (ipOrApiKey: string, salt = '') =>`rl:${hash(`${ipOrApiKey}${salt}`)}`@@ -334,7 +335,7 @@ const deleteProjectClickhouse = async (id: string) => {}const createProjectClickhouse = async (project: Partial<Project>) => {- const { id, name } = project+ const { id, name, adminId } = projectawait clickhouse.insert({table: 'project',@@ -350,12 +351,200 @@ const createProjectClickhouse = async (project: Partial<Project>) => {isPasswordProtected: 0,passwordHash: null,botsProtectionLevel: BotsProtectionLevel.BASIC,+ adminId: adminId || null,created: dayjs.utc().format('YYYY-MM-DD HH:mm:ss'),},],})}+const findProjectShareClickhouse = async (id: string) => {+ try {+ const { data } = await clickhouse+ .query({+ query: `SELECT * FROM project_share WHERE id = {id:FixedString(36)}`,+ query_params: { id },+ })+ .then(resultSet => resultSet.json())++ if (_isEmpty(data)) {+ return null+ }++ return data[0]+ } catch {+ return null+ }+}++const findProjectSharesByProjectClickhouse = async (projectId: string) => {+ try {+ const { data } = await clickhouse+ .query({+ query: `+ SELECT+ ps.id as id,+ ps.role as role,+ ps.confirmed as confirmed,+ ps.created as created,+ ps.updated as updated,+ u.id as userId,+ u.email as email+ FROM project_share ps+ LEFT JOIN user u ON ps.userId = u.id+ WHERE ps.projectId = {projectId:FixedString(12)}+ `,+ query_params: { projectId },+ })+ .then(resultSet => resultSet.json())++ return data+ } catch {+ return []+ }+}++const findProjectSharesByUserClickhouse = async (userId: string) => {+ try {+ const { data } = await clickhouse+ .query({+ query: `+ SELECT+ ps.id AS id,+ ps.role AS role,+ ps.confirmed AS confirmed,+ ps.created AS created,+ ps.updated AS updated,+ p.id AS projectId,+ p.name AS projectName,+ p.origins AS projectOrigins,+ p.ipBlacklist AS projectIpBlacklist,+ p.active AS projectActive,+ p.public AS projectPublic,+ p.isPasswordProtected AS projectIsPasswordProtected,+ p.botsProtectionLevel AS projectBotsProtectionLevel,+ p.created AS projectCreated+ FROM project_share ps+ LEFT JOIN project p ON ps.projectId = p.id+ WHERE ps.userId = {userId:FixedString(36)}+ `,+ query_params: { userId },+ })+ .then(resultSet => resultSet.json())++ return data+ } catch {+ return []+ }+}++const findProjectShareByUserAndProjectClickhouse = async (+ userId: string,+ projectId: string,+) => {+ try {+ const { data } = await clickhouse+ .query({+ query: `+ SELECT * FROM project_share+ WHERE userId = {userId:FixedString(36)}+ AND projectId = {projectId:FixedString(12)}+ `,+ query_params: { userId, projectId },+ })+ .then(resultSet => resultSet.json())++ if (_isEmpty(data)) {+ return null+ }++ return data[0]+ } catch {+ return null+ }+}++const createProjectShareClickhouse = async (share: {+ id: string+ userId: string+ projectId: string+ role: string+ confirmed?: number+}) => {+ const { id, userId, projectId, role } = share+ const confirmed = share.confirmed ?? 0+ const now = dayjs.utc().format('YYYY-MM-DD HH:mm:ss')++ await clickhouse.insert({+ table: 'project_share',+ format: 'JSONEachRow',+ values: [+ {+ id,+ userId,+ projectId,+ role,+ confirmed,+ created: now,+ updated: now,+ },+ ],+ })+}++const updateProjectShareClickhouse = async (+ id: string,+ update: Partial<{ role: string; confirmed: number }>,+) => {+ const filtered = _reduce(+ _filter(_keys(update), key => ALLOWED_SHARE_KEYS.includes(key)),+ (obj, key) => {+ // @ts-expect-error dynamic index+ obj[key] = update[key]+ return obj+ },+ {},+ )++ const columns = _keys(filtered)+ const values = _values(filtered)++ if (_isEmpty(columns)) {+ return+ }++ const assignments = _map(columns, (col, idx) => {+ const val = values[idx]+ const type = col === 'confirmed' ? 'Int8' : 'String'+ return `${col}={v_${idx}:${type}}`+ }).join(', ')++ const params = _reduce(+ values,+ (acc, cur, idx) => ({+ ...acc,+ [`v_${idx}`]: cur,+ }),+ {},+ )++ await clickhouse.command({+ query: `ALTER TABLE project_share UPDATE ${assignments}, updated={updated:String} WHERE id={id:FixedString(36)}`,+ query_params: {+ ...params,+ id,+ updated: dayjs.utc().format('YYYY-MM-DD HH:mm:ss'),+ },+ })+}++const deleteProjectShareClickhouse = async (id: string) => {+ await clickhouse.command({+ query: `ALTER TABLE project_share DELETE WHERE id={id:FixedString(36)}`,+ query_params: { id },+ })+}+const saveRefreshTokenClickhouse = async (userId: string,refreshToken: string,@@ -763,4 +952,12 @@ export {createProjectViewClickhouse,doesProjectViewExistClickhouse,updateProjectViewClickhouse,+ // shares+ findProjectShareClickhouse,+ findProjectSharesByProjectClickhouse,+ findProjectSharesByUserClickhouse,+ findProjectShareByUserAndProjectClickhouse,+ createProjectShareClickhouse,+ updateProjectShareClickhouse,+ deleteProjectShareClickhouse,}diff --git a/backend/apps/community/src/project/project.controller.ts b/backend/apps/community/src/project/project.controller.tsindex f6b7ca06a..9da23068f 100644--- a/backend/apps/community/src/project/project.controller.ts+++ b/backend/apps/community/src/project/project.controller.ts@@ -34,6 +34,8 @@ import _size from 'lodash/size'import _split from 'lodash/split'import _isBoolean from 'lodash/isBoolean'import _omit from 'lodash/omit'+import _find from 'lodash/find'+import _isNil from 'lodash/isNil'import _reduce from 'lodash/reduce'import _head from 'lodash/head'import _includes from 'lodash/includes'@@ -81,6 +83,13 @@ import {getProjectClickhouse,getFunnelClickhouse,generateRandomId,+ findProjectShareClickhouse,+ findProjectSharesByProjectClickhouse,+ findProjectSharesByUserClickhouse,+ findProjectShareByUserAndProjectClickhouse,+ createProjectShareClickhouse,+ updateProjectShareClickhouse,+ deleteProjectShareClickhouse,} from '../common/utils'import { Funnel } from './entity/funnel.entity'import { ProjectViewEntity } from './entity/project-view.entity'@@ -88,6 +97,9 @@ import { ProjectViewIdsDto } from './dto/project-view-ids.dto'import { ProjectIdDto } from './dto/project-id.dto'import { CreateProjectViewDto } from './dto/create-project-view.dto'import { UpdateProjectViewDto } from './dto/update-project-view.dto'+import { UserService } from '../user/user.service'+import { Roles } from '../auth/decorators/roles.decorator'+import { UserType } from '../user/entities/user.entity'@ApiTags('Project')@Controller(['project', 'v1/project'])@@ -95,6 +107,7 @@ export class ProjectController {constructor(private readonly projectService: ProjectService,private readonly logger: AppLoggerService,+ private readonly userService: UserService,) {}@Get('/')@@ -121,6 +134,20 @@ export class ProjectController {const chResults = await getProjectsClickhouse(search, sort)const formatted = _map(chResults, this.projectService.formatFromClickhouse)+ let sharesByProjectId: Record<string, any> = {}++ if (userId) {+ const rawShares = await findProjectSharesByUserClickhouse(userId)+ sharesByProjectId = (rawShares || []).reduce((acc: any, row: any) => {+ acc[row.projectId] = {+ id: row.id,+ role: row.role,+ confirmed: Boolean(row.confirmed),+ }+ return acc+ }, {})+ }+const pidsWithData =await this.projectService.getPIDsWhereAnalyticsDataExists(_map(formatted, ({ id }) => id),@@ -158,18 +185,45 @@ export class ProjectController {{},)- const results = _map(formatted, project => ({- ..._omit(project, ['passwordHash']),- funnels: funnelsMap[project.id],- isDataExists: _includes(pidsWithData, project?.id),- isErrorDataExists: _includes(pidsWithErrorData, project?.id),+ const results = _map(formatted, project => {+ const userShare = sharesByProjectId[project.id]+ const isOwner = userId && project.adminId === userId+ const role = isOwner+ ? 'owner'+ : userShare+ ? userShare.role+ : userId+ ? 'viewer'+ : 'viewer'+ const isAccessConfirmed = isOwner+ ? true+ : userShare+ ? Boolean(userShare.confirmed)+ : true++ const projectShare = userShare+ ? [+ {+ id: userShare.id,+ role: userShare.role,+ confirmed: Boolean(userShare.confirmed),+ user: { id: userId },+ },+ ]+ : undefined- // these ones are not used, but we need to keep them for compatibility with the Cloud version- role: userId ? 'owner' : 'viewer',- isLocked: false,- isAccessConfirmed: true,- isAnalyticsProject: true,- }))+ return {+ ..._omit(project, ['passwordHash']),+ share: projectShare,+ funnels: funnelsMap[project.id],+ isDataExists: _includes(pidsWithData, project?.id),+ isErrorDataExists: _includes(pidsWithErrorData, project?.id),+ role,+ isLocked: false,+ isAccessConfirmed,+ isAnalyticsProject: true,+ }+ })return {results,@@ -189,6 +243,148 @@ export class ProjectController {return formatted}+ @ApiBearerAuth()+ @Post('/:pid/share')+ @HttpCode(200)+ @UseGuards(JwtAccessTokenGuard, RolesGuard)+ @Roles(UserType.CUSTOMER, UserType.ADMIN)+ async share(+ @Param('pid') pid: string,+ @Body() body: { email: string; role: 'admin' | 'viewer' },+ @CurrentUserId() userId: string,+ ) {+ this.logger.log({ userId, pid, body }, 'POST /project/:pid/share')++ if (!isValidPID(pid)) {+ throw new BadRequestException(+ 'The provided Project ID (pid) is incorrect',+ )+ }++ if (!body?.email || !['admin', 'viewer'].includes(body.role)) {+ throw new BadRequestException('The provided ShareDTO is incorrect')+ }++ const inviter = await this.userService.findOne({ id: userId })+ const project = await getProjectClickhouse(pid)++ if (_isEmpty(project)) {+ throw new BadRequestException(`Project with ID ${pid} does not exist`)+ }++ if (inviter.email === body.email) {+ throw new BadRequestException('You cannot share with yourself')+ }++ const invitee = await this.userService.findOne({ email: body.email })++ if (!invitee) {+ throw new BadRequestException(+ `User with email ${body.email} is not registered on Swetrix`,+ )+ }++ const existing = await findProjectShareByUserAndProjectClickhouse(+ invitee.id,+ pid,+ )++ if (existing) {+ throw new BadRequestException(+ `You're already sharing the project with ${invitee.email}`,+ )+ }++ if (project.adminId !== userId) {+ throw new BadRequestException(+ 'You are not allowed to manage this project',+ )+ }++ const id = uuidv4()++ await createProjectShareClickhouse({+ id,+ userId: invitee.id,+ projectId: pid,+ role: body.role,+ confirmed: 0,+ })++ return {+ ...project,+ }+ }++ @ApiBearerAuth()+ @Put('/share/:shareId')+ @HttpCode(200)+ @UseGuards(JwtAccessTokenGuard, RolesGuard)+ @Roles(UserType.CUSTOMER, UserType.ADMIN)+ async updateShare(+ @Param('shareId') shareId: string,+ @Body() body: { role: 'admin' | 'viewer' },+ @CurrentUserId() userId: string,+ ) {+ this.logger.log({ userId, shareId, body }, 'PUT /project/share/:shareId')++ if (!body?.role || !['admin', 'viewer'].includes(body.role)) {+ throw new BadRequestException('The provided ShareUpdateDTO is incorrect')+ }++ const share = await findProjectShareClickhouse(shareId)++ if (!share) {+ throw new BadRequestException(`Share with ID ${shareId} does not exist`)+ }++ const project = await getProjectClickhouse(share.projectId)+ if (!project || project.adminId !== userId) {+ throw new BadRequestException(+ 'You are not allowed to manage this project',+ )+ }++ await updateProjectShareClickhouse(shareId, { role: body.role })++ return await findProjectShareClickhouse(shareId)+ }++ @ApiBearerAuth()+ @Delete('/:pid/:shareId')+ @HttpCode(204)+ @UseGuards(JwtAccessTokenGuard, RolesGuard)+ @Roles(UserType.CUSTOMER, UserType.ADMIN)+ async deleteShare(+ @Param('pid') pid: string,+ @Param('shareId') shareId: string,+ @CurrentUserId() userId: string,+ ) {+ this.logger.log({ userId, pid, shareId }, 'DELETE /project/:pid/:shareId')++ if (!isValidPID(pid)) {+ throw new BadRequestException(+ 'The provided Project ID (pid) is incorrect',+ )+ }++ const share = await findProjectShareClickhouse(shareId)++ if (!share) {+ return+ }++ const project = await getProjectClickhouse(share.projectId)++ if (!project || project.adminId !== userId) {+ throw new BadRequestException(+ 'You are not allowed to manage this project',+ )+ }++ await deleteProjectShareClickhouse(shareId)+ }+@ApiBearerAuth()@Post('/')@ApiResponse({ status: 201, type: Project })@@ -213,7 +409,7 @@ export class ProjectController {project.id = pidproject.name = _trim(projectDTO.name)- await createProjectClickhouse(project)+ await createProjectClickhouse({ ...project, adminId: userId })return project}@@ -598,17 +794,46 @@ export class ProjectController {)const funnels = await getFunnelsClickhouse(id)+ const rawShares = await findProjectSharesByProjectClickhouse(id)++ const share = (rawShares || []).map((row: any) => ({+ id: row.id,+ role: row.role,+ confirmed: Boolean(row.confirmed),+ created: row.created,+ updated: row.updated,+ user: {+ id: row.userId,+ email: row.email,+ },+ }))++ const isOwner = userId && project.adminId === userId+ const userShare = (rawShares || []).find(+ (row: any) => row.userId === userId,+ )+ const role = isOwner+ ? 'owner'+ : userShare+ ? userShare.role+ : userId+ ? 'viewer'+ : 'viewer'+ const isAccessConfirmed = isOwner+ ? true+ : userShare+ ? Boolean(userShare.confirmed)+ : truereturn this.projectService.formatFromClickhouse({..._omit(project, ['passwordHash']),+ share,funnels: this.projectService.formatFunnelsFromClickhouse(funnels),isDataExists,isErrorDataExists,-- // these ones are not used, but we need to keep them for compatibility with the Cloud version- role: userId ? 'owner' : 'viewer',+ role,isLocked: false,- isAccessConfirmed: true,+ isAccessConfirmed,isAnalyticsProject: true,})}diff --git a/backend/apps/community/src/project/project.module.ts b/backend/apps/community/src/project/project.module.tsindex 860e4e9f1..805c36413 100644--- a/backend/apps/community/src/project/project.module.ts+++ b/backend/apps/community/src/project/project.module.ts@@ -3,9 +3,10 @@ import { Module } from '@nestjs/common'import { ProjectService } from './project.service'import { ProjectController } from './project.controller'import { AppLoggerModule } from '../logger/logger.module'+import { UserModule } from '../user/user.module'@Module({- imports: [AppLoggerModule],+ imports: [AppLoggerModule, UserModule],providers: [ProjectService],exports: [ProjectService],controllers: [ProjectController],diff --git a/backend/apps/community/src/user/user.controller.ts b/backend/apps/community/src/user/user.controller.tsindex df0b2fe1d..7f3b161a6 100644--- a/backend/apps/community/src/user/user.controller.ts+++ b/backend/apps/community/src/user/user.controller.ts@@ -10,8 +10,10 @@ import {Ip,ConflictException,Delete,+ Param,} from '@nestjs/common'import { ApiBearerAuth, ApiResponse, ApiTags } from '@nestjs/swagger'+import _isEmpty from 'lodash/isEmpty'import _omit from 'lodash/omit'import _isNull from 'lodash/isNull'import { v4 as uuidv4 } from 'uuid'@@ -26,6 +28,12 @@ import { CurrentUserId } from '../auth/decorators/current-user-id.decorator'import { AppLoggerService } from '../logger/logger.service'import { UserService } from './user.service'import { checkRateLimit, getIPFromHeaders } from '../common/utils'+import {+ findProjectShareClickhouse,+ findProjectSharesByUserClickhouse,+ updateProjectShareClickhouse,+ deleteProjectShareClickhouse,+} from '../common/utils'@ApiTags('User')@Controller('user')@@ -43,10 +51,52 @@ export class UserController {async me(@CurrentUserId() uid: string) {this.logger.log({ uid }, 'GET /user/me')- const user = await this.userService.findOne({ id: uid })+ const [user, rawShares] = await Promise.all([+ this.userService.findOne({ id: uid }),+ findProjectSharesByUserClickhouse(uid),+ ])++ const sharedProjects = (rawShares || []).map((row: any) => ({+ id: row.id,+ confirmed: Boolean(row.confirmed),+ role: row.role,+ created: row.created,+ updated: row.updated,+ project: this.formatProjectFromRow(row),+ uiHidden: false,+ }))++ return {+ user: {+ ...this.userService.omitSensitiveData(user),+ sharedProjects,+ organisationMemberships: [],+ },+ }+ }+ private formatProjectFromRow(row: any) {return {- user: this.userService.omitSensitiveData(user),+ id: row.projectId,+ name: row.projectName,+ origins: row.projectOrigins ? String(row.projectOrigins).split(',') : [],+ ipBlacklist: row.projectIpBlacklist+ ? String(row.projectIpBlacklist).split(',')+ : [],+ active: Boolean(row.projectActive),+ public: Boolean(row.projectPublic),+ isPasswordProtected: Boolean(row.projectIsPasswordProtected),+ botsProtectionLevel: row.projectBotsProtectionLevel,+ created: row.projectCreated,+ isAnalyticsProject: true,+ isCaptchaProject: false,+ isCaptchaEnabled: false,+ captchaSecretKey: null,+ uiHidden: false,+ funnels: [],+ isLocked: false,+ isDataExists: false,+ isErrorDataExists: false,}}@@ -79,6 +129,62 @@ export class UserController {}}+ @ApiBearerAuth()+ @Delete('/share/:actionId')+ @UseGuards(JwtAccessTokenGuard, RolesGuard)+ @Roles(UserType.CUSTOMER, UserType.ADMIN)+ @ApiResponse({ status: 204, description: 'Empty body' })+ async deleteShare(+ @CurrentUserId() uid: string,+ @Param('actionId') actionId: string,+ ) {+ this.logger.log({ uid, actionId }, 'DELETE /user/share/:actionId')++ const share = await findProjectShareClickhouse(actionId)++ if (_isEmpty(share)) {+ throw new BadRequestException(+ 'This invitation does not exist or is no longer valid',+ )+ }++ if (share.userId !== uid) {+ throw new BadRequestException(+ 'You are not allowed to reject this invitation',+ )+ }++ await deleteProjectShareClickhouse(actionId)+ }++ @ApiBearerAuth()+ @Get('/share/:actionId')+ @UseGuards(JwtAccessTokenGuard, RolesGuard)+ @Roles(UserType.CUSTOMER, UserType.ADMIN)+ @ApiResponse({ status: 204, description: 'Empty body' })+ async acceptShare(+ @CurrentUserId() uid: string,+ @Param('actionId') actionId: string,+ ) {+ this.logger.log({ uid, actionId }, 'GET /user/share/:actionId')++ const share = await findProjectShareClickhouse(actionId)++ if (_isEmpty(share)) {+ throw new BadRequestException(+ 'This invitation does not exist or is no longer valid',+ )+ }++ if (share.userId !== uid) {+ throw new BadRequestException(+ 'You are not allowed to accept this invitation',+ )+ }++ await updateProjectShareClickhouse(actionId, { confirmed: 1 })+ }+@ApiBearerAuth()@Post('onboarding/step')@UseGuards(JwtAccessTokenGuard, RolesGuard)diff --git a/backend/migrations/clickhouse/initialise_selfhosted.js b/backend/migrations/clickhouse/initialise_selfhosted.jsindex 1a613d6d0..bb75ae154 100644--- a/backend/migrations/clickhouse/initialise_selfhosted.js+++ b/backend/migrations/clickhouse/initialise_selfhosted.js@@ -80,6 +80,19 @@ const CLICKHOUSE_INIT_QUERIES = [)ENGINE = MergeTree()PRIMARY KEY id;`,++ `CREATE TABLE IF NOT EXISTS ${dbName}.project_share+ (+ id FixedString(36),+ userId FixedString(36),+ projectId FixedString(12),+ role String,+ confirmed Int8,+ created DateTime,+ updated DateTime+ )+ ENGINE = MergeTree()+ PRIMARY KEY id;`,]const initialiseSelfhosted = async () => {