From 8983bc52ebf72f9a2cbf7363e770c5e68eced729 Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Fri, 24 Oct 2025 08:28:53 +0200 Subject: [PATCH] add config option, if dlt-connector is enabled, write communities list for dlt gradido node server out in its home folder as communities.json on each validate communities run --- backend/src/config/index.ts | 1 + backend/src/config/schema.ts | 4 ++ .../client/1_0/model/PublicCommunityInfo.ts | 1 + backend/src/federation/validateCommunities.ts | 52 +++++++++++++++++++ deployment/bare_metal/.env.dist | 1 + .../1_0/model/GetPublicCommunityInfoResult.ts | 4 ++ 6 files changed, 63 insertions(+) diff --git a/backend/src/config/index.ts b/backend/src/config/index.ts index aa0a7dbf2..b583156b5 100644 --- a/backend/src/config/index.ts +++ b/backend/src/config/index.ts @@ -43,6 +43,7 @@ const DLT_CONNECTOR_PORT = process.env.DLT_CONNECTOR_PORT ?? 6010 const dltConnector = { DLT_CONNECTOR: process.env.DLT_CONNECTOR === 'true' || false, DLT_CONNECTOR_URL: process.env.DLT_CONNECTOR_URL ?? `${COMMUNITY_URL}:${DLT_CONNECTOR_PORT}`, + DLT_GRADIDO_NODE_SERVER_HOME_FOLDER: process.env.DLT_GRADIDO_NODE_SERVER_HOME_FOLDER ?? '~/.gradido', } const community = { diff --git a/backend/src/config/schema.ts b/backend/src/config/schema.ts index 144608992..f4e6033ea 100644 --- a/backend/src/config/schema.ts +++ b/backend/src/config/schema.ts @@ -79,6 +79,10 @@ export const schema = Joi.object({ .when('DLT_CONNECTOR', { is: true, then: Joi.required() }) .description('The URL for GDT API endpoint'), + DLT_GRADIDO_NODE_SERVER_HOME_FOLDER: Joi.string() + .default('~/.gradido') + .description('The home folder for the gradido dlt node server'), + EMAIL: Joi.boolean() .default(false) .description('Enable or disable email functionality') diff --git a/backend/src/federation/client/1_0/model/PublicCommunityInfo.ts b/backend/src/federation/client/1_0/model/PublicCommunityInfo.ts index 1abbeb9e7..7c228c799 100644 --- a/backend/src/federation/client/1_0/model/PublicCommunityInfo.ts +++ b/backend/src/federation/client/1_0/model/PublicCommunityInfo.ts @@ -4,4 +4,5 @@ export interface PublicCommunityInfo { creationDate: Date publicKey: string publicJwtKey: string + hieroTopicId: string | null } diff --git a/backend/src/federation/validateCommunities.ts b/backend/src/federation/validateCommunities.ts index 10088cf35..3b910b313 100644 --- a/backend/src/federation/validateCommunities.ts +++ b/backend/src/federation/validateCommunities.ts @@ -2,6 +2,7 @@ import { Community as DbCommunity, FederatedCommunity as DbFederatedCommunity, getHomeCommunity, + getReachableCommunities, } from 'database' import { IsNull } from 'typeorm' @@ -15,6 +16,9 @@ import { getLogger } from 'log4js' import { startCommunityAuthentication } from './authenticateCommunities' import { PublicCommunityInfoLoggingView } from './client/1_0/logging/PublicCommunityInfoLogging.view' import { ApiVersionType } from 'core' +import { CONFIG } from '@/config' +import * as path from 'node:path' +import * as fs from 'node:fs' const logger = getLogger(`${LOG4JS_BASE_CATEGORY_NAME}.federation.validateCommunities`) @@ -83,6 +87,8 @@ export async function validateCommunities(): Promise { logger.error(`Error:`, err) } } + // export communities for gradido dlt node server + await exportCommunitiesToDltNodeServer() } export async function writeJwtKeyPairInHomeCommunity(): Promise { @@ -138,6 +144,52 @@ async function writeForeignCommunity( com.publicKey = dbCom.publicKey com.publicJwtKey = pubInfo.publicJwtKey com.url = dbCom.endPoint + com.hieroTopicId = pubInfo.hieroTopicId await DbCommunity.save(com) } } + +// prototype, later add api call to gradido dlt node server for adding/updating communities +type CommunityForDltNodeServer = { + communityId: string + hieroTopicId: string + alias: string + folder: string +} +async function exportCommunitiesToDltNodeServer(): Promise { + if (!CONFIG.DLT_CONNECTOR) { + return Promise.resolve() + } + const folder = CONFIG.DLT_GRADIDO_NODE_SERVER_HOME_FOLDER + try { + fs.accessSync(folder, fs.constants.R_OK | fs.constants.W_OK) + } catch (err) { + logger.error(`Error: home folder for DLT Gradido Node Server ${folder} does not exist`) + return + } + + const dbComs = await getReachableCommunities(CONFIG.FEDERATION_VALIDATE_COMMUNITY_TIMER * 4) + const communitiesForDltNodeServer: CommunityForDltNodeServer[] = [] + // make sure communityName is unique + const communityName = new Set() + dbComs.forEach((com) => { + if (!com.communityUuid || !com.hieroTopicId) { + return + } + let alias = com.name + if (!alias || communityName.has(alias)) { + alias = com.communityUuid + } + communityName.add(alias) + communitiesForDltNodeServer.push({ + communityId: com.communityUuid, + hieroTopicId: com.hieroTopicId, + alias, + // use only alpha-numeric chars for folder name + folder: alias.replace(/[^a-zA-Z0-9]/g, '_') + }) + }) + const dltNodeServerCommunitiesFile = path.join(folder, 'communities.json') + fs.writeFileSync(dltNodeServerCommunitiesFile, JSON.stringify(communitiesForDltNodeServer, null, 2)) + logger.debug(`Written communitiesForDltNodeServer to ${dltNodeServerCommunitiesFile}`) +} diff --git a/deployment/bare_metal/.env.dist b/deployment/bare_metal/.env.dist index 11f5e1d76..729fc84aa 100644 --- a/deployment/bare_metal/.env.dist +++ b/deployment/bare_metal/.env.dist @@ -88,6 +88,7 @@ GDT_ACTIVE=false # DLT-Connector (still in develop) DLT_CONNECTOR=false DLT_CONNECTOR_PORT=6010 +DLT_GRADIDO_NODE_SERVER_HOME_FOLDER=/home/gradido/.gradido # used for combining a newsletter on klicktipp with this gradido community # if used, user will be subscribed on register and can unsubscribe in his account diff --git a/federation/src/graphql/api/1_0/model/GetPublicCommunityInfoResult.ts b/federation/src/graphql/api/1_0/model/GetPublicCommunityInfoResult.ts index 55292cee2..1c7c5587c 100644 --- a/federation/src/graphql/api/1_0/model/GetPublicCommunityInfoResult.ts +++ b/federation/src/graphql/api/1_0/model/GetPublicCommunityInfoResult.ts @@ -12,6 +12,7 @@ export class GetPublicCommunityInfoResult { this.name = dbCom.name this.description = dbCom.description this.creationDate = dbCom.creationDate + this.hieroTopicId = dbCom.hieroTopicId } @Field(() => String) @@ -28,4 +29,7 @@ export class GetPublicCommunityInfoResult { @Field(() => String) publicJwtKey: string + + @Field(() => String) + hieroTopicId: string | null }