From e9fcf6b445af382ee72cc399d1dd5f5b3c52df3a Mon Sep 17 00:00:00 2001 From: Ajay Date: Mon, 17 Jul 2023 22:42:29 -0400 Subject: [PATCH] Add seperate type for dearrow warning Also add dearrow warning reason as option for user info --- databases/_upgrade_sponsorTimes_36.sql | 7 +++++++ src/routes/getUserInfo.ts | 13 ++++++++++++- src/routes/postSkipSegments.ts | 2 +- src/routes/postWarning.ts | 8 +++++--- src/routes/voteOnSponsorTime.ts | 2 +- src/types/warning.model.ts | 4 ++++ test/cases/postWarning.ts | 2 +- 7 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 databases/_upgrade_sponsorTimes_36.sql create mode 100644 src/types/warning.model.ts diff --git a/databases/_upgrade_sponsorTimes_36.sql b/databases/_upgrade_sponsorTimes_36.sql new file mode 100644 index 0000000..2a4eac2 --- /dev/null +++ b/databases/_upgrade_sponsorTimes_36.sql @@ -0,0 +1,7 @@ +BEGIN TRANSACTION; + +ALTER TABLE "warnings" ADD "type" INTEGER default 0; + +UPDATE "config" SET value = 36 WHERE key = 'version'; + +COMMIT; \ No newline at end of file diff --git a/src/routes/getUserInfo.ts b/src/routes/getUserInfo.ts index 8eed6f5..c1e002b 100644 --- a/src/routes/getUserInfo.ts +++ b/src/routes/getUserInfo.ts @@ -78,6 +78,16 @@ async function dbGetWarningsForUser(userID: HashedUserID): Promise { } } +async function dbGetDeArrowWarningReasonForUser(userID: HashedUserID): Promise { + try { + const row = await db.prepare("get", `SELECT reason FROM "warnings" WHERE "userID" = ? AND "enabled" = 1 AND "type" = 1`, [userID], { useReplica: true }); + return row?.reason ?? 0; + } catch (err) /* istanbul ignore next */ { + Logger.error(`Couldn't get warnings for user ${userID}. returning 0`); + return 0; + } +} + async function dbGetLastSegmentForUser(userID: HashedUserID): Promise { try { const row = await db.prepare("get", `SELECT "UUID" FROM "sponsorTimes" WHERE "userID" = ? ORDER BY "timeSubmitted" DESC LIMIT 1`, [userID], { useReplica: true }); @@ -153,6 +163,7 @@ const dbGetValue = (userID: HashedUserID, property: string): Promise dbGetIgnoredViewsForUser(userID), warnings: () => dbGetWarningsForUser(userID), warningReason: () => dbGetActiveWarningReasonForUser(userID), + deArrowWarningReason: () => dbGetDeArrowWarningReasonForUser(userID), banned: () => dbGetBanned(userID), reputation: () => getReputation(userID), vip: () => isUserVIP(userID), @@ -171,7 +182,7 @@ async function getUserInfo(req: Request, res: Response): Promise { "viewCount", "ignoredViewCount", "warnings", "warningReason", "reputation", "vip", "lastSegmentID"]; const allProperties: string[] = [...defaultProperties, "banned", "permissions", "freeChaptersAccess", - "ignoredSegmentCount", "titleSubmissionCount", "thumbnailSubmissionCount"]; + "ignoredSegmentCount", "titleSubmissionCount", "thumbnailSubmissionCount", "deArrowWarningReason"]; let paramValues: string[] = req.query.values ? JSON.parse(req.query.values as string) : req.query.value diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index 74f88b3..51d1751 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -162,7 +162,7 @@ async function checkUserActiveWarning(userID: HashedUserID): Promise ? AND enabled = 1 + WHERE "userID" = ? AND "issueTime" > ? AND enabled = 1 AND type = 0 ORDER BY "issueTime" DESC`, [ userID, diff --git a/src/routes/postWarning.ts b/src/routes/postWarning.ts index c3c2a0c..cb12eb5 100644 --- a/src/routes/postWarning.ts +++ b/src/routes/postWarning.ts @@ -6,6 +6,7 @@ import { getHashCache } from "../utils/getHashCache"; import { HashedUserID, UserID } from "../types/user.model"; import { config } from "../config"; import { generateWarningDiscord, warningData, dispatchEvent } from "../utils/webhookUtils"; +import { WarningType } from "../types/warning.model"; type warningEntry = { userID: HashedUserID, @@ -32,6 +33,7 @@ export async function postWarning(req: Request, res: Response): Promise ? AND enabled = 1`, + const warnings = (await db.prepare("all", `SELECT "reason" FROM warnings WHERE "userID" = ? AND "issueTime" > ? AND enabled = 1 AND type = 0`, [nonAnonUserID, Math.floor(now - (config.hoursAfterWarningExpires * MILLISECONDS_IN_HOUR))], )); diff --git a/src/types/warning.model.ts b/src/types/warning.model.ts new file mode 100644 index 0000000..079a59b --- /dev/null +++ b/src/types/warning.model.ts @@ -0,0 +1,4 @@ +export enum WarningType { + SponsorBlock = 0, + DeArrow = 1 +} \ No newline at end of file diff --git a/test/cases/postWarning.ts b/test/cases/postWarning.ts index dbd707a..6773ef5 100644 --- a/test/cases/postWarning.ts +++ b/test/cases/postWarning.ts @@ -7,7 +7,7 @@ import { client } from "../utils/httpClient"; describe("postWarning", () => { // constants const endpoint = "/api/warnUser"; - const getWarning = (userID: string) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ?`, [userID]); + const getWarning = (userID: string, type = 0) => db.prepare("get", `SELECT "userID", "issueTime", "issuerUserID", enabled, "reason" FROM warnings WHERE "userID" = ? AND "type" = ?`, [userID, type]); const warneduserID = "warning-0"; const warnedUserPublicID = getHash(warneduserID);