From 51487f1001738b42480fd2f7ff016005b3dedf18 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Thu, 11 Dec 2025 01:31:14 +0100 Subject: [PATCH] =?UTF-8?q?Verrouillage=20des=20communaut=C3=A9s=20lemmy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/sync_lemmy_comments.js | 84 +++++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 5 deletions(-) diff --git a/tools/sync_lemmy_comments.js b/tools/sync_lemmy_comments.js index f9a173d9..bd103582 100644 --- a/tools/sync_lemmy_comments.js +++ b/tools/sync_lemmy_comments.js @@ -39,6 +39,8 @@ async function main() { const lemmyConfig = normalizeLemmyConfig(toolsConfig.lemmy); const client = await createLemmyClient(lemmyConfig); const bundles = await collectBundles(CONTENT_ROOT); + console.log("Vérification des communautés Lemmy pour les fils existants…"); + await ensureRestrictedCommunitiesForExistingThreads(bundles, lemmyConfig, client); const articles = selectArticles(bundles); if (articles.length === 0) { @@ -181,6 +183,45 @@ function selectArticles(bundles) { return articles; } +/** + * Garantit que les communautés associées aux articles déjà reliés à Lemmy + * sont configurées avec une publication restreinte aux modérateurs. + * @param {Array} bundles Bundles collectés sous content/. + * @param {object} lemmyConfig Configuration Lemmy. + * @param {LemmyHttp} client Client Lemmy. + * @returns {Promise} Promesse résolue une fois toutes les communautés vérifiées. + */ +async function ensureRestrictedCommunitiesForExistingThreads(bundles, lemmyConfig, client) { + const visited = new Set(); + + for (const bundle of bundles) { + const frontmatter = readFrontmatterFile(bundle.indexPath); + if (!frontmatter) { + continue; + } + + const existingComments = + typeof frontmatter.data?.[FRONTMATTER_COMMENT_FIELD] === "string" + ? frontmatter.data[FRONTMATTER_COMMENT_FIELD].trim() + : ""; + if (!existingComments) { + continue; + } + + const descriptor = buildCommunityDescriptor(bundle.parts, lemmyConfig.community); + if (visited.has(descriptor.name)) { + continue; + } + + const ensuredCommunity = await ensureCommunity(client, descriptor, lemmyConfig.community); + if (!ensuredCommunity.created && !ensuredCommunity.updated) { + const label = ensuredCommunity.title || descriptor.title || descriptor.name; + console.log(`🔒 Communauté Lemmy ${label} déjà verrouillée.`); + } + visited.add(descriptor.name); + } +} + /** * Synchronise un article unique : communauté, miniature, post Lemmy et mise à jour du frontmatter. * @param {object} article Article préparé par selectArticles. @@ -440,27 +481,60 @@ async function buildThumbnailAsset(coverPath, bundle) { } /** - * Recherche une communauté par nom, et la crée si nécessaire. + * Recherche une communauté par nom, la crée si nécessaire et force la restriction de publication aux modérateurs. * @param {LemmyHttp} client Client Lemmy. * @param {object} descriptor Nom, titre et description attendus. * @param {object} communityConfig Paramètres nsfw/visibilité. - * @returns {Promise<{ id: number, name: string }>} Communauté finalisée. + * @returns {Promise<{ id: number, name: string, title: string, restrictedToMods: boolean, created: boolean, updated: boolean }>} Communauté finalisée. */ async function ensureCommunity(client, descriptor, communityConfig) { const existing = await searchCommunity(client, descriptor.name); if (existing) { - return { id: existing.community.id, name: existing.community.name }; + const existingCommunity = existing.community; + if (existingCommunity.posting_restricted_to_mods !== true) { + console.log( + `🔓→🔒 Communauté Lemmy ${existingCommunity.title} verrouillée (déjà existante).` + ); + const edited = await client.editCommunity({ + community_id: existingCommunity.id, + posting_restricted_to_mods: true, + }); + const editedCommunity = edited.community_view.community; + return { + id: editedCommunity.id, + name: editedCommunity.name, + title: editedCommunity.title, + restrictedToMods: editedCommunity.posting_restricted_to_mods === true, + created: false, + updated: true, + }; + } + return { + id: existingCommunity.id, + name: existingCommunity.name, + title: existingCommunity.title, + restrictedToMods: existingCommunity.posting_restricted_to_mods === true, + created: false, + updated: false, + }; } + console.log(`🆕🔒 Communauté Lemmy ${descriptor.title} créée et verrouillée.`); const response = await client.createCommunity({ name: descriptor.name, title: descriptor.title, description: descriptor.description, nsfw: communityConfig.nsfw, + posting_restricted_to_mods: true, visibility: communityConfig.visibility, }); + const createdCommunity = response.community_view.community; return { - id: response.community_view.community.id, - name: response.community_view.community.name, + id: createdCommunity.id, + name: createdCommunity.name, + title: createdCommunity.title, + restrictedToMods: createdCommunity.posting_restricted_to_mods === true, + created: true, + updated: false, }; }