Improve shadow ban check

This commit is contained in:
Ajay Ramachandran 2021-07-20 17:15:15 -04:00
parent 05ec937b06
commit 31103faf92
2 changed files with 9 additions and 6 deletions

View file

@ -24,12 +24,14 @@ async function prepareCategorySegments(req: Request, videoID: VideoID, category:
return true; return true;
} }
if (cache.shadowHiddenSegmentIPs[videoID] === undefined) { if (cache.shadowHiddenSegmentIPs[videoID] === undefined) cache.shadowHiddenSegmentIPs[videoID] = {};
cache.shadowHiddenSegmentIPs[videoID] = await privateDB.prepare("all", 'SELECT "hashedIP" FROM "sponsorTimes" WHERE "videoID" = ?', [videoID]) as { hashedIP: HashedIP }[]; if (cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] === undefined) {
cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted] = await privateDB.prepare("all", 'SELECT "hashedIP" FROM "sponsorTimes" WHERE "videoID" = ? AND "timeSubmitted = ?',
[videoID, segment.timeSubmitted]) as { hashedIP: HashedIP }[];
} }
//if this isn't their ip, don't send it to them //if this isn't their ip, don't send it to them
return cache.shadowHiddenSegmentIPs[videoID].some((shadowHiddenSegment) => { return cache.shadowHiddenSegmentIPs[videoID][segment.timeSubmitted].some((shadowHiddenSegment) => {
if (cache.userHashedIP === undefined) { if (cache.userHashedIP === undefined) {
//hash the IP only if it's strictly necessary //hash the IP only if it's strictly necessary
cache.userHashedIP = getHash((getIP(req) + config.globalSalt) as IPAddress); cache.userHashedIP = getHash((getIP(req) + config.globalSalt) as IPAddress);
@ -134,7 +136,7 @@ async function getSegmentsFromDBByHash(hashedVideoIDPrefix: VideoIDHash, service
const fetchFromDB = () => db const fetchFromDB = () => db
.prepare( .prepare(
"all", "all",
`SELECT "videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "category", "actionType", "videoDuration", "reputation", "shadowHidden", "hashedVideoID" FROM "sponsorTimes" `SELECT "videoID", "startTime", "endTime", "votes", "locked", "UUID", "userID", "category", "actionType", "videoDuration", "reputation", "shadowHidden", "hashedVideoID", "timeSubmitted" FROM "sponsorTimes"
WHERE "hashedVideoID" LIKE ? AND "service" = ? AND "hidden" = 0 ORDER BY "startTime"`, WHERE "hashedVideoID" LIKE ? AND "service" = ? AND "hidden" = 0 ORDER BY "startTime"`,
[`${hashedVideoIDPrefix}%`, service] [`${hashedVideoIDPrefix}%`, service]
) as Promise<DBSegment[]>; ) as Promise<DBSegment[]>;
@ -150,7 +152,7 @@ async function getSegmentsFromDBByVideoID(videoID: VideoID, service: Service): P
const fetchFromDB = () => db const fetchFromDB = () => db
.prepare( .prepare(
"all", "all",
`SELECT "startTime", "endTime", "votes", "locked", "UUID", "userID", "category", "actionType", "videoDuration", "reputation", "shadowHidden" FROM "sponsorTimes" `SELECT "startTime", "endTime", "votes", "locked", "UUID", "userID", "category", "actionType", "videoDuration", "reputation", "shadowHidden", "timeSubmitted" FROM "sponsorTimes"
WHERE "videoID" = ? AND "service" = ? AND "hidden" = 0 ORDER BY "startTime"`, WHERE "videoID" = ? AND "service" = ? AND "hidden" = 0 ORDER BY "startTime"`,
[videoID, service] [videoID, service]
) as Promise<DBSegment[]>; ) as Promise<DBSegment[]>;

View file

@ -60,6 +60,7 @@ export interface DBSegment {
videoDuration: VideoDuration; videoDuration: VideoDuration;
reputation: number; reputation: number;
hashedVideoID: VideoIDHash; hashedVideoID: VideoIDHash;
timeSubmitted: number;
} }
export interface OverlappingSegmentGroup { export interface OverlappingSegmentGroup {
@ -85,7 +86,7 @@ export interface VideoData {
} }
export interface SegmentCache { export interface SegmentCache {
shadowHiddenSegmentIPs: SBRecord<VideoID, {hashedIP: HashedIP}[]>, shadowHiddenSegmentIPs: SBRecord<VideoID, SBRecord<string, {hashedIP: HashedIP}[]>>,
userHashedIP?: HashedIP userHashedIP?: HashedIP
} }