mirror of
https://github.com/ajayyy/SponsorBlockServer.git
synced 2025-12-18 13:38:22 +03:00
Switch to postgres + promises
This commit is contained in:
@@ -27,14 +27,14 @@ export async function addUserAsVIP(req: Request, res: Response) {
|
||||
}
|
||||
|
||||
//check to see if this user is already a vip
|
||||
const row = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]);
|
||||
const row = await db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]);
|
||||
|
||||
if (enabled && row.userCount == 0) {
|
||||
//add them to the vip list
|
||||
db.prepare('run', "INSERT INTO vipUsers VALUES(?)", [userID]);
|
||||
await db.prepare('run', "INSERT INTO vipUsers VALUES(?)", [userID]);
|
||||
} else if (!enabled && row.userCount > 0) {
|
||||
//remove them from the shadow ban list
|
||||
db.prepare('run', "DELETE FROM vipUsers WHERE userID = ?", [userID]);
|
||||
await db.prepare('run', "DELETE FROM vipUsers WHERE userID = ?", [userID]);
|
||||
}
|
||||
|
||||
res.sendStatus(200);
|
||||
|
||||
@@ -3,7 +3,7 @@ import {isUserVIP} from '../utils/isUserVIP';
|
||||
import {getHash} from '../utils/getHash';
|
||||
import {db} from '../databases/databases';
|
||||
|
||||
export function deleteNoSegments(req: Request, res: Response) {
|
||||
export async function deleteNoSegments(req: Request, res: Response) {
|
||||
// Collect user input data
|
||||
const videoID = req.body.videoID;
|
||||
let userID = req.body.userID;
|
||||
@@ -24,7 +24,7 @@ export function deleteNoSegments(req: Request, res: Response) {
|
||||
|
||||
// Check if user is VIP
|
||||
userID = getHash(userID);
|
||||
const userIsVIP = isUserVIP(userID);
|
||||
const userIsVIP = await isUserVIP(userID);
|
||||
|
||||
if (!userIsVIP) {
|
||||
res.status(403).json({
|
||||
@@ -33,11 +33,13 @@ export function deleteNoSegments(req: Request, res: Response) {
|
||||
return;
|
||||
}
|
||||
|
||||
db.prepare("all", 'SELECT * FROM noSegments WHERE videoID = ?', [videoID]).filter((entry: any) => {
|
||||
const entries = (await db.prepare("all", 'SELECT * FROM noSegments WHERE videoID = ?', [videoID])).filter((entry: any) => {
|
||||
return (categories.indexOf(entry.category) !== -1);
|
||||
}).forEach((entry: any) => {
|
||||
db.prepare('run', 'DELETE FROM noSegments WHERE videoID = ? AND category = ?', [videoID, entry.category]);
|
||||
});
|
||||
|
||||
for (const entry of entries) {
|
||||
await db.prepare('run', 'DELETE FROM noSegments WHERE videoID = ? AND category = ?', [videoID, entry.category]);
|
||||
}
|
||||
|
||||
res.status(200).json({message: 'Removed no segments entrys for video ' + videoID});
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import {db} from '../databases/databases';
|
||||
import {Request, Response} from 'express';
|
||||
|
||||
export function getDaysSavedFormatted(req: Request, res: Response) {
|
||||
let row = db.prepare('get', "SELECT SUM((endTime - startTime) / 60 / 60 / 24 * views) as daysSaved from sponsorTimes where shadowHidden != 1", []);
|
||||
export async function getDaysSavedFormatted(req: Request, res: Response) {
|
||||
let row = await db.prepare('get', "SELECT SUM((endTime - startTime) / 60 / 60 / 24 * views) as daysSaved from sponsorTimes where shadowHidden != 1", []);
|
||||
|
||||
if (row !== undefined) {
|
||||
//send this result
|
||||
|
||||
@@ -4,7 +4,7 @@ import {isUserVIP} from '../utils/isUserVIP';
|
||||
import {Request, Response} from 'express';
|
||||
import { HashedUserID, UserID } from '../types/user.model';
|
||||
|
||||
export function getIsUserVIP(req: Request, res: Response): void {
|
||||
export async function getIsUserVIP(req: Request, res: Response): Promise<void> {
|
||||
const userID = req.query.userID as UserID;
|
||||
|
||||
if (userID == undefined) {
|
||||
@@ -17,7 +17,7 @@ export function getIsUserVIP(req: Request, res: Response): void {
|
||||
const hashedUserID: HashedUserID = getHash(userID);
|
||||
|
||||
try {
|
||||
let vipState = isUserVIP(hashedUserID);
|
||||
let vipState = await isUserVIP(hashedUserID);
|
||||
res.status(200).json({
|
||||
hashedUserID: hashedUserID,
|
||||
vip: vipState,
|
||||
|
||||
@@ -2,7 +2,7 @@ import {db} from '../databases/databases';
|
||||
import {Request, Response} from 'express';
|
||||
import {getHash} from '../utils/getHash';
|
||||
|
||||
export function getSavedTimeForUser(req: Request, res: Response) {
|
||||
export async function getSavedTimeForUser(req: Request, res: Response) {
|
||||
let userID = req.query.userID as string;
|
||||
|
||||
if (userID == undefined) {
|
||||
@@ -15,7 +15,7 @@ export function getSavedTimeForUser(req: Request, res: Response) {
|
||||
userID = getHash(userID);
|
||||
|
||||
try {
|
||||
let row = db.prepare("get", "SELECT SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE userID = ? AND votes > -1 AND shadowHidden != 1 ", [userID]);
|
||||
let row = await db.prepare("get", "SELECT SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE userID = ? AND votes > -1 AND shadowHidden != 1 ", [userID]);
|
||||
|
||||
if (row.minutesSaved != null) {
|
||||
res.send({
|
||||
|
||||
@@ -11,8 +11,8 @@ import { Logger } from '../utils/logger';
|
||||
import redis from '../utils/redis';
|
||||
|
||||
|
||||
function prepareCategorySegments(req: Request, videoID: VideoID, category: Category, segments: DBSegment[], cache: SegmentCache = {shadowHiddenSegmentIPs: {}}): Segment[] {
|
||||
const filteredSegments = segments.filter((segment) => {
|
||||
async function prepareCategorySegments(req: Request, videoID: VideoID, category: Category, segments: DBSegment[], cache: SegmentCache = {shadowHiddenSegmentIPs: {}}): Promise<Segment[]> {
|
||||
const shouldFilter: boolean[] = await Promise.all(segments.map(async (segment) => {
|
||||
if (segment.votes < -1) {
|
||||
return false; //too untrustworthy, just ignore it
|
||||
}
|
||||
@@ -24,7 +24,7 @@ function prepareCategorySegments(req: Request, videoID: VideoID, category: Categ
|
||||
}
|
||||
|
||||
if (cache.shadowHiddenSegmentIPs[videoID] === undefined) {
|
||||
cache.shadowHiddenSegmentIPs[videoID] = privateDB.prepare('all', 'SELECT hashedIP FROM sponsorTimes WHERE videoID = ?', [videoID]) as { hashedIP: HashedIP }[];
|
||||
cache.shadowHiddenSegmentIPs[videoID] = await privateDB.prepare('all', 'SELECT hashedIP FROM sponsorTimes WHERE videoID = ?', [videoID]) as { hashedIP: HashedIP }[];
|
||||
}
|
||||
|
||||
//if this isn't their ip, don't send it to them
|
||||
@@ -36,7 +36,9 @@ function prepareCategorySegments(req: Request, videoID: VideoID, category: Categ
|
||||
|
||||
return shadowHiddenSegment.hashedIP === cache.userHashedIP;
|
||||
});
|
||||
});
|
||||
}));
|
||||
|
||||
const filteredSegments = segments.filter((_, index) => shouldFilter[index]);
|
||||
|
||||
return chooseSegments(filteredSegments).map((chosenSegment) => ({
|
||||
category,
|
||||
@@ -45,17 +47,17 @@ function prepareCategorySegments(req: Request, videoID: VideoID, category: Categ
|
||||
}));
|
||||
}
|
||||
|
||||
function getSegmentsByVideoID(req: Request, videoID: string, categories: Category[]): Segment[] {
|
||||
async function getSegmentsByVideoID(req: Request, videoID: string, categories: Category[]): Promise<Segment[]> {
|
||||
const cache: SegmentCache = {shadowHiddenSegmentIPs: {}};
|
||||
const segments: Segment[] = [];
|
||||
|
||||
try {
|
||||
const segmentsByCategory: SBRecord<Category, DBSegment[]> = db
|
||||
const segmentsByCategory: SBRecord<Category, DBSegment[]> = (await db
|
||||
.prepare(
|
||||
'all',
|
||||
`SELECT startTime, endTime, votes, locked, UUID, category, shadowHidden FROM sponsorTimes WHERE videoID = ? AND category IN (${Array(categories.length).fill('?').join()}) ORDER BY startTime`,
|
||||
[videoID, categories]
|
||||
).reduce((acc: SBRecord<Category, DBSegment[]>, segment: DBSegment) => {
|
||||
)).reduce((acc: SBRecord<Category, DBSegment[]>, segment: DBSegment) => {
|
||||
acc[segment.category] = acc[segment.category] || [];
|
||||
acc[segment.category].push(segment);
|
||||
|
||||
@@ -63,7 +65,7 @@ function getSegmentsByVideoID(req: Request, videoID: string, categories: Categor
|
||||
}, {});
|
||||
|
||||
for (const [category, categorySegments] of Object.entries(segmentsByCategory)) {
|
||||
segments.push(...prepareCategorySegments(req, videoID as VideoID, category as Category, categorySegments, cache));
|
||||
segments.push(...(await prepareCategorySegments(req, videoID as VideoID, category as Category, categorySegments, cache)));
|
||||
}
|
||||
|
||||
return segments;
|
||||
@@ -75,19 +77,19 @@ function getSegmentsByVideoID(req: Request, videoID: string, categories: Categor
|
||||
}
|
||||
}
|
||||
|
||||
function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, categories: Category[]): SBRecord<VideoID, VideoData> {
|
||||
async function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, categories: Category[]): Promise<SBRecord<VideoID, VideoData>> {
|
||||
const cache: SegmentCache = {shadowHiddenSegmentIPs: {}};
|
||||
const segments: SBRecord<VideoID, VideoData> = {};
|
||||
|
||||
try {
|
||||
type SegmentWithHashPerVideoID = SBRecord<VideoID, {hash: VideoIDHash, segmentPerCategory: SBRecord<Category, DBSegment[]>}>;
|
||||
|
||||
const segmentPerVideoID: SegmentWithHashPerVideoID = db
|
||||
const segmentPerVideoID: SegmentWithHashPerVideoID = (await db
|
||||
.prepare(
|
||||
'all',
|
||||
`SELECT videoID, startTime, endTime, votes, locked, UUID, category, shadowHidden, hashedVideoID FROM sponsorTimes WHERE hashedVideoID LIKE ? AND category IN (${Array(categories.length).fill('?').join()}) ORDER BY startTime`,
|
||||
[hashedVideoIDPrefix + '%', categories]
|
||||
).reduce((acc: SegmentWithHashPerVideoID, segment: DBSegment) => {
|
||||
)).reduce((acc: SegmentWithHashPerVideoID, segment: DBSegment) => {
|
||||
acc[segment.videoID] = acc[segment.videoID] || {
|
||||
hash: segment.hashedVideoID,
|
||||
segmentPerCategory: {},
|
||||
@@ -107,7 +109,7 @@ function getSegmentsByHash(req: Request, hashedVideoIDPrefix: VideoIDHash, categ
|
||||
};
|
||||
|
||||
for (const [category, segmentPerCategory] of Object.entries(videoData.segmentPerCategory)) {
|
||||
segments[videoID].segments.push(...prepareCategorySegments(req, videoID as VideoID, category as Category, segmentPerCategory, cache));
|
||||
segments[videoID].segments.push(...(await prepareCategorySegments(req, videoID as VideoID, category as Category, segmentPerCategory, cache)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -241,7 +243,7 @@ async function handleGetSegments(req: Request, res: Response): Promise<Segment[]
|
||||
}
|
||||
}
|
||||
|
||||
const segments = getSegmentsByVideoID(req, videoID, categories);
|
||||
const segments = await getSegmentsByVideoID(req, videoID, categories);
|
||||
|
||||
if (segments === null || segments === undefined) {
|
||||
res.sendStatus(500);
|
||||
|
||||
@@ -6,60 +6,58 @@ import {Request, Response} from 'express';
|
||||
const MILLISECONDS_IN_MINUTE = 60000;
|
||||
const getTopUsersWithCache = createMemoryCache(generateTopUsersStats, config.getTopUsersCacheTimeMinutes * MILLISECONDS_IN_MINUTE);
|
||||
|
||||
function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boolean = false) {
|
||||
return new Promise((resolve) => {
|
||||
const userNames = [];
|
||||
const viewCounts = [];
|
||||
const totalSubmissions = [];
|
||||
const minutesSaved = [];
|
||||
const categoryStats: any[] = categoryStatsEnabled ? [] : undefined;
|
||||
async function generateTopUsersStats(sortBy: string, categoryStatsEnabled: boolean = false) {
|
||||
const userNames = [];
|
||||
const viewCounts = [];
|
||||
const totalSubmissions = [];
|
||||
const minutesSaved = [];
|
||||
const categoryStats: any[] = categoryStatsEnabled ? [] : undefined;
|
||||
|
||||
let additionalFields = '';
|
||||
let additionalFields = '';
|
||||
if (categoryStatsEnabled) {
|
||||
additionalFields += "SUM(CASE WHEN category = 'sponsor' THEN 1 ELSE 0 END) as categorySponsor, " +
|
||||
"SUM(CASE WHEN category = 'intro' THEN 1 ELSE 0 END) as categorySumIntro, " +
|
||||
"SUM(CASE WHEN category = 'outro' THEN 1 ELSE 0 END) as categorySumOutro, " +
|
||||
"SUM(CASE WHEN category = 'interaction' THEN 1 ELSE 0 END) as categorySumInteraction, " +
|
||||
"SUM(CASE WHEN category = 'selfpromo' THEN 1 ELSE 0 END) as categorySelfpromo, " +
|
||||
"SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as categoryMusicOfftopic, ";
|
||||
}
|
||||
|
||||
const rows = await db.prepare('all', "SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount," +
|
||||
"SUM((sponsorTimes.endTime - sponsorTimes.startTime) / 60 * sponsorTimes.views) as minutesSaved, " +
|
||||
"SUM(votes) as userVotes, " +
|
||||
additionalFields +
|
||||
"IFNULL(userNames.userName, sponsorTimes.userID) as userName FROM sponsorTimes LEFT JOIN userNames ON sponsorTimes.userID=userNames.userID " +
|
||||
"LEFT JOIN privateDB.shadowBannedUsers ON sponsorTimes.userID=privateDB.shadowBannedUsers.userID " +
|
||||
"WHERE sponsorTimes.votes > -1 AND sponsorTimes.shadowHidden != 1 AND privateDB.shadowBannedUsers.userID IS NULL " +
|
||||
"GROUP BY IFNULL(userName, sponsorTimes.userID) HAVING userVotes > 20 " +
|
||||
"ORDER BY " + sortBy + " DESC LIMIT 100", []);
|
||||
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
userNames[i] = rows[i].userName;
|
||||
|
||||
viewCounts[i] = rows[i].viewCount;
|
||||
totalSubmissions[i] = rows[i].totalSubmissions;
|
||||
minutesSaved[i] = rows[i].minutesSaved;
|
||||
if (categoryStatsEnabled) {
|
||||
additionalFields += "SUM(CASE WHEN category = 'sponsor' THEN 1 ELSE 0 END) as categorySponsor, " +
|
||||
"SUM(CASE WHEN category = 'intro' THEN 1 ELSE 0 END) as categorySumIntro, " +
|
||||
"SUM(CASE WHEN category = 'outro' THEN 1 ELSE 0 END) as categorySumOutro, " +
|
||||
"SUM(CASE WHEN category = 'interaction' THEN 1 ELSE 0 END) as categorySumInteraction, " +
|
||||
"SUM(CASE WHEN category = 'selfpromo' THEN 1 ELSE 0 END) as categorySelfpromo, " +
|
||||
"SUM(CASE WHEN category = 'music_offtopic' THEN 1 ELSE 0 END) as categoryMusicOfftopic, ";
|
||||
categoryStats[i] = [
|
||||
rows[i].categorySponsor,
|
||||
rows[i].categorySumIntro,
|
||||
rows[i].categorySumOutro,
|
||||
rows[i].categorySumInteraction,
|
||||
rows[i].categorySelfpromo,
|
||||
rows[i].categoryMusicOfftopic,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
const rows = db.prepare('all', "SELECT COUNT(*) as totalSubmissions, SUM(views) as viewCount," +
|
||||
"SUM((sponsorTimes.endTime - sponsorTimes.startTime) / 60 * sponsorTimes.views) as minutesSaved, " +
|
||||
"SUM(votes) as userVotes, " +
|
||||
additionalFields +
|
||||
"IFNULL(userNames.userName, sponsorTimes.userID) as userName FROM sponsorTimes LEFT JOIN userNames ON sponsorTimes.userID=userNames.userID " +
|
||||
"LEFT JOIN privateDB.shadowBannedUsers ON sponsorTimes.userID=privateDB.shadowBannedUsers.userID " +
|
||||
"WHERE sponsorTimes.votes > -1 AND sponsorTimes.shadowHidden != 1 AND privateDB.shadowBannedUsers.userID IS NULL " +
|
||||
"GROUP BY IFNULL(userName, sponsorTimes.userID) HAVING userVotes > 20 " +
|
||||
"ORDER BY " + sortBy + " DESC LIMIT 100", []);
|
||||
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
userNames[i] = rows[i].userName;
|
||||
|
||||
viewCounts[i] = rows[i].viewCount;
|
||||
totalSubmissions[i] = rows[i].totalSubmissions;
|
||||
minutesSaved[i] = rows[i].minutesSaved;
|
||||
if (categoryStatsEnabled) {
|
||||
categoryStats[i] = [
|
||||
rows[i].categorySponsor,
|
||||
rows[i].categorySumIntro,
|
||||
rows[i].categorySumOutro,
|
||||
rows[i].categorySumInteraction,
|
||||
rows[i].categorySelfpromo,
|
||||
rows[i].categoryMusicOfftopic,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
resolve({
|
||||
userNames,
|
||||
viewCounts,
|
||||
totalSubmissions,
|
||||
minutesSaved,
|
||||
categoryStats,
|
||||
});
|
||||
});
|
||||
return {
|
||||
userNames,
|
||||
viewCounts,
|
||||
totalSubmissions,
|
||||
minutesSaved,
|
||||
categoryStats,
|
||||
};
|
||||
}
|
||||
|
||||
export async function getTopUsers(req: Request, res: Response) {
|
||||
|
||||
@@ -13,8 +13,8 @@ let apiUsersCache = 0;
|
||||
|
||||
let lastUserCountCheck = 0;
|
||||
|
||||
export function getTotalStats(req: Request, res: Response) {
|
||||
let row = db.prepare('get', "SELECT COUNT(DISTINCT userID) as userCount, COUNT(*) as totalSubmissions, " +
|
||||
export async function getTotalStats(req: Request, res: Response) {
|
||||
let row = await db.prepare('get', "SELECT COUNT(DISTINCT userID) as userCount, COUNT(*) as totalSubmissions, " +
|
||||
"SUM(views) as viewCount, SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE shadowHidden != 1 AND votes >= 0", []);
|
||||
|
||||
if (row !== undefined) {
|
||||
|
||||
@@ -3,9 +3,9 @@ import {getHash} from '../utils/getHash';
|
||||
import {Request, Response} from 'express';
|
||||
import {Logger} from '../utils/logger'
|
||||
|
||||
function dbGetSubmittedSegmentSummary(userID: string): any {
|
||||
async function dbGetSubmittedSegmentSummary(userID: string): Promise<{ minutesSaved: number, segmentCount: number }> {
|
||||
try {
|
||||
let row = db.prepare("get", "SELECT SUM(((endTime - startTime) / 60) * views) as minutesSaved, count(*) as segmentCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]);
|
||||
let row = await db.prepare("get", "SELECT SUM(((endTime - startTime) / 60) * views) as minutesSaved, count(*) as segmentCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]);
|
||||
if (row.minutesSaved != null) {
|
||||
return {
|
||||
minutesSaved: row.minutesSaved,
|
||||
@@ -18,13 +18,13 @@ function dbGetSubmittedSegmentSummary(userID: string): any {
|
||||
};
|
||||
}
|
||||
} catch (err) {
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function dbGetUsername(userID: string) {
|
||||
async function dbGetUsername(userID: string) {
|
||||
try {
|
||||
let row = db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]);
|
||||
let row = await db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]);
|
||||
if (row !== undefined) {
|
||||
return row.userName;
|
||||
} else {
|
||||
@@ -36,9 +36,9 @@ function dbGetUsername(userID: string) {
|
||||
}
|
||||
}
|
||||
|
||||
function dbGetViewsForUser(userID: string) {
|
||||
async function dbGetViewsForUser(userID: string) {
|
||||
try {
|
||||
let row = db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]);
|
||||
let row = await db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ? AND votes > -2 AND shadowHidden != 1", [userID]);
|
||||
//increase the view count by one
|
||||
if (row.viewCount != null) {
|
||||
return row.viewCount;
|
||||
@@ -50,9 +50,9 @@ function dbGetViewsForUser(userID: string) {
|
||||
}
|
||||
}
|
||||
|
||||
function dbGetWarningsForUser(userID: string): number {
|
||||
async function dbGetWarningsForUser(userID: string): Promise<number> {
|
||||
try {
|
||||
let rows = db.prepare('all', "SELECT * FROM warnings WHERE userID = ?", [userID]);
|
||||
let rows = await db.prepare('all', "SELECT * FROM warnings WHERE userID = ?", [userID]);
|
||||
return rows.length;
|
||||
} catch (err) {
|
||||
Logger.error('Couldn\'t get warnings for user ' + userID + '. returning 0');
|
||||
@@ -60,7 +60,7 @@ function dbGetWarningsForUser(userID: string): number {
|
||||
}
|
||||
}
|
||||
|
||||
export function getUserInfo(req: Request, res: Response) {
|
||||
export async function getUserInfo(req: Request, res: Response) {
|
||||
let userID = req.query.userID as string;
|
||||
|
||||
if (userID == undefined) {
|
||||
@@ -72,13 +72,17 @@ export function getUserInfo(req: Request, res: Response) {
|
||||
//hash the userID
|
||||
userID = getHash(userID);
|
||||
|
||||
const segmentsSummary = dbGetSubmittedSegmentSummary(userID);
|
||||
res.send({
|
||||
userID,
|
||||
userName: dbGetUsername(userID),
|
||||
minutesSaved: segmentsSummary.minutesSaved,
|
||||
segmentCount: segmentsSummary.segmentCount,
|
||||
viewCount: dbGetViewsForUser(userID),
|
||||
warnings: dbGetWarningsForUser(userID),
|
||||
});
|
||||
const segmentsSummary = await dbGetSubmittedSegmentSummary(userID);
|
||||
if (segmentsSummary) {
|
||||
res.send({
|
||||
userID,
|
||||
userName: await dbGetUsername(userID),
|
||||
minutesSaved: segmentsSummary.minutesSaved,
|
||||
segmentCount: segmentsSummary.segmentCount,
|
||||
viewCount: await dbGetViewsForUser(userID),
|
||||
warnings: await dbGetWarningsForUser(userID),
|
||||
});
|
||||
} else {
|
||||
res.status(400).send();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ import {getHash} from '../utils/getHash';
|
||||
import {Logger} from '../utils/logger';
|
||||
import {Request, Response} from 'express';
|
||||
|
||||
export function getUsername(req: Request, res: Response) {
|
||||
export async function getUsername(req: Request, res: Response) {
|
||||
let userID = req.query.userID as string;
|
||||
|
||||
if (userID == undefined) {
|
||||
@@ -16,7 +16,7 @@ export function getUsername(req: Request, res: Response) {
|
||||
userID = getHash(userID);
|
||||
|
||||
try {
|
||||
let row = db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]);
|
||||
let row = await db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]);
|
||||
|
||||
if (row !== undefined) {
|
||||
res.send({
|
||||
|
||||
@@ -3,7 +3,7 @@ import {Request, Response} from 'express';
|
||||
import {getHash} from '../utils/getHash';
|
||||
import {Logger} from '../utils/logger';
|
||||
|
||||
export function getViewsForUser(req: Request, res: Response) {
|
||||
export async function getViewsForUser(req: Request, res: Response) {
|
||||
let userID = req.query.userID as string;
|
||||
|
||||
if (userID == undefined) {
|
||||
@@ -16,7 +16,7 @@ export function getViewsForUser(req: Request, res: Response) {
|
||||
userID = getHash(userID);
|
||||
|
||||
try {
|
||||
let row = db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ?", [userID]);
|
||||
let row = await db.prepare('get', "SELECT SUM(views) as viewCount FROM sponsorTimes WHERE userID = ?", [userID]);
|
||||
|
||||
//increase the view count by one
|
||||
if (row.viewCount != null) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import {isUserVIP} from '../utils/isUserVIP';
|
||||
import {db} from '../databases/databases';
|
||||
import {Request, Response} from 'express';
|
||||
|
||||
export function postNoSegments(req: Request, res: Response) {
|
||||
export async function postNoSegments(req: Request, res: Response) {
|
||||
// Collect user input data
|
||||
let videoID = req.body.videoID;
|
||||
let userID = req.body.userID;
|
||||
@@ -25,7 +25,7 @@ export function postNoSegments(req: Request, res: Response) {
|
||||
|
||||
// Check if user is VIP
|
||||
userID = getHash(userID);
|
||||
let userIsVIP = isUserVIP(userID);
|
||||
let userIsVIP = await isUserVIP(userID);
|
||||
|
||||
if (!userIsVIP) {
|
||||
res.status(403).json({
|
||||
@@ -35,7 +35,7 @@ export function postNoSegments(req: Request, res: Response) {
|
||||
}
|
||||
|
||||
// Get existing no segment markers
|
||||
let noSegmentList = db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID]);
|
||||
let noSegmentList = await db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID]);
|
||||
if (!noSegmentList || noSegmentList.length === 0) {
|
||||
noSegmentList = [];
|
||||
} else {
|
||||
@@ -57,9 +57,9 @@ export function postNoSegments(req: Request, res: Response) {
|
||||
});
|
||||
|
||||
// create database entry
|
||||
categoriesToMark.forEach((category) => {
|
||||
for (const category of categoriesToMark) {
|
||||
try {
|
||||
db.prepare('run', "INSERT INTO noSegments (videoID, userID, category) VALUES(?, ?, ?)", [videoID, userID, category]);
|
||||
await db.prepare('run', "INSERT INTO noSegments (videoID, userID, category) VALUES(?, ?, ?)", [videoID, userID, category]);
|
||||
} catch (err) {
|
||||
Logger.error("Error submitting 'noSegment' marker for category '" + category + "' for video '" + videoID + "'");
|
||||
Logger.error(err);
|
||||
@@ -67,7 +67,7 @@ export function postNoSegments(req: Request, res: Response) {
|
||||
message: "Internal Server Error: Could not write marker to the database.",
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
res.status(200).json({
|
||||
submitted: categoriesToMark,
|
||||
|
||||
@@ -45,7 +45,7 @@ function shiftSegment(segment: any, shift: { startTime: any; endTime: any }) {
|
||||
return {action: ACTION_NONE, segment};
|
||||
}
|
||||
|
||||
export function postSegmentShift(req: Request, res: Response): Response {
|
||||
export async function postSegmentShift(req: Request, res: Response): Promise<Response> {
|
||||
// Collect user input data
|
||||
const videoID = req.body.videoID;
|
||||
const startTime = req.body.startTime;
|
||||
@@ -66,7 +66,7 @@ export function postSegmentShift(req: Request, res: Response): Response {
|
||||
|
||||
// Check if user is VIP
|
||||
userID = getHash(userID);
|
||||
const userIsVIP = isUserVIP(userID);
|
||||
const userIsVIP = await isUserVIP(userID);
|
||||
|
||||
if (!userIsVIP) {
|
||||
res.status(403).json({
|
||||
@@ -76,22 +76,23 @@ export function postSegmentShift(req: Request, res: Response): Response {
|
||||
}
|
||||
|
||||
try {
|
||||
const segments = db.prepare('all', 'SELECT startTime, endTime, UUID FROM sponsorTimes WHERE videoID = ?', [videoID]);
|
||||
const segments = await db.prepare('all', 'SELECT startTime, endTime, UUID FROM sponsorTimes WHERE videoID = ?', [videoID]);
|
||||
const shift = {
|
||||
startTime,
|
||||
endTime,
|
||||
};
|
||||
segments.forEach((segment: any) => {
|
||||
|
||||
for (const segment of segments) {
|
||||
const result = shiftSegment(segment, shift);
|
||||
switch (result.action) {
|
||||
case ACTION_UPDATE:
|
||||
db.prepare('run', 'UPDATE sponsorTimes SET startTime = ?, endTime = ? WHERE UUID = ?', [result.segment.startTime, result.segment.endTime, result.segment.UUID]);
|
||||
await db.prepare('run', 'UPDATE sponsorTimes SET startTime = ?, endTime = ? WHERE UUID = ?', [result.segment.startTime, result.segment.endTime, result.segment.UUID]);
|
||||
break;
|
||||
case ACTION_REMOVE:
|
||||
db.prepare('run', 'UPDATE sponsorTimes SET startTime = ?, endTime = ?, votes = -2 WHERE UUID = ?', [result.segment.startTime, result.segment.endTime, result.segment.UUID]);
|
||||
await db.prepare('run', 'UPDATE sponsorTimes SET startTime = ?, endTime = ?, votes = -2 WHERE UUID = ?', [result.segment.startTime, result.segment.endTime, result.segment.UUID]);
|
||||
break;
|
||||
}
|
||||
});
|
||||
};
|
||||
} catch (err) {
|
||||
Logger.error(err);
|
||||
res.sendStatus(500);
|
||||
|
||||
@@ -15,8 +15,8 @@ import { skipSegmentsKey } from '../middleware/redisKeys';
|
||||
import redis from '../utils/redis';
|
||||
|
||||
|
||||
function sendWebhookNotification(userID: string, videoID: string, UUID: string, submissionCount: number, youtubeData: any, {submissionStart, submissionEnd}: { submissionStart: number; submissionEnd: number; }, segmentInfo: any) {
|
||||
const row = db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]);
|
||||
async function sendWebhookNotification(userID: string, videoID: string, UUID: string, submissionCount: number, youtubeData: any, {submissionStart, submissionEnd}: { submissionStart: number; submissionEnd: number; }, segmentInfo: any) {
|
||||
const row = await db.prepare('get', "SELECT userName FROM userNames WHERE userID = ?", [userID]);
|
||||
const userName = row !== undefined ? row.userName : null;
|
||||
const video = youtubeData.items[0];
|
||||
|
||||
@@ -45,9 +45,9 @@ function sendWebhookNotification(userID: string, videoID: string, UUID: string,
|
||||
});
|
||||
}
|
||||
|
||||
function sendWebhooks(userID: string, videoID: string, UUID: string, segmentInfo: any) {
|
||||
async function sendWebhooks(userID: string, videoID: string, UUID: string, segmentInfo: any) {
|
||||
if (config.youtubeAPIKey !== null) {
|
||||
const userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]);
|
||||
const userSubmissionCountRow = await db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [userID]);
|
||||
|
||||
YouTubeAPI.listVideos(videoID, (err: any, data: any) => {
|
||||
if (err || data.items.length === 0) {
|
||||
@@ -105,8 +105,8 @@ function sendWebhooks(userID: string, videoID: string, UUID: string, segmentInfo
|
||||
}
|
||||
}
|
||||
|
||||
function sendWebhooksNB(userID: string, videoID: string, UUID: string, startTime: number, endTime: number, category: string, probability: number, ytData: any) {
|
||||
const submissionInfoRow = db.prepare('get', "SELECT " +
|
||||
async function sendWebhooksNB(userID: string, videoID: string, UUID: string, startTime: number, endTime: number, category: string, probability: number, ytData: any) {
|
||||
const submissionInfoRow = await db.prepare('get', "SELECT " +
|
||||
"(select count(1) from sponsorTimes where userID = ?) count, " +
|
||||
"(select count(1) from sponsorTimes where userID = ? and votes <= -2) disregarded, " +
|
||||
"coalesce((select userName FROM userNames WHERE userID = ?), ?) userName",
|
||||
@@ -304,20 +304,20 @@ export async function postSkipSegments(req: Request, res: Response) {
|
||||
|
||||
const MILLISECONDS_IN_HOUR = 3600000;
|
||||
const now = Date.now();
|
||||
const warningsCount = db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1",
|
||||
const warningsCount = (await db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1",
|
||||
[userID, Math.floor(now - (config.hoursAfterWarningExpires * MILLISECONDS_IN_HOUR))],
|
||||
).count;
|
||||
)).count;
|
||||
|
||||
if (warningsCount >= config.maxNumberOfActiveWarnings) {
|
||||
return res.status(403).send('Submission rejected due to a warning from a moderator. This means that we noticed you were making some common mistakes that are not malicious, and we just want to clarify the rules. Could you please send a message in Discord or Matrix so we can further help you?');
|
||||
}
|
||||
|
||||
const noSegmentList = db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID]).map((list: any) => {
|
||||
const noSegmentList = (await db.prepare('all', 'SELECT category from noSegments where videoID = ?', [videoID])).map((list: any) => {
|
||||
return list.category;
|
||||
});
|
||||
|
||||
//check if this user is on the vip list
|
||||
const isVIP = db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID]).userCount > 0;
|
||||
const isVIP = (await db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [userID])).userCount > 0;
|
||||
|
||||
const decreaseVotes = 0;
|
||||
|
||||
@@ -366,7 +366,7 @@ export async function postSkipSegments(req: Request, res: Response) {
|
||||
}
|
||||
|
||||
//check if this info has already been submitted before
|
||||
const duplicateCheck2Row = db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE startTime = ? " +
|
||||
const duplicateCheck2Row = await db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE startTime = ? " +
|
||||
"and endTime = ? and category = ? and videoID = ?", [startTime, endTime, segments[i].category, videoID]);
|
||||
if (duplicateCheck2Row.count > 0) {
|
||||
res.sendStatus(409);
|
||||
@@ -401,7 +401,7 @@ export async function postSkipSegments(req: Request, res: Response) {
|
||||
// Disable IP ratelimiting for now
|
||||
if (false) {
|
||||
//check to see if this ip has submitted too many sponsors today
|
||||
const rateLimitCheckRow = privateDB.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE hashedIP = ? AND videoID = ? AND timeSubmitted > ?", [hashedIP, videoID, yesterday]);
|
||||
const rateLimitCheckRow = await privateDB.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE hashedIP = ? AND videoID = ? AND timeSubmitted > ?", [hashedIP, videoID, yesterday]);
|
||||
|
||||
if (rateLimitCheckRow.count >= 10) {
|
||||
//too many sponsors for the same video from the same ip address
|
||||
@@ -414,7 +414,7 @@ export async function postSkipSegments(req: Request, res: Response) {
|
||||
// Disable max submissions for now
|
||||
if (false) {
|
||||
//check to see if the user has already submitted sponsors for this video
|
||||
const duplicateCheckRow = db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE userID = ? and videoID = ?", [userID, videoID]);
|
||||
const duplicateCheckRow = await db.prepare('get', "SELECT COUNT(*) as count FROM sponsorTimes WHERE userID = ? and videoID = ?", [userID, videoID]);
|
||||
|
||||
if (duplicateCheckRow.count >= 16) {
|
||||
//too many sponsors for the same video from the same user
|
||||
@@ -425,7 +425,7 @@ export async function postSkipSegments(req: Request, res: Response) {
|
||||
}
|
||||
|
||||
//check to see if this user is shadowbanned
|
||||
const shadowBanRow = privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?", [userID]);
|
||||
const shadowBanRow = await privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?", [userID]);
|
||||
|
||||
let shadowBanned = shadowBanRow.userCount;
|
||||
|
||||
@@ -445,7 +445,7 @@ export async function postSkipSegments(req: Request, res: Response) {
|
||||
Logger.error("Error while submitting when connecting to YouTube API: " + err);
|
||||
} else {
|
||||
//get all segments for this video and user
|
||||
const allSubmittedByUser = db.prepare('all', "SELECT startTime, endTime FROM sponsorTimes WHERE userID = ? and videoID = ? and votes > -1", [userID, videoID]);
|
||||
const allSubmittedByUser = await db.prepare('all', "SELECT startTime, endTime FROM sponsorTimes WHERE userID = ? and videoID = ? and votes > -1", [userID, videoID]);
|
||||
const allSegmentTimes = [];
|
||||
if (allSubmittedByUser !== undefined) {
|
||||
//add segments the user has previously submitted
|
||||
@@ -489,7 +489,7 @@ export async function postSkipSegments(req: Request, res: Response) {
|
||||
|
||||
const startingLocked = isVIP ? 1 : 0;
|
||||
try {
|
||||
db.prepare('run', "INSERT INTO sponsorTimes " +
|
||||
await db.prepare('run', "INSERT INTO sponsorTimes " +
|
||||
"(videoID, startTime, endTime, votes, locked, UUID, userID, timeSubmitted, views, category, shadowHidden, hashedVideoID)" +
|
||||
"VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", [
|
||||
videoID, segmentInfo.segment[0], segmentInfo.segment[1], startingVotes, startingLocked, UUID, userID, timeSubmitted, 0, segmentInfo.category, shadowBanned, getHash(videoID, 1),
|
||||
@@ -497,7 +497,7 @@ export async function postSkipSegments(req: Request, res: Response) {
|
||||
);
|
||||
|
||||
//add to private db as well
|
||||
privateDB.prepare('run', "INSERT INTO sponsorTimes VALUES(?, ?, ?)", [videoID, hashedIP, timeSubmitted]);
|
||||
await privateDB.prepare('run', "INSERT INTO sponsorTimes VALUES(?, ?, ?)", [videoID, hashedIP, timeSubmitted]);
|
||||
|
||||
// Clear redis cache for this video
|
||||
redis.delAsync(skipSegmentsKey(videoID));
|
||||
|
||||
@@ -5,7 +5,7 @@ import {isUserVIP} from '../utils/isUserVIP';
|
||||
import {getHash} from '../utils/getHash';
|
||||
import { HashedUserID, UserID } from '../types/user.model';
|
||||
|
||||
export function postWarning(req: Request, res: Response) {
|
||||
export async function postWarning(req: Request, res: Response) {
|
||||
// Collect user input data
|
||||
let issuerUserID: HashedUserID = getHash(<UserID> req.body.issuerUserID);
|
||||
let userID: UserID = req.body.userID;
|
||||
@@ -13,7 +13,7 @@ export function postWarning(req: Request, res: Response) {
|
||||
let enabled: boolean = req.body.enabled ?? true;
|
||||
|
||||
// Ensure user is a VIP
|
||||
if (!isUserVIP(issuerUserID)) {
|
||||
if (!await isUserVIP(issuerUserID)) {
|
||||
Logger.warn("Permission violation: User " + issuerUserID + " attempted to warn user " + userID + ".");
|
||||
res.status(403).json({"message": "Not a VIP"});
|
||||
return;
|
||||
@@ -22,17 +22,17 @@ export function postWarning(req: Request, res: Response) {
|
||||
let resultStatus = "";
|
||||
|
||||
if (enabled) {
|
||||
let previousWarning = db.prepare('get', 'SELECT * FROM warnings WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]);
|
||||
let previousWarning = await db.prepare('get', 'SELECT * FROM warnings WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]);
|
||||
|
||||
if (!previousWarning) {
|
||||
db.prepare('run', 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES (?, ?, ?, 1)', [userID, issueTime, issuerUserID]);
|
||||
await db.prepare('run', 'INSERT INTO warnings (userID, issueTime, issuerUserID, enabled) VALUES (?, ?, ?, 1)', [userID, issueTime, issuerUserID]);
|
||||
resultStatus = "issued to";
|
||||
} else {
|
||||
res.status(409).send();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
db.prepare('run', 'UPDATE warnings SET enabled = 0 WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]);
|
||||
await db.prepare('run', 'UPDATE warnings SET enabled = 0 WHERE userID = ? AND issuerUserID = ?', [userID, issuerUserID]);
|
||||
resultStatus = "removed from";
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import {db} from '../databases/databases';
|
||||
import {getHash} from '../utils/getHash';
|
||||
import {Request, Response} from 'express';
|
||||
|
||||
export function setUsername(req: Request, res: Response) {
|
||||
export async function setUsername(req: Request, res: Response) {
|
||||
let userID = req.query.userID as string;
|
||||
let userName = req.query.username as string;
|
||||
|
||||
@@ -38,14 +38,14 @@ export function setUsername(req: Request, res: Response) {
|
||||
|
||||
try {
|
||||
//check if username is already set
|
||||
let row = db.prepare('get', "SELECT count(*) as count FROM userNames WHERE userID = ?", [userID]);
|
||||
let row = await db.prepare('get', "SELECT count(*) as count FROM userNames WHERE userID = ?", [userID]);
|
||||
|
||||
if (row.count > 0) {
|
||||
//already exists, update this row
|
||||
db.prepare('run', "UPDATE userNames SET userName = ? WHERE userID = ?", [userName, userID]);
|
||||
await db.prepare('run', "UPDATE userNames SET userName = ? WHERE userID = ?", [userName, userID]);
|
||||
} else {
|
||||
//add to the db
|
||||
db.prepare('run', "INSERT INTO userNames VALUES(?, ?)", [userID, userName]);
|
||||
await db.prepare('run', "INSERT INTO userNames VALUES(?, ?)", [userID, userName]);
|
||||
}
|
||||
|
||||
res.sendStatus(200);
|
||||
|
||||
@@ -23,7 +23,7 @@ export async function shadowBanUser(req: Request, res: Response) {
|
||||
//hash the userID
|
||||
adminUserIDInput = getHash(adminUserIDInput);
|
||||
|
||||
const isVIP = db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [adminUserIDInput]).userCount > 0;
|
||||
const isVIP = (await db.prepare("get", "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [adminUserIDInput])).userCount > 0;
|
||||
if (!isVIP) {
|
||||
//not authorized
|
||||
res.sendStatus(403);
|
||||
@@ -32,30 +32,30 @@ export async function shadowBanUser(req: Request, res: Response) {
|
||||
|
||||
if (userID) {
|
||||
//check to see if this user is already shadowbanned
|
||||
const row = privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?", [userID]);
|
||||
const row = await privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedUsers WHERE userID = ?", [userID]);
|
||||
|
||||
if (enabled && row.userCount == 0) {
|
||||
//add them to the shadow ban list
|
||||
|
||||
//add it to the table
|
||||
privateDB.prepare('run', "INSERT INTO shadowBannedUsers VALUES(?)", [userID]);
|
||||
await privateDB.prepare('run', "INSERT INTO shadowBannedUsers VALUES(?)", [userID]);
|
||||
|
||||
//find all previous submissions and hide them
|
||||
if (unHideOldSubmissions) {
|
||||
db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE userID = ?"
|
||||
await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE userID = ?"
|
||||
+ " AND NOT EXISTS ( SELECT videoID, category FROM noSegments WHERE"
|
||||
+ " sponsorTimes.videoID = noSegments.videoID AND sponsorTimes.category = noSegments.category)", [userID]);
|
||||
}
|
||||
} else if (!enabled && row.userCount > 0) {
|
||||
//remove them from the shadow ban list
|
||||
privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]);
|
||||
await privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]);
|
||||
|
||||
//find all previous submissions and unhide them
|
||||
if (unHideOldSubmissions) {
|
||||
let segmentsToIgnore = db.prepare('all', "SELECT UUID FROM sponsorTimes st "
|
||||
let segmentsToIgnore = (await db.prepare('all', "SELECT UUID FROM sponsorTimes st "
|
||||
+ "JOIN noSegments ns on st.videoID = ns.videoID AND st.category = ns.category WHERE st.userID = ?"
|
||||
, [userID]).map((item: {UUID: string}) => item.UUID);
|
||||
let allSegments = db.prepare('all', "SELECT UUID FROM sponsorTimes st WHERE st.userID = ?", [userID])
|
||||
, [userID])).map((item: {UUID: string}) => item.UUID);
|
||||
let allSegments = (await db.prepare('all', "SELECT UUID FROM sponsorTimes st WHERE st.userID = ?", [userID]))
|
||||
.map((item: {UUID: string}) => item.UUID);
|
||||
|
||||
allSegments.filter((item: {uuid: string}) => {
|
||||
@@ -68,29 +68,29 @@ export async function shadowBanUser(req: Request, res: Response) {
|
||||
}
|
||||
else if (hashedIP) {
|
||||
//check to see if this user is already shadowbanned
|
||||
// let row = privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedIPs WHERE hashedIP = ?", [hashedIP]);
|
||||
// let row = await privateDB.prepare('get', "SELECT count(*) as userCount FROM shadowBannedIPs WHERE hashedIP = ?", [hashedIP]);
|
||||
|
||||
// if (enabled && row.userCount == 0) {
|
||||
if (enabled) {
|
||||
//add them to the shadow ban list
|
||||
|
||||
//add it to the table
|
||||
// privateDB.prepare('run', "INSERT INTO shadowBannedIPs VALUES(?)", [hashedIP]);
|
||||
// await privateDB.prepare('run', "INSERT INTO shadowBannedIPs VALUES(?)", [hashedIP]);
|
||||
|
||||
|
||||
//find all previous submissions and hide them
|
||||
if (unHideOldSubmissions) {
|
||||
db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE timeSubmitted IN " +
|
||||
await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 1 WHERE timeSubmitted IN " +
|
||||
"(SELECT privateDB.timeSubmitted FROM sponsorTimes LEFT JOIN privateDB.sponsorTimes as privateDB ON sponsorTimes.timeSubmitted=privateDB.timeSubmitted " +
|
||||
"WHERE privateDB.hashedIP = ?)", [hashedIP]);
|
||||
}
|
||||
} /*else if (!enabled && row.userCount > 0) {
|
||||
// //remove them from the shadow ban list
|
||||
// privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]);
|
||||
// await privateDB.prepare('run', "DELETE FROM shadowBannedUsers WHERE userID = ?", [userID]);
|
||||
|
||||
// //find all previous submissions and unhide them
|
||||
// if (unHideOldSubmissions) {
|
||||
// db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE userID = ?", [userID]);
|
||||
// await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE userID = ?", [userID]);
|
||||
// }
|
||||
}*/
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import {db} from '../databases/databases';
|
||||
import {Request, Response} from 'express';
|
||||
|
||||
export function viewedVideoSponsorTime(req: Request, res: Response): Response {
|
||||
export async function viewedVideoSponsorTime(req: Request, res: Response): Promise<Response> {
|
||||
let UUID = req.query.UUID;
|
||||
|
||||
if (UUID == undefined) {
|
||||
@@ -10,7 +10,7 @@ export function viewedVideoSponsorTime(req: Request, res: Response): Response {
|
||||
}
|
||||
|
||||
//up the view count by one
|
||||
db.prepare('run', "UPDATE sponsorTimes SET views = views + 1 WHERE UUID = ?", [UUID]);
|
||||
await db.prepare('run', "UPDATE sponsorTimes SET views = views + 1 WHERE UUID = ?", [UUID]);
|
||||
|
||||
return res.sendStatus(200);
|
||||
}
|
||||
|
||||
@@ -35,14 +35,14 @@ interface VoteData {
|
||||
oldIncrementAmount: number;
|
||||
}
|
||||
|
||||
function sendWebhooks(voteData: VoteData) {
|
||||
const submissionInfoRow = db.prepare('get', "SELECT s.videoID, s.userID, s.startTime, s.endTime, s.category, u.userName, " +
|
||||
async function sendWebhooks(voteData: VoteData) {
|
||||
const submissionInfoRow = await db.prepare('get', "SELECT s.videoID, s.userID, s.startTime, s.endTime, s.category, u.userName, " +
|
||||
"(select count(1) from sponsorTimes where userID = s.userID) count, " +
|
||||
"(select count(1) from sponsorTimes where userID = s.userID and votes <= -2) disregarded " +
|
||||
"FROM sponsorTimes s left join userNames u on s.userID = u.userID where s.UUID=?",
|
||||
[voteData.UUID]);
|
||||
|
||||
const userSubmissionCountRow = db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [voteData.nonAnonUserID]);
|
||||
const userSubmissionCountRow = await db.prepare('get', "SELECT count(*) as submissionCount FROM sponsorTimes WHERE userID = ?", [voteData.nonAnonUserID]);
|
||||
|
||||
if (submissionInfoRow !== undefined && userSubmissionCountRow != undefined) {
|
||||
let webhookURL: string = null;
|
||||
@@ -141,9 +141,9 @@ function sendWebhooks(voteData: VoteData) {
|
||||
}
|
||||
}
|
||||
|
||||
function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmission: boolean, category: string, hashedIP: string, res: Response) {
|
||||
async function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmission: boolean, category: string, hashedIP: string, res: Response) {
|
||||
// Check if they've already made a vote
|
||||
const usersLastVoteInfo = privateDB.prepare('get', "select count(*) as votes, category from categoryVotes where UUID = ? and userID = ?", [UUID, userID]);
|
||||
const usersLastVoteInfo = await privateDB.prepare('get', "select count(*) as votes, category from categoryVotes where UUID = ? and userID = ?", [UUID, userID]);
|
||||
|
||||
if (usersLastVoteInfo?.category === category) {
|
||||
// Double vote, ignore
|
||||
@@ -151,7 +151,7 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmiss
|
||||
return;
|
||||
}
|
||||
|
||||
const currentCategory = db.prepare('get', "select category from sponsorTimes where UUID = ?", [UUID]);
|
||||
const currentCategory = await db.prepare('get', "select category from sponsorTimes where UUID = ?", [UUID]);
|
||||
if (!currentCategory) {
|
||||
// Submission doesn't exist
|
||||
res.status(400).send("Submission doesn't exist.");
|
||||
@@ -163,36 +163,36 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmiss
|
||||
return;
|
||||
}
|
||||
|
||||
const nextCategoryInfo = db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, category]);
|
||||
const nextCategoryInfo = await db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, category]);
|
||||
|
||||
const timeSubmitted = Date.now();
|
||||
|
||||
const voteAmount = isVIP ? 500 : 1;
|
||||
|
||||
// Add the vote
|
||||
if (db.prepare('get', "select count(*) as count from categoryVotes where UUID = ? and category = ?", [UUID, category]).count > 0) {
|
||||
if ((await db.prepare('get', "select count(*) as count from categoryVotes where UUID = ? and category = ?", [UUID, category])).count > 0) {
|
||||
// Update the already existing db entry
|
||||
db.prepare('run', "update categoryVotes set votes = votes + ? where UUID = ? and category = ?", [voteAmount, UUID, category]);
|
||||
await db.prepare('run', "update categoryVotes set votes = votes + ? where UUID = ? and category = ?", [voteAmount, UUID, category]);
|
||||
} else {
|
||||
// Add a db entry
|
||||
db.prepare('run', "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, category, voteAmount]);
|
||||
await db.prepare('run', "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, category, voteAmount]);
|
||||
}
|
||||
|
||||
// Add the info into the private db
|
||||
if (usersLastVoteInfo?.votes > 0) {
|
||||
// Reverse the previous vote
|
||||
db.prepare('run', "update categoryVotes set votes = votes - ? where UUID = ? and category = ?", [voteAmount, UUID, usersLastVoteInfo.category]);
|
||||
await db.prepare('run', "update categoryVotes set votes = votes - ? where UUID = ? and category = ?", [voteAmount, UUID, usersLastVoteInfo.category]);
|
||||
|
||||
privateDB.prepare('run', "update categoryVotes set category = ?, timeSubmitted = ?, hashedIP = ? where userID = ? and UUID = ?", [category, timeSubmitted, hashedIP, userID, UUID]);
|
||||
await privateDB.prepare('run', "update categoryVotes set category = ?, timeSubmitted = ?, hashedIP = ? where userID = ? and UUID = ?", [category, timeSubmitted, hashedIP, userID, UUID]);
|
||||
} else {
|
||||
privateDB.prepare('run', "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, userID, hashedIP, category, timeSubmitted]);
|
||||
await privateDB.prepare('run', "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, userID, hashedIP, category, timeSubmitted]);
|
||||
}
|
||||
|
||||
// See if the submissions category is ready to change
|
||||
const currentCategoryInfo = db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, currentCategory.category]);
|
||||
const currentCategoryInfo = await db.prepare("get", "select votes from categoryVotes where UUID = ? and category = ?", [UUID, currentCategory.category]);
|
||||
|
||||
const submissionInfo = db.prepare("get", "SELECT userID, timeSubmitted, votes FROM sponsorTimes WHERE UUID = ?", [UUID]);
|
||||
const isSubmissionVIP = submissionInfo && isUserVIP(submissionInfo.userID);
|
||||
const submissionInfo = await db.prepare("get", "SELECT userID, timeSubmitted, votes FROM sponsorTimes WHERE UUID = ?", [UUID]);
|
||||
const isSubmissionVIP = submissionInfo && await isUserVIP(submissionInfo.userID);
|
||||
const startingVotes = isSubmissionVIP ? 10000 : 1;
|
||||
|
||||
// Change this value from 1 in the future to make it harder to change categories
|
||||
@@ -201,9 +201,9 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmiss
|
||||
|
||||
// Add submission as vote
|
||||
if (!currentCategoryInfo && submissionInfo) {
|
||||
db.prepare("run", "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, currentCategory.category, currentCategoryCount]);
|
||||
await db.prepare("run", "insert into categoryVotes (UUID, category, votes) values (?, ?, ?)", [UUID, currentCategory.category, currentCategoryCount]);
|
||||
|
||||
privateDB.prepare("run", "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, submissionInfo.userID, "unknown", currentCategory.category, submissionInfo.timeSubmitted]);
|
||||
await privateDB.prepare("run", "insert into categoryVotes (UUID, userID, hashedIP, category, timeSubmitted) values (?, ?, ?, ?, ?)", [UUID, submissionInfo.userID, "unknown", currentCategory.category, submissionInfo.timeSubmitted]);
|
||||
}
|
||||
|
||||
const nextCategoryCount = (nextCategoryInfo?.votes || 0) + voteAmount;
|
||||
@@ -212,7 +212,7 @@ function categoryVote(UUID: string, userID: string, isVIP: boolean, isOwnSubmiss
|
||||
// VIPs change it every time
|
||||
if (nextCategoryCount - currentCategoryCount >= Math.max(Math.ceil(submissionInfo?.votes / 2), 2) || isVIP || isOwnSubmission) {
|
||||
// Replace the category
|
||||
db.prepare('run', "update sponsorTimes set category = ? where UUID = ?", [category, UUID]);
|
||||
await db.prepare('run', "update sponsorTimes set category = ? where UUID = ?", [category, UUID]);
|
||||
}
|
||||
|
||||
res.sendStatus(200);
|
||||
@@ -245,20 +245,20 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
|
||||
const hashedIP = getHash(ip + config.globalSalt);
|
||||
|
||||
//check if this user is on the vip list
|
||||
const isVIP = db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [nonAnonUserID]).userCount > 0;
|
||||
const isVIP = (await db.prepare('get', "SELECT count(*) as userCount FROM vipUsers WHERE userID = ?", [nonAnonUserID])).userCount > 0;
|
||||
|
||||
//check if user voting on own submission
|
||||
const isOwnSubmission = db.prepare("get", "SELECT UUID as submissionCount FROM sponsorTimes where userID = ? AND UUID = ?", [nonAnonUserID, UUID]) !== undefined;
|
||||
const isOwnSubmission = (await db.prepare("get", "SELECT UUID as submissionCount FROM sponsorTimes where userID = ? AND UUID = ?", [nonAnonUserID, UUID])) !== undefined;
|
||||
|
||||
|
||||
// If not upvote
|
||||
if (!isVIP && type !== 1) {
|
||||
const isSegmentLocked = () => !!db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", [UUID])?.locked;
|
||||
const isVideoLocked = () => !!db.prepare('get', 'SELECT noSegments.category from noSegments left join sponsorTimes' +
|
||||
const isSegmentLocked = async () => !!(await db.prepare('get', "SELECT locked FROM sponsorTimes WHERE UUID = ?", [UUID]))?.locked;
|
||||
const isVideoLocked = async () => !!(await db.prepare('get', 'SELECT noSegments.category from noSegments left join sponsorTimes' +
|
||||
' on (noSegments.videoID = sponsorTimes.videoID and noSegments.category = sponsorTimes.category)' +
|
||||
' where UUID = ?', [UUID]);
|
||||
' where UUID = ?', [UUID]));
|
||||
|
||||
if (isSegmentLocked() || isVideoLocked()) {
|
||||
if (await isSegmentLocked() || await isVideoLocked()) {
|
||||
res.status(403).send("Vote rejected: A moderator has decided that this segment is correct");
|
||||
return;
|
||||
}
|
||||
@@ -270,7 +270,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
|
||||
|
||||
if (type == 1 && !isVIP && !isOwnSubmission) {
|
||||
// Check if upvoting hidden segment
|
||||
const voteInfo = db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", [UUID]);
|
||||
const voteInfo = await db.prepare('get', "SELECT votes FROM sponsorTimes WHERE UUID = ?", [UUID]);
|
||||
|
||||
if (voteInfo && voteInfo.votes <= -2) {
|
||||
res.status(403).send("Not allowed to upvote segment with too many downvotes unless you are VIP.");
|
||||
@@ -280,9 +280,9 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
|
||||
|
||||
const MILLISECONDS_IN_HOUR = 3600000;
|
||||
const now = Date.now();
|
||||
const warningsCount = db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1",
|
||||
const warningsCount = (await db.prepare('get', "SELECT count(1) as count FROM warnings WHERE userID = ? AND issueTime > ? AND enabled = 1",
|
||||
[nonAnonUserID, Math.floor(now - (config.hoursAfterWarningExpires * MILLISECONDS_IN_HOUR))],
|
||||
).count;
|
||||
)).count;
|
||||
|
||||
if (warningsCount >= config.maxNumberOfActiveWarnings) {
|
||||
return res.status(403).send('Vote rejected due to a warning from a moderator. This means that we noticed you were making some common mistakes that are not malicious, and we just want to clarify the rules. Could you please send a message in Discord or Matrix so we can further help you?');
|
||||
@@ -292,7 +292,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
|
||||
|
||||
try {
|
||||
//check if vote has already happened
|
||||
const votesRow = privateDB.prepare('get', "SELECT type FROM votes WHERE userID = ? AND UUID = ?", [userID, UUID]);
|
||||
const votesRow = await privateDB.prepare('get', "SELECT type FROM votes WHERE userID = ? AND UUID = ?", [userID, UUID]);
|
||||
|
||||
//-1 for downvote, 1 for upvote. Maybe more depending on reputation in the future
|
||||
let incrementAmount = 0;
|
||||
@@ -338,7 +338,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
|
||||
}
|
||||
|
||||
//check if the increment amount should be multiplied (downvotes have more power if there have been many views)
|
||||
const row = db.prepare('get', "SELECT videoID, votes, views FROM sponsorTimes WHERE UUID = ?", [UUID]) as
|
||||
const row = await db.prepare('get', "SELECT videoID, votes, views FROM sponsorTimes WHERE UUID = ?", [UUID]) as
|
||||
{videoID: VideoID, votes: number, views: number};
|
||||
|
||||
if (voteTypeEnum === voteTypes.normal) {
|
||||
@@ -357,16 +357,16 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
|
||||
|
||||
// Only change the database if they have made a submission before and haven't voted recently
|
||||
const ableToVote = isVIP
|
||||
|| (db.prepare("get", "SELECT userID FROM sponsorTimes WHERE userID = ?", [nonAnonUserID]) !== undefined
|
||||
&& privateDB.prepare("get", "SELECT userID FROM shadowBannedUsers WHERE userID = ?", [nonAnonUserID]) === undefined
|
||||
&& privateDB.prepare("get", "SELECT UUID FROM votes WHERE UUID = ? AND hashedIP = ? AND userID != ?", [UUID, hashedIP, userID]) === undefined);
|
||||
|| ((await db.prepare("get", "SELECT userID FROM sponsorTimes WHERE userID = ?", [nonAnonUserID])) !== undefined
|
||||
&& (await privateDB.prepare("get", "SELECT userID FROM shadowBannedUsers WHERE userID = ?", [nonAnonUserID])) === undefined
|
||||
&& (await privateDB.prepare("get", "SELECT UUID FROM votes WHERE UUID = ? AND hashedIP = ? AND userID != ?", [UUID, hashedIP, userID])) === undefined);
|
||||
|
||||
if (ableToVote) {
|
||||
//update the votes table
|
||||
if (votesRow != undefined) {
|
||||
privateDB.prepare('run', "UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?", [type, userID, UUID]);
|
||||
await privateDB.prepare('run', "UPDATE votes SET type = ? WHERE userID = ? AND UUID = ?", [type, userID, UUID]);
|
||||
} else {
|
||||
privateDB.prepare('run', "INSERT INTO votes VALUES(?, ?, ?, ?)", [UUID, userID, hashedIP, type]);
|
||||
await privateDB.prepare('run', "INSERT INTO votes VALUES(?, ?, ?, ?)", [UUID, userID, hashedIP, type]);
|
||||
}
|
||||
|
||||
let columnName = "";
|
||||
@@ -378,13 +378,13 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
|
||||
|
||||
//update the vote count on this sponsorTime
|
||||
//oldIncrementAmount will be zero is row is null
|
||||
db.prepare('run', "UPDATE sponsorTimes SET " + columnName + " = " + columnName + " + ? WHERE UUID = ?", [incrementAmount - oldIncrementAmount, UUID]);
|
||||
await db.prepare('run', "UPDATE sponsorTimes SET " + columnName + " = " + columnName + " + ? WHERE UUID = ?", [incrementAmount - oldIncrementAmount, UUID]);
|
||||
if (isVIP && incrementAmount > 0 && voteTypeEnum === voteTypes.normal) {
|
||||
// Lock this submission
|
||||
db.prepare('run', "UPDATE sponsorTimes SET locked = 1 WHERE UUID = ?", [UUID]);
|
||||
await db.prepare('run', "UPDATE sponsorTimes SET locked = 1 WHERE UUID = ?", [UUID]);
|
||||
} else if (isVIP && incrementAmount < 0 && voteTypeEnum === voteTypes.normal) {
|
||||
// Unlock if a VIP downvotes it
|
||||
db.prepare('run', "UPDATE sponsorTimes SET locked = 0 WHERE UUID = ?", [UUID]);
|
||||
await db.prepare('run', "UPDATE sponsorTimes SET locked = 0 WHERE UUID = ?", [UUID]);
|
||||
}
|
||||
|
||||
// Clear redis cache for this video
|
||||
@@ -393,7 +393,7 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
|
||||
//for each positive vote, see if a hidden submission can be shown again
|
||||
if (incrementAmount > 0 && voteTypeEnum === voteTypes.normal) {
|
||||
//find the UUID that submitted the submission that was voted on
|
||||
const submissionUserIDInfo = db.prepare('get', "SELECT userID FROM sponsorTimes WHERE UUID = ?", [UUID]);
|
||||
const submissionUserIDInfo = await db.prepare('get', "SELECT userID FROM sponsorTimes WHERE UUID = ?", [UUID]);
|
||||
if (!submissionUserIDInfo) {
|
||||
// They are voting on a non-existent submission
|
||||
res.status(400).send("Voting on a non-existent submission");
|
||||
@@ -403,14 +403,14 @@ export async function voteOnSponsorTime(req: Request, res: Response) {
|
||||
const submissionUserID = submissionUserIDInfo.userID;
|
||||
|
||||
//check if any submissions are hidden
|
||||
const hiddenSubmissionsRow = db.prepare('get', "SELECT count(*) as hiddenSubmissions FROM sponsorTimes WHERE userID = ? AND shadowHidden > 0", [submissionUserID]);
|
||||
const hiddenSubmissionsRow = await db.prepare('get', "SELECT count(*) as hiddenSubmissions FROM sponsorTimes WHERE userID = ? AND shadowHidden > 0", [submissionUserID]);
|
||||
|
||||
if (hiddenSubmissionsRow.hiddenSubmissions > 0) {
|
||||
//see if some of this users submissions should be visible again
|
||||
|
||||
if (await isUserTrustworthy(submissionUserID)) {
|
||||
//they are trustworthy again, show 2 of their submissions again, if there are two to show
|
||||
db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE ROWID IN (SELECT ROWID FROM sponsorTimes WHERE userID = ? AND shadowHidden = 1 LIMIT 2)", [submissionUserID]);
|
||||
await db.prepare('run', "UPDATE sponsorTimes SET shadowHidden = 0 WHERE ROWID IN (SELECT ROWID FROM sponsorTimes WHERE userID = ? AND shadowHidden = 1 LIMIT 2)", [submissionUserID]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user