Skip to content

📈 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!

code quality improvements

Blue Mouse4660b5efe14b
diff --git a/backend/apps/cloud/src/project/dto/project.dto.ts b/backend/apps/cloud/src/project/dto/project.dto.ts
index 43ea9bd70..66a9df2b1 100644
--- a/backend/apps/cloud/src/project/dto/project.dto.ts
+++ b/backend/apps/cloud/src/project/dto/project.dto.ts
@@ -1,5 +1,11 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsNotEmpty, Length, IsEnum, IsOptional } from 'class-validator'
+import {
+ IsNotEmpty,
+ Length,
+ IsEnum,
+ IsOptional,
+ IsArray,
+} from 'class-validator'
import { BotsProtectionLevel } from '../entity/project.entity'
export class ProjectDTO {
@@ -44,11 +50,13 @@ export class ProjectDTO {
ipBlacklist: string[] | null
@ApiProperty({
- example: 'RU,BY,KP',
+ example: ['RU', 'BY', 'KP'],
required: false,
description:
'Array of blocked country codes (ISO 3166-1 alpha-2). Traffic from these countries will not be tracked.',
})
+ @IsOptional()
+ @IsArray()
countryBlacklist: string[] | null
@ApiProperty({
diff --git a/backend/apps/cloud/src/project/project.service.ts b/backend/apps/cloud/src/project/project.service.ts
index 3a9cc7d1e..a1bbd519a 100644
--- a/backend/apps/cloud/src/project/project.service.ts
+++ b/backend/apps/cloud/src/project/project.service.ts
@@ -318,6 +318,7 @@ export class ProjectService {
'project.active',
'project.public',
'project.ipBlacklist',
+ 'project.countryBlacklist',
'project.botsProtectionLevel',
'project.captchaSecretKey',
'project.isCaptchaEnabled',
@@ -787,6 +788,29 @@ export class ProjectService {
})
}
+ validateCountryBlacklist(
+ projectDTO: ProjectDTO | UpdateProjectDto | CreateProjectDTO,
+ ) {
+ if (!projectDTO.countryBlacklist) {
+ return
+ }
+
+ if (_size(projectDTO.countryBlacklist) > 250)
+ throw new UnprocessableEntityException(
+ 'The list of blocked countries cannot exceed 250 entries.',
+ )
+
+ const countryCodeRegex = /^[A-Z]{2}$/
+ _map(projectDTO.countryBlacklist, code => {
+ const trimmedCode = _trim(code).toUpperCase()
+ if (!countryCodeRegex.test(trimmedCode)) {
+ throw new ConflictException(
+ `Country code "${code}" is not a valid ISO 3166-1 alpha-2 code`,
+ )
+ }
+ })
+ }
+
validateProject(
projectDTO: ProjectDTO | UpdateProjectDto | CreateProjectDTO,
creatingProject = false,
@@ -806,6 +830,7 @@ export class ProjectService {
this.validateOrigins(projectDTO)
this.validateIPBlacklist(projectDTO)
+ this.validateCountryBlacklist(projectDTO)
}
// Returns amount of existing events starting from month
diff --git a/backend/apps/community/src/common/utils.ts b/backend/apps/community/src/common/utils.ts
index 3a0b838dc..d9a34ba2f 100644
--- a/backend/apps/community/src/common/utils.ts
+++ b/backend/apps/community/src/common/utils.ts
@@ -64,6 +64,7 @@ interface ClickhouseProjectShareWithProject {
projectName: string
projectOrigins: string
projectIpBlacklist: string
+ projectCountryBlacklist: string
projectActive: number
projectPublic: number
projectIsPasswordProtected: number
@@ -526,6 +527,7 @@ const findProjectSharesByUserClickhouse = async (
p.name AS projectName,
p.origins AS projectOrigins,
p.ipBlacklist AS projectIpBlacklist,
+ p.countryBlacklist AS projectCountryBlacklist,
p.active AS projectActive,
p.public AS projectPublic,
p.isPasswordProtected AS projectIsPasswordProtected,
diff --git a/backend/apps/community/src/project/dto/project.dto.ts b/backend/apps/community/src/project/dto/project.dto.ts
index 1e0d4d944..9e5de381b 100644
--- a/backend/apps/community/src/project/dto/project.dto.ts
+++ b/backend/apps/community/src/project/dto/project.dto.ts
@@ -1,5 +1,11 @@
import { ApiProperty } from '@nestjs/swagger'
-import { IsEnum, IsNotEmpty, IsOptional, Length } from 'class-validator'
+import {
+ IsEnum,
+ IsNotEmpty,
+ IsOptional,
+ Length,
+ IsArray,
+} from 'class-validator'
import { BotsProtectionLevel } from '../entity'
export class ProjectDTO {
@@ -44,11 +50,13 @@ export class ProjectDTO {
ipBlacklist: string[] | null
@ApiProperty({
- example: 'RU,BY,KP',
+ example: ['RU', 'BY', 'KP'],
required: false,
description:
'Array of blocked country codes (ISO 3166-1 alpha-2). Traffic from these countries will not be tracked.',
})
+ @IsOptional()
+ @IsArray()
countryBlacklist: string[] | null
@ApiProperty({
diff --git a/backend/apps/community/src/project/project.controller.ts b/backend/apps/community/src/project/project.controller.ts
index 4ca95f0e3..6c01af25e 100644
--- a/backend/apps/community/src/project/project.controller.ts
+++ b/backend/apps/community/src/project/project.controller.ts
@@ -167,6 +167,7 @@ export class ProjectController {
name: row.projectName,
origins: row.projectOrigins,
ipBlacklist: row.projectIpBlacklist,
+ countryBlacklist: row.projectCountryBlacklist,
active: row.projectActive,
public: row.projectPublic,
isPasswordProtected: row.projectIsPasswordProtected,
diff --git a/backend/apps/community/src/project/project.service.ts b/backend/apps/community/src/project/project.service.ts
index fac5d1f1e..7d300d80b 100644
--- a/backend/apps/community/src/project/project.service.ts
+++ b/backend/apps/community/src/project/project.service.ts
@@ -380,6 +380,12 @@ export class ProjectService {
: _join(updProject.ipBlacklist, ',')
}
+ if (!_isNull(updProject.countryBlacklist)) {
+ updProject.countryBlacklist = _isString(updProject.countryBlacklist)
+ ? updProject.countryBlacklist
+ : _join(updProject.countryBlacklist, ',')
+ }
+
return updProject
}
@@ -397,6 +403,10 @@ export class ProjectService {
? []
: _split(updProject.ipBlacklist, ',')
+ updProject.countryBlacklist = _isNull(updProject.countryBlacklist)
+ ? []
+ : _split(updProject.countryBlacklist, ',')
+
return updProject
}
@@ -544,6 +554,27 @@ export class ProjectService {
})
}
+ validateCountryBlacklist(projectDTO: ProjectDTO | UpdateProjectDto) {
+ if (!projectDTO.countryBlacklist) {
+ return
+ }
+
+ if (_size(projectDTO.countryBlacklist) > 250)
+ throw new UnprocessableEntityException(
+ 'The list of blocked countries cannot exceed 250 entries.',
+ )
+
+ const countryCodeRegex = /^[A-Z]{2}$/
+ _map(projectDTO.countryBlacklist, code => {
+ const trimmedCode = _trim(code).toUpperCase()
+ if (!countryCodeRegex.test(trimmedCode)) {
+ throw new ConflictException(
+ `Country code "${code}" is not a valid ISO 3166-1 alpha-2 code`,
+ )
+ }
+ })
+ }
+
validateProject(
projectDTO: ProjectDTO | UpdateProjectDto,
creatingProject = false,
@@ -562,5 +593,6 @@ export class ProjectService {
this.validateOrigins(projectDTO)
this.validateIPBlacklist(projectDTO)
+ this.validateCountryBlacklist(projectDTO)
}
}
diff --git a/backend/apps/community/src/user/user.controller.ts b/backend/apps/community/src/user/user.controller.ts
index 5e33eb81a..03cab10f6 100644
--- a/backend/apps/community/src/user/user.controller.ts
+++ b/backend/apps/community/src/user/user.controller.ts
@@ -100,6 +100,9 @@ export class UserController {
ipBlacklist: row.projectIpBlacklist
? String(row.projectIpBlacklist).split(',')
: [],
+ countryBlacklist: row.projectCountryBlacklist
+ ? String(row.projectCountryBlacklist).split(',')
+ : [],
active: Boolean(row.projectActive),
public: Boolean(row.projectPublic),
isPasswordProtected: Boolean(row.projectIsPasswordProtected),