mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-26 09:28:34 +03:00
add addUserAsTempVIP
This commit is contained in:
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
|
||||
|
||||
Reference in New Issue
Block a user