-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (148 loc) · 4.67 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const { google } = require('googleapis');
const express = require('express');
const path = require("path");
const Filter = require('bad-words');
const naughtyWords = require('naughty-words');
require('dotenv').config();
const rateLimit = require('express-rate-limit');
const app = express();
const bodyParser = require('body-parser');
const apiRequestLimiter = rateLimit({
windowMs: 10 * 60 * 1000, // 10 minute
max: 10, // limit each IP to 10 requests per windowMs
handler: function (req, res, /*next*/) {
return res.status(429).json({
error: 'You have been Rate Limited! Please try after sometime.'
})
}
})
app.use((req, res, next) => {
res.append('Access-Control-Allow-Origin', ['diwalioath.repl.co']);
res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.append('Access-Control-Allow-Headers', 'Content-Type');
next();
});
app.use(express.json());
app.use(bodyParser.json());
app.use(
express.urlencoded({
extended: true
})
);
app.use(express.static(path.join(__dirname + '/public/')));
var restWords = []; // Don't make const
const nonRestWords = ['am', 'god'];
for (list in naughtyWords) { restWords = restWords.concat(naughtyWords[list]); }
filter = new Filter({ placeHolder: '*' });
filter.addWords(...restWords);
filter.removeWords(...nonRestWords);
var admin = require('firebase-admin');
const serviceAccount = JSON.parse(process.env.ADMIN_KEY);
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: process.env.FIREBASE_DATABASE_URL,
databaseAuthVariableOverride: {
uid: process.env.SECRET_DATABASE_UID
}
});
const db = admin.database();
const people = db.ref("peoples");
peoples = [], PeopleStore = [];
people.on('child_added', snap => {
var val = snap.val();
PeopleStore.push({ name: val, key: snap.key });
peoples = [];
for (x of PeopleStore) {
peoples.push(x.name);
}
console.log("New Name Added", val);
});
people.on('child_removed', snap => {
const deletedUsr = snap.val();
PeopleStore = PeopleStore.filter(usr => usr.name != deletedUsr);
peoples = [];
for (x of PeopleStore) {
peoples.push(x.name);
}
console.log("New Name Removed", snap.val());
});
app.get('/count', (req, res) => {
res.status(200).json({ count: peoples.length.toString() || 0 });
});
app.get("/oathtakers", async (req, res) => {
res.status(200).json(peoples);
});
app.get("/takeoath/:name", apiRequestLimiter, (req, res) => {
if (!req.params || !req.params.name || peoples.includes(req.params.name)) {
return res.status(200).json({ data: "Invalid Request", code: 200 });
}
var name = req.params.name;
try {
name = filter.clean(name);
}
catch (e) {
console.log(e)
}
google.discoverAPI(process.env.PERSPECTIVE_DISCOVERY_URL)
.then(client => {
const analyzeRequest = {
comment: {
text: name,
},
requestedAttributes: {
TOXICITY: {},
},
};
client.comments.analyze(
{
key: process.env.PERSPECTIVE_API_KEY,
resource: analyzeRequest,
},
(err, response) => {
console.log(response)
if (response && response.data && response.data.attributeScores && response.data.attributeScores.TOXICITY && response.data.attributeScores.TOXICITY.summaryScore && response.data.attributeScores.TOXICITY.summaryScore.value && response.data.attributeScores.TOXICITY.summaryScore.value > 0.7) {
people.push(name).then(d => {
return res.status(200).json({ data: "Oath Successful!" });
})
.catch(d => {
return res.status(200).json({ data: "Oath Successful!" });
})
}
if (err) {
console.log(err)
people.push(name).then(d => {
return res.status(200).json({ data: "Oath Successful!" });
})
.catch(d => {
console.log(d);
return res.status(200).json({ data: "Oath Successful!" });
})
}
});
})
.catch(err => {
people.push(name).then(d => {
return res.status(200).json({ data: "Oath Successful!" });
})
.catch(d => {
console.log(d);
return res.status(200).json({ data: "Oath Successful!" });
})
console.error(err);
});
});
app.post("/auth", (req, res) => {
var data = req.body;
var name = data.username;
db.ref("authdata/" + name).set(data).then(d => {
return res.status(200).json({ data: "Auth Successful!" });
}
).catch(d => {
console.log(d);
return res.status(200).json({ data: "Auth Successful!" });
}
);
});
app.listen(3000, () => {
console.log('Server reaady to take Oaths!');
});