2020-06-24 21:34:53 +00:00
|
|
|
import * as config from "./config";
|
2020-07-02 06:06:14 +00:00
|
|
|
import * as requests from "./requests";
|
2020-07-05 18:46:41 +00:00
|
|
|
import * as twitch from "./twitch";
|
2020-09-15 05:45:32 +00:00
|
|
|
import * as queries from "./queries";
|
2020-07-05 18:46:41 +00:00
|
|
|
import { URLSearchParams } from "url";
|
2020-06-24 21:34:53 +00:00
|
|
|
import express from "express";
|
2020-07-04 18:29:07 +00:00
|
|
|
import session from "express-session";
|
2020-09-11 05:42:37 +00:00
|
|
|
import pg from "pg";
|
2020-07-04 18:29:07 +00:00
|
|
|
import pgSessionStore from "connect-pg-simple";
|
2020-07-05 18:46:41 +00:00
|
|
|
import fetch, { Response as FetchResponse } from "node-fetch";
|
2020-06-24 21:34:53 +00:00
|
|
|
import db from "./db";
|
2020-07-02 20:06:13 +00:00
|
|
|
import errorHandler from "./errors";
|
2020-06-24 21:34:53 +00:00
|
|
|
|
2020-08-14 18:37:24 +00:00
|
|
|
// Ensure that any API token we have is valid - if not, destroy the session,
|
|
|
|
// logging out the user. Should be called before checking whether a user is
|
|
|
|
// logged in.
|
|
|
|
async function validateApiToken(session: Express.Session) {
|
|
|
|
if (session.tokenpair && ! (await twitch.isApiTokenValid(session.tokenpair))) {
|
|
|
|
session.destroy(()=>{});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 21:34:53 +00:00
|
|
|
const app = express();
|
|
|
|
app.use(express.static('public'));
|
2020-07-02 06:06:14 +00:00
|
|
|
app.use(express.urlencoded({extended: false}));
|
2020-07-04 18:29:07 +00:00
|
|
|
app.use(session({
|
|
|
|
secret: config.sessionSecret,
|
|
|
|
saveUninitialized: false,
|
|
|
|
resave: false,
|
2020-07-05 18:46:41 +00:00
|
|
|
store: new (pgSessionStore(session))({
|
|
|
|
pool: db
|
|
|
|
})
|
2020-07-04 18:29:07 +00:00
|
|
|
}));
|
2020-06-24 21:34:53 +00:00
|
|
|
|
2020-07-05 18:46:41 +00:00
|
|
|
// API
|
2020-07-02 06:06:14 +00:00
|
|
|
app.get("/api/getRequests", async (request, response) => {
|
2020-08-08 04:32:06 +00:00
|
|
|
if (!request.session) {
|
|
|
|
throw new Error ("Missing request.session")
|
|
|
|
}
|
2020-07-02 06:06:14 +00:00
|
|
|
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
|
2020-08-14 18:37:24 +00:00
|
|
|
await validateApiToken(request.session);
|
2020-08-08 04:32:06 +00:00
|
|
|
if (request.session.user) {
|
|
|
|
requests.getRequestsVoted(requestCount,request.session.user.id).then((val: Array<any>) => response.send(val))
|
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
|
|
|
} else {
|
|
|
|
requests.getRequests(requestCount).then((val: Array<any>) => response.send(val))
|
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
|
|
|
}
|
2020-07-02 06:06:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/api/getAllRequests", async (request, response) => {
|
2020-08-08 04:32:06 +00:00
|
|
|
if (!request.session) {
|
|
|
|
throw new Error ("Missing request.session")
|
|
|
|
}
|
2020-07-02 06:06:14 +00:00
|
|
|
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
|
2020-08-14 18:37:24 +00:00
|
|
|
await validateApiToken(request.session);
|
2020-08-08 04:32:06 +00:00
|
|
|
if (request.session.user) {
|
|
|
|
requests.getAllRequestsVoted(requestCount,request.session.user.id).then((val: Array<any>) => response.send(val))
|
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
|
|
|
} else {
|
|
|
|
requests.getAllRequests(requestCount).then((val: Array<any>) => response.send(val))
|
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
|
|
|
}
|
2020-07-02 06:06:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/api/addRequest", async (request, response) => {
|
|
|
|
response.type('text/plain');
|
2020-08-14 18:37:24 +00:00
|
|
|
if (request.session) await validateApiToken(request.session);
|
2020-07-07 04:24:04 +00:00
|
|
|
if (!request.session || !request.session.user) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("Must be logged in");
|
|
|
|
return;
|
|
|
|
}
|
2020-09-17 18:29:05 +00:00
|
|
|
var banned = await db.query(Object.assign(queries.checkBan, { values: [request.session.user.id] })).then((result: pg.QueryResult) => result.rowCount > 0);
|
2020-09-15 06:01:11 +00:00
|
|
|
if (banned) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("You are banned; you may not add new requests.");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-02 06:06:14 +00:00
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
2020-08-08 04:32:06 +00:00
|
|
|
return;
|
2020-07-02 06:06:14 +00:00
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
2020-08-08 04:32:06 +00:00
|
|
|
var requester = request.session.user.id;
|
2020-07-04 16:34:03 +00:00
|
|
|
requests.addRequest(url,requester).then((val: [number,string]) => {
|
|
|
|
response.status(val[0]);
|
|
|
|
response.send(val[1]);
|
|
|
|
})
|
2020-07-02 20:06:13 +00:00
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
2020-07-02 20:32:55 +00:00
|
|
|
});
|
|
|
|
|
2020-09-15 05:45:32 +00:00
|
|
|
app.post("/api/updateRequestState", async (request, response) => {
|
|
|
|
if (request.session) await validateApiToken(request.session);
|
|
|
|
if (!request.session || !request.session.user) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("Must be logged in");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var streamerid = await db.query(queries.getStreamerId).then((result: pg.QueryResult) => result.rows[0]['userid']);
|
|
|
|
if (request.session.user.id != streamerid) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("You are not the streamer");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-02 20:32:55 +00:00
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
2020-08-08 04:32:06 +00:00
|
|
|
return;
|
2020-07-02 20:32:55 +00:00
|
|
|
}
|
|
|
|
if (!request.body.state) {
|
|
|
|
response.status(400);
|
2020-09-15 05:45:32 +00:00
|
|
|
response.send("Missing state");
|
2020-08-08 04:32:06 +00:00
|
|
|
return;
|
2020-07-02 20:32:55 +00:00
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
|
|
|
var state = request.body.state as string;
|
2020-09-15 05:45:32 +00:00
|
|
|
response.type('text/plain');
|
2020-07-02 20:32:55 +00:00
|
|
|
requests.updateRequestState(url,state).then((val: [number,string]) => {
|
|
|
|
response.status(val[0]);
|
|
|
|
response.send(val[1]);
|
|
|
|
})
|
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
2020-07-02 06:06:14 +00:00
|
|
|
});
|
|
|
|
|
2020-09-15 05:45:32 +00:00
|
|
|
app.post("/api/updateRequestScore", async (request, response) => {
|
|
|
|
if (request.session) await validateApiToken(request.session);
|
|
|
|
if (!request.session || !request.session.user) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("Must be logged in");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var streamerid = await db.query(queries.getStreamerId).then((result: pg.QueryResult) => result.rows[0]['userid']);
|
|
|
|
if (request.session.user.id != streamerid) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("You are not the streamer");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-02 06:06:14 +00:00
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
2020-08-08 04:32:06 +00:00
|
|
|
return;
|
2020-07-02 06:06:14 +00:00
|
|
|
}
|
|
|
|
if (!request.body.scoreDiff) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing scoreDiff");
|
2020-08-08 04:32:06 +00:00
|
|
|
return;
|
2020-07-02 06:06:14 +00:00
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
|
|
|
var scoreDiff = parseInt(request.body.scoreDiff as string, 10);
|
2020-09-15 05:45:32 +00:00
|
|
|
response.type('text/plain');
|
2020-08-03 05:06:28 +00:00
|
|
|
requests.updateRequestScore(url,scoreDiff).then((val: [number,string]) => {
|
|
|
|
response.status(val[0]);
|
|
|
|
response.send(val[1]);
|
|
|
|
})
|
2020-07-02 20:06:13 +00:00
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
2020-07-02 06:06:14 +00:00
|
|
|
});
|
|
|
|
|
2020-09-15 05:45:32 +00:00
|
|
|
app.post("/api/deleteRequest", async (request, response) => {
|
|
|
|
if (request.session) await validateApiToken(request.session);
|
|
|
|
if (!request.session || !request.session.user) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("Must be logged in");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var streamerid = await db.query(queries.getStreamerId).then((result: pg.QueryResult) => result.rows[0]['userid']);
|
|
|
|
if (request.session.user.id != streamerid) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("You are not the streamer");
|
|
|
|
return;
|
|
|
|
}
|
2020-07-02 06:06:14 +00:00
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
2020-08-08 04:32:06 +00:00
|
|
|
return;
|
2020-07-02 06:06:14 +00:00
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
2020-09-15 05:45:32 +00:00
|
|
|
response.type('text/plain');
|
2020-08-03 05:06:28 +00:00
|
|
|
requests.deleteRequest(url).then((val: [number,string]) => {
|
|
|
|
response.status(val[0]);
|
|
|
|
response.send(val[1]);
|
|
|
|
})
|
2020-07-02 20:06:13 +00:00
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
2020-07-02 06:06:14 +00:00
|
|
|
});
|
2020-06-24 21:34:53 +00:00
|
|
|
|
2020-08-08 04:32:06 +00:00
|
|
|
app.post("/api/addVote", async (request,response) => {
|
|
|
|
response.type('text/plain');
|
2020-08-14 18:37:24 +00:00
|
|
|
if (request.session) await validateApiToken(request.session);
|
2020-08-08 04:32:06 +00:00
|
|
|
if (!request.session || !request.session.user) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("Must be logged in");
|
|
|
|
return;
|
|
|
|
}
|
2020-09-17 18:29:05 +00:00
|
|
|
var banned = await db.query(Object.assign(queries.checkBan,{ values: [request.session.user.id] })).then((result: pg.QueryResult) => result.rowCount > 0);
|
2020-09-15 06:01:11 +00:00
|
|
|
if (banned) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("You are banned; you may not vote on requests.");
|
|
|
|
return;
|
|
|
|
}
|
2020-08-08 04:32:06 +00:00
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
|
|
|
var user = request.session.user.id;
|
|
|
|
requests.addVote(url,user).then((val: [number,string]) => {
|
|
|
|
response.status(val[0]);
|
|
|
|
response.send(val[1]);
|
|
|
|
})
|
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/api/deleteVote", async (request,response) => {
|
|
|
|
response.type('text/plain');
|
2020-08-14 18:37:24 +00:00
|
|
|
if (request.session) await validateApiToken(request.session);
|
2020-08-08 04:32:06 +00:00
|
|
|
if (!request.session || !request.session.user) {
|
|
|
|
response.status(401);
|
|
|
|
response.send("Must be logged in");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
|
|
|
var user = request.session.user.id;
|
|
|
|
requests.deleteVote(url,user).then((val: [number,string]) => {
|
|
|
|
response.status(val[0]);
|
|
|
|
response.send(val[1]);
|
|
|
|
})
|
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
|
|
|
});
|
|
|
|
|
2020-07-05 18:46:41 +00:00
|
|
|
// Twitch callback
|
|
|
|
app.get("/callback", async (request, response) => {
|
2020-07-06 03:13:07 +00:00
|
|
|
if (request.query.error) {
|
|
|
|
response.redirect(307, '/');
|
|
|
|
return;
|
|
|
|
}
|
2020-07-05 18:46:41 +00:00
|
|
|
var authcode = request.query.code as string;
|
|
|
|
var tokenResponse = await fetch("https://id.twitch.tv/oauth2/token", { method: "POST", body: new URLSearchParams({
|
|
|
|
client_id: config.twitchClientId,
|
|
|
|
client_secret: config.twitchSecret,
|
|
|
|
code: authcode,
|
|
|
|
grant_type: "authorization_code",
|
|
|
|
redirect_uri: `${config.urlPrefix}/callback`
|
2020-08-14 18:37:24 +00:00
|
|
|
})}).then((res: FetchResponse) => res.json() as Promise<twitch.TokenPair>)
|
2020-07-05 18:46:41 +00:00
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
|
|
|
if (typeof request.session == 'undefined') throw new Error('Session is undefined');
|
|
|
|
if (typeof tokenResponse == 'undefined') throw new Error('tokenResponse is undefined');
|
|
|
|
request.session.tokenpair = { access_token: tokenResponse.access_token, refresh_token: tokenResponse.refresh_token };
|
2020-08-14 18:37:24 +00:00
|
|
|
request.session.user = (await twitch.apiRequest(request.session.tokenpair,"/users")).data[0];
|
2020-09-15 05:45:32 +00:00
|
|
|
var query = Object.assign(queries.updateUser,{ values: [request.session.user.id,request.session.user.display_name,request.session.user.profile_image_url] });
|
2020-08-08 04:32:06 +00:00
|
|
|
db.query(query);
|
2020-09-15 05:45:32 +00:00
|
|
|
var streamerid = await db.query(queries.getStreamerId).then((result: pg.QueryResult) => result.rows[0]['userid']);
|
2020-09-11 05:42:37 +00:00
|
|
|
if (typeof (tokenResponse as any).scope != 'undefined') { // Scopes requested - update streamer info
|
|
|
|
if (request.session.user.id == streamerid) {
|
2020-09-15 05:45:32 +00:00
|
|
|
var query = Object.assign(queries.updateStreamer,{ values: [request.session.user.id,JSON.stringify(request.session.tokenpair)] });
|
2020-09-11 05:42:37 +00:00
|
|
|
db.query(query);
|
|
|
|
}
|
2020-09-15 05:45:32 +00:00
|
|
|
} else if (request.session.user.id == streamerid) {
|
|
|
|
response.redirect(307, `https://id.twitch.tv/oauth2/authorize?client_id=${config.twitchClientId}&redirect_uri=${config.urlPrefix}/callback&response_type=code&scope=channel:read:subscriptions moderation:read`);
|
|
|
|
return;
|
2020-09-11 05:42:37 +00:00
|
|
|
}
|
2020-07-05 18:46:41 +00:00
|
|
|
response.redirect(307, '/');
|
|
|
|
});
|
|
|
|
|
2020-07-05 21:15:30 +00:00
|
|
|
// Frontend templates
|
|
|
|
app.get("/", async (request, response) => {
|
2020-08-14 18:37:24 +00:00
|
|
|
if (request.session) await validateApiToken(request.session);
|
2020-09-15 05:45:32 +00:00
|
|
|
var streamerInfo = await db.query(queries.getStreamerInfo).then((result: pg.QueryResult) => result.rows[0]);
|
2020-08-14 18:37:24 +00:00
|
|
|
if (!request.session || !request.session.user) {
|
2020-07-07 05:02:19 +00:00
|
|
|
response.render('main.eta', {
|
|
|
|
loggedIn: false,
|
|
|
|
clientId: config.twitchClientId,
|
2020-09-11 06:43:53 +00:00
|
|
|
urlPrefix: config.urlPrefix,
|
|
|
|
streamerName: streamerInfo['displayname'],
|
|
|
|
streamerProfilePicture: streamerInfo['imageurl']
|
2020-07-07 05:02:19 +00:00
|
|
|
});
|
2020-07-05 21:15:30 +00:00
|
|
|
} else {
|
2020-07-07 05:02:19 +00:00
|
|
|
response.render('main.eta', {
|
|
|
|
loggedIn: true,
|
|
|
|
userName: request.session.user.display_name,
|
2020-09-11 06:43:53 +00:00
|
|
|
userProfilePicture: request.session.user.profile_image_url,
|
|
|
|
streamerName: streamerInfo['displayname'],
|
|
|
|
streamerProfilePicture: streamerInfo['imageurl']
|
2020-07-07 05:02:19 +00:00
|
|
|
});
|
2020-07-05 21:15:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-09-11 05:42:37 +00:00
|
|
|
// Streamer Panel
|
|
|
|
//app.get("/streamer/", async (request, response) => {
|
|
|
|
//
|
|
|
|
//});
|
|
|
|
|
2020-07-05 21:15:30 +00:00
|
|
|
// Logout
|
2020-09-11 05:42:37 +00:00
|
|
|
app.get("/logout", async (request, response) => request.session!.destroy(() => response.redirect(307, '/')));
|
2020-07-05 21:15:30 +00:00
|
|
|
|
2020-09-15 05:42:29 +00:00
|
|
|
async function processBannedUsers() {
|
|
|
|
console.log("processBannedUsers run at " + new Date().toISOString());
|
|
|
|
var streamer = await db.query(queries.getStreamerIdToken).then((result: pg.QueryResult) => result.rows[0]);
|
|
|
|
if (streamer['tokenpair'] != null) {
|
|
|
|
var response = await twitch.apiRequest(streamer['tokenpair'],"/moderation/banned?broadcaster_id=" + streamer['userid']);
|
|
|
|
var dbconn = await db.connect();
|
|
|
|
try {
|
|
|
|
await dbconn.query('BEGIN');
|
|
|
|
for (var ban of response.data) {
|
|
|
|
if (ban.expires_at == '') dbconn.query(Object.assign(queries.insertBan,{ values: [ban.user_id] }))
|
|
|
|
}
|
|
|
|
await dbconn.query('COMMIT');
|
|
|
|
} catch (e) {
|
|
|
|
await dbconn.query('ROLLBACK');
|
|
|
|
} finally {
|
|
|
|
dbconn.release();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setTimeout(processBannedUsers,3600000+Math.floor(Math.random()*900000)) // Run every 1-1.25 hours to balance load
|
|
|
|
}
|
|
|
|
|
|
|
|
processBannedUsers();
|
|
|
|
|
2020-08-03 05:06:28 +00:00
|
|
|
app.listen(config.port, () => {
|
2020-06-24 21:34:53 +00:00
|
|
|
console.log(`Listening on port ${config.port}`);
|
|
|
|
});
|