From 197a2d05c4a73d150b95241b7f9a7d734f47a084 Mon Sep 17 00:00:00 2001 From: Bennie Rosas Date: Thu, 31 Oct 2024 19:37:11 -0500 Subject: [PATCH 1/2] only use jigsaw API if key is provided; replace `console` with `logger` --- server/src/server.ts | 58 ++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 21 deletions(-) diff --git a/server/src/server.ts b/server/src/server.ts index 3897c27ee..fd0b1ff61 100644 --- a/server/src/server.ts +++ b/server/src/server.ts @@ -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); } }); @@ -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); } } @@ -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!); @@ -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"); @@ -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); }); } From c233a8e5123b26e50ae36cc5e7391c2fe4e290f3 Mon Sep 17 00:00:00 2001 From: Bennie Rosas Date: Thu, 31 Oct 2024 19:38:24 -0500 Subject: [PATCH 2/2] npm run format --- server/src/auth/create-user.ts | 9 +++--- server/src/utils/cookies.ts | 54 +++++++--------------------------- 2 files changed, 15 insertions(+), 48 deletions(-) diff --git a/server/src/auth/create-user.ts b/server/src/auth/create-user.ts index 2c2916b50..3b175bf1a 100644 --- a/server/src/auth/create-user.ts +++ b/server/src/auth/create-user.ts @@ -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); diff --git a/server/src/utils/cookies.ts b/server/src/utils/cookies.ts index e2b5c6aa7..3ca67a7bb 100644 --- a/server/src/utils/cookies.ts +++ b/server/src/utils/cookies.ts @@ -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 @@ -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, }); @@ -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, }); @@ -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); });