Skip to content

Commit

Permalink
fix: grpc callet url and nip 11
Browse files Browse the repository at this point in the history
  • Loading branch information
ZigBalthazar committed Dec 21, 2024
1 parent 7b342d3 commit 75ea184
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 76 deletions.
3 changes: 0 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ ENABLE_DOCUMENTATION=true

#== MONGO
MONGO_DB_URL="mongodb://..."
MONGO_DB_NAME=IMMORTAL
MONGO_DB_USERNAME=IMMORTAL
MONGO_DB_PASSWORD=IMMORTAL
MONGO_DB_CONNECTION_TIMEOUT_IN_MS=500

#== JWT
Expand Down
12 changes: 12 additions & 0 deletions src/modules/config/dto/fees.dto.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,51 @@
import { ApiProperty } from '@nestjs/swagger';

Check failure on line 1 in src/modules/config/dto/fees.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/fees.dto.ts#L1

Run autofix to sort these imports! (simple-import-sort/imports)
import { NumberFieldOptional, StringFieldOptional } from '../../../../src/decorators';

export class SubscriptionDto {
@ApiProperty({ required: false })
@NumberFieldOptional()
amount: number;

@ApiProperty({ required: false })
@StringFieldOptional()
unit: string;

@ApiProperty({ required: false })
@NumberFieldOptional()
period: number;
}

export class PublicationDto {
@ApiProperty({ type: [Number], required: false })
@NumberFieldOptional({ each: true, isArray: true })
kinds: number[];

@ApiProperty({ required: false })
@NumberFieldOptional()
amount: number;

@ApiProperty({ required: false })
@StringFieldOptional()
unit: string;
}

export class AdmissionDto {
@ApiProperty({ required: false })
@NumberFieldOptional()
amount: number;

@ApiProperty({ required: false })
@StringFieldOptional()
unit: string;
}

export class FeesDto {
@ApiProperty({ type: [SubscriptionDto], required: false })
subscription?: SubscriptionDto[];

@ApiProperty({ type: [PublicationDto], required: false })
publication?: PublicationDto[];

@ApiProperty({ type: [AdmissionDto], required: false })
admission?: AdmissionDto[];
}
122 changes: 67 additions & 55 deletions src/modules/config/dto/nip11.dto.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { ApiProperty } from '@nestjs/swagger';

Check failure on line 1 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L1

Run autofix to sort these imports! (simple-import-sort/imports)
import { IsArray, IsOptional, IsString } from 'class-validator';

import { AbstractDto } from '../../../common/dto/abstract.dto';
import type { Nip11Entity } from '../entities/nip11.entity';
import { FeesDto } from './fees.dto';
import { RetentionDto } from './retention.dto';
import { LimitationDto } from './limitation.dto';
import { FeesDto } from './fees.dto';

