feat: importer les réponses bokante en commentaires
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
'use strict';
|
||||
|
||||
const bokanteMastodon = require('./bokante-mastodon');
|
||||
|
||||
function stripHtml(html) {
|
||||
return (html || '').replace(/<[^>]*>/g, '').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({
|
||||
data: {
|
||||
contenu: 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,
|
||||
data: {commentaires: {connect: newCommentIds}}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {imported};
|
||||
}
|
||||
|
||||
module.exports = {importBokanteComments};
|
||||
Reference in New Issue
Block a user