mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-30 11:28:19 +03:00
Allow voting and viewing with partial UUID
This commit is contained in:
@@ -3,14 +3,18 @@ import { Request, Response } from "express";
|
|||||||
|
|
||||||
export async function viewedVideoSponsorTime(req: Request, res: Response): Promise<Response> {
|
export async function viewedVideoSponsorTime(req: Request, res: Response): Promise<Response> {
|
||||||
const UUID = req.query?.UUID;
|
const UUID = req.query?.UUID;
|
||||||
|
const videoID = req.query?.videoID;
|
||||||
|
|
||||||
if (!UUID) {
|
if (!UUID) {
|
||||||
//invalid request
|
//invalid request
|
||||||
return res.sendStatus(400);
|
return res.sendStatus(400);
|
||||||
}
|
}
|
||||||
|
|
||||||
//up the view count by one
|
if (!videoID) {
|
||||||
await db.prepare("run", `UPDATE "sponsorTimes" SET views = views + 1 WHERE "UUID" = ?`, [UUID]);
|
await db.prepare("run", `UPDATE "sponsorTimes" SET views = views + 1 WHERE "UUID" = ?`, [UUID]);
|
||||||
|
} else {
|
||||||
|
await db.prepare("run", `UPDATE "sponsorTimes" SET views = views + 1 WHERE "UUID" LIKE ? AND "videoID" = ?`, [`${UUID}%`, videoID]);
|
||||||
|
}
|
||||||
|
|
||||||
return res.sendStatus(200);
|
return res.sendStatus(200);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -303,9 +303,10 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise<Re
|
|||||||
const paramUserID = getUserID(req);
|
const paramUserID = getUserID(req);
|
||||||
const type = req.query.type !== undefined ? parseInt(req.query.type as string) : undefined;
|
const type = req.query.type !== undefined ? parseInt(req.query.type as string) : undefined;
|
||||||
const category = req.query.category as Category;
|
const category = req.query.category as Category;
|
||||||
|
const videoID = req.query.videoID as VideoID;
|
||||||
const ip = getIP(req);
|
const ip = getIP(req);
|
||||||
|
|
||||||
const result = await vote(ip, UUID, paramUserID, type, category);
|
const result = await vote(ip, UUID, paramUserID, type, videoID, category);
|
||||||
|
|
||||||
const response = res.status(result.status);
|
const response = res.status(result.status);
|
||||||
if (result.message) {
|
if (result.message) {
|
||||||
@@ -317,7 +318,7 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise<Re
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID, type: number, category?: Category): Promise<{ status: number, message?: string, json?: unknown }> {
|
export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID, type: number, videoID?: VideoID, category?: Category): Promise<{ status: number, message?: string, json?: unknown }> {
|
||||||
// missing key parameters
|
// missing key parameters
|
||||||
if (!UUID || !paramUserID || !(type !== undefined || category)) {
|
if (!UUID || !paramUserID || !(type !== undefined || category)) {
|
||||||
return { status: 400 };
|
return { status: 400 };
|
||||||
@@ -327,6 +328,14 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
|
|||||||
return { status: 200 };
|
return { status: 200 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (videoID && UUID.length < 60) {
|
||||||
|
// Get the full UUID
|
||||||
|
const segmentInfo: DBSegment = await db.prepare("get", `SELECT "UUID" from "sponsorTimes" WHERE "UUID" LIKE ? AND "videoID" = ?`, [`${UUID}%`, videoID]);
|
||||||
|
if (segmentInfo) {
|
||||||
|
UUID = segmentInfo.UUID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const originalType = type;
|
const originalType = type;
|
||||||
|
|
||||||
//hash the userID
|
//hash the userID
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ describe("voteOnSponsorTime", () => {
|
|||||||
|
|
||||||
const insertSponsorTimeQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "shadowHidden", "hidden") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
const insertSponsorTimeQuery = 'INSERT INTO "sponsorTimes" ("videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "timeSubmitted", "views", "category", "actionType", "shadowHidden", "hidden") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
|
||||||
await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest", 1, 11, 2, 0, "vote-uuid-0", "testman", 0, 50, "sponsor", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest", 1, 11, 2, 0, "vote-uuid-0", "testman", 0, 50, "sponsor", "skip", 0, 0]);
|
||||||
|
await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest---sdaas", 1, 11, 2, 0, "vote-u34113123", "testman", 0, 50, "sponsor", "skip", 0, 0]);
|
||||||
await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 2, 0, "vote-uuid-1", "testman", 0, 50, "sponsor", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 2, 0, "vote-uuid-1", "testman", 0, 50, "sponsor", "skip", 0, 0]);
|
||||||
await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 10, 0, "vote-uuid-1.5", "testman", 0, 50, "outro", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 10, 0, "vote-uuid-1.5", "testman", 0, 50, "outro", "skip", 0, 0]);
|
||||||
await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 10, 0, "vote-uuid-1.6", "testman", 0, 50, "interaction", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["vote-testtesttest2", 1, 11, 10, 0, "vote-uuid-1.6", "testman", 0, 50, "interaction", "skip", 0, 0]);
|
||||||
@@ -41,6 +42,7 @@ describe("voteOnSponsorTime", () => {
|
|||||||
await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 1, 11, 2, 0, "vote-uuid-6", "testman", 0, 50, "intro", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 1, 11, 2, 0, "vote-uuid-6", "testman", 0, 50, "intro", "skip", 0, 0]);
|
||||||
await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 20, 33, 2, 0, "vote-uuid-7", "testman", 0, 50, "intro", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["vote-multiple", 20, 33, 2, 0, "vote-uuid-7", "testman", 0, 50, "intro", "skip", 0, 0]);
|
||||||
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter", 1, 11, 2, 0, "vote-uuid-8", getHash("randomID"), 0, 50, "sponsor", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter", 1, 11, 2, 0, "vote-uuid-8", getHash("randomID"), 0, 50, "sponsor", "skip", 0, 0]);
|
||||||
|
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter", 1, 11, 2, 0, "vote-uuid-87", getHash("randomIDpartial"), 0, 50, "sponsor", "skip", 0, 0]);
|
||||||
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, 0, "vote-uuid-9", randomID2Hashed, 0, 50, "sponsor", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, 0, "vote-uuid-9", randomID2Hashed, 0, 50, "sponsor", "skip", 0, 0]);
|
||||||
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, 0, "vote-uuid-10", getHash("randomID3"), 0, 50, "sponsor", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, 0, "vote-uuid-10", getHash("randomID3"), 0, 50, "sponsor", "skip", 0, 0]);
|
||||||
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, 0, "vote-uuid-11", getHash("randomID4"), 0, 50, "sponsor", "skip", 0, 0]);
|
await db.prepare("run", insertSponsorTimeQuery, ["voter-submitter2", 1, 11, 2, 0, "vote-uuid-11", getHash("randomID4"), 0, 50, "sponsor", "skip", 0, 0]);
|
||||||
@@ -107,13 +109,14 @@ describe("voteOnSponsorTime", () => {
|
|||||||
});
|
});
|
||||||
// constants
|
// constants
|
||||||
const endpoint = "/api/voteOnSponsorTime";
|
const endpoint = "/api/voteOnSponsorTime";
|
||||||
const postVote = (userID: string, UUID: string, type: number) => client({
|
const postVote = (userID: string, UUID: string, type: number, videoID?: string) => client({
|
||||||
method: "POST",
|
method: "POST",
|
||||||
url: endpoint,
|
url: endpoint,
|
||||||
params: {
|
params: {
|
||||||
userID,
|
userID,
|
||||||
UUID,
|
UUID,
|
||||||
type
|
type,
|
||||||
|
videoID
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
const postVoteCategory = (userID: string, UUID: string, category: string) => client({
|
const postVoteCategory = (userID: string, UUID: string, category: string) => client({
|
||||||
@@ -142,6 +145,18 @@ describe("voteOnSponsorTime", () => {
|
|||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Should be able to upvote a segment with a partial ID", (done) => {
|
||||||
|
const UUID = "vote-u34113123";
|
||||||
|
postVote("randomIDpartial", UUID.substring(0, 9), 1, "vote-testtesttest---sdaas")
|
||||||
|
.then(async res => {
|
||||||
|
assert.strictEqual(res.status, 200);
|
||||||
|
const row = await getSegmentVotes(UUID);
|
||||||
|
assert.strictEqual(row.votes, 3);
|
||||||
|
done();
|
||||||
|
})
|
||||||
|
.catch(err => done(err));
|
||||||
|
});
|
||||||
|
|
||||||
it("Should be able to downvote a segment", (done) => {
|
it("Should be able to downvote a segment", (done) => {
|
||||||
const UUID = "vote-uuid-2";
|
const UUID = "vote-uuid-2";
|
||||||
postVote(randomID2, UUID, 0)
|
postVote(randomID2, UUID, 0)
|
||||||
|
|||||||
Reference in New Issue
Block a user