export class Nip11DTO extends AbstractDto {
@ApiProperty()
Expand Down Expand Up @@ -87,58 +86,71 @@ export class Nip11DTO extends AbstractDto {
constructor(e: Nip11Entity) {

Check failure on line 86 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L86

Refactor this function to reduce its Cognitive Complexity from 47 to the 15 allowed (sonarjs/cognitive-complexity)
super(e);

this.name = e.name;
this.description = e.description;
this.pubkey = e.pubkey;
this.contact = e.contact;
this.banner = e.banner;
this.software = e.software;
this.supported_nips = e.supported_nips;
this.version = e.version;
this.relay_countries = e.relay_countries;
this.language_tags = e.language_tags;
this.tags = e.tags;
this.posting_policy = e.posting_policy;
this.payments_url = e.payments_url;
this.icon = e.icon;
this.url = e.url;
this.retention = {
count: e.retention?.count,
kinds: e.retention?.kinds,
time: e.retention?.time,
};
this.fees = {
admission: e.fees?.admission?.map((a) => ({
amount: a.amount,
unit: a.unit,
})),
publication: e.fees?.publication?.map((p) => ({
amount: p.amount,
kinds: p.kinds,
unit: p.unit,
})),
subscription: e.fees?.subscription?.map((s) => ({
amount: s.amount,
period: s.period,
unit: s.unit,
})),
};

this.limitations = {
auth_required: e.limitations?.auth_required,
max_message_length: e.limitations?.max_message_length,
max_subid_length: e.limitations?.max_subid_length,
max_filters: e.limitations?.max_filters,
max_subscriptions: e.limitations?.max_subscriptions,
min_pow_difficulty: e.limitations?.min_pow_difficulty,
payment_required: e.limitations?.payment_required,
restricted_writes: e.limitations?.restricted_writes,
max_event_tags: e.limitations?.max_event_tags,
max_content_length: e.limitations?.max_content_length,
created_at_lower_limit: e.limitations?.created_at_lower_limit,
created_at_upper_limit: e.limitations?.created_at_upper_limit,
default_query_limit: e.limitations?.max_limit,
max_limit: e.limitations?.max_limit,
};
this.name = e.name ?? undefined;

Check failure on line 89 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L89

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.description = e.description ?? undefined;

Check failure on line 90 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L90

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.banner = e.banner ?? undefined;

Check failure on line 91 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L91

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.pubkey = e.pubkey ?? undefined;

Check failure on line 92 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L92

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.contact = e.contact ?? undefined;

Check failure on line 93 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L93

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.software = e.software ?? undefined;

Check failure on line 94 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L94

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.supported_nips = e.supported_nips ?? undefined;

Check failure on line 95 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L95

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.version = e.version ?? undefined;

Check failure on line 96 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L96

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.relay_countries = e.relay_countries ?? undefined;

Check failure on line 97 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L97

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.language_tags = e.language_tags ?? undefined;

Check failure on line 98 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L98

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.tags = e.tags ?? undefined;

Check failure on line 99 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L99

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)

// Optional fields
this.posting_policy = e.posting_policy ?? undefined;

Check failure on line 102 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L102

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.payments_url = e.payments_url ?? undefined;

Check failure on line 103 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L103

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.icon = e.icon ?? undefined;

Check failure on line 104 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L104

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)
this.url = e.url ?? undefined;

Check failure on line 105 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L105

Unnecessary conditional, expected left-hand side of `??` operator to be possibly null or undefined (@typescript-eslint/no-unnecessary-condition)

// Handle Retention
this.retention = e.retention && (e.retention.count || e.retention.kinds || e.retention.time)

Check failure on line 108 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L108

Insert `⏎·····` (prettier/prettier)

Check failure on line 108 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L108

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator (@typescript-eslint/prefer-nullish-coalescing)

Check failure on line 108 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L108

Prefer using nullish coalescing operator (`??`) instead of a logical or (`||`), as it is a safer operator (@typescript-eslint/prefer-nullish-coalescing)
? {

Check failure on line 109 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L109

Insert `··` (prettier/prettier)
count: e.retention.count ?? undefined,

Check failure on line 110 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L110

Insert `··` (prettier/prettier)
kinds: e.retention.kinds ?? undefined,

Check failure on line 111 in src/modules/config/dto/nip11.dto.ts

View workflow job for this annotation

GitHub Actions / ESLint

src/modules/config/dto/nip11.dto.ts#L111

Insert `··` (prettier/prettier)
time: e.retention.time ?? undefined,
}
: undefined;

// Handle Fees
this.fees = e.fees && (e.fees.admission || e.fees.publication || e.fees.subscription)
? {
admission: e.fees.admission?.map(a => ({
amount: a.amount ?? undefined,
unit: a.unit ?? undefined,
})),
publication: e.fees.publication?.map(p => ({
amount: p.amount ?? undefined,
kinds: p.kinds ?? undefined,
unit: p.unit ?? undefined,
})),
subscription: e.fees.subscription?.map(s => ({
amount: s.amount ?? undefined,
period: s.period ?? undefined,
unit: s.unit ?? undefined,
})),
}
: undefined;

// Handle Limitations
this.limitations = e.limitations
? {
auth_required: e.limitations.auth_required ?? undefined,
max_message_length: e.limitations.max_message_length ?? undefined,
max_subid_length: e.limitations.max_subid_length ?? undefined,
max_filters: e.limitations.max_filters ?? undefined,
max_subscriptions: e.limitations.max_subscriptions ?? undefined,
min_pow_difficulty: e.limitations.min_pow_difficulty ?? undefined,
payment_required: e.limitations.payment_required ?? undefined,
restricted_writes: e.limitations.restricted_writes ?? undefined,
max_event_tags: e.limitations.max_event_tags ?? undefined,
max_content_length: e.limitations.max_content_length ?? undefined,
created_at_lower_limit: e.limitations.created_at_lower_limit ?? undefined,
created_at_upper_limit: e.limitations.created_at_upper_limit ?? undefined,
default_query_limit: e.limitations.max_limit ?? undefined,
max_limit: e.limitations.max_limit ?? undefined,
}
: undefined;
}
}
7 changes: 0 additions & 7 deletions src/modules/config/entities/nip11.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,26 +96,20 @@ export class Nip11Entity extends AbstractEntity<Nip11DTO> {

// Handle Retention
this.retention = {
...this.retention,
...(item.retention ?? {}),
time: item.retention?.time ?? this.retention?.time,
count: item.retention?.count ?? this.retention?.count,
kinds: item.retention?.kinds ?? this.retention?.kinds,
};

// Handle Fees
this.fees = {
...this.fees,
...(item.fees ?? {}),
subscription: item.fees?.subscription ?? this.fees?.subscription,
publication: item.fees?.publication ?? this.fees?.publication,
admission: item.fees?.admission ?? this.fees?.admission,
};

// Handle Limitations
this.limitations = {
...this.limitations,
...(item.limitations ?? {}),
max_message_length: item.limitations?.max_message_length ?? this.limitations?.max_message_length,
max_subscriptions: item.limitations?.max_subscriptions ?? this.limitations?.max_subscriptions,
max_filters: item.limitations?.max_filters ?? this.limitations?.max_filters,
Expand All @@ -132,5 +126,4 @@ export class Nip11Entity extends AbstractEntity<Nip11DTO> {
default_query_limit: item.limitations?.default_query_limit ?? this.limitations?.default_query_limit,
};
}

}
6 changes: 3 additions & 3 deletions src/modules/config/entities/retention.entity.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Column } from 'typeorm';

