In the following script, I tried a method to get the comment author, but it does not work.
What is the right way to get data of a comment?
const fibery = context.getService('fibery');
const threadsDb = 'Threads/Channel'; //
async function updateEntityDescriptionWithComments(entityId) {
// Fetch the current entity along with its comments
const entityWithComments = await fibery.getEntityById(threadsDb, entityId, ['Comments']);
let content = "";
// Check if there are any comments to process
if (entityWithComments.Comments && entityWithComments.Comments.length > 0) {
// Iterate through each comment to concatenate the author's name and comment text
for (const comment of entityWithComments.Comments) {
// Fetch the author's name and comment content
const authorName = comment['Author.Name'] || "Unknown Author";
const commentContent = await fibery.getDocumentContent(comment['Document Secret'], 'md');
// Combine the author name and comment content
content += `**${authorName}**: ${commentContent}\n\n`;
}
} else {
console.log(`No comments found for entity ${entityId}.`);
return;
}
// Fetch the current entity's Description to get its secret for updating
const entityWithDescription = await fibery.getEntityById(threadsDb, entityId, ['Description']);
if (entityWithDescription.Description && entityWithDescription.Description.Secret) {
// Update the Description field with the aggregated comments content
await fibery.setDocumentContent(entityWithDescription.Description.Secret, content, 'md');
console.log(`Description for entity ${entityId} updated successfully with comments.`);
} else {
console.log(`Description field not found or not accessible for entity ${entityId}.`);
}
}
async function processEntityComments() {
try {
if (!args.currentEntities || args.currentEntities.length === 0) {
throw new Error('No current entity found.');
}
// Process each current entity
for (const currentEntity of args.currentEntities) {
await updateEntityDescriptionWithComments(currentEntity.id);
}
} catch (error) {
console.error(`Error processing comments for the entity: ${error.message}`, error);
}
}
await processEntityComments();