mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-12 06:27:10 +03:00
rewrite voteOnSponsorTimes
This commit is contained in:
@@ -10,7 +10,7 @@ import { getIP } from "../utils/getIP";
|
|||||||
import { getHashCache } from "../utils/getHashCache";
|
import { getHashCache } from "../utils/getHashCache";
|
||||||
import { config } from "../config";
|
import { config } from "../config";
|
||||||
import { HashedUserID, UserID } from "../types/user.model";
|
import { HashedUserID, UserID } from "../types/user.model";
|
||||||
import { Category, HashedIP, IPAddress, SegmentUUID, Service, VideoID, VideoIDHash, Visibility, VideoDuration, ActionType } from "../types/segments.model";
|
import { DBSegment, Category, HashedIP, IPAddress, SegmentUUID, Service, VideoID, VideoIDHash, VideoDuration, ActionType } from "../types/segments.model";
|
||||||
import { QueryCacher } from "../utils/queryCacher";
|
import { QueryCacher } from "../utils/queryCacher";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
import redis from "../utils/redis";
|
import redis from "../utils/redis";
|
||||||
@@ -52,23 +52,24 @@ interface VoteData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise<APIVideoInfo> {
|
function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise<APIVideoInfo> {
|
||||||
if (config.newLeafURLs !== null) {
|
return config.newLeafURLs ? YouTubeAPI.listVideos(videoID, ignoreCache) : null;
|
||||||
return YouTubeAPI.listVideos(videoID, ignoreCache);
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const isUserTempVIP = async (nonAnonUserID: HashedUserID, videoID: VideoID): Promise<boolean> => {
|
const isUserTempVIP = async (nonAnonUserID: HashedUserID, videoID: VideoID): Promise<boolean> => {
|
||||||
|
// fetch videoInfo from cache
|
||||||
const apiVideoInfo = await getYouTubeVideoInfo(videoID);
|
const apiVideoInfo = await getYouTubeVideoInfo(videoID);
|
||||||
const channelID = apiVideoInfo?.data?.authorId;
|
// get channelID or fallback to invalid channelID (<> are invalid URL characters and & is not a valid ChannelID character)
|
||||||
|
const channelID = apiVideoInfo?.data?.authorId ?? "<INVALID_CHANNEL_ID_&&&&&>";
|
||||||
const { err, reply } = await redis.getAsync(tempVIPKey(nonAnonUserID));
|
const { err, reply } = await redis.getAsync(tempVIPKey(nonAnonUserID));
|
||||||
return err ? false : (reply == channelID);
|
// return false if error, reply mismatch or reply is not length 21 and not in testing mode
|
||||||
|
return err ? false :
|
||||||
|
(config.mode === "test") ? reply == channelID
|
||||||
|
: (reply?.length === 21 && reply == channelID);
|
||||||
};
|
};
|
||||||
|
|
||||||
const videoDurationChanged = (segmentDuration: number, APIDuration: number) => (APIDuration > 0 && Math.abs(segmentDuration - APIDuration) > 2);
|
const videoDurationChanged = (segmentDuration: number, APIDuration: number) => (APIDuration > 0 && Math.abs(segmentDuration - APIDuration) > 2);
|
||||||
|
|
||||||
async function checkVideoDurationChange(UUID: SegmentUUID) {
|
async function updateSegmentVideoDuration(UUID: SegmentUUID) {
|
||||||
const { videoDuration, videoID, service } = await db.prepare("get", `select "videoDuration", "videoID", "service" from "sponsorTimes" where "UUID" = ?`, [UUID]);
|
const { videoDuration, videoID, service } = await db.prepare("get", `select "videoDuration", "videoID", "service" from "sponsorTimes" where "UUID" = ?`, [UUID]);
|
||||||
let apiVideoInfo: APIVideoInfo = null;
|
let apiVideoInfo: APIVideoInfo = null;
|
||||||
if (service == Service.YouTube) {
|
if (service == Service.YouTube) {
|
||||||
@@ -82,6 +83,34 @@ async function checkVideoDurationChange(UUID: SegmentUUID) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function checkVideoDuration(UUID: SegmentUUID) {
|
||||||
|
const { videoID, service } = await db.prepare("get", `select "videoID", "service" from "sponsorTimes" where "UUID" = ?`, [UUID]);
|
||||||
|
let apiVideoInfo: APIVideoInfo = null;
|
||||||
|
if (service == Service.YouTube) {
|
||||||
|
// don't use cache since we have no information about the video length
|
||||||
|
apiVideoInfo = await getYouTubeVideoInfo(videoID, true);
|
||||||
|
}
|
||||||
|
const apiVideoDuration = apiVideoInfo?.data?.lengthSeconds as VideoDuration;
|
||||||
|
// if no videoDuration return early
|
||||||
|
if (!apiVideoDuration && apiVideoDuration != 0) return;
|
||||||
|
// fetch latest submission
|
||||||
|
const latestSubmission = await db.prepare("get", `SELECT "videoDuration, UUID, submissionTime
|
||||||
|
FROM "sponsorTimes
|
||||||
|
WHERE "videoID" = ? AND "service" = ? AND
|
||||||
|
"hidden" = 0 AND "shadowHidden" = 0 AND
|
||||||
|
"actionType" != 'full' AND
|
||||||
|
"votes" > -2 AND "videoDuration" != 0
|
||||||
|
ORDER BY "submissionTime" DESC
|
||||||
|
TOP 1`,
|
||||||
|
[videoID, service]) as {videoDuration: VideoDuration, UUID: SegmentUUID, submissionTime: number};
|
||||||
|
|
||||||
|
if (videoDurationChanged(latestSubmission.videoDuration, apiVideoDuration)) {
|
||||||
|
Logger.info(`Video duration changed for ${videoID} from ${latestSubmission.videoDuration} to ${apiVideoDuration}`);
|
||||||
|
await db.prepare("run", `UPDATE "sponsorTimes" SET "hidden" = 1 WHERE videoID = ? AND service = ? AND submissionTime < ?`,
|
||||||
|
[videoID, service, latestSubmission.submissionTime]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function sendWebhooks(voteData: VoteData) {
|
async function sendWebhooks(voteData: VoteData) {
|
||||||
const submissionInfoRow = await db.prepare("get", `SELECT "s"."videoID", "s"."userID", s."startTime", s."endTime", s."category", u."userName",
|
const submissionInfoRow = await db.prepare("get", `SELECT "s"."videoID", "s"."userID", s."startTime", s."endTime", s."category", u."userName",
|
||||||
(select count(1) from "sponsorTimes" where "userID" = s."userID") count,
|
(select count(1) from "sponsorTimes" where "userID" = s."userID") count,
|
||||||
@@ -186,7 +215,7 @@ async function sendWebhooks(voteData: VoteData) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, isOwnSubmission: boolean, category: Category
|
async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, isTempVIP: boolean, isOwnSubmission: boolean, category: Category
|
||||||
, hashedIP: HashedIP, finalResponse: FinalResponse): Promise<{ status: number, message?: string }> {
|
, hashedIP: HashedIP, finalResponse: FinalResponse): Promise<{ status: number, message?: string }> {
|
||||||
// Check if they've already made a vote
|
// Check if they've already made a vote
|
||||||
const usersLastVoteInfo = await privateDB.prepare("get", `select count(*) as votes, category from "categoryVotes" where "UUID" = ? and "userID" = ? group by category`, [UUID, userID]);
|
const usersLastVoteInfo = await privateDB.prepare("get", `select count(*) as votes, category from "categoryVotes" where "UUID" = ? and "userID" = ? group by category`, [UUID, userID]);
|
||||||
@@ -196,32 +225,28 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i
|
|||||||
return { status: finalResponse.finalStatus };
|
return { status: finalResponse.finalStatus };
|
||||||
}
|
}
|
||||||
|
|
||||||
const videoInfo = (await db.prepare("get", `SELECT "category", "actionType", "videoID", "hashedVideoID", "service", "userID", "locked" FROM "sponsorTimes" WHERE "UUID" = ?`,
|
const segmentInfo = (await db.prepare("get", `SELECT "category", "actionType", "videoID", "hashedVideoID", "service", "userID", "locked" FROM "sponsorTimes" WHERE "UUID" = ?`,
|
||||||
[UUID])) as {category: Category, actionType: ActionType, videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, userID: UserID, locked: number};
|
[UUID])) as {category: Category, actionType: ActionType, videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, userID: UserID, locked: number};
|
||||||
if (!videoInfo) {
|
|
||||||
// Submission doesn't exist
|
|
||||||
return { status: 400, message: "Submission doesn't exist." };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (videoInfo.actionType === ActionType.Full) {
|
if (segmentInfo.actionType === ActionType.Full) {
|
||||||
return { status: 400, message: "Not allowed to change category of a full video segment" };
|
return { status: 400, message: "Not allowed to change category of a full video segment" };
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!config.categoryList.includes(category)) {
|
if (!config.categoryList.includes(category)) {
|
||||||
return { status: 400, message: "Category doesn't exist." };
|
return { status: 400, message: "Category doesn't exist." };
|
||||||
}
|
}
|
||||||
if (videoInfo.actionType === ActionType.Poi) {
|
if (segmentInfo.actionType === ActionType.Poi) {
|
||||||
return { status: 400, message: "Not allowed to change category for single point segments" };
|
return { status: 400, message: "Not allowed to change category for single point segments" };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore vote if the next category is locked
|
// Ignore vote if the next category is locked
|
||||||
const nextCategoryLocked = await db.prepare("get", `SELECT "videoID", "category" FROM "lockCategories" WHERE "videoID" = ? AND "service" = ? AND "category" = ?`, [videoInfo.videoID, videoInfo.service, category]);
|
const nextCategoryLocked = await db.prepare("get", `SELECT "videoID", "category" FROM "lockCategories" WHERE "videoID" = ? AND "service" = ? AND "category" = ?`, [segmentInfo.videoID, segmentInfo.service, category]);
|
||||||
if (nextCategoryLocked && !isVIP) {
|
if (nextCategoryLocked && !isVIP) {
|
||||||
return { status: 200 };
|
return { status: 200 };
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ignore vote if the segment is locked
|
// Ignore vote if the segment is locked
|
||||||
if (!isVIP && videoInfo.locked === 1) {
|
if (!isVIP && segmentInfo.locked === 1) {
|
||||||
return { status: 200 };
|
return { status: 200 };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -229,8 +254,8 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i
|
|||||||
|
|
||||||
const timeSubmitted = Date.now();
|
const timeSubmitted = Date.now();
|
||||||
|
|
||||||
const voteAmount = isVIP ? 500 : 1;
|
const voteAmount = (isVIP || isTempVIP) ? 500 : 1;
|
||||||
const ableToVote = isVIP || finalResponse.finalStatus === 200 || true;
|
const ableToVote = isVIP || isTempVIP || finalResponse.finalStatus === 200 || true;
|
||||||
|
|
||||||
if (ableToVote) {
|
if (ableToVote) {
|
||||||
// Add the vote
|
// Add the vote
|
||||||
@@ -253,7 +278,7 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i
|
|||||||
}
|
}
|
||||||
|
|
||||||
// See if the submissions category is ready to change
|
// See if the submissions category is ready to change
|
||||||
const currentCategoryInfo = await db.prepare("get", `select votes from "categoryVotes" where "UUID" = ? and category = ?`, [UUID, videoInfo.category]);
|
const currentCategoryInfo = await db.prepare("get", `select votes from "categoryVotes" where "UUID" = ? and category = ?`, [UUID, segmentInfo.category]);
|
||||||
|
|
||||||
const submissionInfo = await db.prepare("get", `SELECT "userID", "timeSubmitted", "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
|
const submissionInfo = await db.prepare("get", `SELECT "userID", "timeSubmitted", "votes" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
|
||||||
const isSubmissionVIP = submissionInfo && await isUserVIP(submissionInfo.userID);
|
const isSubmissionVIP = submissionInfo && await isUserVIP(submissionInfo.userID);
|
||||||
@@ -265,23 +290,20 @@ async function categoryVote(UUID: SegmentUUID, userID: UserID, isVIP: boolean, i
|
|||||||
|
|
||||||
// Add submission as vote
|
// Add submission as vote
|
||||||
if (!currentCategoryInfo && submissionInfo) {
|
if (!currentCategoryInfo && submissionInfo) {
|
||||||
await db.prepare("run", `insert into "categoryVotes" ("UUID", "category", "votes") values (?, ?, ?)`, [UUID, videoInfo.category, currentCategoryCount]);
|
await db.prepare("run", `insert into "categoryVotes" ("UUID", "category", "votes") values (?, ?, ?)`, [UUID, segmentInfo.category, currentCategoryCount]);
|
||||||
|
await privateDB.prepare("run", `insert into "categoryVotes" ("UUID", "userID", "hashedIP", "category", "timeSubmitted") values (?, ?, ?, ?, ?)`, [UUID, submissionInfo.userID, "unknown", segmentInfo.category, submissionInfo.timeSubmitted]);
|
||||||
await privateDB.prepare("run", `insert into "categoryVotes" ("UUID", "userID", "hashedIP", "category", "timeSubmitted") values (?, ?, ?, ?, ?)`, [UUID, submissionInfo.userID, "unknown", videoInfo.category, submissionInfo.timeSubmitted]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextCategoryCount = (nextCategoryInfo?.votes || 0) + voteAmount;
|
const nextCategoryCount = (nextCategoryInfo?.votes || 0) + voteAmount;
|
||||||
|
|
||||||
//TODO: In the future, raise this number from zero to make it harder to change categories
|
//TODO: In the future, raise this number from zero to make it harder to change categories
|
||||||
// VIPs change it every time
|
// VIPs change it every time
|
||||||
if (nextCategoryCount - currentCategoryCount >= Math.max(Math.ceil(submissionInfo?.votes / 2), 2) || isVIP || isOwnSubmission) {
|
if (nextCategoryCount - currentCategoryCount >= Math.max(Math.ceil(submissionInfo?.votes / 2), 2) || isVIP || isTempVIP || isOwnSubmission) {
|
||||||
// Replace the category
|
// Replace the category
|
||||||
await db.prepare("run", `update "sponsorTimes" set "category" = ? where "UUID" = ?`, [category, UUID]);
|
await db.prepare("run", `update "sponsorTimes" set "category" = ? where "UUID" = ?`, [category, UUID]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
QueryCacher.clearSegmentCache(segmentInfo);
|
||||||
QueryCacher.clearSegmentCache(videoInfo);
|
|
||||||
|
|
||||||
return { status: finalResponse.finalStatus };
|
return { status: finalResponse.finalStatus };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,12 +331,12 @@ 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, category?: Category): Promise<{ status: number, message?: string, json?: unknown }> {
|
||||||
if (UUID === undefined || paramUserID === undefined || (type === undefined && category === undefined)) {
|
// missing key parameters
|
||||||
//invalid request
|
if (!UUID || !paramUserID || !(type !== undefined || category)) {
|
||||||
return { status: 400 };
|
return { status: 400 };
|
||||||
}
|
}
|
||||||
|
// Ignore this vote, invalid
|
||||||
if (paramUserID.length < 30 && config.mode !== "test") {
|
if (paramUserID.length < 30 && config.mode !== "test") {
|
||||||
// Ignore this vote, invalid
|
|
||||||
return { status: 200 };
|
return { status: 200 };
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -334,51 +356,49 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
|
|||||||
//hash the ip 5000 times so no one can get it from the database
|
//hash the ip 5000 times so no one can get it from the database
|
||||||
const hashedIP: HashedIP = await getHashCache((ip + config.globalSalt) as IPAddress);
|
const hashedIP: HashedIP = await getHashCache((ip + config.globalSalt) as IPAddress);
|
||||||
|
|
||||||
//check if this user is on the vip list
|
const segmentInfo: DBSegment = await db.prepare("get", `SELECT * from "sponsorTimes" WHERE "UUID" = ?`, [UUID]);
|
||||||
const videoID = await db.prepare("get", `select "videoID" from "sponsorTimes" where "UUID" = ?`, [UUID]);
|
// segment doesnt exist
|
||||||
const isTempVIP = await isUserTempVIP(nonAnonUserID, videoID?.videoID || null);
|
if (!segmentInfo) {
|
||||||
const isVIP = await isUserVIP(nonAnonUserID) || isTempVIP;
|
return { status: 404 };
|
||||||
|
}
|
||||||
|
|
||||||
|
const isTempVIP = await isUserTempVIP(nonAnonUserID, segmentInfo.videoID);
|
||||||
|
const isVIP = await isUserVIP(nonAnonUserID);
|
||||||
|
|
||||||
//check if user voting on own submission
|
//check if user voting on own submission
|
||||||
const isOwnSubmission = (await db.prepare("get", `SELECT "UUID" as "submissionCount" FROM "sponsorTimes" where "userID" = ? AND "UUID" = ?`, [nonAnonUserID, UUID])) !== undefined;
|
const isOwnSubmission = nonAnonUserID === segmentInfo.userID;
|
||||||
|
|
||||||
// disallow vote types 10/11
|
// disallow vote types 10/11
|
||||||
if (type === 10 || type === 11) {
|
if (type === 10 || type === 11) {
|
||||||
// no longer allow type 10/11 alternative votes
|
|
||||||
return { status: 400 };
|
return { status: 400 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// no type but has category, categoryVote
|
||||||
|
if (!type && category) {
|
||||||
|
return categoryVote(UUID, nonAnonUserID, isVIP, isTempVIP, isOwnSubmission, category, hashedIP, finalResponse);
|
||||||
|
}
|
||||||
|
|
||||||
// If not upvote
|
// If not upvote
|
||||||
if (!isVIP && type != 1) {
|
if (!isVIP && type != 1) {
|
||||||
const isSegmentLocked = async () => !!(await db.prepare("get", `SELECT "locked" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]))?.locked;
|
const isSegmentLocked = segmentInfo.locked;
|
||||||
const isVideoLocked = async () => !!(await db.prepare("get", `SELECT "lockCategories".category from "lockCategories" left join "sponsorTimes"
|
const isVideoLocked = async () => !!(await db.prepare("get", `SELECT "category" FROM "lockCategories" WHERE
|
||||||
on ("lockCategories"."videoID" = "sponsorTimes"."videoID" and
|
"videoID" = ? AND "service" = ? AND "category" = ? AND "actionType" = ?`,
|
||||||
"lockCategories"."service" = "sponsorTimes"."service" and "lockCategories".category = "sponsorTimes".category)
|
[segmentInfo.videoID, segmentInfo.service, segmentInfo.category, segmentInfo.actionType]));
|
||||||
where "UUID" = ?`, [UUID]));
|
if (isSegmentLocked || await isVideoLocked()) {
|
||||||
|
|
||||||
if (await isSegmentLocked() || await isVideoLocked()) {
|
|
||||||
finalResponse.blockVote = true;
|
finalResponse.blockVote = true;
|
||||||
finalResponse.webhookType = VoteWebhookType.Rejected;
|
finalResponse.webhookType = VoteWebhookType.Rejected;
|
||||||
finalResponse.webhookMessage = "Vote rejected: A moderator has decided that this segment is correct";
|
finalResponse.webhookMessage = "Vote rejected: A moderator has decided that this segment is correct";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (type === undefined && category !== undefined) {
|
// if on downvoted non-full segment and is not VIP/ tempVIP/ submitter
|
||||||
return categoryVote(UUID, nonAnonUserID, isVIP, isOwnSubmission, category, hashedIP, finalResponse);
|
if (!isNaN(type) && segmentInfo.votes <= -2 && segmentInfo.actionType !== ActionType.Full &&
|
||||||
}
|
!(isVIP || isTempVIP || isOwnSubmission)) {
|
||||||
|
if (type == 1) {
|
||||||
if (type !== undefined && !isVIP && !isOwnSubmission) {
|
return { status: 403, message: "Not allowed to upvote segment with too many downvotes unless you are VIP." };
|
||||||
// Check if upvoting hidden segment
|
} else if (type == 0) {
|
||||||
const voteInfo = await db.prepare("get", `SELECT votes, "actionType" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]) as
|
// Already downvoted enough, ignore
|
||||||
{ votes: number, actionType: ActionType };
|
return { status: 200 };
|
||||||
|
|
||||||
if (voteInfo && voteInfo.votes <= -2 && voteInfo.actionType !== ActionType.Full) {
|
|
||||||
if (type == 1) {
|
|
||||||
return { status: 403, message: "Not allowed to upvote segment with too many downvotes unless you are VIP." };
|
|
||||||
} else if (type == 0) {
|
|
||||||
// Already downvoted enough, ignore
|
|
||||||
return { status: 200 };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -389,18 +409,20 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
|
|||||||
));
|
));
|
||||||
|
|
||||||
if (warnings.length >= config.maxNumberOfActiveWarnings) {
|
if (warnings.length >= config.maxNumberOfActiveWarnings) {
|
||||||
|
const warningReason = warnings[0]?.reason;
|
||||||
return { status: 403, message: "Vote 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. " +
|
return { status: 403, message: "Vote 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. " +
|
||||||
"Could you please send a message in Discord or Matrix so we can further help you?" +
|
"Could you please send a message in Discord or Matrix so we can further help you?" +
|
||||||
`${(warnings[0]?.reason?.length > 0 ? ` Warning reason: '${warnings[0].reason}'` : "")}` };
|
`${(warningReason.length > 0 ? ` Warning reason: '${warningReason}'` : "")}` };
|
||||||
}
|
}
|
||||||
|
|
||||||
const voteTypeEnum = (type == 0 || type == 1 || type == 20) ? voteTypes.normal : voteTypes.incorrect;
|
const voteTypeEnum = (type == 0 || type == 1 || type == 20) ? voteTypes.normal : voteTypes.incorrect;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//check if vote has already happened
|
// check if vote has already happened
|
||||||
const votesRow = await privateDB.prepare("get", `SELECT "type" FROM "votes" WHERE "userID" = ? AND "UUID" = ?`, [userID, UUID]);
|
const votesRow = await privateDB.prepare("get", `SELECT "type" FROM "votes" WHERE "userID" = ? AND "UUID" = ?`, [userID, UUID]);
|
||||||
|
|
||||||
//-1 for downvote, 1 for upvote. Maybe more depending on reputation in the future
|
// -1 for downvote, 1 for upvote. Maybe more depending on reputation in the future
|
||||||
|
// oldIncrementAmount will be zero if row is null
|
||||||
let incrementAmount = 0;
|
let incrementAmount = 0;
|
||||||
let oldIncrementAmount = 0;
|
let oldIncrementAmount = 0;
|
||||||
|
|
||||||
@@ -417,7 +439,7 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
|
|||||||
//unrecongnised type of vote
|
//unrecongnised type of vote
|
||||||
return { status: 400 };
|
return { status: 400 };
|
||||||
}
|
}
|
||||||
if (votesRow != undefined) {
|
if (votesRow) {
|
||||||
if (votesRow.type === 1) {
|
if (votesRow.type === 1) {
|
||||||
//upvote
|
//upvote
|
||||||
oldIncrementAmount = 1;
|
oldIncrementAmount = 1;
|
||||||
@@ -442,65 +464,56 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//check if the increment amount should be multiplied (downvotes have more power if there have been many views)
|
// check if the increment amount should be multiplied (downvotes have more power if there have been many views)
|
||||||
const videoInfo = await db.prepare("get", `SELECT "videoID", "hashedVideoID", "service", "votes", "views", "userID", "hidden" FROM "sponsorTimes" WHERE "UUID" = ?`, [UUID]) as
|
// user is temp/ VIP/ own submission and downvoting
|
||||||
{videoID: VideoID, hashedVideoID: VideoIDHash, service: Service, votes: number, views: number, userID: UserID, hidden: Visibility};
|
if ((isVIP || isTempVIP || isOwnSubmission) && incrementAmount < 0) {
|
||||||
|
incrementAmount = -(segmentInfo.votes + 2 - oldIncrementAmount);
|
||||||
if (voteTypeEnum === voteTypes.normal) {
|
type = incrementAmount;
|
||||||
if ((isVIP || isOwnSubmission) && incrementAmount < 0) {
|
|
||||||
//this user is a vip and a downvote
|
|
||||||
incrementAmount = -(videoInfo.votes + 2 - oldIncrementAmount);
|
|
||||||
type = incrementAmount;
|
|
||||||
}
|
|
||||||
} else if (voteTypeEnum == voteTypes.incorrect) {
|
|
||||||
if (isVIP || isOwnSubmission) {
|
|
||||||
//this user is a vip and a downvote
|
|
||||||
incrementAmount = 500 * incrementAmount;
|
|
||||||
type = incrementAmount < 0 ? 12 : 13;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only change the database if they have made a submission before and haven't voted recently
|
// Only change the database if they have made a submission before and haven't voted recently
|
||||||
const ableToVote = isVIP
|
const userAbleToVote = (!(isOwnSubmission && incrementAmount > 0 && oldIncrementAmount >= 0)
|
||||||
|| (!(isOwnSubmission && incrementAmount > 0 && oldIncrementAmount >= 0)
|
|
||||||
&& (await db.prepare("get", `SELECT "userID" FROM "sponsorTimes" WHERE "userID" = ?`, [nonAnonUserID])) !== undefined
|
&& (await db.prepare("get", `SELECT "userID" FROM "sponsorTimes" WHERE "userID" = ?`, [nonAnonUserID])) !== undefined
|
||||||
&& (await db.prepare("get", `SELECT "userID" FROM "shadowBannedUsers" WHERE "userID" = ?`, [nonAnonUserID])) === undefined
|
&& (await db.prepare("get", `SELECT "userID" FROM "shadowBannedUsers" WHERE "userID" = ?`, [nonAnonUserID])) === undefined
|
||||||
&& (await privateDB.prepare("get", `SELECT "UUID" FROM "votes" WHERE "UUID" = ? AND "hashedIP" = ? AND "userID" != ?`, [UUID, hashedIP, userID])) === undefined)
|
&& (await privateDB.prepare("get", `SELECT "UUID" FROM "votes" WHERE "UUID" = ? AND "hashedIP" = ? AND "userID" != ?`, [UUID, hashedIP, userID])) === undefined)
|
||||||
&& !finalResponse.blockVote
|
&& !finalResponse.blockVote
|
||||||
&& finalResponse.finalStatus === 200;
|
&& finalResponse.finalStatus === 200;
|
||||||
|
|
||||||
|
const tempVIPAbleToVote = isTempVIP
|
||||||
|
&& (await db.prepare("get", `SELECT COUNT(*) FROM "sponsorTimes" WHERE "userID" = ?`, [nonAnonUserID])) >= 5
|
||||||
|
&& userAbleToVote;
|
||||||
|
|
||||||
|
const ableToVote = isVIP || userAbleToVote || tempVIPAbleToVote;
|
||||||
|
|
||||||
if (ableToVote) {
|
if (ableToVote) {
|
||||||
//update the votes table
|
//update the votes table
|
||||||
if (votesRow != undefined) {
|
if (votesRow) {
|
||||||
await privateDB.prepare("run", `UPDATE "votes" SET "type" = ? WHERE "userID" = ? AND "UUID" = ?`, [type, userID, UUID]);
|
await privateDB.prepare("run", `UPDATE "votes" SET "type" = ? WHERE "userID" = ? AND "UUID" = ?`, [type, userID, UUID]);
|
||||||
} else {
|
} else {
|
||||||
await privateDB.prepare("run", `INSERT INTO "votes" VALUES(?, ?, ?, ?)`, [UUID, userID, hashedIP, type]);
|
await privateDB.prepare("run", `INSERT INTO "votes" VALUES(?, ?, ?, ?)`, [UUID, userID, hashedIP, type]);
|
||||||
}
|
}
|
||||||
|
|
||||||
let columnName = "";
|
// update the vote count on this sponsorTime
|
||||||
if (voteTypeEnum === voteTypes.normal) {
|
await db.prepare("run", `UPDATE "sponsorTimes" SET "votes" = "votes" + ? WHERE "UUID" = ?`, [incrementAmount - oldIncrementAmount, UUID]);
|
||||||
columnName = "votes";
|
|
||||||
} else if (voteTypeEnum === voteTypes.incorrect) {
|
// check duration of all submissions on this video
|
||||||
columnName = "incorrectVotes";
|
if (type < 0) {
|
||||||
|
checkVideoDuration(UUID);
|
||||||
}
|
}
|
||||||
|
|
||||||
//update the vote count on this sponsorTime
|
// additional procesing for VIP
|
||||||
//oldIncrementAmount will be zero is row is null
|
// on VIP upvote
|
||||||
await db.prepare("run", `UPDATE "sponsorTimes" SET "${columnName}" = "${columnName}" + ? WHERE "UUID" = ?`, [incrementAmount - oldIncrementAmount, UUID]);
|
|
||||||
if (isVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) {
|
if (isVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) {
|
||||||
// check for video duration change
|
// Update video duration in case that caused it to be hidden
|
||||||
await checkVideoDurationChange(UUID);
|
await updateSegmentVideoDuration(UUID);
|
||||||
// Unhide and Lock this submission
|
// unhide & unlock
|
||||||
await db.prepare("run", 'UPDATE "sponsorTimes" SET locked = 1, hidden = 0, "shadowHidden" = 0 WHERE "UUID" = ?', [UUID]);
|
await db.prepare("run", 'UPDATE "sponsorTimes" SET locked = 1, hidden = 0, "shadowHidden" = 0 WHERE "UUID" = ?', [UUID]);
|
||||||
|
// on VIP downvote/ undovote, also unlock submission
|
||||||
// Reset video duration in case that caused it to be hidden
|
|
||||||
if (videoInfo.hidden) await db.prepare("run", 'UPDATE "sponsorTimes" SET "videoDuration" = 0 WHERE "UUID" = ?', [UUID]);
|
|
||||||
} else if (isVIP && incrementAmount <= 0 && voteTypeEnum === voteTypes.normal) {
|
} else if (isVIP && incrementAmount <= 0 && voteTypeEnum === voteTypes.normal) {
|
||||||
// Unlock if a VIP downvotes it
|
|
||||||
await db.prepare("run", 'UPDATE "sponsorTimes" SET locked = 0 WHERE "UUID" = ?', [UUID]);
|
await db.prepare("run", 'UPDATE "sponsorTimes" SET locked = 0 WHERE "UUID" = ?', [UUID]);
|
||||||
}
|
}
|
||||||
|
|
||||||
QueryCacher.clearSegmentCache(videoInfo);
|
QueryCacher.clearSegmentCache(segmentInfo);
|
||||||
}
|
}
|
||||||
if (incrementAmount - oldIncrementAmount !== 0) {
|
if (incrementAmount - oldIncrementAmount !== 0) {
|
||||||
sendWebhooks({
|
sendWebhooks({
|
||||||
@@ -510,7 +523,7 @@ export async function vote(ip: IPAddress, UUID: SegmentUUID, paramUserID: UserID
|
|||||||
isTempVIP,
|
isTempVIP,
|
||||||
isVIP,
|
isVIP,
|
||||||
isOwnSubmission,
|
isOwnSubmission,
|
||||||
row: videoInfo,
|
row: segmentInfo,
|
||||||
category,
|
category,
|
||||||
incrementAmount,
|
incrementAmount,
|
||||||
oldIncrementAmount,
|
oldIncrementAmount,
|
||||||
|
|||||||
@@ -73,9 +73,9 @@ describe("voteOnSponsorTime", () => {
|
|||||||
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(vipUser)]);
|
await db.prepare("run", 'INSERT INTO "vipUsers" ("userID") VALUES (?)', [getHash(vipUser)]);
|
||||||
await db.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]);
|
await db.prepare("run", 'INSERT INTO "shadowBannedUsers" ("userID") VALUES (?)', [getHash("randomID4")]);
|
||||||
|
|
||||||
const insertlockCategoriesQuerry = 'INSERT INTO "lockCategories" ("videoID", "userID", "category", "reason") VALUES (?, ?, ?, ?)';
|
const insertlockCategoriesQuery = 'INSERT INTO "lockCategories" ("videoID", "userID", "category", "actionType") VALUES (?, ?, ?, ?)';
|
||||||
await db.prepare("run", insertlockCategoriesQuerry, ["no-sponsor-segments-video", "someUser", "sponsor", ""]);
|
await db.prepare("run", insertlockCategoriesQuery, ["no-sponsor-segments-video", "someUser", "sponsor", "skip"]);
|
||||||
await db.prepare("run", insertlockCategoriesQuerry, ["category-change-test-1", "someUser", "preview", ""]); // sponsor should stay unlocked
|
await db.prepare("run", insertlockCategoriesQuery, ["category-change-test-1", "someUser", "preview", "skip"]); // sponsor should stay unlocked
|
||||||
});
|
});
|
||||||
// constants
|
// constants
|
||||||
const endpoint = "/api/voteOnSponsorTime";
|
const endpoint = "/api/voteOnSponsorTime";
|
||||||
@@ -409,15 +409,15 @@ describe("voteOnSponsorTime", () => {
|
|||||||
const UUID = "invalid-uuid";
|
const UUID = "invalid-uuid";
|
||||||
postVoteCategory("randomID3", UUID, "intro")
|
postVoteCategory("randomID3", UUID, "intro")
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 400);
|
assert.strictEqual(res.status, 404);
|
||||||
done();
|
done();
|
||||||
})
|
})
|
||||||
.catch(err => done(err));
|
.catch(err => done(err));
|
||||||
});
|
});
|
||||||
|
|
||||||
it("Should not be able to category-vote on an a full video segment", (done) => {
|
it("Should not be able to category-vote on a full video segment", (done) => {
|
||||||
const UUID = "invalid-uuid";
|
const UUID = "full-video-uuid-1";
|
||||||
postVoteCategory("full-video-uuid-1", UUID, "selfpromo")
|
postVoteCategory("randomID3", UUID, "selfpromo")
|
||||||
.then(res => {
|
.then(res => {
|
||||||
assert.strictEqual(res.status, 400);
|
assert.strictEqual(res.status, 400);
|
||||||
done();
|
done();
|
||||||
|
|||||||
Reference in New Issue
Block a user