-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
120 lines (99 loc) · 2.84 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const atob = require('atob-lite');
const discord = require('./discordBot');
const github = require('./github.js');
const session = require('express-session');
const request = require('request');
function decode(code) {
return JSON.parse(atob(code));
}
async function getTPCode(installUrl) {
return new Promise((resolve, reject) => {
request(installUrl, { json: true }, (err, res, body) => {
if (err) {
console.log(err);
reject(err);
}
resolve(body);
});
});
}
var app = express();
app.set('trust proxy', 1) // trust first proxy
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
//cookie: { secure: true }
}));
app.use(morgan('tiny'))
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json());
github.Middleware(app);
app.get('/approve/fail', (req, res) => {
res.send("Submission Approvel Failed");
})
app.get('/auth/github', github.Authenticate())
app.get('/approve/:code', async (req, res, next) => {
if (req.user) {
res.send('YAY');
var info = decode(req.params.code);
github.publishTP(req.user.token,info).then(tpURL=>{
res.send(tpURL);
}).catch(err=>{
res.send(err);
});
} else {
req.session.returnTo = req.originalUrl
res.redirect('/auth/github')
}
});
app.get('/auth/github/callback', github.AuthCallback('/auth/github/fail'), (req, res) => {
res.redirect(req.session.returnTo || '/approve/fail');
delete req.session.returnTo;
})
app.get('/auth/github/fail', (req, res) => {
res.send("GitHub Login Failed")
});
app.get('/avatar/:id/avatar.png', async (req, res) => {
var img = await discord.getAvatar(req.params.id);
///TODO: Display Image
});
app.post('/submit', async (req, res) => {
var id = req.body.id;
var infoCode = req.body.info;
var info = decode(infoCode);
var tp;
if (!info.code) {
if (info.install) {
tp = await getTPCode(info.install);
} else {
res.send("no tp data");
return;
}
} else {
var tpcode = atob(info.code);
tp = JSON.parse(tpcode);
}
var tpsubmission = {
name: tp.name,
authorname: tp.author,
authorid: id,
description: tp.description,
code: info.code,
icon: info.icon,
banner: info.banner,
video: info.video,
install:info.install
}
console.log(info);
discord.SubmitTP(tpsubmission);
res.send("It works");
});
app.get("/", (req, res) => {
res.send(JSON.stringify(global.submissions), 0, 2);
});
module.exports = app;