aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--assets/js/script.js8
-rw-r--r--config/passport.js9
-rw-r--r--node_simple.js96
-rw-r--r--routes/index.js11
-rw-r--r--routes/user.js53
-rw-r--r--views/layouts/layout.hbs3
-rw-r--r--views/user_profile_alt.hbs58
7 files changed, 191 insertions, 47 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/config/passport.js b/config/passport.js
index 27f835f..6cb6292 100644
--- a/config/passport.js
+++ b/config/passport.js
@@ -7,22 +7,27 @@ var exports = module.exports = {};
var encryptPassword = exports.encryptPassword = function (password) {
return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null);
-}
+};
var comparePassword = function (password1, password2) {
return bcrypt.compareSync(password1, password2);
-}
+};
+
passport.serializeUser(function (user, done) {
+
done(null, user);
});
passport.deserializeUser(function(user, done) {
+
done(null, user);
});
+
+
// fields - [email, user_name, f_name, l_name, uni, department, password, phone_num]
passport.use('local_signup', new LocalStrategy({
diff --git a/node_simple.js b/node_simple.js
index 05f04b1..d3eddb4 100644
--- a/node_simple.js
+++ b/node_simple.js
@@ -191,7 +191,6 @@ exports.retrieveFollows = function (user_name, callback) {
});
};
-
// will add comments ASAP
/* callback(success, data/message) => callback(boolean, object/String);
*
@@ -309,11 +308,11 @@ exports.add_user = function (fields, callbackUser) {
};
// find out if this user already exists by checking their email
- exports.find_user( fields[0] ,callbackUser, function (result, callbackUser) {
+ exports.find_user( fields[0], function (result) {
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");
@@ -367,7 +366,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) {
@@ -394,6 +393,7 @@ exports.find_user_name = function (user_name, callbackUser, callback) {
*/
exports.retrieveUser = function (username, callback) {
+
mongoFactory.getConnection(uri).then(function (db) {
var users = db.collection('users');
@@ -422,7 +422,6 @@ exports.retrieveUser = function (username, callback) {
*/
exports.retrievePassword = function (username, callback) {
-
mongoFactory.getConnection(uri).then(function (db) {
var collection = db.collection('logins');
@@ -445,7 +444,7 @@ exports.retrievePassword = function (username, callback) {
/*
* This (helper) function returns true IFF email already exists in the database
* */
-exports.find_user = function (email, callbackUser, callback) {
+exports.find_user = function (email, callback) {
// make a connection
console.log("inside find_user");
mongoFactory.getConnection(uri)
@@ -455,10 +454,10 @@ exports.find_user = function (email, callbackUser, callback) {
logins.find( { email: email } ).toArray(function (err, result) {
if (err) throw err;
else if (result.length == 0) { // nothing was found so this user is new
- callback(false, callbackUser);
+ callback(false);
}
else {
- callback(true, callbackUser);
+ callback(true);
}
});
// db.close();
@@ -995,7 +994,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) {
@@ -1052,7 +1051,86 @@ 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');
+ }
+ });
+ });
+
+}
+//testing
+exports.findUserByID = function (id, callback) {
+ // make a connection
+ console.log("inside findUserByID");
+ mongoFactory.getConnection(uri)
+ .then(function (db) {
+ var logins = db.collection('logins');
+ logins.find( { _id : id }, function (err, result) {
+ callback(err, result);
+ db.close();
+ });
+ })
+ .catch(function (err) {
+ console.err(err);
+ })
+};
diff --git a/routes/index.js b/routes/index.js
index 6d5fdac..961a60d 100644
--- a/routes/index.js
+++ b/routes/index.js
@@ -11,7 +11,7 @@ var bcrypt = require('bcrypt-nodejs');
/* Render/GET homepage. */
router.get('/', function(req, res, next) {
- addFirstAdmin();
+ //addFirstAdmin();
res.render('index');
req.session.errors = null;
req.session.success = null;
@@ -75,7 +75,6 @@ router.get('/exams/:id', function(req, res, next) {
minExamInfoArray.push(minExamInfo);
}
}
- console.log(minExamInfoArray);
res.render('exams', {query: req.params.id, result: minExamInfoArray});
});
});
@@ -137,9 +136,6 @@ router.get('/questions/:exam_id', function (req,res) {
};
res.render('questions', {query: qList, examInfo: examInfo});
});
-
-
- console.log(exam);
});
/*dbFile.get_exam_info_by_ID(examID, function (questions) {
@@ -193,7 +189,6 @@ router.get('/solutions/:exam_id/:q_num', function (req, res) {
soln.commentCount = soln.comments.length;
});
res.render('user_solutions', {query: solutions, examID: examID, qID: qID});
- console.log(solutions);
});
});
@@ -204,11 +199,11 @@ router.post('/add_solutions/submit', function (req, res) {
/**** Helpers ****/
function addFirstAdmin() {
- console.log("Inside addFirstAdmin()");
+ //console.log("Inside addFirstAdmin()");
//admin_data = {fname: firstname, lname: lastname, username: username, password: password}
var password = 'lamptable';
var hash_pwd = passport_file.encryptPassword(password);
- console.log("Admin hashed: " + hash_pwd);
+ //console.log("Admin hashed: " + hash_pwd);
var admin_data = {
fname: 'Admin',
lname: 'Admin',
diff --git a/routes/user.js b/routes/user.js
index 6ae0d80..b2864b8 100644
--- a/routes/user.js
+++ b/routes/user.js
@@ -54,7 +54,29 @@ 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});
});
/* Adds a solution into the database, redirect to exam/question/solution page */
@@ -91,7 +113,7 @@ router.post('/submit_solution/:examID/:qID', function(req,res){
//Redirect to solutions page again
res.redirect('/solutions/' + examID + '/' + qID);
-
+
});
});
@@ -111,7 +133,6 @@ router.get('/add_solution/:examID/:qID', function (req, res, next) {
});
-
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;
@@ -186,6 +207,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 ********************/
@@ -205,8 +249,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 @@
<script src="/assets/js/jquery-2.2.4.min.js"></script>
<script src="/assets/js/bootstrap.min.js"></script>
<script src="/assets/js/script.js"></script>
- <script>
- var socket = io();
- </script>
</head>
<body>
{{> 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 @@
<div class="collapse navbar-collapse" id="profile-nav">
<ul class="nav navbar-nav " >
<li><a href="#" class="prf-page">General</a></li>
- <li><a href="#" class="prf-page">Follows</a></li>
+ <li><a href="#" class="prf-page">Following</a></li>
<li><a href="#" class="prf-page">Friends</a></li>
+ <li><a href="#" class="prf-page">Send Message</a></li>
<li><a href="#" class="prf-page">Inbox</a></li>
<li><a href="#" class="prf-page">Comments</a></li>
</ul>
@@ -150,7 +151,36 @@
</table>
</div>
</div>
+ {{# if success }}
+ <section class="alert alert-success">
+ <!-- Change those to banners later -->
+ <h2>{{ success }}</h2>
+ </section>
+ {{ else }}
+ {{# if error }}
+ <section class="alert alert-danger">
+ <h2>{{ error }}</h2>
+ </section>
+ {{/if}}
+ {{/if}}
+ <div class="row" id="profile-send-message">
+ <h3>Send Message</h3>
+ <div class="col-xs-offset-1 col-sm-offset-3 col-xs-10 col-sm-6">
+ <form action="/user_profile/send_message" method="GET">
+ <div class="form-group">
+ <label for="receiver">Send To:</label>
+ <input type="text" class="form-control" placeholder="Username" name="receiver_username">
+ </div>
+ <div class="form-group">
+ <label for="sending-message">Message:</label>
+ <textarea class="form-control" rows="3" name="message"></textarea>
+ </div>
+ <button class="btn btn-primary">Send Message</button>
+ </form>
+ </div>
+ </div>
<div class="row" id="profile-inbox">
+ {{# if inbox }}
<h3>Inbox</h3>
<div class="col-sm-10 col-sm-offset-1 prf-tables">
<table class="table">
@@ -162,29 +192,19 @@
</tr>
</thead>
<tbody>
+ {{# each inbox }}
<tr>
- <td>2016-05-01</td>
- <td>Great Solution!1</td>
- <td>The Dude</td>
- </tr>
- <tr>
- <td>2016-04-05</td>
- <td>Woah look at this</td>
- <td>Lady Lips</td>
- </tr>
- <tr>
- <td>2016-02-30</td>
- <td>I don't get this, help!</td>
- <td>The Dude</td>
- </tr>
- <tr>
- <td>2016-01-15</td>
- <td>I solved your problem</td>
- <td>Lisa</td>
+ <td>{{ this.date }}</td>
+ <td>{{ this.message }}</td>
+ <td>{{ this.sender }}</td>
</tr>
+ {{/each}}
</tbody>
</table>
</div>
+ {{ else }}
+ <h3>No Messages.</h3>
+ {{/if}}
</div>
<div class="row" id="profile-comments">