Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create spam detection and reporting in posts.js #10575

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions spam detection and reporting in posts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
class GlobalVars {
static reasonWeights = {
"blacklisted user": 500,
"all-caps title": 100,
"repeating characters in body": 50
};

static updateReasonWeights() {
this.reasonWeights["spam"] = 200;
}
}

class Post {
constructor(postId, postUrl, userUrl, postSite, title, body) {
this.postId = postId;
this.postUrl = postUrl;
this.userUrl = userUrl;
this.postSite = postSite;
this.title = title;
this.body = body;
this.isAnswer = false;
this.edited = false;
this.upVoteCount = 0;
this.downVoteCount = 0;
this.ownerRep = 0;
this.postScore = 0;
}
}

function isWhitelistedUser(userUrl) {
return false;
}

function isBlacklistedUser(userUrl) {
return false;
}

function sumWeight(reasons) {
if (Object.keys(GlobalVars.reasonWeights).length === 0) {
GlobalVars.updateReasonWeights();
}

let sum = 0;
reasons.forEach(reason => {
if (reason.includes("(")) {
reason = reason.replace(/\s*\(.*$/, "");
}
sum += GlobalVars.reasonWeights[reason.toLowerCase()] || 0;
});
return sum;
}

function checkIfSpam(post, reasons) {
let isSpam = false;
let why = "";

if (isBlacklistedUser(post.userUrl)) {
reasons.push("blacklisted user");
why += "User is blacklisted";
}

if (post.title === post.title.toUpperCase()) {
reasons.push("all-caps title");
}

if (reasons.length > 0) {
isSpam = true;
}

return isSpam;
}

function buildMessage(post, reasons) {
const messageFormat = "[SmokeDetector] %s: [%s](%s) by %s on `%s`";
const postUrl = post.postUrl;
const user = post.userUrl || "a deleted user";
const title = post.title || "Unknown title";
const site = post.postSite;
const weight = sumWeight(reasons);
const reasonText = reasons.join(", ");
return `[SmokeDetector] ${reasonText}: [${title}](${postUrl}) by ${user} on \`${site}\``;
}

function main() {
const examplePost = new Post("123", "http://example.com/post/123", "http://example.com/user/123", "example.com", "THIS IS SPAM", "Spammy content");
const reasons = [];
const isSpam = checkIfSpam(examplePost, reasons);

if (isSpam) {
const message = buildMessage(examplePost, reasons);
console.log("Detected spam: " + message);
} else {
console.log("Post is not spam.");
}
}

main();