Make processBans support pagination

master
Dessa Simpson 2020-11-29 01:17:58 -07:00
parent a86e8a5667
commit af97647033
1 changed files with 38 additions and 20 deletions

View File

@ -3,31 +3,49 @@ import { log, LogLevel } from "../logging"
import db from "../db";
export async function processBans(streamer: twitch.StreamerUserIdTokenPair) {
var response = await twitch.streamerApiRequest(streamer,
"/moderation/banned?broadcaster_id=" + streamer.userid);
var dbconn = await db.connect();
var dbconn = await db.connect();
try {
await dbconn.query('BEGIN');
await dbconn.query("DELETE FROM bans");
log(LogLevel.DEBUG,"Ban list:")
log(LogLevel.DEBUG,JSON.stringify(response.data,null,2));
var insertBanQuery = "INSERT INTO bans (userid) VALUES ";
var banRow = 0;
var bansArray: number[] = [];
for (var ban of response.data) {
if (ban.expires_at == '') {
banRow++;
insertBanQuery += `($${banRow}), `;
bansArray.push(ban.user_id as number);
//log(LogLevel.DEBUG,"Ban API response:")
//log(LogLevel.DEBUG,JSON.stringify(response,null,2));
var response = await twitch.streamerApiRequest(streamer,
`/moderation/banned?broadcaster_id=${streamer.userid}&first=100`);
while (true) {
var insertBanQuery = "INSERT INTO bans (userid) VALUES ";
var banRow = 0;
var bansArray: number[] = [];
if (Object.keys(response.data).length > 0) {
for (var ban of response.data) {
if (ban.expires_at == '') {
banRow++;
insertBanQuery += `($${banRow}), `;
bansArray.push(ban.user_id as number);
}
}
insertBanQuery = insertBanQuery.slice(0,-2); // Cut last `, ` off of the end
insertBanQuery += " ON CONFLICT DO NOTHING"; // Deal with broken endpoint returning dupes
var banQueryConfig = {
text: insertBanQuery,
values: bansArray
};
log(LogLevel.DEBUG,"banQueryConfig object:")
log(LogLevel.DEBUG,JSON.stringify(banQueryConfig,null,2))
await dbconn.query(banQueryConfig);
}
if (response.pagination.cursor) {
var oldFirstUserid = response.data[0].user_id;
response = await twitch.streamerApiRequest(streamer,
`/moderation/banned?broadcaster_id=${streamer.userid}&after=${response.pagination.cursor}&first=100`);
// Work around broken api endpoint giving a cursor referring to the
// current page, causing an infinite loop
if (oldFirstUserid == response.data[0].user_id) break;
//log(LogLevel.DEBUG,"Ban API response:");
//log(LogLevel.DEBUG,JSON.stringify(response,null,2));
} else {
break;
}
}
var banQueryConfig = {
text: insertBanQuery.slice(0,-2), // Cut last `, ` off of the end
values: bansArray
};
log(LogLevel.DEBUG,"banQueryConfig object:")
log(LogLevel.DEBUG,JSON.stringify(banQueryConfig,null,2))
await dbconn.query(banQueryConfig);
await dbconn.query("CALL update_scores()");
await dbconn.query('COMMIT');
} catch (e) {