diff --git a/DatabaseSchema.md b/DatabaseSchema.md index 26f2319..935f978 100644 --- a/DatabaseSchema.md +++ b/DatabaseSchema.md @@ -43,6 +43,7 @@ | reputation | REAL | not null, default '0' | | shadowHidden | INTEGER | not null | | hashedVideoID | TEXT | not null, default '', sha256 | +| userAgent | TEXT | not null, default '' | | index | field | | -- | :--: | @@ -164,6 +165,7 @@ | reputation | REAL | not null, default '0' | | shadowHidden | INTEGER | not null | | hashedVideoID | TEXT | not null, default '', sha256 | +| userAgent | TEXT | not null, default '' | # Private diff --git a/databases/_upgrade_sponsorTimes_22.sql b/databases/_upgrade_sponsorTimes_22.sql new file mode 100644 index 0000000..a6ec31a --- /dev/null +++ b/databases/_upgrade_sponsorTimes_22.sql @@ -0,0 +1,10 @@ +BEGIN TRANSACTION; + +/* Add hash field */ +ALTER TABLE "sponsorTimes" ADD "userAgent" TEXT NOT NULL default ''; + +ALTER TABLE "archivedSponsorTimes" ADD "userAgent" TEXT NOT NULL default ''; + +UPDATE "config" SET value = 22 WHERE key = 'version'; + +COMMIT; diff --git a/src/routes/postSkipSegments.ts b/src/routes/postSkipSegments.ts index ae6cd32..a489d4d 100644 --- a/src/routes/postSkipSegments.ts +++ b/src/routes/postSkipSegments.ts @@ -550,7 +550,10 @@ function preprocessInput(req: Request) { } }); - return {videoID, userID, service, videoDuration, videoDurationParam, segments}; + const userAgentAsArray = req.get("user-agent")?.split("/"); + const userAgent = userAgentAsArray[0] || ""; + + return {videoID, userID, service, videoDuration, videoDurationParam, segments, userAgent}; } export async function postSkipSegments(req: Request, res: Response): Promise { @@ -559,7 +562,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise { return e; } }).timeout(5000); + + it("Should be able to submit with custom user-agent 1", (done: Done) => { + fetch(`${getbaseURL()}/api/postVideoSponsorTimes`, { + method: "POST", + headers: { + "Content-Type": "application/json", + "User-Agent": "MeaBot/5.0" + }, + body: JSON.stringify({ + userID: "testtesttesttesttesttesttesttesttest", + videoID: "userAgent-1", + segments: [{ + segment: [0, 10], + category: "sponsor", + }], + }), + }) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-1"]); + assert.strictEqual(row.startTime, 0); + assert.strictEqual(row.endTime, 10); + assert.strictEqual(row.userAgent, "MeaBot"); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to submit with custom user-agent 2", (done: Done) => { + fetch(`${getbaseURL()}/api/postVideoSponsorTimes`, { + method: "POST", + headers: { + "Content-Type": "application/json", + "User-Agent": "MeaBot" + }, + body: JSON.stringify({ + userID: "testtesttesttesttesttesttesttesttest", + videoID: "userAgent-2", + segments: [{ + segment: [0, 10], + category: "sponsor", + }], + }), + }) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-2"]); + assert.strictEqual(row.startTime, 0); + assert.strictEqual(row.endTime, 10); + assert.strictEqual(row.userAgent, "MeaBot"); + done(); + }) + .catch(err => done(err)); + }); + + it("Should be able to submit with empty user-agent", (done: Done) => { + fetch(`${getbaseURL()}/api/postVideoSponsorTimes`, { + method: "POST", + headers: { + "Content-Type": "application/json", + "User-Agent": "" + }, + body: JSON.stringify({ + userID: "testtesttesttesttesttesttesttesttest", + videoID: "userAgent-3", + segments: [{ + segment: [0, 10], + category: "sponsor", + }], + }), + }) + .then(async res => { + assert.strictEqual(res.status, 200); + const row = await db.prepare("get", `SELECT "startTime", "endTime", "locked", "category", "userAgent" FROM "sponsorTimes" WHERE "videoID" = ?`, ["userAgent-3"]); + assert.strictEqual(row.startTime, 0); + assert.strictEqual(row.endTime, 10); + assert.strictEqual(row.userAgent, ""); + done(); + }) + .catch(err => done(err)); + }); });