From 7ab8922e33aff2676d3f234bc429335626722ca1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Claus-Peter=20H=C3=BCbner?= Date: Thu, 1 Dec 2022 00:26:46 +0100 Subject: [PATCH] rework PR comments --- backend/src/federation/index.ts | 48 +++++++++++-------- .../0055-add_communities_table/Community.ts | 23 +++++++-- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/backend/src/federation/index.ts b/backend/src/federation/index.ts index 39d298448..6dfb23be3 100644 --- a/backend/src/federation/index.ts +++ b/backend/src/federation/index.ts @@ -6,6 +6,7 @@ import DHT from '@hyperswarm/dht' import { backendLogger as logger } from '@/server/logger' import CONFIG from '@/config' import { Community as DbCommunity } from '@entity/Community' +import { getConnection } from '@dbTools/typeorm' const KEY_SECRET_SEEDBYTES = 32 const getSeed = (): Buffer | null => @@ -55,38 +56,45 @@ export const startDHT = async ( const json = JSON.parse(data.toString('ascii')) if ( json.apiVersions && - Object.prototype.toString.call(json.apiVersions) === '[object Array]' && + Array.isArray(json.apiVersions) && json.apiVersions.length > 0 && - Object.prototype.toString.call(json.apiVersions[0].api) === '[object String]' && - Object.prototype.toString.call(json.apiVersions[0].url) === '[object String]' + typeof json.apiVersions[0].api === 'string' && + typeof json.apiVersions[0].url === 'string' ) { const communities = new Array() for (let i = 0; i < json.apiVersions.length; i++) { const apiVersion = json.apiVersions[i] - let community = await DbCommunity.findOne({ - publicKey: socket.remotePublicKey.toString('hex'), - apiVersion: apiVersion.api, - }) - if (!community) { - community = DbCommunity.create() - logger.debug(`new federation community...`) - } - community.apiVersion = apiVersion.api - community.endPoint = apiVersion.url - community.publicKey = socket.remotePublicKey.toString('hex') - community.lastAnnouncedAt = new Date() - communities.push(community) - } - await DbCommunity.save(communities) - logger.debug(`federation communities stored: ${JSON.stringify(communities)}`) + const variables = { + apiVersion: apiVersion.api, + endPoint: apiVersion.url, + publicKey: socket.remotePublicKey.toString('hex'), + lastAnnouncedAt: new Date(), + } + logger.debug(`upsert with variables=${JSON.stringify(variables)}`) + await DbCommunity.createQueryBuilder() + .insert() + .into(DbCommunity) + .values(variables) + .orUpdate({ + conflict_target: ['id', 'publicKey', 'apiVersion'], + overwrite: ['end_point', 'last_announced_at'], + }) + .execute() + } + logger.info(`federation community apiVersions stored...`) + const entity = await DbCommunity.findOne({ id: 147 }) + if (entity) { + entity.endPoint = 'test' + DbCommunity.save(entity) + logger.debug(`updated entity...`) + } } } catch (e) { logger.error(`Error on receiving data from socket: ${JSON.stringify(e)}`) } }) - // process.stdin.pipe(noiseSocket).pipe(process.stdout); }) await server.listen() diff --git a/database/entity/0055-add_communities_table/Community.ts b/database/entity/0055-add_communities_table/Community.ts index 26f1d56d1..f2d071ce4 100644 --- a/database/entity/0055-add_communities_table/Community.ts +++ b/database/entity/0055-add_communities_table/Community.ts @@ -1,4 +1,11 @@ -import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm' +import { + BaseEntity, + Entity, + PrimaryGeneratedColumn, + Column, + CreateDateColumn, + UpdateDateColumn, +} from 'typeorm' @Entity('communities') export class Community extends BaseEntity { @@ -17,9 +24,19 @@ export class Community extends BaseEntity { @Column({ name: 'last_announced_at', type: 'datetime', nullable: false }) lastAnnouncedAt: Date - @Column({ name: 'created_at', default: () => 'CURRENT_TIMESTAMP', nullable: false }) + @CreateDateColumn({ + name: 'created_at', + type: 'datetime', + default: () => 'CURRENT_TIMESTAMP(3)', + nullable: false, + }) createdAt: Date - @Column({ name: 'updated_at', type: 'datetime', nullable: true, default: null }) + @UpdateDateColumn({ + name: 'updated_at', + type: 'datetime', + onUpdate: 'CURRENT_TIMESTAMP(3)', + nullable: true, + }) updatedAt: Date | null }