change raw-query back to querybuilder-statement

This commit is contained in:
Claus-Peter Hübner 2023-01-05 03:50:09 +01:00
parent 8cd70ef05d
commit 9d7224f128
4 changed files with 37 additions and 23 deletions

View File

@ -4,7 +4,7 @@ import { FdCommunity } from '@/federation/graphql/1_0/model/FdCommunity'
export async function requestGetPublicKey(fdCom: FdCommunity): Promise<string | undefined> {
let endpoint = fdCom.url.endsWith('/') ? fdCom.url : fdCom.url + '/'
endpoint = `${endpoint}graphql/${fdCom.apiVersion}/getPublicKey`
endpoint = `${endpoint}${fdCom.apiVersion}/getPublicKey`
logger.info(`requestGetPublicKey with endpoint='${endpoint}'...`)
const graphQLClient = new GraphQLClient(endpoint, {

View File

@ -0,0 +1,3 @@
export enum ApiVersionType {
V1_0 = '1_0',
}

View File

@ -1,18 +1,18 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { ObjectType, Field } from 'type-graphql'
import { Community as DbCommunity } from '@entity/Community'
@ObjectType()
export class FdCommunity {
// using NOT the entity DbCommunity, because of necessary RAW-Sql to find the correct announced communities
constructor(dbCommunity: any) {
this.apiVersion = dbCommunity.api_version
this.createdAt = dbCommunity.created_at
constructor(dbCommunity: DbCommunity) {
this.apiVersion = dbCommunity.apiVersion
this.createdAt = dbCommunity.createdAt
this.id = dbCommunity.id
this.lastAnnouncedAt = dbCommunity.last_announced_at
this.publicKey = dbCommunity.public_key.toString('hex')
this.updatedAt = dbCommunity.updated_at
this.url = dbCommunity.end_point
this.lastAnnouncedAt = dbCommunity.lastAnnouncedAt
this.publicKey = dbCommunity.publicKey.toString('hex')
this.updatedAt = dbCommunity.updatedAt
this.url = dbCommunity.endPoint
}
@Field(() => Number, { nullable: true })

View File

@ -1,35 +1,46 @@
import { Community as DbCommunity } from '@entity/Community'
// import { IsNull, LessThan, Raw } from '@dbTools/typeorm'
import { IsNull, LessThan, Raw } from '@dbTools/typeorm'
import { requestGetPublicKey } from './client/1_0/FederationClient'
import { FdCommunity } from './graphql/1_0/model/FdCommunity'
import { backendLogger as logger } from '@/server/logger'
import { ApiVersionType } from './enum/apiVersionType'
export async function startValidateCommunities(timerInterval: number): Promise<void> {
logger.info(
`Federation: startValidateCommunities loop with an interval of ${timerInterval} ms...`,
)
while (true) {
const dbCommunities: DbCommunity[] = await DbCommunity.createQueryBuilder()
.where({ verifiedAt: IsNull() })
.orWhere('verified_at < last_announced_at')
.getMany()
/*
const dbCommunities: DbCommunity[] = await DbCommunity.find({
where: [{ verifiedAt: IsNull() }, { verifiedAt: LessThan('Community.last_announced_at') }],
})
*/
const dbCommunities: DbCommunity[] = await DbCommunity.getRepository().manager.query(
'SELECT * FROM `communities` `Community` WHERE (`Community`.`verified_at` IS NULL OR `Community`.`verified_at` < `Community`.`last_announced_at`)',
)
*/
logger.debug(`Federation: found ${dbCommunities.length} dbCommunities`)
if (dbCommunities) {
dbCommunities.forEach(async function (dbCom) {
logger.debug(`Federation: validate publicKey for dbCom: ${JSON.stringify(dbCom)}`)
logger.debug(`Federation: dbCom: ${JSON.stringify(dbCom)}`)
const fdCom = new FdCommunity(dbCom)
const pubKey = await requestGetPublicKey(fdCom)
logger.debug(`Federation: received publicKey: ${pubKey}`)
if (pubKey && pubKey === dbCom.publicKey.toString('hex')) {
// if (!pubKey) {
logger.debug(`Federation: matching publicKey: ${pubKey}`)
DbCommunity.update({ id: dbCom.id }, { verifiedAt: new Date() })
logger.debug(`Federation: updated dbCom: ${JSON.stringify(dbCom)}`)
console.log(`ApiVersionType=`, ApiVersionType)
const apiValueStrings: string[] = Object.values(ApiVersionType)
if (apiValueStrings.includes(fdCom.apiVersion)) {
logger.debug(
`Federation: validate publicKey for dbCom: ${dbCom.id} with apiVersion=${dbCom.apiVersion}`,
)
const pubKey = await requestGetPublicKey(fdCom)
logger.debug(`Federation: received publicKey: ${pubKey}`)
if (pubKey && pubKey === fdCom.publicKey) {
logger.debug(`Federation: matching publicKey: ${pubKey}`)
DbCommunity.update({ id: fdCom.id }, { verifiedAt: new Date() })
logger.debug(`Federation: updated dbCom: ${JSON.stringify(dbCom)}`)
}
} else {
logger.debug(
`Federation: dbCom: ${fdCom.id} with unsupported apiVersion=${fdCom.apiVersion}; supported versions=${apiValueStrings}`,
)
}
})
}