Merge pull request #327 from HaiDang666/324_user-agent

Store user-agent in postSkipSegment
This commit is contained in:
Ajay Ramachandran
2021-07-31 20:56:00 -04:00
committed by GitHub
5 changed files with 103 additions and 6 deletions

View File

@@ -43,6 +43,7 @@
| reputation | REAL | not null, default '0' | | reputation | REAL | not null, default '0' |
| shadowHidden | INTEGER | not null | | shadowHidden | INTEGER | not null |
| hashedVideoID | TEXT | not null, default '', sha256 | | hashedVideoID | TEXT | not null, default '', sha256 |
| userAgent | TEXT | not null, default '' |
| index | field | | index | field |
| -- | :--: | | -- | :--: |
@@ -164,6 +165,7 @@
| reputation | REAL | not null, default '0' | | reputation | REAL | not null, default '0' |
| shadowHidden | INTEGER | not null | | shadowHidden | INTEGER | not null |
| hashedVideoID | TEXT | not null, default '', sha256 | | hashedVideoID | TEXT | not null, default '', sha256 |
| userAgent | TEXT | not null, default '' |
# Private # Private

View File

@@ -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;

View File

@@ -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<Response> { export async function postSkipSegments(req: Request, res: Response): Promise<Response> {
@@ -559,7 +562,7 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
} }
// eslint-disable-next-line prefer-const // eslint-disable-next-line prefer-const
let {videoID, userID, service, videoDuration, videoDurationParam, segments} = preprocessInput(req); let {videoID, userID, service, videoDuration, videoDurationParam, segments, userAgent} = preprocessInput(req);
const invalidCheckResult = checkInvalidFields(videoID, userID, segments); const invalidCheckResult = checkInvalidFields(videoID, userID, segments);
if (!invalidCheckResult.pass) { if (!invalidCheckResult.pass) {
@@ -636,9 +639,9 @@ export async function postSkipSegments(req: Request, res: Response): Promise<Res
const startingLocked = isVIP ? 1 : 0; const startingLocked = isVIP ? 1 : 0;
try { try {
await db.prepare("run", `INSERT INTO "sponsorTimes" await db.prepare("run", `INSERT INTO "sponsorTimes"
("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "reputation", "shadowHidden", "hashedVideoID") ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "service", "videoDuration", "reputation", "shadowHidden", "hashedVideoID", "userAgent")
VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [ VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, [
videoID, segmentInfo.segment[0], segmentInfo.segment[1], startingVotes, startingLocked, UUID, userID, timeSubmitted, 0, segmentInfo.category, segmentInfo.actionType, service, videoDuration, reputation, shadowBanned, hashedVideoID, videoID, segmentInfo.segment[0], segmentInfo.segment[1], startingVotes, startingLocked, UUID, userID, timeSubmitted, 0, segmentInfo.category, segmentInfo.actionType, service, videoDuration, reputation, shadowBanned, hashedVideoID, userAgent
], ],
); );

View File

@@ -61,6 +61,7 @@ export interface DBSegment {
reputation: number; reputation: number;
hashedVideoID: VideoIDHash; hashedVideoID: VideoIDHash;
timeSubmitted: number; timeSubmitted: number;
userAgent: string;
} }
export interface OverlappingSegmentGroup { export interface OverlappingSegmentGroup {

View File

@@ -589,7 +589,7 @@ describe("postSkipSegments", () => {
const expected = "Submission rejected due to a warning from a moderator. This means that we noticed you were making some common mistakes" const expected = "Submission rejected due to a warning from a moderator. This means that we noticed you were making some common mistakes"
+ " that are not malicious, and we just want to clarify the rules. " + " that are not malicious, and we just want to clarify the rules. "
+ "Could you please send a message in discord.gg/SponsorBlock or matrix.to/#/+sponsor:ajay.app so we can further help you? " + "Could you please send a message in discord.gg/SponsorBlock or matrix.to/#/+sponsor:ajay.app so we can further help you? "
+ `Your userID is ${userID}.\n\nWarning reason: ${reason}`; + `Your userID is ${userID}.\n\nWarning reason: '${reason}'`;
assert.strictEqual(errorMessage, expected); assert.strictEqual(errorMessage, expected);
done(); done();
@@ -845,4 +845,85 @@ describe("postSkipSegments", () => {
return e; return e;
} }
}).timeout(5000); }).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));
});
}); });