diff --git a/packages/server/src/controllers/PostsController/services/flagNsfwByAttachments.js b/packages/server/src/controllers/PostsController/services/flagNsfwByAttachments.js index 22f71945..b845e2a7 100755 --- a/packages/server/src/controllers/PostsController/services/flagNsfwByAttachments.js +++ b/packages/server/src/controllers/PostsController/services/flagNsfwByAttachments.js @@ -5,44 +5,50 @@ import isNSFW from "../../../utils/is-nsfw" import modifyPostData from "./modifyPostData" export default async (post_id) => { - if (!post_id) { - throw new Error("Post ID is required") - } + try { + if (!post_id) { + throw new Error("Post ID is required") + } - let post = await Post.findById(post_id) + let post = await Post.findById(post_id) - if (!post) { - throw new Error("Post not found") - } + if (!post) { + throw new Error("Post not found") + } - let flags = [] + let flags = [] - // run indecentPrediction to all attachments - if (Array.isArray(post.attachments) && post.attachments.length > 0) { - for await (const attachment of post.attachments) { - const prediction = await indecentPrediction({ - url: attachment.url, - }).catch((err) => { - console.log("Error while checking", attachment, err) - return null - }) + // run indecentPrediction to all attachments + if (Array.isArray(post.attachments) && post.attachments.length > 0) { + for await (const attachment of post.attachments) { + const prediction = await indecentPrediction({ + url: attachment.url, + }).catch((err) => { + console.log("Error while checking", attachment, err) + return null + }) - if (prediction) { - const isNsfw = isNSFW(prediction) + if (prediction) { + const isNsfw = isNSFW(prediction) - if (isNsfw) { - flags.push("nsfw") + if (isNsfw) { + flags.push("nsfw") + } } } } - } - // if is there new flags update post - if (post.flags !== flags) { - await modifyPostData(post_id, { - flags: flags, - }) - } + // if is there new flags update post + if (post.flags !== flags) { + await modifyPostData(post_id, { + flags: flags, + }) + } - return flags + return flags + } catch (error) { + console.error(error) + + return [] + } } \ No newline at end of file