From fe4198d8844a5db64d9cb35615b2deb72daa92ed Mon Sep 17 00:00:00 2001 From: srgooglo Date: Sun, 16 Oct 2022 20:47:58 +0200 Subject: [PATCH] implement `flagNsfwByAttachments` method --- .../methods/flagNsfwByAttachments.js | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 packages/server/src/controllers/PostsController/methods/flagNsfwByAttachments.js diff --git a/packages/server/src/controllers/PostsController/methods/flagNsfwByAttachments.js b/packages/server/src/controllers/PostsController/methods/flagNsfwByAttachments.js new file mode 100644 index 00000000..22f71945 --- /dev/null +++ b/packages/server/src/controllers/PostsController/methods/flagNsfwByAttachments.js @@ -0,0 +1,48 @@ +import { Post } from "../../../models" +import indecentPrediction from "../../../utils/indecent-prediction" +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") + } + + let post = await Post.findById(post_id) + + if (!post) { + throw new Error("Post not found") + } + + 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 + }) + + if (prediction) { + const isNsfw = isNSFW(prediction) + + if (isNsfw) { + flags.push("nsfw") + } + } + } + } + + // if is there new flags update post + if (post.flags !== flags) { + await modifyPostData(post_id, { + flags: flags, + }) + } + + return flags +} \ No newline at end of file