Skip to content

Commit

Permalink
fix: apply consistent formatting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeCap08055 committed Aug 9, 2024
1 parent f40e6eb commit d1103fe
Show file tree
Hide file tree
Showing 60 changed files with 977 additions and 318 deletions.
11 changes: 1 addition & 10 deletions services/content-publishing/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,10 +1 @@
{
"arrowParens": "always",
"bracketSpacing": true,
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 180,
"tabWidth": 2,
"useTabs": false
}
"@projectlibertylabs/prettier-config"
38 changes: 31 additions & 7 deletions services/content-publishing/apps/api/src/api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ import { BulkJobOptions } from 'bullmq/dist/esm/interfaces';
import { InjectRedis } from '@songkeys/nestjs-redis';
import Redis from 'ioredis';
import { HttpErrorByCode } from '@nestjs/common/utils/http-error-by-code.util';
import { AnnouncementTypeDto, RequestTypeDto, AnnouncementResponseDto, AssetIncludedRequestDto, isImage, UploadResponseDto, AttachmentType } from '#libs/dtos';
import {
AnnouncementTypeDto,
RequestTypeDto,
AnnouncementResponseDto,
AssetIncludedRequestDto,
isImage,
UploadResponseDto,
AttachmentType,
} from '#libs/dtos';
import { IRequestJob, IAssetMetadata, IAssetJob } from '#libs/interfaces';
import { REQUEST_QUEUE_NAME, ASSET_QUEUE_NAME } from '#libs/queues/queue.constants';
import { calculateIpfsCID } from '#libs/utils/ipfs';
Expand Down Expand Up @@ -53,10 +61,14 @@ export class ApiService {
};
}

