From 0c785ab730997972b573c48d25f958dea5ad62b9 Mon Sep 17 00:00:00 2001 From: nanalelfe Date: Sun, 24 Jul 2016 01:49:46 -0400 Subject: Implemented messaging system. Form neesds to be fixed --- assets/js/script.js | 8 ++++-- node_simple.js | 68 +++++++++++++++++++++++++++++++++++++++++++--- routes/user.js | 51 ++++++++++++++++++++++++++++++++-- views/layouts/layout.hbs | 3 -- views/user_profile_alt.hbs | 58 ++++++++++++++++++++++++++------------- 5 files changed, 158 insertions(+), 30 deletions(-) diff --git a/assets/js/script.js b/assets/js/script.js index f08ca7a..5531c1e 100644 --- a/assets/js/script.js +++ b/assets/js/script.js @@ -6,7 +6,8 @@ function main(){ $("#profile-follows"), $("#profile-friends"), $("#profile-inbox"), - $("#profile-comments")]; + $("#profile-comments"), + $('#profile-send-message')]; function hidePages(){ items.forEach(function(item){ @@ -32,7 +33,7 @@ function main(){ console.log("clicked general"); $("#profile-general").show(); break; - case "Follows": + case "Following": $("#profile-follows").show(); break; case "Friends": @@ -44,6 +45,9 @@ function main(){ case "Comments": $("#profile-comments").show(); break; + case "Send Message": + $('#profile-send-message').show(); + break; default: console.log("Error: User page not found"); } diff --git a/node_simple.js b/node_simple.js index 2976b58..cf5c372 100644 --- a/node_simple.js +++ b/node_simple.js @@ -157,7 +157,6 @@ exports.retrieveFollows = function (user_name, callback) { }); }; - // will add comments ASAP /* callback(success, data/message) => callback(boolean, object/String); * @@ -279,7 +278,7 @@ exports.add_user = function (fields, callbackUser) { if (result == false) { // find out if the user_name is taken - exports.find_user_name( fields[1], callbackUser, function (docs) { + exports.find_user_name( fields[1], function (docs) { if (docs == false) { // if not ... // continue console.log("user name is valid"); @@ -333,7 +332,7 @@ exports.add_user = function (fields, callbackUser) { * This (helper) function returns true IFF user_name already exists in the database * Params: user_name - the user name * */ -exports.find_user_name = function (user_name, callbackUser, callback) { +exports.find_user_name = function (user_name, callback) { // make a connection mongoFactory.getConnection(uri) .then(function (db) { @@ -961,7 +960,7 @@ exports.addAdmin = function (admin_data, callback) { console.log("inside addAdmin"); - // Check if the admin username already exists. If it does, then we don't add the admin_data and return a message. + // Check if the admin username already exists. Also check for user username. If it does, then we don't add the admin_data and return a message. exports.adminExists( admin_data.username , function (error, exists, data, message) { console.log("adminExists: " + message); if (!exists && !error) { @@ -1018,7 +1017,68 @@ exports.adminExists = function (username, callback) { }; +/****************************** MAIL *********************************/ + +/* + * mail_data = { + * sender: sender_username, + * receiver: receiver_username, + * message: message, + * date: date, + * } + * + * callback(success, error, message) + * + */ + +exports.sendMail = function (mail_data, callback) { + exports.find_user_name(mail_data.receiver, function (exist){ + if (exist) { + mongoFactory.getConnection(uri).then(function(db) { + var mail = db.collection('mail'); + + mail.insertOne(mail_data, function(err) { + if (err) { + callback(false, true, 'Error: could not send message.'); + db.close(); + } + else { + callback(true, false, "Message sent."); + db.close(); + } + }); + }); + } else { + callback(false, false, 'The receiver\'s username is undefined.'); + } + }); +} +/* + * + * callback(success, error, data, message) + * Returns the user's inbox (array of message objects) + * + */ + +exports.checkMailbox = function (username, callback) { + + mongoFactory.getConnection(uri).then(function(db) { + + var mail = db.collection('mail'); + + mail.find({receiver: username}).toArray(function (err, data) { + if (err) { + callback(false, true, null, 'Error: could not retrieve inbox messages.'); + } else if (!data) { + callback(false, false, null, 'No inbox.'); + } else { + callback(true, false, data, 'Retrieved inbox'); + } + }); + }); + +} diff --git a/routes/user.js b/routes/user.js index 96cbd67..5fb7fb9 100644 --- a/routes/user.js +++ b/routes/user.js @@ -54,9 +54,32 @@ router.get('/user_profile', loggedIn, isUser, function(req, res, next) { comments.push(obj); } - res.render('user_profile_alt', {comments : comments}); + // GET INBOX + var inbox = []; + dbFile.checkMailbox(req.user.user_name, function(success, error, data, message){ + if (success) { + inbox = data; + } else if (error) { + // Need to redirect to an error page instead. + console.log("Could not retrieve mail."); + } + }); + + // For testing purposes, remove later: + if (!inbox.length) { + var mail_data = { + sender: 'The Dude', receiver: req.user.user_name, + message: 'Whoa look at this', + date: '2016-04-05' + } + } + inbox.push(mail_data); + inbox.push(mail_data); + + res.render('user_profile_alt', {comments : comments, inbox: inbox}); }); + router.get('/signup', loggedOut, function(req, res, next) { res.render('signup', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors}); req.session.errors = null; @@ -131,6 +154,29 @@ router.post('/signin', loggedOut, function(req, res, next) { }); +router.get('/user_profile/send_message/:id', loggedIn, function(req, res, next) { + var date = new Date(); + var current_date = date.toString().slice(0, 24); + var mail_data = { + sender: req.user.user_name, + receiver: req.body.receiver_username, + message: req.body.message, + date: current_date + } + + dbFile.sendMail(mail_data, function(success, error, message) { + if ((!success && !error) || (error)) { + res.redirect('/user/user_profile', {error: message}); + $('#profile-send-message').show(); + } else { + res.redirect('/user/user_profile', {success: message}); + $('#profile-send-message').show(); + } + }); + + //res.redirect('/user/user_profile'); +}); + module.exports = router; /************** Route protection ********************/ @@ -150,8 +196,9 @@ function loggedOut(req, res, next) { } function isUser(req, res, next) { - if (req.user.email) { + if (req.user && req.user.email) { return next(); } res.redirect('/admin'); } + diff --git a/views/layouts/layout.hbs b/views/layouts/layout.hbs index ddfa945..62e9134 100644 --- a/views/layouts/layout.hbs +++ b/views/layouts/layout.hbs @@ -24,9 +24,6 @@ - {{> header }} diff --git a/views/user_profile_alt.hbs b/views/user_profile_alt.hbs index bad6fe5..652cfee 100644 --- a/views/user_profile_alt.hbs +++ b/views/user_profile_alt.hbs @@ -20,8 +20,9 @@ + {{# if success }} +
+ +

{{ success }}

+
+ {{ else }} + {{# if error }} +
+

{{ error }}

+
+ {{/if}} + {{/if}} +
+

Send Message

+
+
+
+ + +
+
+ + +
+ +
+
+
+ {{# if inbox }}

Inbox

@@ -162,29 +192,19 @@ + {{# each inbox }} - - - - - - - - - - - - - - - - - - + + + + {{/each}}
2016-05-01Great Solution!1The Dude
2016-04-05Woah look at thisLady Lips
2016-02-30I don't get this, help!The Dude
2016-01-15I solved your problemLisa{{ this.date }}{{ this.message }}{{ this.sender }}
+ {{ else }} +

No Messages.

+ {{/if}}
-- cgit v1.2.3