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";
|
|
|
|
import { URLSearchParams } from "url";
|
2020-07-02 06:06:14 +00:00
|
|
|
import { QueryResult } from "pg";
|
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";
|
|
|
|
import pgSessionStore from "connect-pg-simple";
|
2020-07-05 18:46:41 +00:00
|
|
|
import fetch, { Response as FetchResponse } from "node-fetch";
|
2020-07-05 21:15:30 +00:00
|
|
|
import * as eta from "eta";
|
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
|
|
|
|
|
|
|
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) => {
|
|
|
|
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
|
|
|
|
requests.getRequests(requestCount).then((val: QueryResult) => response.send(val))
|
2020-07-02 20:06:13 +00:00
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
2020-07-02 06:06:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/api/getAllRequests", async (request, response) => {
|
|
|
|
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
|
|
|
|
requests.getAllRequests(requestCount).then((val: QueryResult) => response.send(val))
|
2020-07-02 20:06:13 +00:00
|
|
|
.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');
|
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!request.body.requester) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing requester");
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
|
|
|
var requester = request.body.requester as string;
|
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
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/api/updateRequestState", async (request, response) => {
|
|
|
|
response.type('text/plain');
|
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!request.body.state) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing scoreDiff");
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
|
|
|
var state = request.body.state as string;
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/api/updateRequestScore", async (request, response) => {
|
|
|
|
response.type('text/plain');
|
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (!request.body.scoreDiff) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing scoreDiff");
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
|
|
|
var scoreDiff = parseInt(request.body.scoreDiff as string, 10);
|
|
|
|
requests.updateRequestScore(url,scoreDiff).then((val: string) => response.send(val))
|
2020-07-02 20:06:13 +00:00
|
|
|
.catch((e: any) => errorHandler(request,response,e));
|
2020-07-02 06:06:14 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/api/deleteRequest", async (request, response) => {
|
|
|
|
response.type('text/plain');
|
|
|
|
if (!request.body.url) {
|
|
|
|
response.status(400);
|
|
|
|
response.send("Missing url");
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var url = request.body.url as string;
|
|
|
|
requests.deleteRequest(url).then((val: string) => response.send(val))
|
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-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`
|
|
|
|
})}).then((res: FetchResponse) => res.json() as Promise<twitch.TokenResponse>)
|
|
|
|
.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 };
|
|
|
|
request.session.user = (await twitch.apiRequest(request.session.tokenpair,"GET","/users")).data[0];
|
|
|
|
response.redirect(307, '/');
|
|
|
|
});
|
|
|
|
|
2020-07-05 21:15:30 +00:00
|
|
|
// Frontend templates
|
|
|
|
app.get("/", async (request, response) => {
|
|
|
|
if (!request.session) {
|
|
|
|
throw new Error ("Missing request.session")
|
|
|
|
}
|
|
|
|
if (!request.session.user) {
|
|
|
|
response.render('main.eta', { loggedIn: false });
|
|
|
|
} else {
|
|
|
|
response.render('main.eta', { loggedIn: true, userName: request.session.user.display_name, userProfilePicture: request.session.user.profile_image_url });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Logout
|
|
|
|
app.get ("/logout", async (request, response) => request.session!.destroy((err) => response.redirect(307, '/')));
|
|
|
|
|
2020-07-05 18:46:41 +00:00
|
|
|
|
2020-06-24 21:34:53 +00:00
|
|
|
const server = app.listen(config.port, () => {
|
|
|
|
console.log(`Listening on port ${config.port}`);
|
|
|
|
});
|