rework PR comments

This commit is contained in:
Claus-Peter Hübner 2022-12-01 00:26:46 +01:00
parent 6c043a0eb0
commit 7ab8922e33
2 changed files with 48 additions and 23 deletions

View File

@ -6,6 +6,7 @@ import DHT from '@hyperswarm/dht'
import { backendLogger as logger } from '@/server/logger' import { backendLogger as logger } from '@/server/logger'
import CONFIG from '@/config' import CONFIG from '@/config'
import { Community as DbCommunity } from '@entity/Community' import { Community as DbCommunity } from '@entity/Community'
import { getConnection } from '@dbTools/typeorm'
const KEY_SECRET_SEEDBYTES = 32 const KEY_SECRET_SEEDBYTES = 32
const getSeed = (): Buffer | null => const getSeed = (): Buffer | null =>
@ -55,38 +56,45 @@ export const startDHT = async (
const json = JSON.parse(data.toString('ascii')) const json = JSON.parse(data.toString('ascii'))
if ( if (
json.apiVersions && json.apiVersions &&
Object.prototype.toString.call(json.apiVersions) === '[object Array]' && Array.isArray(json.apiVersions) &&
json.apiVersions.length > 0 && json.apiVersions.length > 0 &&
Object.prototype.toString.call(json.apiVersions[0].api) === '[object String]' && typeof json.apiVersions[0].api === 'string' &&
Object.prototype.toString.call(json.apiVersions[0].url) === '[object String]' typeof json.apiVersions[0].url === 'string'
) { ) {
const communities = new Array<DbCommunity>() const communities = new Array<DbCommunity>()
for (let i = 0; i < json.apiVersions.length; i++) { for (let i = 0; i < json.apiVersions.length; i++) {
const apiVersion = json.apiVersions[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) const variables = {
logger.debug(`federation communities stored: ${JSON.stringify(communities)}`) 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) { } catch (e) {
logger.error(`Error on receiving data from socket: ${JSON.stringify(e)}`) logger.error(`Error on receiving data from socket: ${JSON.stringify(e)}`)
} }
}) })
// process.stdin.pipe(noiseSocket).pipe(process.stdout);
}) })
await server.listen() await server.listen()

View File

@ -1,4 +1,11 @@
import { BaseEntity, Entity, PrimaryGeneratedColumn, Column } from 'typeorm' import {
BaseEntity,
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
} from 'typeorm'
@Entity('communities') @Entity('communities')
export class Community extends BaseEntity { export class Community extends BaseEntity {
@ -17,9 +24,19 @@ export class Community extends BaseEntity {
@Column({ name: 'last_announced_at', type: 'datetime', nullable: false }) @Column({ name: 'last_announced_at', type: 'datetime', nullable: false })
lastAnnouncedAt: Date 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 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 updatedAt: Date | null
} }