-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
118 lines (108 loc) · 3.37 KB
/
index.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
const path = require('path');
const request = require('request-promise-native');
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const ts = require('./utils/timestamp');
const config = require('./config.json');
const Datastore = require('nedb');
global.preferences = new Datastore({ filename: path.join(__dirname, 'data', 'preferences.db'), autoload: true });
global.history = new Datastore({ filename: path.join(__dirname, 'data', 'history.db'), autoload: true });
// global.preferences.persistence.setAutocompactionInterval(24 * 60 * 60 * 1000);
global.history.ensureIndex({ fieldName: 'at' }, (err) => err && console.log('Error creating index on history', err));
global.ioUsers = {};
// Delete events from over a year ago once a day
// Hopefully that will lower the RAM usage without having to switch to a real DB
setInterval(() => {
let deleteBefore = new Date();
deleteBefore.setFullYear(deleteBefore.getFullYear() - 1);
global.history.remove({ at: { $lt: deleteBefore } }, { multi: true }, (err, num) => num && console.log(ts(), 'Archived ' + num + ' history events'))
}, 24 * 60 * 60 * 1000);
require('./server')(io);
let users = {};
if (config.debugMode) {
users = {
bbbotmaster: Promise.resolve({
"username": "Bali Balo",
"discriminator": "5436",
"mfa_enabled": false,
"id": "125119938603122688",
"avatar": "2779a3a810879b93f81e76c76ba59cc0"
})
};
}
app.get('/user-info', (req, res) => {
const code = req.query.code;
if (!code) {
return res.send('{}');
}
if (!users[code]) {
users[code] = request({
method: 'POST',
uri: 'https://discordapp.com/api/oauth2/token',
form: {
grant_type: 'authorization_code',
code: code,
redirect_uri: 'http://discordtv.balibalo.xyz/'
},
headers: { Authorization: 'Basic ' + config.discordAuth }
}).then(data => {
if (typeof data === 'string') {
try {
data = JSON.parse(data);
} catch(e) { /* */ }
}
if (!data || !data.access_token) {
throw new Error('No access token ' + JSON.stringify(data));
}
let type = data.token_type || 'Bearer';
const auth = type + ' ' + data.access_token;
return request({
method: 'GET',
uri: 'https://discordapp.com/api/users/@me',
headers: { Authorization: auth },
json: true
});
}).catch(e => {
let errMsg = e && e.error && e.error.error || e;
console.log('Error getting user info', code, errMsg);
delete users[code];
return {};
});
}
users[code].then(data => res.send(data));
});
app.get('/users.json', (req, res) => {
res.send(global.ioUsers);
});
app.get('/preferences.json', (req, res) => {
if (!req.query.user) {
return res.status(400).send('{ "error": "Bad Request" }');
}
global.preferences.findOne({ user: req.query.user }, (err, pref) => {
if (err || !pref) {
pref = { data: {} };
}
res.send(pref);
});
});
app.get('/history.json', (req, res) => {
let query = req.query;
let before = new Date();
if (query.before) {
before = new Date(+query.before || query.before);
}
let count = +query.count || 50;
let at = { $lt: before };
if (query.after) {
at.$gt = new Date(+query.after || query.after);
}
global.history.find({ at }).sort({ at: -1 }).limit(count).exec((err, data) => {
res.send(data || {});
});
});
app.use(express.static('public'));
http.listen(7410, () => {
console.log(ts(), 'Server running');
});