adjust permissions and use parrallel processing

This commit is contained in:
Ajay
2022-07-29 11:47:28 -04:00
parent 186f07b20c
commit da1ed70c66
4 changed files with 37 additions and 9 deletions

View File

@@ -4,7 +4,7 @@ import { Client, Pool, QueryResult, types } from "pg";
import fs from "fs"; import fs from "fs";
import { CustomPostgresConfig, CustomPostgresReadOnlyConfig } from "../types/config.model"; import { CustomPostgresConfig, CustomPostgresReadOnlyConfig } from "../types/config.model";
import { timeoutPomise, PromiseWithState, savePromiseState, nextFulfilment } from "../utils/promiseTimeout"; import { timeoutPomise, PromiseWithState, savePromiseState, nextFulfilment } from "../utils/promise";
// return numeric (pg_type oid=1700) as float // return numeric (pg_type oid=1700) as float
types.setTypeParser(1700, function(val) { types.setTypeParser(1700, function(val) {

View File

@@ -11,7 +11,7 @@ import { Logger } from "../utils/logger";
import { QueryCacher } from "../utils/queryCacher"; import { QueryCacher } from "../utils/queryCacher";
import { getReputation } from "../utils/reputation"; import { getReputation } from "../utils/reputation";
import { getService } from "../utils/getService"; import { getService } from "../utils/getService";
import { promiseOrTimeout } from "../utils/promiseTimeout"; import { promiseOrTimeout } from "../utils/promise";
async function prepareCategorySegments(req: Request, videoID: VideoID, service: Service, segments: DBSegment[], cache: SegmentCache = { shadowHiddenSegmentIPs: {} }, useCache: boolean): Promise<Segment[]> { async function prepareCategorySegments(req: Request, videoID: VideoID, service: Service, segments: DBSegment[], cache: SegmentCache = { shadowHiddenSegmentIPs: {} }, useCache: boolean): Promise<Segment[]> {

View File

@@ -4,6 +4,7 @@ import { Category } from "../types/segments.model";
import { Feature, HashedUserID } from "../types/user.model"; import { Feature, HashedUserID } from "../types/user.model";
import { hasFeature } from "./features"; import { hasFeature } from "./features";
import { isUserVIP } from "./isUserVIP"; import { isUserVIP } from "./isUserVIP";
import { oneOf } from "./promise";
import { getReputation } from "./reputation"; import { getReputation } from "./reputation";
interface CanSubmitResult { interface CanSubmitResult {
@@ -15,16 +16,18 @@ export async function canSubmit(userID: HashedUserID, category: Category): Promi
switch (category) { switch (category) {
case "chapter": case "chapter":
return { return {
canSubmit: (await isUserVIP(userID)) canSubmit: await oneOf([isUserVIP(userID),
|| (await getReputation(userID)) > config.minReputationToSubmitChapter (async () => (await getReputation(userID)) > config.minReputationToSubmitChapter)(),
|| (await hasFeature(userID, Feature.ChapterSubmitter)) hasFeature(userID, Feature.ChapterSubmitter)
])
}; };
case "filler": case "filler":
return { return {
canSubmit: (await isUserVIP(userID)) canSubmit: await oneOf([isUserVIP(userID),
|| (await getReputation(userID)) > config.minReputationToSubmitFiller (async () => (await getReputation(userID)) > config.minReputationToSubmitFiller)(),
|| (await hasFeature(userID, Feature.FillerSubmitter)) hasFeature(userID, Feature.FillerSubmitter),
|| (await db.prepare("get", `SELECT count(*) as "submissionCount" FROM "sponsorTimes" WHERE "userID" = ? AND category != 'filler' AND "timeSubmitted" < 1654010691000 LIMIT 4`, [userID], { useReplica: true }))?.submissionCount > 3, (async () => (await db.prepare("get", `SELECT count(*) as "submissionCount" FROM "sponsorTimes" WHERE "userID" = ? AND category != 'filler' AND "timeSubmitted" < 1654010691000 LIMIT 4`, [userID], { useReplica: true }))?.submissionCount > 3)()
]),
reason: "Someone has submitted over 1.9 million spam filler submissions and refuses to stop even after talking with them, so we have to restrict it for now. You can request submission access on chat.sponsor.ajay.app/#filler, discord.gg/SponsorBlock or matrix.to/#/#sponsor:ajay.app" reason: "Someone has submitted over 1.9 million spam filler submissions and refuses to stop even after talking with them, so we have to restrict it for now. You can request submission access on chat.sponsor.ajay.app/#filler, discord.gg/SponsorBlock or matrix.to/#/#sponsor:ajay.app"
}; };
} }

View File

@@ -1,3 +1,5 @@
import { Logger } from "./logger";
export class PromiseTimeoutError<T> extends Error { export class PromiseTimeoutError<T> extends Error {
promise?: Promise<T>; promise?: Promise<T>;
@@ -48,3 +50,26 @@ export function savePromiseState<T>(promise: Promise<T>): PromiseWithState<T> {
export function nextFulfilment<T>(promises: PromiseWithState<T>[]): Promise<T> { export function nextFulfilment<T>(promises: PromiseWithState<T>[]): Promise<T> {
return Promise.race(promises.filter((p) => !p.isRejected)); return Promise.race(promises.filter((p) => !p.isRejected));
} }
export function oneOf<T>(promises: Promise<T>[]): Promise<T> {
return new Promise((resolve, reject) => {
let fulfilments = 0;
for (const promise of promises) {
promise.then((result) => {
fulfilments++;
if (result || fulfilments === promises.length) {
resolve(result);
}
}).catch((err) => {
fulfilments++;
if (fulfilments === promises.length) {
reject(err);
} else {
Logger.error(`oneOf ignore error (promise): ${err}`);
}
});
}
});
}