Skip to content

Commit

Permalink
Support for multiple sites
Browse files Browse the repository at this point in the history
  • Loading branch information
HalfdanJ committed Nov 14, 2015
1 parent e878346 commit 9d7dbb5
Show file tree
Hide file tree
Showing 16 changed files with 172 additions and 140 deletions.
62 changes: 16 additions & 46 deletions api.js
Expand Up @@ -27,14 +27,15 @@ module.exports = {
query("SELECT * FROM information_schema.tables where table_name = 'notes'", function(rows, ret){
if(ret.length == 0){
query('CREATE TABLE "public"."notes" (\
"id" serial,\
"id" serial,\
"time_begin" int,\
"time_end" int,\
"note" text,\
"ip" cidr,\
"timestamp" timestamp,\
"path" float[][],\
"hidden" boolean,\
"site" int NOT NULL DEFAULT 0, \
PRIMARY KEY ("id"));')
}
});
Expand Down Expand Up @@ -121,14 +122,15 @@ module.exports = {
var startTime = Math.round(req.query.timeframeStart);
var endTime = Math.round(req.query.timeframeEnd);
var ip = req.query.ip || req.ip;

var site = req.query.site || 0;
query(
'select id, time_begin, time_end, note, path ' +
'from notes ' +
'where time_end >= $1 ' +
'and time_begin <= $2 ' +
'and site = $3 ' +
'and ( ' +
'ip = $3 ' +
'ip = $4 ' +
'or not ( ' +
'hidden is true ' +
'or ( ' +
Expand All @@ -138,8 +140,7 @@ module.exports = {
'from blacklist ' +
'where ip = notes.ip)))) ' +
'limit 100',

[startTime, endTime, ip], function(err, ret){
[startTime, endTime, site, ip], function(err, ret){
if(err ){
res.status(500).send('Could not select notes');
console.log(err);
Expand Down Expand Up @@ -168,6 +169,7 @@ module.exports = {

var paths = req.body.path;
var text = req.body.text;
var site = req.body.site;

if(paths.length < 2){
res.status(500).send('At least 2 points in a path are required');
Expand All @@ -179,6 +181,11 @@ module.exports = {
return;
}

if(site === undefined){
res.status(500).send('Site is missing');
return;
}

if(text.length > 140) {
text = text.substr(0, 140);
}
Expand Down Expand Up @@ -224,14 +231,15 @@ module.exports = {
}

var hidden = boring.check(text);
query('INSERT INTO "public"."notes" ("time_begin", "time_end", "note", "ip", "timestamp", "path", "hidden") VALUES ($1, $2, $3, $4, now(), $5, $6) RETURNING id',
query('INSERT INTO "public"."notes" ("time_begin", "time_end", "note", "ip", "timestamp", "path", "hidden", "site") VALUES ($1, $2, $3, $4, now(), $5, $6, $7) RETURNING id',
[
Math.round(paths[0].time),
Math.round(paths[paths.length-1].time),
text,
req.ip,
"{"+ q.join(",")+"}",
hidden ? true : null
hidden ? true : null,
site
], function(err, ret) {

if(err || ret.length == 0){
Expand All @@ -245,45 +253,7 @@ module.exports = {
})
}
);
})



/* query('INSERT INTO "public"."notes" ("time_begin", "time_end", "note", "ip", "timestamp") VALUES ($1, $2, $3, $4, now()) RETURNING id',
[
Math.round(paths[0].time),
Math.round(paths[paths.length-1].time),
"test",
req.ip
], function(err, ret){
if(err || ret.length == 0){
res.status(500).send('Could not submit note');
console.log(err);
return;
}
var note_id = ret[0].id;
var q = '';
for(var i=0;i<paths.length;i++){
q += 'INSERT INTO "public"."paths" ("coordinate", "time", "note_id") VALUES (point('+paths[i].x+','+paths[i].y+') ,'+paths[i].time+','+note_id+');'
}
query(q, function(err, ret){
if(err){
res.status(500).send('Could not submit note path');
console.log(err);
return;
}
res.send({id:note_id});
});
});*/


});
})

}
Expand Down
33 changes: 24 additions & 9 deletions index.js
Expand Up @@ -53,15 +53,20 @@ app.use(sassMiddleware({

app.use(raven.middleware.express('https://edf1ff6b26ca41b0a9bbb280902b8c4e:e709b93edcdf49aabf54f637c90bf6b0@app.getsentry.com/41348'));

app.get('/', function(req, res){
md = new MobileDetect(req.headers['user-agent']);
if(md.mobile()) {
res.sendFile(__dirname + '/public/mobile.html');
} else {
res.sendFile(__dirname + '/public/index.html');
// want to use "next()" here
}
})


app.get('/', function(req, res) {
res.redirect('/london');
});

app.get('/site2', function(req, res) {
returnSite(req,res);
});

app.get('/london', function(req, res) {
returnSite(req,res);
});


app.use('/', express.static(__dirname + '/public'));

Expand All @@ -72,3 +77,13 @@ app.use('/api', api.api);
app.listen(app.get('port'), function() {
console.log("Node app is running at localhost:" + app.get('port'));
});


function returnSite(req,res){
md = new MobileDetect(req.headers['user-agent']);
if(md.mobile()) {
res.sendFile(__dirname + '/public/mobile.html');
} else {
res.sendFile(__dirname + '/public/index.html');
}
}
10 changes: 5 additions & 5 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "node-js-getting-started",
"version": "0.1.2",
"description": "A sample Node.js app using Express 4",
"name": "exhausting-a-crowd",
"version": "0.2.0",
"description": "Exhausting a crowd",
"main": "index.js",
"scripts": {
"start": "node index.js"
Expand All @@ -11,7 +11,7 @@
"body-parser": "^1.12.2",
"express": "~4.9.x",
"mobile-detect": "^1.2.0",
"node-sass-middleware": "^0.5.0",
"node-sass-middleware": "^0.9.7",
"pg": "^4.3.0",
"pg-query": "^0.11.0",
"raven": "^0.7.3",
Expand All @@ -25,7 +25,7 @@
},
"repository": {
"type": "git",
"url": "https://github.com/heroku/node-js-getting-started"
"url": "https://github.com/kylemcdonald/exhaustingacrowd"
},
"keywords": [
"node",
Expand Down
45 changes: 22 additions & 23 deletions public/compiled/VideoPlayer.js
@@ -1,19 +1,21 @@
/// <reference path="../typings/tsd.d.ts" />
var VideoPlayer = (function () {
function VideoPlayer(events) {
function VideoPlayer(playlist, durations, modulusHours, events) {
var _this = this;
this.aspect = 16.0 / 9.0;
this.zoom = 1.0;
this.zoomPos = { x: 0, y: 0 };
this.loading = true;
this.startTimes = [];
this.durations = [7650, 4941, 7424, 7264, 6835, 7128];
this.durations = [];
this.modulusHours = 1;
this.totalDur = 0;
/** Current time in millis **/
this.currentTime = 0;
// onStateChange callback
this.stateChangeCallback = function (state) {
};
this.stateChangeCallback = function (state) { };
this.durations = durations;
this.modulusHours = modulusHours;
// Populate the startTimes array
var _dur = 0;
for (var i = 0; i < this.durations.length; i++) {
Expand All @@ -37,17 +39,13 @@ var VideoPlayer = (function () {
origin: 'localhost',
rel: 0,
showinfo: 0,
list: 'PLscUku2aaZnFE-7wKovrbi76b26VKxIT-',
list: playlist,
listType: 'playlist',
start: 0
},
events: {
'onReady': function () {
_this.onPlayerReady();
},
'onStateChange': function () {
_this.onPlayerStateChange();
}
'onReady': function () { _this.onPlayerReady(); },
'onStateChange': function () { _this.onPlayerStateChange(); }
}
});
}
Expand Down Expand Up @@ -131,7 +129,7 @@ var VideoPlayer = (function () {
};
VideoPlayer.prototype.seek = function (ms, cb, dontFetchApi) {
var _this = this;
console.log('seek: ' + ms);
//console.log('seek: ' + ms);
if (ms > this.totalDur) {
ms %= this.totalDur; // loops back around to 3:00 - 3:27
}
Expand Down Expand Up @@ -175,9 +173,7 @@ var VideoPlayer = (function () {
if (this.events.onLoadComplete) {
this.events.onLoadComplete(this);
}
setInterval(function () {
_this.frameUpdate();
}, 10);
setInterval(function () { _this.frameUpdate(); }, 10);
}
};
// use this from the frontend for testing
Expand All @@ -189,19 +185,22 @@ var VideoPlayer = (function () {
// use the startTime data
var target = moment(Clock.startTime);
// use the time hours, minutes, seconds
target.hour(time.hour() % 12);
target.hour(time.hour());
target.minute(time.minute());
target.second(time.second());
// bermuda triangle modifies hour randomly after 2:27
var sincetwo = time.minute() * 60 + time.second();
if (target.hour() == 2 && sincetwo > 1642) {
target.hour(Math.floor(Math.random() * 12));
}
target.hour(12 + target.hour()); // always assume afternoon
// If the target is before the start clock of the video (its in the morning)
if (target.isBefore(moment(Clock.startTime))) {
target = target.add(12, 'hours');
target = target.add(24, 'hours');
}
var hourMillis = 60 * 60 * 1000;
var diff = target.diff(moment(Clock.startTime));
// modulus with the number of hours specified
diff %= this.modulusHours * hourMillis;
// Handle the case where the time is longer then the playlist, then pick a random hour
if (diff > this.totalDur) {
diff -= Math.floor(Math.random() * this.modulusHours) * hourMillis;
}
console.log(moment(Clock.startTime).add(diff, 'milliseconds').format());
video.seek(diff, cb);
};
return VideoPlayer;
Expand Down
16 changes: 8 additions & 8 deletions public/compiled/api.js
@@ -1,18 +1,18 @@
/// <reference path="../typings/tsd.d.ts" />
/// <reference path="note.ts" />
var NotesApi = (function () {
function NotesApi() {
function NotesApi(site) {
var _this = this;
this.notes = [];
this.currentTime = 0;
this.submitNoteThrottle = _.throttle(function (note) {
console.log("Submit ", note.path.points, note.text);
//console.log("Submit ", note.path.points, note.text);
ga('send', 'event', 'API', 'SubmitNote', 'submit');
$.ajax({
type: "POST",
dataType: "json",
url: "/api/notes",
data: JSON.stringify({ path: note.path.points, text: note.text }),
data: JSON.stringify({ path: note.path.points, text: note.text, site: _this.siteId }),
contentType: "application/json; charset=utf-8",
success: function () {
setTimeout(function () {
Expand All @@ -21,28 +21,28 @@ var NotesApi = (function () {
}
});
}, 5000);
this.siteId = site;
}
NotesApi.prototype.startFetching = function (fetchRate, fetchWindowSize) {
var _this = this;
this.fetchRate = fetchRate;
this.fetchWindowSize = fetchWindowSize;
setInterval(function () {
_this.fetchNotes();
}, 15000);
setInterval(function () { _this.fetchNotes(); }, 15000);
this.fetchNotes();
};
NotesApi.prototype.fetchNotes = function (_currentTime) {
var _this = this;
if (_currentTime) {
this.currentTime = _currentTime;
}
console.log("Fetch", this.fetchWindowSize, this.currentTime);
//console.log("Fetch",this.fetchWindowSize,this.currentTime);
$.ajax({
dataType: "json",
url: "/api/notes",
data: {
timeframeStart: this.currentTime - 2000,
timeframeEnd: this.currentTime + this.fetchWindowSize
timeframeEnd: this.currentTime + this.fetchWindowSize,
site: this.siteId
},
success: function (data) {
for (var i = 0; i < data.length; i++) {
Expand Down
5 changes: 1 addition & 4 deletions public/compiled/clock.js
Expand Up @@ -5,9 +5,7 @@ var Clock = (function () {
var _this = this;
this.clockTime = new Date(Clock.startTime);
this.colon = $('#colon');
setInterval(function () {
_this.updateClock();
}, 1000);
setInterval(function () { _this.updateClock(); }, 1000);
}
Clock.prototype.blink = function (elm) {
if (elm.css('opacity') == '1') {
Expand All @@ -34,6 +32,5 @@ var Clock = (function () {
this.clockTime = new Date(Clock.startTime);
this.clockTime.setSeconds(this.clockTime.getSeconds() + ytplayer.currentTime / 1000.0);
};
Clock.startTime = "April 15, 2015 15:00:00";
return Clock;
})();

0 comments on commit 9d7dbb5

Please sign in to comment.