From 7fb7ff9e8cb542ca044c32c66227177a24510feb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus-Peter=20H=C3=BCbner?= Date: Thu, 20 Apr 2023 02:28:29 +0200 Subject: [PATCH] rename communities to federated_communities --- backend/src/config/index.ts | 2 +- .../federation/client/1_0/FederationClient.ts | 6 +- .../federation/client/1_1/FederationClient.ts | 6 +- backend/src/federation/validateCommunities.ts | 23 ++++--- backend/src/graphql/model/Community.ts | 37 +++++------- .../src/graphql/model/FederatedCommunity.ts | 45 ++++++++++++++ .../resolver/CommunityResolver.test.ts | 40 ++++++------- .../src/graphql/resolver/CommunityResolver.ts | 24 ++++++-- .../Community.ts | 60 +++++++++++++++++++ .../FederatedCommunity.ts | 51 ++++++++++++++++ database/entity/Community.ts | 2 +- database/entity/FederatedCommunity.ts | 1 + database/entity/index.ts | 2 + .../0065-refactor_communities_table.ts | 37 ++++++++++++ dht-node/src/config/index.ts | 2 +- dht-node/src/dht_node/index.test.ts | 18 +++--- dht-node/src/dht_node/index.ts | 16 ++--- federation/src/config/index.ts | 2 +- .../1_0/resolver/PublicKeyResolver.test.ts | 8 +-- .../api/1_0/resolver/PublicKeyResolver.ts | 4 +- 20 files changed, 301 insertions(+), 85 deletions(-) create mode 100644 backend/src/graphql/model/FederatedCommunity.ts create mode 100644 database/entity/0065-refactor_communities_table/Community.ts create mode 100644 database/entity/0065-refactor_communities_table/FederatedCommunity.ts create mode 100644 database/entity/FederatedCommunity.ts create mode 100644 database/migrations/0065-refactor_communities_table.ts diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 0c36e4d5a..fc88011ea 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -12,7 +12,7 @@ Decimal.set({ }) const constants = { - DB_VERSION: '0064-event_rename', + DB_VERSION: '0065-refactor_communities_table', DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0 LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info diff --git a/backend/src/federation/client/1_0/FederationClient.ts b/backend/src/federation/client/1_0/FederationClient.ts index 13f05e761..743d17348 100644 --- a/backend/src/federation/client/1_0/FederationClient.ts +++ b/backend/src/federation/client/1_0/FederationClient.ts @@ -1,14 +1,16 @@ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-return */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { gql } from 'graphql-request' import { GraphQLGetClient } from '@/federation/client/GraphQLGetClient' import { LogError } from '@/server/LogError' import { backendLogger as logger } from '@/server/logger' -export async function requestGetPublicKey(dbCom: DbCommunity): Promise { +export async function requestGetPublicKey( + dbCom: DbFederatedCommunity, +): Promise { let endpoint = dbCom.endPoint.endsWith('/') ? dbCom.endPoint : dbCom.endPoint + '/' endpoint = `${endpoint}${dbCom.apiVersion}/` logger.info(`requestGetPublicKey with endpoint='${endpoint}'...`) diff --git a/backend/src/federation/client/1_1/FederationClient.ts b/backend/src/federation/client/1_1/FederationClient.ts index bda185fba..35c88bf3b 100644 --- a/backend/src/federation/client/1_1/FederationClient.ts +++ b/backend/src/federation/client/1_1/FederationClient.ts @@ -1,14 +1,16 @@ /* eslint-disable @typescript-eslint/no-unsafe-return */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { gql } from 'graphql-request' import { GraphQLGetClient } from '@/federation/client/GraphQLGetClient' import { LogError } from '@/server/LogError' import { backendLogger as logger } from '@/server/logger' -export async function requestGetPublicKey(dbCom: DbCommunity): Promise { +export async function requestGetPublicKey( + dbCom: DbFederatedCommunity, +): Promise { let endpoint = dbCom.endPoint.endsWith('/') ? dbCom.endPoint : dbCom.endPoint + '/' endpoint = `${endpoint}${dbCom.apiVersion}/` logger.info(`requestGetPublicKey with endpoint='${endpoint}'...`) diff --git a/backend/src/federation/validateCommunities.ts b/backend/src/federation/validateCommunities.ts index 0e8c7cb12..b38f38ee9 100644 --- a/backend/src/federation/validateCommunities.ts +++ b/backend/src/federation/validateCommunities.ts @@ -1,5 +1,7 @@ +/** eslint-disable @typescript-eslint/no-unsafe-call */ +/** eslint-disable @typescript-eslint/no-unsafe-assignment */ import { IsNull } from '@dbTools/typeorm' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { LogError } from '@/server/LogError' import { backendLogger as logger } from '@/server/logger' @@ -23,13 +25,14 @@ export function startValidateCommunities(timerInterval: number): void { } export async function validateCommunities(): Promise { - const dbCommunities: DbCommunity[] = await DbCommunity.createQueryBuilder() - .where({ foreign: true, verifiedAt: IsNull() }) - .orWhere('verified_at < last_announced_at') - .getMany() + const dbFederatedCommunities: DbFederatedCommunity[] = + await DbFederatedCommunity.createQueryBuilder() + .where({ foreign: true, verifiedAt: IsNull() }) + .orWhere('verified_at < last_announced_at') + .getMany() - logger.debug(`Federation: found ${dbCommunities.length} dbCommunities`) - for (const dbCom of dbCommunities) { + logger.debug(`Federation: found ${dbFederatedCommunities.length} dbCommunities`) + for (const dbCom of dbFederatedCommunities) { logger.debug('Federation: dbCom', dbCom) const apiValueStrings: string[] = Object.values(ApiVersionType) logger.debug(`suppported ApiVersions=`, apiValueStrings) @@ -46,7 +49,7 @@ export async function validateCommunities(): Promise { ) if (pubKey && pubKey === dbCom.publicKey.toString()) { logger.info(`Federation: matching publicKey: ${pubKey}`) - await DbCommunity.update({ id: dbCom.id }, { verifiedAt: new Date() }) + await DbFederatedCommunity.update({ id: dbCom.id }, { verifiedAt: new Date() }) logger.debug(`Federation: updated dbCom: ${JSON.stringify(dbCom)}`) } else { logger.warn( @@ -74,7 +77,9 @@ function isLogError(err: unknown) { return err instanceof LogError } -async function invokeVersionedRequestGetPublicKey(dbCom: DbCommunity): Promise { +async function invokeVersionedRequestGetPublicKey( + dbCom: DbFederatedCommunity, +): Promise { switch (dbCom.apiVersion) { case ApiVersionType.V1_0: return v1_0_requestGetPublicKey(dbCom) diff --git a/backend/src/graphql/model/Community.ts b/backend/src/graphql/model/Community.ts index d79d40034..bc310a39f 100644 --- a/backend/src/graphql/model/Community.ts +++ b/backend/src/graphql/model/Community.ts @@ -6,14 +6,12 @@ export class Community { constructor(dbCom: DbCommunity) { this.id = dbCom.id this.foreign = dbCom.foreign - this.publicKey = dbCom.publicKey.toString() - this.url = - (dbCom.endPoint.endsWith('/') ? dbCom.endPoint : dbCom.endPoint + '/') + dbCom.apiVersion - this.lastAnnouncedAt = dbCom.lastAnnouncedAt - this.verifiedAt = dbCom.verifiedAt - this.lastErrorAt = dbCom.lastErrorAt - this.createdAt = dbCom.createdAt - this.updatedAt = dbCom.updatedAt + this.name = dbCom.name + this.description = dbCom.description + this.url = dbCom.url + this.creationDate = dbCom.creationDate + this.uuid = dbCom.communityUuid + this.authenticatedAt = dbCom.authenticatedAt } @Field(() => Int) @@ -22,24 +20,21 @@ export class Community { @Field(() => Boolean) foreign: boolean - @Field(() => String) - publicKey: string + @Field(() => String, { nullable: true }) + name: string | null + + @Field(() => String, { nullable: true }) + description: string | null @Field(() => String) url: string @Field(() => Date, { nullable: true }) - lastAnnouncedAt: Date | null + creationDate: Date | null + + @Field(() => String, { nullable: true }) + uuid: string | null @Field(() => Date, { nullable: true }) - verifiedAt: Date | null - - @Field(() => Date, { nullable: true }) - lastErrorAt: Date | null - - @Field(() => Date, { nullable: true }) - createdAt: Date | null - - @Field(() => Date, { nullable: true }) - updatedAt: Date | null + authenticatedAt: Date | null } diff --git a/backend/src/graphql/model/FederatedCommunity.ts b/backend/src/graphql/model/FederatedCommunity.ts new file mode 100644 index 000000000..856a10d23 --- /dev/null +++ b/backend/src/graphql/model/FederatedCommunity.ts @@ -0,0 +1,45 @@ +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' +import { ObjectType, Field, Int } from 'type-graphql' + +@ObjectType() +export class FederatedCommunity { + constructor(dbCom: DbFederatedCommunity) { + this.id = dbCom.id + this.foreign = dbCom.foreign + this.publicKey = dbCom.publicKey.toString() + this.url = + (dbCom.endPoint.endsWith('/') ? dbCom.endPoint : dbCom.endPoint + '/') + dbCom.apiVersion + this.lastAnnouncedAt = dbCom.lastAnnouncedAt + this.verifiedAt = dbCom.verifiedAt + this.lastErrorAt = dbCom.lastErrorAt + this.createdAt = dbCom.createdAt + this.updatedAt = dbCom.updatedAt + } + + @Field(() => Int) + id: number + + @Field(() => Boolean) + foreign: boolean + + @Field(() => String) + publicKey: string + + @Field(() => String) + url: string + + @Field(() => Date, { nullable: true }) + lastAnnouncedAt: Date | null + + @Field(() => Date, { nullable: true }) + verifiedAt: Date | null + + @Field(() => Date, { nullable: true }) + lastErrorAt: Date | null + + @Field(() => Date, { nullable: true }) + createdAt: Date | null + + @Field(() => Date, { nullable: true }) + updatedAt: Date | null +} diff --git a/backend/src/graphql/resolver/CommunityResolver.test.ts b/backend/src/graphql/resolver/CommunityResolver.test.ts index 26f4a3390..0e08896c0 100644 --- a/backend/src/graphql/resolver/CommunityResolver.test.ts +++ b/backend/src/graphql/resolver/CommunityResolver.test.ts @@ -5,7 +5,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { testEnvironment } from '@test/helpers' @@ -21,7 +21,7 @@ beforeAll(async () => { testEnv = await testEnvironment() query = testEnv.query con = testEnv.con - await DbCommunity.clear() + await DbFederatedCommunity.clear() }) afterAll(async () => { @@ -30,12 +30,12 @@ afterAll(async () => { describe('CommunityResolver', () => { describe('getCommunities', () => { - let homeCom1: DbCommunity - let homeCom2: DbCommunity - let homeCom3: DbCommunity - let foreignCom1: DbCommunity - let foreignCom2: DbCommunity - let foreignCom3: DbCommunity + let homeCom1: DbFederatedCommunity + let homeCom2: DbFederatedCommunity + let homeCom3: DbFederatedCommunity + let foreignCom1: DbFederatedCommunity + let foreignCom2: DbFederatedCommunity + let foreignCom3: DbFederatedCommunity describe('with empty list', () => { it('returns no community entry', async () => { @@ -53,29 +53,29 @@ describe('CommunityResolver', () => { beforeEach(async () => { jest.clearAllMocks() - homeCom1 = DbCommunity.create() + homeCom1 = DbFederatedCommunity.create() homeCom1.foreign = false homeCom1.publicKey = Buffer.from('publicKey-HomeCommunity') homeCom1.apiVersion = '1_0' homeCom1.endPoint = 'http://localhost/api' homeCom1.createdAt = new Date() - await DbCommunity.insert(homeCom1) + await DbFederatedCommunity.insert(homeCom1) - homeCom2 = DbCommunity.create() + homeCom2 = DbFederatedCommunity.create() homeCom2.foreign = false homeCom2.publicKey = Buffer.from('publicKey-HomeCommunity') homeCom2.apiVersion = '1_1' homeCom2.endPoint = 'http://localhost/api' homeCom2.createdAt = new Date() - await DbCommunity.insert(homeCom2) + await DbFederatedCommunity.insert(homeCom2) - homeCom3 = DbCommunity.create() + homeCom3 = DbFederatedCommunity.create() homeCom3.foreign = false homeCom3.publicKey = Buffer.from('publicKey-HomeCommunity') homeCom3.apiVersion = '2_0' homeCom3.endPoint = 'http://localhost/api' homeCom3.createdAt = new Date() - await DbCommunity.insert(homeCom3) + await DbFederatedCommunity.insert(homeCom3) }) it('returns 3 home-community entries', async () => { @@ -125,29 +125,29 @@ describe('CommunityResolver', () => { beforeEach(async () => { jest.clearAllMocks() - foreignCom1 = DbCommunity.create() + foreignCom1 = DbFederatedCommunity.create() foreignCom1.foreign = true foreignCom1.publicKey = Buffer.from('publicKey-ForeignCommunity') foreignCom1.apiVersion = '1_0' foreignCom1.endPoint = 'http://remotehost/api' foreignCom1.createdAt = new Date() - await DbCommunity.insert(foreignCom1) + await DbFederatedCommunity.insert(foreignCom1) - foreignCom2 = DbCommunity.create() + foreignCom2 = DbFederatedCommunity.create() foreignCom2.foreign = true foreignCom2.publicKey = Buffer.from('publicKey-ForeignCommunity') foreignCom2.apiVersion = '1_1' foreignCom2.endPoint = 'http://remotehost/api' foreignCom2.createdAt = new Date() - await DbCommunity.insert(foreignCom2) + await DbFederatedCommunity.insert(foreignCom2) - foreignCom3 = DbCommunity.create() + foreignCom3 = DbFederatedCommunity.create() foreignCom3.foreign = true foreignCom3.publicKey = Buffer.from('publicKey-ForeignCommunity') foreignCom3.apiVersion = '1_2' foreignCom3.endPoint = 'http://remotehost/api' foreignCom3.createdAt = new Date() - await DbCommunity.insert(foreignCom3) + await DbFederatedCommunity.insert(foreignCom3) }) it('returns 3 home community and 3 foreign community entries', async () => { diff --git a/backend/src/graphql/resolver/CommunityResolver.ts b/backend/src/graphql/resolver/CommunityResolver.ts index 19a499a9b..7306fdffb 100644 --- a/backend/src/graphql/resolver/CommunityResolver.ts +++ b/backend/src/graphql/resolver/CommunityResolver.ts @@ -1,22 +1,38 @@ import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' +import { Community } from '@model/Community' import { Resolver, Query, Authorized } from 'type-graphql' -import { Community } from '@model/Community' +import { FederatedCommunity } from '@model/FederatedCommunity' + import { RIGHTS } from '@/auth/RIGHTS' @Resolver() export class CommunityResolver { @Authorized([RIGHTS.COMMUNITIES]) - @Query(() => [Community]) - async getCommunities(): Promise { - const dbCommunities: DbCommunity[] = await DbCommunity.find({ + @Query(() => [FederatedCommunity]) + async getCommunities(): Promise { + const dbFederatedCommunities: DbFederatedCommunity[] = await DbFederatedCommunity.find({ order: { foreign: 'ASC', createdAt: 'DESC', lastAnnouncedAt: 'DESC', }, }) + return dbFederatedCommunities.map( + (dbCom: DbFederatedCommunity) => new FederatedCommunity(dbCom), + ) + } + + @Authorized([RIGHTS.COMMUNITIES]) + @Query(() => [Community]) + async getCommunitySelections(): Promise { + const dbCommunities: DbCommunity[] = await DbCommunity.find({ + order: { + name: 'ASC', + }, + }) return dbCommunities.map((dbCom: DbCommunity) => new Community(dbCom)) } } diff --git a/database/entity/0065-refactor_communities_table/Community.ts b/database/entity/0065-refactor_communities_table/Community.ts new file mode 100644 index 000000000..5857634a6 --- /dev/null +++ b/database/entity/0065-refactor_communities_table/Community.ts @@ -0,0 +1,60 @@ +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm' + +@Entity('communities') +export class Community extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'foreign', type: 'bool', nullable: false, default: true }) + foreign: boolean + + @Column({ name: 'url', length: 255, nullable: false }) + url: string + + @Column({ name: 'public_key', type: 'binary', length: 64, nullable: false }) + publicKey: Buffer + + @Column({ + name: 'community_uuid', + type: 'char', + length: 36, + nullable: true, + collation: 'utf8mb4_unicode_ci', + }) + communityUuid: string | null + + @Column({ name: 'authenticated_at', type: 'datetime', nullable: true }) + authenticatedAt: Date | null + + @Column({ name: 'name', type: 'varchar', length: 40, nullable: true }) + name: string | null + + @Column({ name: 'description', type: 'varchar', length: 255, nullable: true }) + description: string | null + + @CreateDateColumn({ name: 'creation_date', type: 'datetime', nullable: true }) + creationDate: Date | null + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP(3)', + nullable: false, + }) + createdAt: Date + + @UpdateDateColumn({ + name: 'updated_at', + type: 'datetime', + onUpdate: 'CURRENT_TIMESTAMP(3)', + nullable: true, + }) + updatedAt: Date | null +} diff --git a/database/entity/0065-refactor_communities_table/FederatedCommunity.ts b/database/entity/0065-refactor_communities_table/FederatedCommunity.ts new file mode 100644 index 000000000..0adbf4612 --- /dev/null +++ b/database/entity/0065-refactor_communities_table/FederatedCommunity.ts @@ -0,0 +1,51 @@ +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm' + +@Entity('federated_communities') +export class FederatedCommunity extends BaseEntity { + @PrimaryGeneratedColumn('increment', { unsigned: true }) + id: number + + @Column({ name: 'foreign', type: 'bool', nullable: false, default: true }) + foreign: boolean + + @Column({ name: 'public_key', type: 'binary', length: 64, default: null, nullable: true }) + publicKey: Buffer + + @Column({ name: 'api_version', length: 10, nullable: false }) + apiVersion: string + + @Column({ name: 'end_point', length: 255, nullable: false }) + endPoint: string + + @Column({ name: 'last_announced_at', type: 'datetime', nullable: true }) + lastAnnouncedAt: Date | null + + @Column({ name: 'verified_at', type: 'datetime', nullable: true }) + verifiedAt: Date | null + + @Column({ name: 'last_error_at', type: 'datetime', nullable: true }) + lastErrorAt: Date | null + + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP(3)', + nullable: false, + }) + createdAt: Date + + @UpdateDateColumn({ + name: 'updated_at', + type: 'datetime', + onUpdate: 'CURRENT_TIMESTAMP(3)', + nullable: true, + }) + updatedAt: Date | null +} diff --git a/database/entity/Community.ts b/database/entity/Community.ts index 80e5ace30..ee08323b6 100644 --- a/database/entity/Community.ts +++ b/database/entity/Community.ts @@ -1 +1 @@ -export { Community } from './0060-update_communities_table/Community' +export { Community } from './0065-refactor_communities_table/Community' diff --git a/database/entity/FederatedCommunity.ts b/database/entity/FederatedCommunity.ts new file mode 100644 index 000000000..cacaaff9c --- /dev/null +++ b/database/entity/FederatedCommunity.ts @@ -0,0 +1 @@ +export { FederatedCommunity } from './0065-refactor_communities_table/FederatedCommunity' diff --git a/database/entity/index.ts b/database/entity/index.ts index 2d9d04c3b..d44029754 100644 --- a/database/entity/index.ts +++ b/database/entity/index.ts @@ -10,6 +10,7 @@ import { Contribution } from './Contribution' import { Event } from './Event' import { ContributionMessage } from './ContributionMessage' import { Community } from './Community' +import { FederatedCommunity } from './FederatedCommunity' export const entities = [ Community, @@ -17,6 +18,7 @@ export const entities = [ ContributionLink, ContributionMessage, Event, + FederatedCommunity, LoginElopageBuys, LoginEmailOptIn, Migration, diff --git a/database/migrations/0065-refactor_communities_table.ts b/database/migrations/0065-refactor_communities_table.ts new file mode 100644 index 000000000..6cba8a174 --- /dev/null +++ b/database/migrations/0065-refactor_communities_table.ts @@ -0,0 +1,37 @@ +/* MIGRATION TO CREATE THE FEDERATION COMMUNITY TABLES + * + * This migration creates the `community` and 'communityfederation' tables in the `apollo` database (`gradido_community`). + */ + +/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ +/* eslint-disable @typescript-eslint/no-explicit-any */ + +export async function upgrade(queryFn: (query: string, values?: any[]) => Promise>) { + await queryFn(`RENAME TABLE communities TO federated_communities;`) + await queryFn(` + CREATE TABLE communities ( + \`id\` int unsigned NOT NULL AUTO_INCREMENT, + \`foreign\` tinyint(4) NOT NULL DEFAULT 1, + \`url\` varchar(255) NOT NULL, + \`public_key\` binary(64) NOT NULL, + \`community_uuid\` char(36) NULL, + \`authenticated_at\` datetime(3) NULL, + \`name\` varchar(40) NULL, + \`description\` varchar(255) NULL, + \`creation_date\` datetime(3) NULL, + \`created_at\` datetime(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3), + \`updated_at\` datetime(3), + PRIMARY KEY (id), + UNIQUE KEY url_key (url), + UNIQUE KEY uuid_key (community_uuid), + UNIQUE KEY public_key_key (public_key) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + `) +} + +export async function downgrade(queryFn: (query: string, values?: any[]) => Promise>) { + // write downgrade logic as parameter of queryFn + await queryFn(`DROP TABLE communities;`) + await queryFn(`RENAME TABLE federated_communities TO communities;`) + +} diff --git a/dht-node/src/config/index.ts b/dht-node/src/config/index.ts index 078c0fbf3..5c4676337 100644 --- a/dht-node/src/config/index.ts +++ b/dht-node/src/config/index.ts @@ -3,7 +3,7 @@ import dotenv from 'dotenv' dotenv.config() const constants = { - DB_VERSION: '0064-event_rename', + DB_VERSION: '0065-refactor_communities_table', LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info LOG_LEVEL: process.env.LOG_LEVEL || 'info', diff --git a/dht-node/src/dht_node/index.test.ts b/dht-node/src/dht_node/index.test.ts index ac5b1b21a..e76e6ac9f 100644 --- a/dht-node/src/dht_node/index.test.ts +++ b/dht-node/src/dht_node/index.test.ts @@ -5,7 +5,7 @@ import { startDHT } from './index' import DHT from '@hyperswarm/dht' import CONFIG from '@/config' import { logger } from '@test/testSetup' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { testEnvironment, cleanDB } from '@test/helpers' CONFIG.FEDERATION_DHT_SEED = '64ebcb0e3ad547848fef4197c6e2332f' @@ -261,7 +261,7 @@ describe('federation', () => { describe('with receiving wrong but tolerated property data', () => { let jsonArray: any[] - let result: DbCommunity[] = [] + let result: DbFederatedCommunity[] = [] beforeAll(async () => { jest.clearAllMocks() jsonArray = [ @@ -277,7 +277,7 @@ describe('federation', () => { }, ] await socketEventMocks.data(Buffer.from(JSON.stringify(jsonArray))) - result = await DbCommunity.find({ foreign: true }) + result = await DbFederatedCommunity.find({ foreign: true }) }) afterAll(async () => { @@ -523,7 +523,7 @@ describe('federation', () => { describe('with receiving data of exact max allowed properties length', () => { let jsonArray: any[] - let result: DbCommunity[] = [] + let result: DbFederatedCommunity[] = [] beforeAll(async () => { jest.clearAllMocks() jsonArray = [ @@ -538,7 +538,7 @@ describe('federation', () => { { api: 'toolong api', url: 'some valid url' }, ] await socketEventMocks.data(Buffer.from(JSON.stringify(jsonArray))) - result = await DbCommunity.find({ foreign: true }) + result = await DbFederatedCommunity.find({ foreign: true }) }) afterAll(async () => { @@ -570,7 +570,7 @@ describe('federation', () => { describe('with receiving data of exact max allowed buffer length', () => { let jsonArray: any[] - let result: DbCommunity[] = [] + let result: DbFederatedCommunity[] = [] beforeAll(async () => { jest.clearAllMocks() jsonArray = [ @@ -592,7 +592,7 @@ describe('federation', () => { }, ] await socketEventMocks.data(Buffer.from(JSON.stringify(jsonArray))) - result = await DbCommunity.find({ foreign: true }) + result = await DbFederatedCommunity.find({ foreign: true }) }) afterAll(async () => { @@ -711,7 +711,7 @@ describe('federation', () => { }) describe('with proper data', () => { - let result: DbCommunity[] = [] + let result: DbFederatedCommunity[] = [] beforeAll(async () => { jest.clearAllMocks() await socketEventMocks.data( @@ -728,7 +728,7 @@ describe('federation', () => { ]), ), ) - result = await DbCommunity.find({ foreign: true }) + result = await DbFederatedCommunity.find({ foreign: true }) }) afterAll(async () => { diff --git a/dht-node/src/dht_node/index.ts b/dht-node/src/dht_node/index.ts index d101037ae..0db7a28c2 100644 --- a/dht-node/src/dht_node/index.ts +++ b/dht-node/src/dht_node/index.ts @@ -3,7 +3,7 @@ import DHT from '@hyperswarm/dht' import { logger } from '@/server/logger' import CONFIG from '@/config' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' const KEY_SECRET_SEEDBYTES = 32 const getSeed = (): Buffer | null => @@ -31,7 +31,7 @@ export const startDHT = async (topic: string): Promise => { logger.info(`keyPairDHT: publicKey=${keyPair.publicKey.toString('hex')}`) logger.debug(`keyPairDHT: secretKey=${keyPair.secretKey.toString('hex')}`) - const ownApiVersions = await writeHomeCommunityEnries(keyPair.publicKey) + const ownApiVersions = await writeFederatedHomeCommunityEnries(keyPair.publicKey) logger.info(`ApiList: ${JSON.stringify(ownApiVersions)}`) const node = new DHT({ keyPair }) @@ -92,9 +92,9 @@ export const startDHT = async (topic: string): Promise => { } logger.debug(`upsert with variables=${JSON.stringify(variables)}`) // this will NOT update the updatedAt column, to distingue between a normal update and the last announcement - await DbCommunity.createQueryBuilder() + await DbFederatedCommunity.createQueryBuilder() .insert() - .into(DbCommunity) + .into(DbFederatedCommunity) .values(variables) .orUpdate({ conflict_target: ['id', 'publicKey', 'apiVersion'], @@ -179,7 +179,7 @@ export const startDHT = async (topic: string): Promise => { } } -async function writeHomeCommunityEnries(pubKey: any): Promise { +async function writeFederatedHomeCommunityEnries(pubKey: any): Promise { const homeApiVersions: CommunityApi[] = Object.values(ApiVersionType).map(function (apiEnum) { const comApi: CommunityApi = { api: apiEnum, @@ -189,17 +189,17 @@ async function writeHomeCommunityEnries(pubKey: any): Promise { }) try { // first remove privious existing homeCommunity entries - DbCommunity.createQueryBuilder().delete().where({ foreign: false }).execute() + DbFederatedCommunity.createQueryBuilder().delete().where({ foreign: false }).execute() homeApiVersions.forEach(async function (homeApi) { - const homeCom = new DbCommunity() + const homeCom = new DbFederatedCommunity() homeCom.foreign = false homeCom.apiVersion = homeApi.api homeCom.endPoint = homeApi.url homeCom.publicKey = pubKey.toString('hex') // this will NOT update the updatedAt column, to distingue between a normal update and the last announcement - await DbCommunity.insert(homeCom) + await DbFederatedCommunity.insert(homeCom) logger.info(`federation home-community inserted successfully: ${JSON.stringify(homeCom)}`) }) } catch (err) { diff --git a/federation/src/config/index.ts b/federation/src/config/index.ts index ce0c5a9a5..70a155d63 100644 --- a/federation/src/config/index.ts +++ b/federation/src/config/index.ts @@ -11,7 +11,7 @@ Decimal.set({ */ const constants = { - DB_VERSION: '0064-event_rename', + DB_VERSION: '0065-refactor_communities_table', // DECAY_START_TIME: new Date('2021-05-13 17:46:31-0000'), // GMT+0 LOG4JS_CONFIG: 'log4js-config.json', // default log level on production should be info diff --git a/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.test.ts b/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.test.ts index 20e6c8228..18d2a7599 100644 --- a/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.test.ts +++ b/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.test.ts @@ -2,7 +2,7 @@ /* eslint-disable @typescript-eslint/explicit-module-boundary-types */ import { createTestClient } from 'apollo-server-testing' import createServer from '@/server/createServer' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' let query: any @@ -13,7 +13,7 @@ beforeAll(async () => { const server = await createServer() con = server.con query = createTestClient(server.apollo).query - DbCommunity.clear() + DbFederatedCommunity.clear() }) afterAll(async () => { @@ -32,12 +32,12 @@ describe('PublicKeyResolver', () => { describe('getPublicKey', () => { beforeEach(async () => { - const homeCom = new DbCommunity() + const homeCom = new DbFederatedCommunity() homeCom.foreign = false homeCom.apiVersion = '1_0' homeCom.endPoint = 'endpoint-url' homeCom.publicKey = Buffer.from('homeCommunity-publicKey') - await DbCommunity.insert(homeCom) + await DbFederatedCommunity.insert(homeCom) }) it('returns homeCommunity-publicKey', async () => { diff --git a/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.ts b/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.ts index f96f20c58..0145324fc 100644 --- a/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.ts +++ b/federation/src/graphql/api/1_0/resolver/PublicKeyResolver.ts @@ -1,7 +1,7 @@ // eslint-disable-next-line @typescript-eslint/no-unused-vars import { Query, Resolver } from 'type-graphql' import { federationLogger as logger } from '@/server/logger' -import { Community as DbCommunity } from '@entity/Community' +import { FederatedCommunity as DbFederatedCommunity } from '@entity/FederatedCommunity' import { GetPublicKeyResult } from '../model/GetPublicKeyResult' @Resolver() @@ -10,7 +10,7 @@ export class PublicKeyResolver { @Query(() => GetPublicKeyResult) async getPublicKey(): Promise { logger.debug(`getPublicKey() via apiVersion=1_0 ...`) - const homeCom = await DbCommunity.findOneOrFail({ + const homeCom = await DbFederatedCommunity.findOneOrFail({ foreign: false, apiVersion: '1_0', })