async validateAssetsAndFetchMetadata(content: AssetIncludedRequestDto): Promise<IRequestJob['assetToMimeType'] | undefined> {
async validateAssetsAndFetchMetadata(
content: AssetIncludedRequestDto,
): Promise<IRequestJob['assetToMimeType'] | undefined> {
const checkingList: { onlyImage: boolean; referenceId: string }[] = [];
if (content.profile) {
content.profile.icon?.forEach((reference) => checkingList.push({ onlyImage: true, referenceId: reference.referenceId }));
content.profile.icon?.forEach((reference) =>
checkingList.push({ onlyImage: true, referenceId: reference.referenceId }),
);
} else if (content.content) {
content.content.assets?.forEach((asset) =>
asset.references?.forEach((reference) =>
Expand All @@ -68,12 +80,16 @@ export class ApiService {
);
}

const redisResults = await Promise.all(checkingList.map((obj) => this.redis.get(getAssetMetadataKey(obj.referenceId))));
const redisResults = await Promise.all(
checkingList.map((obj) => this.redis.get(getAssetMetadataKey(obj.referenceId))),
);
const errors: string[] = [];
const map = new Map();
redisResults.forEach((res, index) => {
if (res === null) {
errors.push(`${content.profile ? 'profile.icon' : 'content.assets'}.referenceId ${checkingList[index].referenceId} does not exist!`);
errors.push(
`${content.profile ? 'profile.icon' : 'content.assets'}.referenceId ${checkingList[index].referenceId} does not exist!`,
);
} else {
const metadata: IAssetMetadata = JSON.parse(res);
map[checkingList[index].referenceId] = { mimeType: metadata.mimeType, attachmentType: metadata.type };
Expand Down Expand Up @@ -101,7 +117,11 @@ export class ApiService {
const jobs: any[] = [];
files.forEach((f, index) => {
// adding data and metadata to the transaction
dataTransaction = dataTransaction.setex(getAssetDataKey(references[index]), STORAGE_EXPIRE_UPPER_LIMIT_SECONDS, f.buffer);
dataTransaction = dataTransaction.setex(
getAssetDataKey(references[index]),
STORAGE_EXPIRE_UPPER_LIMIT_SECONDS,
f.buffer,
);
const type = ((m) => {
switch (m) {
case 'image':
Expand All @@ -122,7 +142,11 @@ export class ApiService {
type: type,
};

metadataTransaction = metadataTransaction.setex(getAssetMetadataKey(references[index]), STORAGE_EXPIRE_UPPER_LIMIT_SECONDS, JSON.stringify(assetCache));
metadataTransaction = metadataTransaction.setex(
getAssetMetadataKey(references[index]),
STORAGE_EXPIRE_UPPER_LIMIT_SECONDS,
JSON.stringify(assetCache),
);

// adding asset job to the jobs
jobs.push({
Expand Down
3 changes: 2 additions & 1 deletion services/content-publishing/apps/api/src/build-openapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ process.env.ENVIRONMENT = 'dev';
process.env.REDIS_URL = 'http://127.0.0.1';
process.env.FREQUENCY_URL = 'http://127.0.0.1';
process.env.FREQUENCY_HTTP_URL = 'http://127.0.0.1';
process.env.PROVIDER_ACCOUNT_SEED_PHRASE = 'offer debate skin describe light badge fish turtle actual inject struggle border';
process.env.PROVIDER_ACCOUNT_SEED_PHRASE =
'offer debate skin describe light badge fish turtle actual inject struggle border';
process.env.PROVIDER_ID = '0';
process.env.PROVIDER_BASE_URL = 'http://127.0.0.1';
process.env.CAPACITY_LIMIT = '{"type":"amount","value":0}';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { FilesUploadDto, UploadResponseDto } from '../../../../../libs/common/src/dtos/common.dto';
import { DSNP_VALID_MIME_TYPES } from '#libs/dtos/validation.dto';
import { Controller, HttpCode, HttpStatus, Logger, ParseFilePipeBuilder, Put, UploadedFiles, UseInterceptors } from '@nestjs/common';
import {
Controller,
HttpCode,
HttpStatus,
Logger,
ParseFilePipeBuilder,
Put,
UploadedFiles,
UseInterceptors,
} from '@nestjs/common';
import { FilesInterceptor } from '@nestjs/platform-express';
import { ApiBody, ApiConsumes, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiService } from '../../api.service';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
import { Body, Controller, Delete, HttpCode, Logger, Param, Post, Put } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiService } from '../../api.service';
import { DsnpUserIdParam, BroadcastDto, AnnouncementResponseDto, AssetIncludedRequestDto, AnnouncementTypeDto, ReplyDto, ReactionDto, UpdateDto, TombstoneDto } from '#libs/dtos';
import {
DsnpUserIdParam,
BroadcastDto,
AnnouncementResponseDto,
AssetIncludedRequestDto,
AnnouncementTypeDto,
ReplyDto,
ReactionDto,
UpdateDto,
TombstoneDto,
} from '#libs/dtos';

@Controller('v1/content')
@ApiTags('v1/content')
Expand All @@ -16,7 +26,10 @@ export class ContentControllerV1 {
@ApiOperation({ summary: 'Create DSNP Broadcast for User' })
@HttpCode(202)
@ApiResponse({ status: '2XX', type: AnnouncementResponseDto })
async broadcast(@Param() userDsnpId: DsnpUserIdParam, @Body() broadcastDto: BroadcastDto): Promise<AnnouncementResponseDto> {
async broadcast(
@Param() userDsnpId: DsnpUserIdParam,
@Body() broadcastDto: BroadcastDto,
): Promise<AnnouncementResponseDto> {
const metadata = await this.apiService.validateAssetsAndFetchMetadata(broadcastDto as AssetIncludedRequestDto);
return this.apiService.enqueueRequest(AnnouncementTypeDto.BROADCAST, userDsnpId.userDsnpId, broadcastDto, metadata);
}
Expand All @@ -34,7 +47,10 @@ export class ContentControllerV1 {
@ApiOperation({ summary: 'Create DSNP Reaction for User' })
@HttpCode(202)
@ApiResponse({ status: '2XX', type: AnnouncementResponseDto })
async reaction(@Param() userDsnpId: DsnpUserIdParam, @Body() reactionDto: ReactionDto): Promise<AnnouncementResponseDto> {
async reaction(
@Param() userDsnpId: DsnpUserIdParam,
@Body() reactionDto: ReactionDto,
): Promise<AnnouncementResponseDto> {
return this.apiService.enqueueRequest(AnnouncementTypeDto.REACTION, userDsnpId.userDsnpId, reactionDto);
}

Expand All @@ -51,7 +67,10 @@ export class ContentControllerV1 {
@ApiOperation({ summary: 'Delete DSNP Content for User' })
@HttpCode(202)
@ApiResponse({ status: '2XX', type: AnnouncementResponseDto })
async delete(@Param() userDsnpId: DsnpUserIdParam, @Body() tombstoneDto: TombstoneDto): Promise<AnnouncementResponseDto> {
async delete(
@Param() userDsnpId: DsnpUserIdParam,
@Body() tombstoneDto: TombstoneDto,
): Promise<AnnouncementResponseDto> {
return this.apiService.enqueueRequest(AnnouncementTypeDto.TOMBSTONE, userDsnpId.userDsnpId, tombstoneDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,13 @@ export class DevelopmentControllerV1 {
const promises: Promise<Job>[] = [];
// eslint-disable-next-line no-plusplus
for (let i = 0; i < count; i++) {
let data: BroadcastAnnouncement | ProfileAnnouncement | UpdateAnnouncement | ReplyAnnouncement | ReactionAnnouncement | TombstoneAnnouncement;
let data:
| BroadcastAnnouncement
| ProfileAnnouncement
| UpdateAnnouncement
| ReplyAnnouncement
| ReactionAnnouncement
| TombstoneAnnouncement;
// eslint-disable-next-line default-case
const fromId = `${Math.floor(Math.random() * 100000000)}`;
const hash = `${Math.floor(Math.random() * 100000000)}`;
Expand All @@ -94,7 +100,13 @@ export class DevelopmentControllerV1 {
data = createProfile(fromId, `https://example.com/${Math.floor(Math.random() * 100000000)}`, hash);
break;
case AnnouncementTypeDto.UPDATE:
data = createUpdate(fromId, `https://example.com/${Math.floor(Math.random() * 100000000)}`, hash, AnnouncementType.Broadcast, `${Math.floor(Math.random() * 100000000)}`);
data = createUpdate(
fromId,
`https://example.com/${Math.floor(Math.random() * 100000000)}`,
hash,
AnnouncementType.Broadcast,
`${Math.floor(Math.random() * 100000000)}`,
);
break;
case AnnouncementTypeDto.REPLY:
data = createReply(
Expand All @@ -105,7 +117,12 @@ export class DevelopmentControllerV1 {
);
break;
case AnnouncementTypeDto.REACTION:
data = createReaction(fromId, '🤌🏼', `dsnp://0x${Math.floor(Math.random() * 100000000)}/0x${Math.floor(Math.random() * 100000000)}`, 1);
data = createReaction(
fromId,
'🤌🏼',
`dsnp://0x${Math.floor(Math.random() * 100000000)}/0x${Math.floor(Math.random() * 100000000)}`,
1,
);
break;
case AnnouncementTypeDto.TOMBSTONE:
data = createTombstone(fromId, AnnouncementType.Reply, hash);
Expand All @@ -117,7 +134,9 @@ export class DevelopmentControllerV1 {
const jobId = await calculateDsnpHash(Buffer.from(JSON.stringify(data)));
const queue = this.queueMapper.get(queueType);
if (queue) {
promises.push(queue.add(`Dummy Job - ${data.id}`, data, { jobId, removeOnFail: false, removeOnComplete: true }));
promises.push(
queue.add(`Dummy Job - ${data.id}`, data, { jobId, removeOnFail: false, removeOnComplete: true }),
);
}
}
await Promise.all(promises);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Body, Controller, HttpCode, Logger, Param, Put } from '@nestjs/common';
import { ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiService } from '../../api.service';
import { DsnpUserIdParam, ProfileDto, AnnouncementResponseDto, AssetIncludedRequestDto, AnnouncementTypeDto } from '#libs/dtos';
import {
DsnpUserIdParam,
ProfileDto,
AnnouncementResponseDto,
AssetIncludedRequestDto,
AnnouncementTypeDto,
} from '#libs/dtos';

@Controller('v1/profile')
@ApiTags('v1/profile')
Expand All @@ -16,7 +22,10 @@ export class ProfileControllerV1 {
@ApiOperation({ summary: "Update a user's Profile" })
@HttpCode(202)
@ApiResponse({ status: '2XX', type: AnnouncementResponseDto })
async profile(@Param() userDsnpId: DsnpUserIdParam, @Body() profileDto: ProfileDto): Promise<AnnouncementResponseDto> {
async profile(
@Param() userDsnpId: DsnpUserIdParam,
@Body() profileDto: ProfileDto,
): Promise<AnnouncementResponseDto> {
const metadata = await this.apiService.validateAssetsAndFetchMetadata(profileDto as AssetIncludedRequestDto);
return this.apiService.enqueueRequest(AnnouncementTypeDto.PROFILE, userDsnpId.userDsnpId, profileDto, metadata);
}
Expand Down
4 changes: 3 additions & 1 deletion services/content-publishing/apps/api/src/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ export default async () => {
},
],
],
controllers: [[import('./controllers/health.controller'), { HealthController: { healthz: {}, livez: {}, readyz: {} } }]],
controllers: [
[import('./controllers/health.controller'), { HealthController: { healthz: {}, livez: {}, readyz: {} } }],
],
},
};
};
47 changes: 37 additions & 10 deletions services/content-publishing/apps/api/test/app.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import request from 'supertest';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { randomFill } from 'crypto';
import { ApiModule } from '../src/api.module';
import { validBroadCastNoUploadedAssets, validContentNoUploadedAssets, validProfileNoUploadedAssets, validReaction, validReplyNoUploadedAssets } from './mockRequestData';
import {
validBroadCastNoUploadedAssets,
validContentNoUploadedAssets,
validProfileNoUploadedAssets,
validReaction,
validReplyNoUploadedAssets,
} from './mockRequestData';

describe('AppController E2E request verification!', () => {
let app: INestApplication;
Expand All @@ -28,11 +34,14 @@ describe('AppController E2E request verification!', () => {
await app.init();
});

it('(GET) /healthz', () => request(app.getHttpServer()).get('/healthz').expect(200).expect({ status: 200, message: 'Service is healthy' }));
it('(GET) /healthz', () =>
request(app.getHttpServer()).get('/healthz').expect(200).expect({ status: 200, message: 'Service is healthy' }));

it('(GET) /livez', () => request(app.getHttpServer()).get('/livez').expect(200).expect({ status: 200, message: 'Service is live' }));
it('(GET) /livez', () =>
request(app.getHttpServer()).get('/livez').expect(200).expect({ status: 200, message: 'Service is live' }));

it('(GET) /readyz', () => request(app.getHttpServer()).get('/readyz').expect(200).expect({ status: 200, message: 'Service is ready' }));
it('(GET) /readyz', () =>
request(app.getHttpServer()).get('/readyz').expect(200).expect({ status: 200, message: 'Service is ready' }));

describe('Validate Route params', () => {
it('invalid userDsnpId should fail', async () => {
Expand All @@ -55,7 +64,10 @@ describe('AppController E2E request verification!', () => {

it('valid broadcast request with uploaded assets should work!', async () => {
const file = Buffer.from('g'.repeat(30 * 1000 * 1000)); // 30MB
const response = await request(app.getHttpServer()).put(`/v1/asset/upload`).attach('files', file, 'file1.jpg').expect(202);
const response = await request(app.getHttpServer())
.put(`/v1/asset/upload`)
.attach('files', file, 'file1.jpg')
.expect(202);
await sleep(1000);
const validBroadCastWithUploadedAssets = {
content: {
Expand Down Expand Up @@ -363,7 +375,10 @@ describe('AppController E2E request verification!', () => {

it('valid request with uploaded assets should work!', async () => {
const file = Buffer.from('h'.repeat(30 * 1000 * 1000)); // 30MB
const response = await request(app.getHttpServer()).put(`/v1/asset/upload`).attach('files', file, 'file1.jpg').expect(202);
const response = await request(app.getHttpServer())
.put(`/v1/asset/upload`)
.attach('files', file, 'file1.jpg')
.expect(202);
await sleep(1000);
const validReplyWithUploadedAssets = {
...validReplyNoUploadedAssets,
Expand Down Expand Up @@ -501,7 +516,10 @@ describe('AppController E2E request verification!', () => {

it('valid request with uploaded assets should work!', async () => {
const file = Buffer.from('g'.repeat(30 * 1000 * 1000)); // 30MB
const response = await request(app.getHttpServer()).put(`/v1/asset/upload`).attach('files', file, 'file1.jpg').expect(202);
const response = await request(app.getHttpServer())
.put(`/v1/asset/upload`)
.attach('files', file, 'file1.jpg')
.expect(202);
await sleep(1000);
const validContentWithUploadedAssets = {
...validContentNoUploadedAssets,
Expand Down Expand Up @@ -624,7 +642,10 @@ describe('AppController E2E request verification!', () => {

it('valid request with uploaded assets should work!', async () => {
const file = Buffer.from('n'.repeat(30 * 1000 * 1000)); // 30MB
const response = await request(app.getHttpServer()).put(`/v1/asset/upload`).attach('files', file, 'file.jpg').expect(202);
const response = await request(app.getHttpServer())
.put(`/v1/asset/upload`)
.attach('files', file, 'file.jpg')
.expect(202);
await sleep(1000);
const validUploadWithUploadedAssets = {
...validProfileNoUploadedAssets,
Expand Down Expand Up @@ -668,7 +689,10 @@ describe('AppController E2E request verification!', () => {

it('request with non-image uploaded assets should fail!', async () => {
const file = Buffer.from('s'.repeat(30 * 1000 * 1000)); // 30MB
const response = await request(app.getHttpServer()).put(`/v1/asset/upload`).attach('files', file, 'file.mp3').expect(202);
const response = await request(app.getHttpServer())
.put(`/v1/asset/upload`)
.attach('files', file, 'file.mp3')
.expect(202);
await sleep(1000);
const profileContent = {
...validProfileNoUploadedAssets,
Expand Down Expand Up @@ -759,7 +783,10 @@ describe('AppController E2E request verification!', () => {
randomFill(buffer, (err, buf) => {
if (err) throw err;
});
const response = await request(app.getHttpServer()).put(`/v1/asset/upload`).attach('files', Buffer.from(buffer), 'file1.jpg').expect(202);
const response = await request(app.getHttpServer())
.put(`/v1/asset/upload`)
.attach('files', Buffer.from(buffer), 'file1.jpg')
.expect(202);
const assetId = response.body.assetIds[0];
await sleep(2000);
return request(app.getHttpServer())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,11 @@ export class AssetProcessorService extends BaseConsumer {
const secondsPassed = Math.round((Date.now() - job.timestamp) / 1000);
const expectedSecondsToExpire = this.configService.assetExpirationIntervalSeconds;
const secondsToExpire = Math.max(0, expectedSecondsToExpire - secondsPassed);
const result = await this.redis.pipeline().expire(job.data.contentLocation, secondsToExpire, 'LT').expire(job.data.metadataLocation, secondsToExpire, 'LT').exec();
const result = await this.redis
.pipeline()
.expire(job.data.contentLocation, secondsToExpire, 'LT')
.expire(job.data.metadataLocation, secondsToExpire, 'LT')
.exec();
this.logger.debug(result);
// calling in the end for graceful shutdowns
super.onCompleted(job);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ describe('BatchAnnouncer', () => {
const mockClient = new Redis();

beforeEach(async () => {
ipfsAnnouncer = new BatchAnnouncer(mockClient, mockConfigService as any, mockBlockchainService as any, mockIpfsService as any);
ipfsAnnouncer = new BatchAnnouncer(
mockClient,
mockConfigService as any,
mockBlockchainService as any,
mockIpfsService as any,
);
});
it('should be defined', () => {
expect(ipfsAnnouncer).toBeDefined();
Expand Down
Loading

0 comments on commit d1103fe

Please sign in to comment.