export class RetentionEntity {
@Column()
@Column({ nullable: true })
time?: number;

@Column()
@Column({ nullable: true })
count?: number;

@Column()
@Column({ nullable: true })
kinds?: number[];
}
2 changes: 1 addition & 1 deletion src/modules/grpc/gen/ts/immortal-health-service.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/modules/grpc/gen/ts/kraken.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/modules/grpc/proto/kraken.proto
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ service Log {

// Request to register a service
message registerServiceRequest {
string url = 1; // Service URL
string port = 1; // Service grpc port
uint32 heartbeat_duration_in_sec = 2; // Heartbeat interval in seconds
ServiceTypeEnum type = 3; // Type of the service (e.g., RELAY)
string region = 4; // Region of the service
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ export class ServiceRegistryGrpcController implements ServiceRegistryGrpcControl
call: ServerUnaryCall<registerServiceRequest, registerServiceResponse>,
): Promise<registerServiceResponse> {
try {
const { heartbeatDurationInSec, type, region } = request;
const { port, heartbeatDurationInSec, type, region } = request;

const serviceTypeKey = ServiceTypeEnum[type];

if (!serviceTypeKey || !(serviceTypeKey in ServiceType)) {
throw new Error(`Invalid service type: ${type}`);
}

const callerIp = call.getPeer();
const callerIp = call.getPeer().split(':')[0] + ':' + port;

const { token } = await this.serviceRegistryService.register({
heartbeatDurationInSec,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';

import type RegisterServiceRegistryDto from '../dtos/service-registry-register.dto';
import ServiceRegistryService from '../services/service-registry.service';
import JwtAuthGuard from 'src/modules/auth/guards/jwt-auth.guard';
import JwtAuthGuard from '../../../../src/modules/auth/guards/jwt-auth.guard';

@Controller('service-registry')
@ApiTags('service-registry')
Expand Down

0 comments on commit 75ea184

Please sign in to comment.