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

handle non-jigsaw config gracefully #1833

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions server/src/auth/create-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ function createUser(req: any, res: any) {
);
return;
}
cookies.addCookies(req, res, token, uid)
.then(function() {
cookies
.addCookies(req, res, token, uid)
.then(function () {
res.json({
uid: uid,
hname: hname,
email: email
})
email: email,
});
})
.catch(function (err: any) {
fail(res, 500, "polis_err_adding_user", err);
Expand Down
58 changes: 37 additions & 21 deletions server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6428,15 +6428,15 @@ Email verified! You can close this tab or hit the back button.
"UPDATE comments SET active = $1, mod = $2, is_meta = $3 WHERE zid = $4 AND tid = $5";
let params = [active, mod, is_meta, zid, tid];

console.log("Executing query:", query);
console.log("With parameters:", params);
logger.debug("Executing query:", { query });
logger.debug("With parameters:", { params });

pgQuery(query, params, (err: any, result: any) => {
if (err) {
console.error("Database error:", err);
logger.error("moderateComment pgQuery error:", err);
reject(err);
} else {
console.log("Query executed successfully");
logger.debug("moderateComment pgQuery executed successfully");
resolve(result);
}
});
Expand Down Expand Up @@ -6553,7 +6553,7 @@ Email verified! You can close this tab or hit the back button.

return response.data;
} catch (err) {
console.error("Error:", err);
logger.error("analyzeComment error", err);
}
}

Expand Down Expand Up @@ -6652,7 +6652,10 @@ Email verified! You can close this tab or hit the back button.
return false;
});

const jigsawModerationPromise = analyzeComment(txt);
// Only analyze comments if we have a Jigsaw API key
const jigsawModerationPromise = Config.googleJigsawPerspectiveApiKey
? analyzeComment(txt)
: Promise.resolve(null);

const isModeratorPromise = isModerator(zid!, uid!);
const conversationInfoPromise = getConversationInfo(zid!);
Expand Down Expand Up @@ -6721,20 +6724,33 @@ Email verified! You can close this tab or hit the back button.
let active = true;
const classifications = [];

console.log("JIGSAW RESPONSE", txt);
console.log(
`Jigsaw toxicty Score for comment "${txt}": ${jigsawResponse?.attributeScores?.TOXICITY?.summaryScore?.value}`
);
const toxicityScore =
jigsawResponse?.attributeScores?.TOXICITY?.summaryScore?.value;

if (
conv.profanity_filter &&
jigsawResponse?.attributeScores?.TOXICITY?.summaryScore?.value >
jigsawToxicityThreshold
) {
if (typeof toxicityScore === "number" && !isNaN(toxicityScore)) {
logger.debug(
`Jigsaw toxicity Score for comment "${txt}": ${toxicityScore}`
);

if (toxicityScore > jigsawToxicityThreshold && conv.profanity_filter) {
active = false;
classifications.push("bad");
logger.info(
"active=false because (jigsawToxicity && conv.profanity_filter)"
);
}
// Fall back to bad words filter if Jigsaw API is not available or fails to return a numeric value
} else if (bad && conv.profanity_filter) {
active = false;
classifications.push("bad");
logger.info("active=false because (bad && conv.profanity_filter)");
}

if (spammy && conv.spam_filter) {
active = false;
classifications.push("spammy");
logger.info("active=false because (spammy && conv.spam_filter)");
}
if (spammy && conv.spam_filter) {
active = false;
classifications.push("spammy");
Expand Down Expand Up @@ -7794,31 +7810,31 @@ Email verified! You can close this tab or hit the back button.
let mod = req.p.mod;
let is_meta = req.p.is_meta;

console.log(
logger.debug(
`Attempting to update comment. zid: ${zid}, tid: ${tid}, uid: ${uid}`
);

isModerator(zid, uid)
.then(function (isModerator: any) {
console.log(`isModerator result: ${isModerator}`);
logger.debug(`isModerator result: ${isModerator}`);
if (isModerator) {
moderateComment(zid, tid, active, mod, is_meta).then(
function () {
console.log("Comment moderated successfully");
logger.debug("Comment moderated successfully");
res.status(200).json({});
},
function (err: any) {
console.error("Error in moderateComment:", err);
logger.error("Error in moderateComment:", err);
fail(res, 500, "polis_err_update_comment", err);
}
);
} else {
console.log("User is not a moderator");
logger.debug("User is not a moderator");
fail(res, 403, "polis_err_update_comment_auth");
}
})
.catch(function (err: any) {
console.error("Error in isModerator:", err);
logger.error("Error in isModerator:", err);
fail(res, 500, "polis_err_update_comment", err);
});
}
Expand Down
54 changes: 10 additions & 44 deletions server/src/utils/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,19 @@ function setCookie(
res.cookie(name, value, opts);
}

function setParentReferrerCookie(
req: any,
res: any,
referrer: any
) {
function setParentReferrerCookie(req: any, res: any, referrer: any) {
setCookie(req, res, COOKIES.PARENT_REFERRER, referrer, {
httpOnly: true,
});
}

function setParentUrlCookie(
req: any,
res: any,
parent_url: any
) {
function setParentUrlCookie(req: any, res: any, parent_url: any) {
setCookie(req, res, COOKIES.PARENT_URL, parent_url, {
httpOnly: true,
});
}

function setHasEmailCookie(
req: any,
res: any,
email: any
) {
function setHasEmailCookie(req: any, res: any, email: any) {
if (email) {
setCookie(req, res, COOKIES.HAS_EMAIL, 1, {
// not httpOnly - needed by JS
Expand All @@ -109,27 +97,13 @@ function setHasEmailCookie(
// else falsy
}

function setUserCreatedTimestampCookie(
req: any,
res: any,
timestamp: any
) {
setCookie(
req,
res,
COOKIES.USER_CREATED_TIMESTAMP,
timestamp,
{
// not httpOnly - needed by JS
}
);
function setUserCreatedTimestampCookie(req: any, res: any, timestamp: any) {
setCookie(req, res, COOKIES.USER_CREATED_TIMESTAMP, timestamp, {
// not httpOnly - needed by JS
});
}

function setTokenCookie(
req: any,
res: any,
token: any
) {
function setTokenCookie(req: any, res: any, token: any) {
setCookie(req, res, COOKIES.TOKEN, token, {
httpOnly: true,
});
Expand All @@ -141,11 +115,7 @@ function setUidCookie(req: any, res: any, uid: any) {
});
}

function setPermanentCookie(
req: any,
res: any,
token: any
) {
function setPermanentCookie(req: any, res: any, token: any) {
setCookie(req, res, COOKIES.PERMANENT_COOKIE, token, {
httpOnly: true,
});
Expand Down Expand Up @@ -176,11 +146,7 @@ function addCookies(
setUserCreatedTimestampCookie(req, res, created);

if (!req.cookies[COOKIES.PERMANENT_COOKIE]) {
setPermanentCookie(
req,
res,
Session.makeSessionToken()
);
setPermanentCookie(req, res, Session.makeSessionToken());
}
res.header("x-polis", token);
});
Expand Down
Loading