Add ability to add manually choose who can submit chapters

This commit is contained in:
Ajay
2022-07-06 00:11:45 -04:00
parent 47f460bb2c
commit c2b0ecd6f6
14 changed files with 292 additions and 22 deletions

11
src/utils/features.ts Normal file
View File

@@ -0,0 +1,11 @@
import { db } from "../databases/databases";
import { Feature, HashedUserID } from "../types/user.model";
import { QueryCacher } from "./queryCacher";
import { userFeatureKey } from "./redisKeys";
export async function hasFeature(userID: HashedUserID, feature: Feature): Promise<boolean> {
return await QueryCacher.get(async () => {
const result = await db.prepare("get", 'SELECT "feature" from "userFeatures" WHERE "userID" = ? AND "feature" = ?', [userID, feature]);
return !!result;
}, userFeatureKey(userID, feature));
}

11
src/utils/permissions.ts Normal file
View File

@@ -0,0 +1,11 @@
import { config } from "../config";
import { Feature, HashedUserID } from "../types/user.model";
import { hasFeature } from "./features";
import { isUserVIP } from "./isUserVIP";
import { getReputation } from "./reputation";
export async function canSubmitChapter(userID: HashedUserID): Promise<boolean> {
return (await isUserVIP(userID))
|| (await getReputation(userID)) > config.minReputationToSubmitChapter
|| (await hasFeature(userID, Feature.ChapterSubmitter));
}

View File

@@ -1,8 +1,8 @@
import redis from "../utils/redis";
import { Logger } from "../utils/logger";
import { skipSegmentsHashKey, skipSegmentsKey, reputationKey, ratingHashKey, skipSegmentGroupsKey } from "./redisKeys";
import { skipSegmentsHashKey, skipSegmentsKey, reputationKey, ratingHashKey, skipSegmentGroupsKey, userFeatureKey } from "./redisKeys";
import { Service, VideoID, VideoIDHash } from "../types/segments.model";
import { UserID } from "../types/user.model";
import { Feature, HashedUserID, UserID } from "../types/user.model";
async function get<T>(fetchFromDB: () => Promise<T>, key: string): Promise<T> {
try {
@@ -90,9 +90,14 @@ function clearRatingCache(videoInfo: { hashedVideoID: VideoIDHash; service: Serv
}
}
function clearFeatureCache(userID: HashedUserID, feature: Feature): void {
redis.del(userFeatureKey(userID, feature)).catch((err) => Logger.error(err));
}
export const QueryCacher = {
get,
getAndSplit,
clearSegmentCache,
clearRatingCache
clearRatingCache,
clearFeatureCache
};

View File

@@ -1,5 +1,5 @@
import { Service, VideoID, VideoIDHash } from "../types/segments.model";
import { HashedUserID, UserID } from "../types/user.model";
import { Feature, HashedUserID, UserID } from "../types/user.model";
import { HashedValue } from "../types/hash.model";
import { Logger } from "./logger";
@@ -36,4 +36,8 @@ export function shaHashKey(singleIter: HashedValue): string {
}
export const tempVIPKey = (userID: HashedUserID): string =>
`vip.temp.${userID}`;
`vip.temp.${userID}`;
export function userFeatureKey (userID: HashedUserID, feature: Feature): string {
return `user.${userID}.feature.${feature}`;
}