correct query for announced communities

This commit is contained in:
Claus-Peter Hübner 2023-01-04 23:06:52 +01:00
parent ed43c19ff4
commit 998f673dd0
3 changed files with 29 additions and 11 deletions

View File

@ -44,7 +44,7 @@ export async function requestGetPublicKey(fdCom: FdCommunity): Promise<string |
}
logger.warn(`requestGetPublicKey processed without response data`)
} catch (err) {
logger.error(`Request-Error: ${JSON.stringify(err)}`)
logger.error(`Request-Error:`, err) // ${JSON.stringify(err)}`)
}
return undefined
}

View File

@ -1,18 +1,18 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Community as DbCommunity } from '@entity/Community'
import { ObjectType, Field } from 'type-graphql'
@ObjectType()
export class FdCommunity {
constructor(dbCommunity: DbCommunity) {
this.apiVersion = dbCommunity.apiVersion
this.createdAt = dbCommunity.createdAt
// 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
this.id = dbCommunity.id
this.lastAnnouncedAt = dbCommunity.lastAnnouncedAt
this.publicKey = dbCommunity.publicKey.toString('hex')
this.updatedAt = dbCommunity.updatedAt ? dbCommunity.updatedAt : null
this.url = dbCommunity.endPoint
this.lastAnnouncedAt = dbCommunity.last_announced_at
this.publicKey = dbCommunity.public_key.toString('hex')
this.updatedAt = dbCommunity.updated_at
this.url = dbCommunity.end_point
}
@Field(() => Number, { nullable: true })

View File

@ -2,22 +2,40 @@ import { Community as DbCommunity } from '@entity/Community'
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'
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.find({
where: [{ verifiedAt: IsNull() }, { verifiedAt: LessThan(`last_announced_at:`) }],
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)}`)
const fdCom = new FdCommunity(dbCom)
const pubKey = await requestGetPublicKey(fdCom)
logger.debug(`Federation: received publicKey: ${pubKey}`)
if (pubKey && pubKey === dbCom.publicKey.toString('hex')) {
DbCommunity.update({ verifiedAt: new Date() }, { id: dbCom.id })
// if (!pubKey) {
logger.debug(`Federation: matching publicKey: ${pubKey}`)
DbCommunity.update({ id: dbCom.id }, { verifiedAt: new Date() })
logger.debug(`Federation: updated dbCom: ${JSON.stringify(dbCom)}`)
}
})
}
logger.debug(`Federation: loop starts sleeping...`)
await sleep(timerInterval)
logger.debug(`Federation: loop ends sleeping`)
}
}