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,17 +3,19 @@ import { log, LogLevel } from "../logging"
import db from "../db"; import db from "../db";
export async function processBans(streamer: twitch.StreamerUserIdTokenPair) { 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 { try {
await dbconn.query('BEGIN'); await dbconn.query('BEGIN');
await dbconn.query("DELETE FROM bans"); await dbconn.query("DELETE FROM bans");
log(LogLevel.DEBUG,"Ban list:") //log(LogLevel.DEBUG,"Ban API response:")
log(LogLevel.DEBUG,JSON.stringify(response.data,null,2)); //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 insertBanQuery = "INSERT INTO bans (userid) VALUES ";
var banRow = 0; var banRow = 0;
var bansArray: number[] = []; var bansArray: number[] = [];
if (Object.keys(response.data).length > 0) {
for (var ban of response.data) { for (var ban of response.data) {
if (ban.expires_at == '') { if (ban.expires_at == '') {
banRow++; banRow++;
@ -21,13 +23,29 @@ export async function processBans(streamer: twitch.StreamerUserIdTokenPair) {
bansArray.push(ban.user_id as number); 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 = { var banQueryConfig = {
text: insertBanQuery.slice(0,-2), // Cut last `, ` off of the end text: insertBanQuery,
values: bansArray values: bansArray
}; };
log(LogLevel.DEBUG,"banQueryConfig object:") log(LogLevel.DEBUG,"banQueryConfig object:")
log(LogLevel.DEBUG,JSON.stringify(banQueryConfig,null,2)) log(LogLevel.DEBUG,JSON.stringify(banQueryConfig,null,2))
await dbconn.query(banQueryConfig); 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;
}
}
await dbconn.query("CALL update_scores()"); await dbconn.query("CALL update_scores()");
await dbconn.query('COMMIT'); await dbconn.query('COMMIT');
} catch (e) { } catch (e) {