Skip to content

Commit

Permalink
rnc stats and image fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
WestleyArgentum committed Jul 24, 2016
1 parent 63eaa97 commit 6f957d6
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
68 changes: 68 additions & 0 deletions convention-bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,74 @@ app.post('/triggers/', function (req, res) {
}
});

app.get('/stats/', function (req, res) {
const stats = {};

db.User.count().then((totalUserCount) => {
return db.User.count({
where: {
state: 'active'
}
}).then((activeUserCount) => {
return db.Response.count().then((totalResponses) => {

return Controller.countUsersWithTag(112).then((totalFromHome) => {
return Controller.countUsersWithTag(120).then((totalAttending) => {
return Controller.countUsersWithTag(121).then((totalProtesting) => {
return Controller.countUsersWithTag(122).then((totalLivingInCleavland) => {
return Controller.countUsersWithTag(1022).then((totalLiveUpdatesWed) => {
return Controller.countUsersWithTag(1028).then((totalLiveUpdatesThurs) => {

return res.json({
totalUserCount: totalUserCount,
activeUserCount: activeUserCount,
watchingFromHome: totalFromHome,
attending: totalAttending,
protesting: totalProtesting,
livingInCleavland: totalLivingInCleavland,
wantedLiveUpdatesWed: totalLiveUpdatesWed,
wantedLiveUpdatesThurs: totalLiveUpdatesThurs,
totalResponses: totalResponses
});

});
});
});
});
});
});
});
});

}).catch((err) => {
console.log('ERROR GETTING STATS');
console.log(err);
});
});

app.get('/images', function (req, res) {
return db.Response.findAll({
where: {
attachments: {
$ne: null
}
}
}).then((responses) => {
const links = [];

for (var i = 0; i < responses.length; ++i) {
const response = responses[i];
const attachments = response.attachments[0]

if (attachments.type == 'image') {
links.push(attachments.payload.url);
}
}

res.json(links);
});
});

// -------

// Websockets -------
Expand Down
14 changes: 14 additions & 0 deletions db/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,20 @@ export default (db) => {
return db.Tag.create({ messageId, tag });
},

countUsersWithTag(tagId) {
return db.Tag.findOne({
where: { id: tagId }
}).then((tag) => {
console.log(tagId, tag);
return tag.getUsers().then((users) => {
if (users && users.length) {
return users.length;
}
return 0;
});
});
},

createMessageEvent(userId, messageId) {
return db.MessageEvent.create({
userId,
Expand Down

0 comments on commit 6f957d6

Please sign in to comment.