Skip to content

Commit

Permalink
set new fields to improve akismet spam payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-funk committed Nov 28, 2023
1 parent 8f8fba1 commit e5cf25d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
13 changes: 10 additions & 3 deletions server/src/core/server/services/comments/pipeline/phases/spam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
IntermediateModerationPhase,
IntermediatePhaseResult,
} from "coral-server/services/comments/pipeline";
import { AkismetSpamCheckPayload } from "coral-server/services/spam";

import {
GQLCOMMENT_FLAG_REASON,
Expand All @@ -20,6 +21,7 @@ export const spam: IntermediateModerationPhase = async ({
req,
nudge,
log,
now,
}): Promise<IntermediatePhaseResult | void> => {
const integration = tenant.integrations.akismet;

Expand Down Expand Up @@ -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
Expand All @@ -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");

Expand Down
64 changes: 53 additions & 11 deletions server/src/core/server/services/spam/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,74 @@ import { Request } from "coral-server/types/express";

export interface Parameters {
apiKey: string;
blog: string;
comment: Readonly<Comment>;
story: Readonly<Story>;
author: Readonly<User>;
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) => {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
});
};

0 comments on commit e5cf25d

Please sign in to comment.