'use strict'; const bokanteMastodon = require('./bokante-mastodon'); function stripHtml(html) { return (html || '').replace(/<[^>]*>/g, '').trim(); } // Mastodon préfixe toujours une réponse par la mention du compte cité, // déjà visible via l'avatar/le nom : redondant à l'affichage. function stripMentionsInisyal(texte) { return texte.replace(/^(@\S+\s*)+/, '').trim(); } async function importBokanteComments({strapi}) { const paroles = await strapi.db.query('api::parole.parole').findMany({ where: {bokanteStatusId: {$notNull: true}} }); let imported = 0; for (const parole of paroles) { let context; try { context = await bokanteMastodon.getStatusContext(parole.bokanteStatusId); } catch (err) { strapi.log.error(`Import bokante (${parole.slug}) : ${err.message}`); continue; } const newCommentIds = []; for (const status of context.descendants || []) { if (status.sensitive) { continue; } const exists = await strapi.db.query('api::commentaire.commentaire').findOne({ where: {remoteId: status.id} }); if (exists) { continue; } const commentaire = await strapi.documents('api::commentaire.commentaire').create({ status: 'published', data: { contenu: stripMentionsInisyal(stripHtml(status.content)), datePublication: status.created_at, origine: 'activitypub', auteurNom: status.account?.display_name, auteurHandle: status.account?.acct ? `@${status.account.acct}` : undefined, auteurAvatarUrl: status.account?.avatar, auteurProfilUrl: status.account?.url, remoteId: status.id, remoteUrl: status.url, parole: parole.id } }); newCommentIds.push(commentaire.id); imported += 1; } if (newCommentIds.length > 0) { await strapi.documents('api::parole.parole').update({ documentId: parole.documentId, status: 'published', data: {commentaires: {connect: newCommentIds}} }); } } return {imported}; } module.exports = {importBokanteComments};