-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
294 lines (276 loc) · 9.19 KB
/
app.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Module dependencies.
*/
var mongojs = require('mongojs');
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var fb = require('facebook-js');
var app = express();
var db = mongojs('WTBass', ['pictures', 'votes', 'users']);
var appData = require('./ID.js');
// all environments
app.set('port', process.env.PORT || 3456);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(require('stylus').middleware(__dirname + '/public'));
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/start', function (req, res) {
res.redirect(fb.getAuthorizeUrl({
client_id: appData.AppID,
redirect_uri: 'http://aryazeghbali.ir:3456/auth',
scope: 'offline_access,user_photos'
}));
});
//Get user profile
app.get('/auth', function (req, res) {
fb.getAccessToken(appData.AppID, appData.AppSecret, req.param('code'), 'http://aryazeghbali.ir:3456/auth', function (error, access_token, refresh_token) {
if(error) {
res.render('error', {error: error.data});
}
else {
req.session.access_token = access_token;
fb.apiCall('GET', '/me/profile', {access_token: req.session.access_token}, function (error, response, body) {
if(body.data == null) {
res.render('error', {error: "login first"});
}
else
{
var userID = body.data[0].id;
db.pictures.findOne({id: userID}, function (err, data) {
if(!err && data != null && data.lastPic >= data.pictures.length) {
res.render('error', {error: 'You had been subscribed to this vote'});
}
else {
req.session.access_token = access_token;
// res.render('client', {title: 'Do you go?'});
res.redirect('photo');
}
});
}
});
}
});
});
function shuffle(o){ //v1.0
for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
//This function get all user's photos and save it to database and shuffle them,
app.get('/photo', function (req, res) {
fb.apiCall('GET', '/me/profile', {access_token: req.session.access_token}, function (error, response, body) {
if(body.data == null) {
console.log(body);
res.render('error', {error: "login first"});
}
else {
db.users.save(body, function (err7) {
var userID = body.data[0].id;
req.session.userID = userID;
var pictures = [];
fb.apiCall('GET', '/me/photos', {access_token: req.session.access_token, limit: 300}, function (error, response, bbody) {
if(bbody == null)
res.render('error', {error: "no picture!"});
else {
for(i in bbody.data) {
var pic = bbody.data[i];
var validTags = [];
var users = [];
if(pic.tags)
{
for (var i = pic.tags.data.length - 1; i >= 0; i--) {
if(pic.tags.data[i].id) {
validTags.push(pic.tags.data[i]);
users.push(pic.tags.data[i].id);
}
}
if(validTags.length >= 3 && validTags.length <= 10) {
pictures.push({picID: pic.id, users: users, pic: pic.source, width: pic.width, height: pic.height, tags: validTags, votes: []});
}
}
}
// console.log(pictures[pictures.length-1].picID);
pictures = shuffle(pictures);
// console.log(pictures[pictures.length-1].picID);
console.log(pictures.length);
function insert2db (n, err, callback) {
if(n < 0)
return callback(err);
db.pictures.find({picID: pictures[n].picID}, function (err, data) {
if (err || data.length == 0) {
db.pictures.save(pictures[n], function (err2) {
return insert2db(n-1, err2, callback);
});
}
else {
return insert2db(n-1, err, callback);
}
});
}
insert2db (pictures.length-1, null, function (err) {
if(err){
res.render('error', {error: err});
}
else{
res.redirect('vote');
}
});
}
});
});
}
});
});
// This function generate some question about the people who were tagged.
function getNextPic (req, res, callback, callback2) {
db.pictures.find({users: req.session.userID}, function (err, data) {
for (var i = data.length - 1; i >= 0; i--) {
var flag = false;
var votedUsers = []
for (var j = data[i].votes.length - 1; j >= 0; j--) {
if(data[i].votes[j].userID == req.session.userID)
flag = true;
else
{
votedUsers.push(data[i].votes[j].userID);
console.log("DD " + data[i].votes[j].userID )
}
}
if(!flag) {
var Qs = [];
data[i].votedUsers = votedUsers;
var tars = shuffle(["Cinema", "Park", "a Trip", "Karting", "Climbing", "Jogging", "Workshop"]);
var qCount = 0;
if (data[i].tags.length < 4)
qCount = 2
else if (data[i].tags.length < 5)
qCount = 3
else if (data[i].tags.length < 7)
qCount = 6
else
qCount =7
for (var k = 0; k < qCount; k++) {
var question = {voter : req.session.userID, target: tars[k], fn: [], fy: [], id : 1};
for (var j = data[i].tags.length - 1; j >= 0; j--) {
if(data[i].tags[j].id != req.session.userID){
var r = Math.random();
// This is a random selection of people who goes to the event or not
if(r < 1.3/3.0)
{
question.fn.push({name : data[i].tags[j].name, id : data[i].tags[j].id} );
}
else if(r < 2.6/3.0)
question.fy.push({name : data[i].tags[j].name, id : data[i].tags[j].id} );
}
}
Qs.push(question);
}
function insert2db (n, err, callback) {
if(n < 0)
return callback(err);
db.votes.find({voter: req.session.userID, fn : Qs[n].fn, fy : Qs[n].fy}, function (err, data2) {
if (err || data2.length == 0) {
Qs[n].qid = db.ObjectId();
db.votes.insert({voter: req.session.userID, fn : Qs[n].fn, fy : Qs[n].fy, _id: Qs[n].qid, yCount: 0, nCount: 0}, function (err2) {
return insert2db(n-1, err2, callback);
});
}
else {
Qs[n].qid = db.ObjectId();
return insert2db(n-1, err, callback);
}
});
}
return insert2db (Qs.length-1, null, function (err) {
if(err){
res.render('error', {error: "There are some problems."});
}
else{
return callback(data[i], Qs);
}
});
}
}
return callback2();
});
}
app.get('/finish', function (req, res) {
res.render('done' ,{title: 'Done', body : 'Thank\'s for your participation :)'});
});
// This function
app.get('/vote', function (req, res) {
if(req.query.id){
db.pictures.findOne({picID: req.query.id}, function (err3, data3) {
if(data3) {
var flag = false;
for (var i = data3.votes.length - 1; i >= 0; i--) {
if(data3.votes[i].userID == req.session.userID)
flag = true;
}
if(!flag) {
db.pictures.update({picID: req.query.id}, {$push: {votes: {userID: req.session.userID, vote: true}}}, function (err2, data2) {
for(xxx in req.query) {
if(xxx.substring(0, 1) == 'V'){
//Store answers in database
if(req.query[xxx] == 'y')
db.votes.update({_id: db.ObjectId(xxx.substring(2))}, {$inc: {yCount: 1}});
else if(req.query[xxx] == 'n')
db.votes.update({_id: db.ObjectId(xxx.substring(2))}, {$inc: {nCount: 1}});
}
}
getNextPic(req, res, function (d, q) {
return res.render('vote2', {title: 'vote', picture: d, questions: q, userID : req.session.userID });
}, function () {
res.render('done' ,{title: 'Done', body : 'It\'s finished, Thank You! :)'});
});
});
}
else {
getNextPic(req, res, function (d, q) {
return res.render('vote2', {title: 'vote', picture: d, questions: q, userID : req.session.userID});
}, function () {
res.render('done' ,{title: 'Done', body : 'It\'s finished, Thank You! :)'});
});
}
}
else {
getNextPic(req, res, function (d, q) {
return res.render('vote2', {title: 'vote', picture: d, questions: q, userID : req.session.userID});
}, function () {
res.render('done' ,{title: 'Done', body : 'It\'s finished, Thank You! :)'});
});
}
});
}
else {
getNextPic(req, res, function (d, q) {
return res.render('vote2', {title: 'vote', picture: d, questions: q, userID : req.session.userID});
}, function () {
res.render('done' ,{title: 'Done', body : 'You don\'t have any pictures left. Thanks for your time :)'});
});
}
});
app.get(appData.resetURL, function (req, res) {
db.pictures.drop(function (err) {
db.votes.drop(function (err2) {
res.redirect('/start');
});
});
});
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});