From e5cf25d1796dc6d31b912d84981d9b31a6a4bfd4 Mon Sep 17 00:00:00 2001 From: nick-funk Date: Tue, 28 Nov 2023 16:08:15 -0700 Subject: [PATCH] set new fields to improve akismet spam payloads --- .../services/comments/pipeline/phases/spam.ts | 13 +++- server/src/core/server/services/spam/index.ts | 64 +++++++++++++++---- 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/server/src/core/server/services/comments/pipeline/phases/spam.ts b/server/src/core/server/services/comments/pipeline/phases/spam.ts index e8d7a38014..f7a71c4052 100644 --- a/server/src/core/server/services/comments/pipeline/phases/spam.ts +++ b/server/src/core/server/services/comments/pipeline/phases/spam.ts @@ -6,6 +6,7 @@ import { IntermediateModerationPhase, IntermediatePhaseResult, } from "coral-server/services/comments/pipeline"; +import { AkismetSpamCheckPayload } from "coral-server/services/spam"; import { GQLCOMMENT_FLAG_REASON, @@ -20,6 +21,7 @@ export const spam: IntermediateModerationPhase = async ({ req, nudge, log, + now, }): Promise => { const integration = tenant.integrations.akismet; @@ -93,8 +95,9 @@ export const spam: IntermediateModerationPhase = async ({ try { log.trace("checking comment for spam"); - // Check the comment for spam. - const isSpam = await client.checkSpam({ + const payload: AkismetSpamCheckPayload = { + api_key: integration.key, + blog: integration.site, user_ip: userIP, // REQUIRED referrer, // REQUIRED user_agent: userAgent, // REQUIRED @@ -103,7 +106,11 @@ export const spam: IntermediateModerationPhase = async ({ comment_author: author.username || "", comment_type: "comment", is_test: false, - }); + comment_date_gmt: now.toISOString(), + }; + + // Check the comment for spam. + const isSpam = await client.checkSpam(payload); // NICK: make not any if (isSpam) { log.trace({ isSpam }, "comment contained spam"); diff --git a/server/src/core/server/services/spam/index.ts b/server/src/core/server/services/spam/index.ts index 557b18d6cd..9e4b710e3b 100644 --- a/server/src/core/server/services/spam/index.ts +++ b/server/src/core/server/services/spam/index.ts @@ -11,35 +11,74 @@ import { Request } from "coral-server/types/express"; export interface Parameters { apiKey: string; + blog: string; comment: Readonly; story: Readonly; author: Readonly; userIP: string | undefined; userAgent: string | undefined; referrer: string | undefined; + recheckReason?: string | undefined; } -const createBody = (params: Parameters) => { - const { comment, story, author, userIP, userAgent, referrer } = params; - const latestRevision = getLatestRevision(comment); - const username = author ? (author.username ? author.username : "") : ""; +export interface AkismetSpamCheckPayload { + api_key: string; + blog: string; + + user_ip: string; + user_agent: string; + referrer: string; + + permalink: string; + comment_type: string; + comment_author: string; + comment_content: string; + is_test: boolean; + comment_date_gmt: string; - return { - user_ip: userIP, - user_agent: userAgent, + recheck_reason?: string; +} + +const createBody = (params: Parameters): AkismetSpamCheckPayload => { + const { + comment, + story, + author, + userIP, + userAgent, referrer, + recheckReason, + apiKey, + blog, + } = params; + const latestRevision = getLatestRevision(comment); + + const body: AkismetSpamCheckPayload = { + api_key: apiKey, + blog, + + user_ip: userIP || "", + user_agent: userAgent || "", + referrer: referrer || "", permalink: story.url, comment_type: "comment", - comment_author: username, + comment_author: author?.username || "", comment_content: latestRevision ? latestRevision.body : "", - comment_date_gmt: latestRevision - ? latestRevision.createdAt.toISOString() - : "", is_test: false, + + comment_date_gmt: latestRevision + ? latestRevision.createdAt.toISOString() + : new Date().toISOString(), }; + + if (recheckReason) { + body.recheck_reason = recheckReason; + } + + return body; }; const submitSpam = async (params: Parameters) => { @@ -118,6 +157,7 @@ export const submitCommentAsNotSpam = async ( apiKey: tenant.integrations.akismet.key ? tenant.integrations.akismet.key : "", + blog: tenant.integrations.akismet.site || "", comment, story, author, @@ -161,11 +201,13 @@ export const submitCommentAsSpam = async ( apiKey: tenant.integrations.akismet.key ? tenant.integrations.akismet.key : "", + blog: tenant.integrations.akismet.site || "", comment, story, author, userIP, userAgent, referrer, + recheckReason: comment.revisions.length > 1 ? "edit" : undefined, }); };