mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-17 13:08:49 +03:00
add addUserAsTempVIP
This commit is contained in:
@@ -46,6 +46,7 @@ import { getChapterNames } from "./routes/getChapterNames";
|
||||
import { postRating } from "./routes/ratings/postRating";
|
||||
import { getRating } from "./routes/ratings/getRating";
|
||||
import { postClearCache as ratingPostClearCache } from "./routes/ratings/postClearCache";
|
||||
import { addUserAsTempVIP } from "./routes/addUserAsTempVIP";
|
||||
|
||||
export function createServer(callback: () => void): Server {
|
||||
// Create a service (the app object is just a callback).
|
||||
@@ -117,6 +118,8 @@ function setupRoutes(router: Router) {
|
||||
|
||||
//Endpoint used to make a user a VIP user with special privileges
|
||||
router.post("/api/addUserAsVIP", addUserAsVIP);
|
||||
//Endpoint to add a user as a temporary VIP
|
||||
router.post("/api/addUserAsTempVIP", addUserAsTempVIP);
|
||||
|
||||
//Gets all the views added up for one userID
|
||||
//Useful to see how much one user has contributed
|
||||
|
||||
71
src/routes/addUserAsTempVIP.ts
Normal file
71
src/routes/addUserAsTempVIP.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import { VideoID } from "../types/segments.model";
|
||||
import { YouTubeAPI } from "../utils/youtubeApi";
|
||||
import { APIVideoInfo } from "../types/youtubeApi.model";
|
||||
import { config } from "../config";
|
||||
import { getHashCache } from "../utils/getHashCache";
|
||||
import { privateDB } from "../databases/databases";
|
||||
import { Request, Response } from "express";
|
||||
import { isUserVIP } from "../utils/isUserVIP";
|
||||
import { HashedUserID } from "../types/user.model";
|
||||
import redis from "../utils/redis";
|
||||
import { tempVIPKey } from "../utils/redisKeys";
|
||||
|
||||
interface AddUserAsTempVIPRequest extends Request {
|
||||
query: {
|
||||
userID: HashedUserID;
|
||||
adminUserID: string;
|
||||
enabled: string;
|
||||
channelVideoID: string;
|
||||
}
|
||||
}
|
||||
|
||||
function getYouTubeVideoInfo(videoID: VideoID, ignoreCache = false): Promise<APIVideoInfo> {
|
||||
return (config.newLeafURLs) ? YouTubeAPI.listVideos(videoID, ignoreCache) : null;
|
||||
}
|
||||
|
||||
const getChannelInfo = async (videoID: VideoID): Promise<{id: string | null, name: string | null }> => {
|
||||
const videoInfo = await getYouTubeVideoInfo(videoID);
|
||||
return {
|
||||
id: videoInfo?.data?.authorId,
|
||||
name: videoInfo?.data?.author
|
||||
};
|
||||
};
|
||||
|
||||
export async function addUserAsTempVIP(req: AddUserAsTempVIPRequest, res: Response): Promise<Response> {
|
||||
const { query: { userID, adminUserID } } = req;
|
||||
|
||||
const enabled = req.query?.enabled === "true";
|
||||
const channelVideoID = req.query?.channelVideoID as VideoID;
|
||||
|
||||
if (!userID || !adminUserID || !channelVideoID ) {
|
||||
// invalid request
|
||||
return res.sendStatus(400);
|
||||
}
|
||||
|
||||
// hash the issuer userID
|
||||
const issuerUserID = await getHashCache(adminUserID);
|
||||
// check if issuer is VIP
|
||||
const issuerIsVIP = await isUserVIP(issuerUserID as HashedUserID);
|
||||
if (!issuerIsVIP) {
|
||||
return res.sendStatus(403);
|
||||
}
|
||||
|
||||
// check to see if this user is already a vip
|
||||
const targetIsVIP = await isUserVIP(userID);
|
||||
if (targetIsVIP) {
|
||||
return res.sendStatus(409);
|
||||
}
|
||||
|
||||
const startTime = Date.now();
|
||||
const dayInSeconds = 86400;
|
||||
const channelInfo = await getChannelInfo(channelVideoID);
|
||||
|
||||
await privateDB.prepare("run", `INSERT INTO "tempVipLog" VALUES (?, ?, ?, ?)`, [adminUserID, userID, + enabled, startTime]);
|
||||
if (enabled) { // add to redis
|
||||
await redis.setAsyncEx(tempVIPKey(userID), channelInfo?.id, dayInSeconds);
|
||||
} else { // delete key
|
||||
await redis.delAsync(tempVIPKey(userID));
|
||||
}
|
||||
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
@@ -322,7 +322,7 @@ export async function voteOnSponsorTime(req: Request, res: Response): Promise<Re
|
||||
|
||||
//check if this user is on the vip list
|
||||
const videoID = await db.prepare("get", `select "videoID" from "sponsorTimes" where "UUID" = ?`, [UUID]);
|
||||
const isTempVIP = await isUserTempVIP(nonAnonUserID, videoID);
|
||||
const isTempVIP = await isUserTempVIP(nonAnonUserID, videoID?.videoID || null);
|
||||
const isVIP = await isUserVIP(nonAnonUserID) || isTempVIP;
|
||||
|
||||
//check if user voting on own submission
|
||||
|
||||
@@ -7,6 +7,7 @@ interface RedisSB {
|
||||
getAsync?(key: string): Promise<{err: Error | null, reply: string | null}>;
|
||||
set(key: string, value: string, callback?: Callback<string | null>): void;
|
||||
setAsync?(key: string, value: string): Promise<{err: Error | null, reply: string | null}>;
|
||||
setAsyncEx?(key: string, value: string, seconds: number): Promise<{err: Error | null, reply: string | null}>;
|
||||
delAsync?(...keys: [string]): Promise<Error | null>;
|
||||
close?(flush?: boolean): void;
|
||||
}
|
||||
@@ -18,6 +19,8 @@ let exportObject: RedisSB = {
|
||||
set: (key, value, callback) => callback(null, undefined),
|
||||
setAsync: () =>
|
||||
new Promise((resolve) => resolve({ err: null, reply: undefined })),
|
||||
setAsyncEx: () =>
|
||||
new Promise((resolve) => resolve({ err: null, reply: undefined })),
|
||||
delAsync: () =>
|
||||
new Promise((resolve) => resolve(null)),
|
||||
};
|
||||
@@ -29,6 +32,7 @@ if (config.redis) {
|
||||
|
||||
exportObject.getAsync = (key) => new Promise((resolve) => client.get(key, (err, reply) => resolve({ err, reply })));
|
||||
exportObject.setAsync = (key, value) => new Promise((resolve) => client.set(key, value, (err, reply) => resolve({ err, reply })));
|
||||
exportObject.setAsyncEx = (key, value, seconds) => new Promise((resolve) => client.setex(key, seconds, value, (err, reply) => resolve({ err, reply })));
|
||||
exportObject.delAsync = (...keys) => new Promise((resolve) => client.del(keys, (err) => resolve(err)));
|
||||
exportObject.close = (flush) => client.end(flush);
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Service, VideoID, VideoIDHash } from "../types/segments.model";
|
||||
import { UserID } from "../types/user.model";
|
||||
import { HashedUserID, UserID } from "../types/user.model";
|
||||
import { HashedValue } from "../types/hash.model";
|
||||
import { Logger } from "./logger";
|
||||
|
||||
@@ -32,5 +32,5 @@ export function shaHashKey(singleIter: HashedValue): string {
|
||||
return `sha.hash.${singleIter}`;
|
||||
}
|
||||
|
||||
export const tempVIPKey = (userID: UserID): string =>
|
||||
export const tempVIPKey = (userID: HashedUserID): string =>
|
||||
`vip.temp.${userID}`;
|
||||
Reference in New Issue
Block a user