forked from jerryh91/turnabout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
executable file
·335 lines (290 loc) · 11.2 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
//
// app.js
//
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var passport = require('passport');
var mongoose = require('mongoose'); //Object Data Mapper: mongoose enforces Schema
var models = require('./models/models.js'); // 'var models' not used anywhere yet
var api = require('./routes/api'); //router
var authenticate = require('./routes/authenticate')(passport); //router
var session = require('express-session');
var initPassport = require('./passport-init'); //needs models.js to be loaded first
var debug = require('debug')('app');
// Initialize Passport
initPassport(passport);
//Connect to MongoDB
//mongoose.connect('mongodb://localhost:27017/whysosingle');
mongoose.connect('mongodb://user1:[email protected]:61415/heroku_2jjn5lqq');
var User = mongoose.model('User');
var Conversation = mongoose.model('Conversation');
// var Message = mongoose.model('Message');
var app = express();
var port = process.env.PORT || 1337;
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser('my_secret'));
app.use(session({secret: 'our secret',
saveUninitialized: true,
resave: true}));
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
//map routers to uri
app.use('/', api);
app.use('/auth', authenticate);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handlers
if(app.get('env') === 'development') {
app.use(function(err, req, res, next) {
console.log('development error handler');
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err // will print stacktrace
});
});
} else {
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
console.log('production error handler');
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
}
//Passed the ExpressJS server to Socket.io.
//In effect, our real time communication will still happen on the same port.
var io = require('socket.io').listen(app.listen(port, function()
{
console.log('ready on port: ' + port);
}));
io.sockets.on('connection', function (socket)
{
console.log('socket:s connection event');
//Load existing Conversations for logged in user
//to message var
socket.emit('joined_server', { message: 'welcome to the chat' });
socket.on('join', function (data) {
socket.join(data.username); // We are using room of socket io
console.log("socket io room joined: " + data.username);
});
//socket listens for "send" event
socket.on('send', function (data)
{
var msgSenderUsername = data.senderUsername;
var msgReceiverUsername = data.receiverUsername;
var msgDate = data.date;
var msgContent = data.content;
var messageObject = {
senderUsername: msgSenderUsername,
receiverUsername: msgReceiverUsername,
content: msgContent,
date: msgDate
};
console.log(messageObject);
// if(socket.handshake.headers.cookie)
// {
// console.log("Cookie found");
// // var cookie = cookie.parse(socket.handshake.headers.cookie);
// // var sessionID = parseSignedCookie(cookie['connect.sid'], 'secret');
// }
//TODO:
//Limit broadcast only to person
//listening/receiving this particular conversation
//io.sockets.emit('my_message', data);
io.sockets.in(msgReceiverUsername).emit('new_message', {username: msgSenderUsername, message: msgContent, dateOfMessage: msgDate});
io.sockets.in(msgSenderUsername).emit('new_message', {username: msgSenderUsername, message: msgContent, dateOfMessage: msgDate});
//data.message, data.username (sender)
// console.log("data.message: " + data.message);
// console.log("data.username: " + data.username);
//Create message document
//add doc to Conversation doc's msg list
// TODO:
// Retrieve receiver ID
// var messageDoc = {
// senderUsername: data.username,
// receiverUsername: 'test1',
// content: data.message
// };
//C:\Projects\whysosingle\app_js.txt
//Find Gender of Sender Profile
// var msgID = messageDoc._id;
// console.log('messageDoc::msgID: ' + msgID);
Conversation.findOne()
.or([{initiatorUsername: msgSenderUsername, responderUsername: msgReceiverUsername},
{initiatorUsername: msgReceiverUsername, responderUsername: msgSenderUsername}])
.exec(function(err, conversation){
if(err){
console.log(err);
}
if(conversation){
//
// ADD TO EXISTING CONVERSATION
//
conversation.messages.push(messageObject);
Conversation.findOneAndUpdate({_id: conversation._id}, {$push: {messages: messageObject}}, function(err, raw){
if(err){
console.log(err);
}
if(raw){
console.log("updated conversation");
} else{
console.log("conversation not found!");
}
});
} else{
//
// CREATE CONVERSATION
//
console.log("conversation not found. Will create conversation if valid");
checkIfValidConversation(msgSenderUsername, function (){
var conversationDoc = new Conversation({
initiatorUsername: msgSenderUsername,
responderUsername: msgReceiverUsername,
messages: [messageObject]
});
conversationDoc.save(function(err, newConv){
if(err){
console.log(err);
}
console.log('Successfully added new Conv: ', newConv);
User.findOneAndUpdate({username: newConv.initiatorUsername}, {$push: {conversations: newConv._id}}, function(err, raw){
if(err){
console.log(err);
}
if(raw){
console.log("updated ", newConv.initiatorUsername);
} else{
console.log("user doesn't exist: ", newConv.initiatorUsername);
}
});
User.findOneAndUpdate({username: newConv.responderUsername}, {$push: {conversations: newConv._id}}, function(err, raw){
if(err){
console.log(err);
}
if(raw){
console.log("updated ", newConv.responderUsername);
} else{
console.log("user doesn't exist: ", newConv.responderUsername);
}
});
});
});
}
});
function checkIfValidConversation(senderUsername, callback){
var msgSenderGender;
var convQuery;
var senderQuery = User.findOne({ username: senderUsername});
senderQuery.exec(function (err, user) {
msgSenderGender = user.gender;
console.log("msgSenderGender (found in db): " + msgSenderGender);
if(msgSenderGender == "female"){
callback();
} else {
console.log("gender male. Cannot start conversation");
}
});
}
// //PASS:10/22
// if (msgSenderGender == "male")
// {
// console.log('msgSenderGender: male');
// //DEBUG:
// //Create a Conversation, if no conversation
// convQuery = {initiatorUsername: msgReceiverUsername, responderUsername: msgSenderUsername};
// Conversation.update(convQuery, {$push: {messages: msgID}}, function (err, raw)
// {
// if (err)
// {
// console.log(err);
// }
// console.log("Mongo raw response: ", raw);
// console.log("Added MsgID: ", msgID);
// });
// } else //female
// {
// console.log('msgSenderGender: ' + msgSenderGender);
// console.log('msgReceiverUsername: ' + msgReceiverUsername);
// //Message:
// //part of Existing Conversation
// //or
// //initial messag in new Conversation
// //Check existing Conversation
// convQuery = { initiatorUsername:msgSenderUsername,
// responderUsername: msgReceiverUsername} ;
// Conversation.findOne(convQuery, function (err, conv) {
// //No Conversation
// //PASS: 10/23
// if (!conv)
// {
// console.log('no conversationDoc: ');
// //Create new Conversation
// var conversationDoc = new Conversation ({
// initiatorUsername: msgSenderUsername,
// responderUsername: msgReceiverUsername,
// //TODO:
// //Add new message to Conversation
// messages: [msgID]
// });
// var conversationDocID = conversationDoc._id;
// console.log('conversationDocID: ' + conversationDocID);
// conversationDoc.save(function(err, newConv)
// {
// if (err)
// {
// return (err, false);
// }
// console.log('Successfully added new Conv: ', newConv);
// // return done(null, newConv);
// });
// //Add to User: new ConversationID
// var userQuery = {username: msgSenderUsername};
// User.update(userQuery, {$push: {conversations: conversationDocID + ''}}, function (err, raw)
// {
// if (err)
// {
// console.log(err);
// }
// console.log("Mongo raw response: ", raw);
// console.log("Added convId: ", conversationDocID );
// });
// }
// else
// {
// //add message to existing Conversation
// // conv.messages.add()
// conv.messages.push(msgID);
// conv.save(function(err, updateConv)
// {
// if (err)
// {
// return (err, false);
// }
// console.log('successfully added ', msgID, 'to existing conv');
// });
// }
// });
// }
// });
});
});
module.exports = app;