diff --git a/controllers/prototype/dbrequests.js b/controllers/prototype/dbrequests.js index 6333294..e4f2671 100644 --- a/controllers/prototype/dbrequests.js +++ b/controllers/prototype/dbrequests.js @@ -1270,7 +1270,13 @@ DBReq.UserVerifier.prototype.execute = function() { }, function(err, result) { var ok = result.reduce(function(prev, next) { return prev && next; }); - self.finish(ok); + self.client.query( + "UPDATE Users SET valid = $1 WHERE id = $2", + [ok, self.userId], + function(err, result) { + self.finish(ok); + } + ); } ); } @@ -1286,7 +1292,40 @@ DBReq.UserVerifier.prototype.finish = function(finalResult) { this.finishAction(finalResult); }; +DBReq.UserGetter = function(userId, finishAction) { + this.userId = userId; + this.finishAction = finishAction; + var self = this; + pg.connect(pgc.url, function(err, client, release) { + self.client = client; + self.release = release; + + self.execute(); + }); +}; + +DBReq.UserGetter.prototype.execute = function() { + var self = this; + + this.client.query( + 'SELECT worker_id AS "workerId", valid FROM Users WHERE id = $1', + [self.userId], + function(err, result) { + self.finish(result.rows[0].workerId, result.rows[0].valid); + } + ); +}; + +DBReq.UserGetter.prototype.finish = function(workerId, valid) { + + this.release(); + this.release = null; + this.client = null; + + this.finishAction(workerId, valid); + +}; /** * Try to get a user by id, and creates it if it doesn't exists @@ -1386,4 +1425,8 @@ DBReq.verifyUser = function(id, callback) { new DBReq.UserVerifier(id, callback); }; +DBReq.getUser = function(id, callback) { + new DBReq.UserGetter(id, callback); +} + module.exports = DBReq; diff --git a/controllers/thankyou/index.js b/controllers/thankyou/index.js index fb4accc..d17582d 100644 --- a/controllers/thankyou/index.js +++ b/controllers/thankyou/index.js @@ -3,24 +3,23 @@ var vcode = require('../../lib/vcode.js'); module.exports.index = function(req, res) { - db.verifyUser(req.session.userId, function(ok) { + // If not micro-worker + if (req.session.workerId === undefined) { - if (ok) { + res.setHead('Content-Type', 'text/html'); - res.locals.vcode = vcode(req.session.workerId, req.session.campaignIp); - - } - - res.locals.workerId = req.session.workerId; - req.session = null; - res.locals.session = null; - - res.setHeader('Content-Type', 'text/html'); - - res.render('index.jade', res.locals, function(err, result) { + res.render('normal.jade', res.locals, function(err, result) { res.send(result); }); + return; + } + + res.setHeader('Content-Type', 'text/html'); + + res.render('vcode.jade', res.locals, function(err, result) { + console.log(err); + res.send(result); }); }; diff --git a/controllers/thankyou/views/index.jade b/controllers/thankyou/views/normal.jade similarity index 58% rename from controllers/thankyou/views/index.jade rename to controllers/thankyou/views/normal.jade index 53a86d8..821cda0 100644 --- a/controllers/thankyou/views/index.jade +++ b/controllers/thankyou/views/normal.jade @@ -6,8 +6,3 @@ block content source(src="/static/data/music/thankyou.ogg") source(src="/static/data/music/thankyou.mp3") - if (vcode !== undefined) - p Your vcode is #{vcode}. - else if (workerId !== undefined) - p You have no vcode because you're bad - diff --git a/controllers/thankyou/views/vcode.jade b/controllers/thankyou/views/vcode.jade new file mode 100644 index 0000000..73a826b --- /dev/null +++ b/controllers/thankyou/views/vcode.jade @@ -0,0 +1,36 @@ +extends ../../../views/base.jade + +block content + h1 Thank you for everything ! + audio(autoplay) + source(src="/static/data/music/thankyou.ogg") + source(src="/static/data/music/thankyou.mp3") + + p We are verifying that the experiment was correctly done... + #vcode + + script. + function tryVcode() { + var xhr = new XMLHttpRequest(); + xhr.open('GET', '/vcode', true); + xhr.onreadystatechange = function (aEvt) { + if (xhr.readyState == 4) { + if(xhr.status == 200) { + changeHtml(xhr.responseText); + } + } + }; + xhr.send(null); + } + + function changeHtml(vcode) { + if (vcode === 'not ready') { + setTimeout(tryVcode, 1000); + } else if (vcode === 'no vcode') { + $('#vcode').html('Sorry, the experiment has not been correctly done ! You have no vcode.'); + } else { + $('#vcode').html('Your vcode is ' + vcode); + } + } + + tryVcode(); diff --git a/controllers/vcode/index.js b/controllers/vcode/index.js new file mode 100644 index 0000000..3ac1ca5 --- /dev/null +++ b/controllers/vcode/index.js @@ -0,0 +1,39 @@ +var db = require('../prototype/dbrequests.js'); +var vcode = require('../../lib/vcode.js'); + +module.exports.index = function(req, res) { + + // If not micro-worker + if (req.session.workerId === undefined) { + + res.setHeader('Content-Type', 'text/html'); + + res.send(null); + + return; + } + + // Else, check that exp was correctly done + db.getUser(req.session.userId, function(workerId, ok) { + + res.setHeader('Content-Type', 'text/html'); + + if (ok === true) { + + var code = vcode(req.session.workerId); + res.send(code); + + } else if (ok === false) { + + res.send('no vcode'); + + } else { + + res.send('not ready'); + + } + + }); + +}; + diff --git a/controllers/vcode/urls.js b/controllers/vcode/urls.js new file mode 100644 index 0000000..42d5339 --- /dev/null +++ b/controllers/vcode/urls.js @@ -0,0 +1,3 @@ +module.exports = { + '/vcode': 'index', +}; diff --git a/posts/feedback-target/index.js b/posts/feedback-target/index.js index 53a573d..8af8894 100644 --- a/posts/feedback-target/index.js +++ b/posts/feedback-target/index.js @@ -1,16 +1,20 @@ var pg = require('pg'); var pgc = require('../../private.js'); +var db = require('../../controllers/prototype/dbrequests.js'); var mail = require('../../lib/mail.js'); var Log = require('../../lib/NodeLog.js'); module.exports.index = function(req, res) { + db.verifyUser(req.session.userId, function() {}); + var text = ''; for (var i in req.body) { text += i + ' : ' + req.body[i] + '\n'; } + pg.connect(pgc.url, function(err, client, release) { client.query( diff --git a/sql/backup.pgsql b/sql/backup.pgsql index bb572eb..17508a1 100644 --- a/sql/backup.pgsql +++ b/sql/backup.pgsql @@ -42,7 +42,8 @@ CREATE TABLE Users( age VARCHAR(10), rating INT, lasttime INT, - male BOOLEAN + male BOOLEAN, + valid BOOLEAN DEFAULT NULL ); CREATE TABLE Scene(