insert startValidateCommunities loop

This commit is contained in:
Claus-Peter Hübner 2023-01-03 03:13:57 +01:00
parent 72cc65d14a
commit 290987c40c
3 changed files with 32 additions and 0 deletions

View File

@ -126,6 +126,7 @@ const federation = {
: process.env.FEDERATION_COMMUNITY_URL.endsWith('/')
? process.env.FEDERATION_COMMUNITY_URL
: process.env.FEDERATION_COMMUNITY_URL + '/',
FEDERATION_VALIDATE_COMMUNITY_TIMER: process.env.FEDERATION_VALIDATE_COMMUNITY_TIMER || 60000,
}
const CONFIG = {

View File

@ -0,0 +1,29 @@
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'
export async function startValidateCommunities(timerInterval: number): Promise<void> {
while (true) {
const dbCommunities: DbCommunity[] = await DbCommunity.find({
where: [
{ verifiedAt: IsNull() },
{ verifiedAt: LessThan(Raw((lastAnnouncedAt) => `${lastAnnouncedAt}`)) },
],
})
if (dbCommunities) {
dbCommunities.forEach(async function (dbCom) {
const fdCom = new FdCommunity(dbCom)
const pubKey = await requestGetPublicKey(fdCom)
if (pubKey && pubKey === dbCom.publicKey.toString('hex')) {
DbCommunity.update({ verifiedAt: new Date() }, { id: dbCom.id })
}
})
}
await sleep(timerInterval)
}
}
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms))
}

View File

@ -5,6 +5,7 @@ import { startDHT } from '@/federation/index'
// config
import CONFIG from './config'
import { startValidateCommunities } from './federation/validateCommunities'
async function main() {
const { app } = await createServer()
@ -17,6 +18,7 @@ async function main() {
console.log(`GraphIQL available at http://localhost:${CONFIG.PORT}`)
}
})
startValidateCommunities(Number(CONFIG.FEDERATION_VALIDATE_COMMUNITY_TIMER))
// start DHT hyperswarm when DHT_TOPIC is set in .env
if (CONFIG.FEDERATION_DHT_TOPIC) {