Merge pull request #18 from ajayyy/experimental

Username support
This commit is contained in:
Ajay Ramachandran 2019-08-12 23:43:17 -04:00 committed by GitHub
commit 0072cdb17b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -251,6 +251,65 @@ app.get('/api/viewedVideoSponsorTime', function (req, res) {
res.sendStatus(200); res.sendStatus(200);
}); });
//To set your username for the stats view
app.post('/api/setUsername', function (req, res) {
let userID = req.query.userID;
let userName = req.query.username;
if (userID == undefined || userName == undefined) {
//invalid request
res.sendStatus(400);
return;
}
//hash the userID
userID = getHash(userID);
//check if username is already set
db.prepare("SELECT count(*) as count FROM userNames WHERE userID = ?").get(userID, function(err, row) {
if (err) console.log(err);
if (row.count > 0) {
//already exists, update this row
db.prepare("UPDATE userNames SET userName = ? WHERE userID = ?").run(userName, userID);
} else {
//add to the db
db.prepare("INSERT INTO userNames VALUES(?, ?)").run(userID, userName);
}
res.sendStatus(200);
});
});
//get what username this user has
app.get('/api/getUsername', function (req, res) {
let userID = req.query.userID;
if (userID == undefined) {
//invalid request
res.sendStatus(400);
return;
}
//hash the userID
userID = getHash(userID);
db.prepare("SELECT userName FROM userNames WHERE userID = ?").get(userID, function(err, row) {
if (err) console.log(err);
if (row != null) {
res.send({
userName: row.userName
});
} else {
//no username yet, just send back the userID
res.send({
userName: userID
});
}
});
});
//Gets all the views added up for one userID //Gets all the views added up for one userID
//Useful to see how much one user has contributed //Useful to see how much one user has contributed
app.get('/api/getViewsForUser', function (req, res) { app.get('/api/getViewsForUser', function (req, res) {
@ -307,9 +366,14 @@ app.get('/api/getTopUsers', function (req, res) {
let totalSubmissions = []; let totalSubmissions = [];
let minutesSaved = []; let minutesSaved = [];
db.prepare("SELECT userID, COUNT(*) as totalSubmissions, SUM(views) as viewCount, SUM((endTime - startTime) / 60 * views) as minutesSaved FROM sponsorTimes WHERE votes > -1 GROUP BY userID ORDER BY " + sortBy + " DESC LIMIT 50").all(function(err, rows) { db.prepare("SELECT sponsorTimes.userID as userID, COUNT(*) as totalSubmissions, SUM(views) as viewCount, SUM((sponsorTimes.endTime - sponsorTimes.startTime) / 60 * sponsorTimes.views) as minutesSaved, userNames.userName as userName FROM sponsorTimes LEFT JOIN userNames ON sponsorTimes.userID=userNames.userID WHERE sponsorTimes.votes > -1 GROUP BY sponsorTimes.userID ORDER BY " + sortBy + " DESC LIMIT 50").all(function(err, rows) {
for (let i = 0; i < rows.length; i++) { for (let i = 0; i < rows.length; i++) {
if (rows[i].userName != null) {
userNames[i] = rows[i].userName;
} else {
userNames[i] = rows[i].userID; userNames[i] = rows[i].userID;
}
viewCounts[i] = rows[i].viewCount; viewCounts[i] = rows[i].viewCount;
totalSubmissions[i] = rows[i].totalSubmissions; totalSubmissions[i] = rows[i].totalSubmissions;
minutesSaved[i] = rows[i].minutesSaved; minutesSaved[i] = rows[i].minutesSaved;