parent
6e26650077
commit
7897fe6f3d
5 changed files with 85 additions and 11 deletions
|
@ -130,3 +130,8 @@ export const updateCronJobLastSuccess = {
|
|||
name: "updateCronJobLastSuccess",
|
||||
text: "UPDATE cron SET lastSuccess = now() WHERE jobName = $1"
|
||||
}
|
||||
|
||||
export const getRateLimitStatus = {
|
||||
name: "getRateLimitStatus",
|
||||
text: "SELECT * FROM ratelimit_vw WHERE userid = $1"
|
||||
}
|
||||
|
|
|
@ -87,6 +87,7 @@ async function retrieveYoutubeMetadata(url: string) {
|
|||
}
|
||||
|
||||
export async function addRequest(url: string, requester: string): Promise<[number,string]> {
|
||||
// Check that URL is of an accepted format
|
||||
var validUrl = false;
|
||||
for (var regex of validUrlRegexes) {
|
||||
if (regex.test(url)) {
|
||||
|
@ -95,11 +96,24 @@ export async function addRequest(url: string, requester: string): Promise<[numbe
|
|||
}
|
||||
}
|
||||
if (!validUrl) return [400, "Invalid song URL."];
|
||||
var result = await checkRequestExists(url)
|
||||
if (result) {
|
||||
console.log(result);
|
||||
return [200,`Song already requested by ${result.rows[0].requester}. State: ${result.rows[0].state}`]
|
||||
|
||||
// Check whether the URL has already been requested
|
||||
var existsResult = await checkRequestExists(url)
|
||||
if (existsResult) {
|
||||
console.log(existsResult);
|
||||
return [200,`Song already requested by ${existsResult.rows[0].requester}. State: ${existsResult.rows[0].state}`]
|
||||
}
|
||||
|
||||
// Check whether the user has hit their rate limit
|
||||
var rateLimitQuery = Object.assign(queries.getRateLimitStatus, { values: [requester] });
|
||||
var rateLimitResult = (await db.query(rateLimitQuery)).rows[0];
|
||||
if (rateLimitResult.status) {
|
||||
return [429,`You have reached your maximum of ${rateLimitResult.max} requests per day.
|
||||
Please try again later.\n
|
||||
Tip: Removing one of your requests from the past 24 hours by retracting your vote will allow you to replace it with another.`];
|
||||
}
|
||||
|
||||
// Add the request
|
||||
var query = Object.assign(queries.addRequest, { values: [url,requester] });
|
||||
return db.query(query)
|
||||
.then(async () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue