diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index 698b17e67..1559e7571 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -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 = { diff --git a/backend/src/federation/validateCommunities.ts b/backend/src/federation/validateCommunities.ts new file mode 100644 index 000000000..8a9429973 --- /dev/null +++ b/backend/src/federation/validateCommunities.ts @@ -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 { + 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)) +} diff --git a/backend/src/index.ts b/backend/src/index.ts index 329e63f87..a549172be 100644 --- a/backend/src/index.ts +++ b/backend/src/index.ts @@ -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) {