diff options
| -rw-r--r-- | assets/css/user_profile.css | 13 | ||||
| -rw-r--r-- | assets/js/user_profile.js | 34 | ||||
| -rw-r--r-- | node_simple.js | 27 | ||||
| -rw-r--r-- | routes/user.js | 131 | ||||
| -rw-r--r-- | test/data/messages.json | 0 | ||||
| -rw-r--r-- | views/user_profile_alt.hbs | 112 |
6 files changed, 255 insertions, 62 deletions
diff --git a/assets/css/user_profile.css b/assets/css/user_profile.css index 39d86b0..042d842 100644 --- a/assets/css/user_profile.css +++ b/assets/css/user_profile.css @@ -1,4 +1,4 @@ -/*** USER PROFILE PAGE ***/ + /*** USER PROFILE PAGE ***/ .user-info-content img{ max-width: 120px; @@ -135,5 +135,16 @@ nav .nav-o:hover .nav-o-container{ } +/* Inbox */ + + +.u-table-row { + display:table-row; + cursor: pointer; +} +.u-table tbody tr:hover{ + background-color: #ededed; +} + /*** USER PROFILE PAGE END ***/
\ No newline at end of file diff --git a/assets/js/user_profile.js b/assets/js/user_profile.js index fa7add4..c26299b 100644 --- a/assets/js/user_profile.js +++ b/assets/js/user_profile.js @@ -3,10 +3,8 @@ function main(){ var items = [ $("#profile-general"), $("#profile-follows"), - $("#profile-friends"), - $("#profile-inbox"), $("#profile-comments"), - $('#profile-send-message')]; + $("#profile-inbox")]; function hidePages(){ items.forEach(function(item){ @@ -20,35 +18,37 @@ function main(){ // Show General page items[0].show(); - $('.prf-page').bind('click', function(e){ + $('.nav-o').bind('click', function(e){ e.preventDefault(); hidePages(); /* Each case will need to be changed to call a handler for fetching data * from server*/ - switch($(this).text()) { - case "General": + switch($(this).attr('id')) { + case "nav-profile": console.log("clicked general"); $("#profile-general").show(); break; - case "Following": + case "nav-follows": $("#profile-follows").show(); break; - case "Friends": - $("#profile-friends").show(); - break; - case "Inbox": - $("#profile-inbox").show(); - break; - case "Comments": + case "nav-comments": $("#profile-comments").show(); break; - case "Send Message": - $('#profile-send-message').show(); + case "nav-inbox": + $("#profile-inbox").show(); break; default: console.log("Error: User page not found"); } - });} + }); + + + + +} + + + $(document).ready(main);
\ No newline at end of file diff --git a/node_simple.js b/node_simple.js index 360125e..7ff70c6 100644 --- a/node_simple.js +++ b/node_simple.js @@ -1365,3 +1365,30 @@ exports.removeToken = function(token, callback) { }); }; + + +/** + * Check if the user with the given username has mail. + * If the user does, then send the mail that falls within the specified page range. + * + * @param {object} username object + * @param {function} callback of the form (<boolean1>, <boolean2>, <array of objects>, <string>) + * - callback(error, success, mail, message) + * */ +exports.getMail = function (username, callback) { + + 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 { + // Mail retrieved + callback(true, false, data, 'Retrieved inbox'); + } + + }); + +}; diff --git a/routes/user.js b/routes/user.js index ee644b5..6c53989 100644 --- a/routes/user.js +++ b/routes/user.js @@ -13,13 +13,11 @@ var pass = require('../config/passport'); var csrfProtection = csrf(); router.use(csrfProtection); // router is protected - router.get('/logout', loggedIn, function (req, res, next) { req.logout(); res.redirect('/'); }); - /** Render/GET user_profile page. */ router.get('/user_profile', loggedIn, isUser, function(req, res, next) { @@ -64,6 +62,7 @@ router.get('/user_profile', loggedIn, isUser, function(req, res, next) { function getMail(){ return new Promise(function(resolve, reject) { + // We assume that the messages stored in data are sorted by date dbFile.checkMailbox(req.user.user_name, function(success, error, data, message){ if (success) { inbox = data; @@ -97,32 +96,33 @@ router.get('/user_profile', loggedIn, isUser, function(req, res, next) { function getFollows(){ return new Promise(function(resolve, reject) { dbFile.retrieveFollows(req.user.user_name, function(success, data){ - req.user.examfollows = []; - req.user.followerCount = data.length; - if(success && data.length){ - var count = 1; - data.forEach(function(item){ - dbFile.get_exam_byID(item, function(success, error, data){ - if(success){ - req.user.examfollows.push(data); - if(count == req.user.followerCount ){ - resolve(1); - }else{ - count++; - } - }else{ + + // Add a new attribute to users, examFollows & followerCount + req.user.examfollows = []; + req.user.followerCount = data.length; + if(success && data.length){ + var count = 1; + data.forEach(function(item){ + dbFile.get_exam_byID(item, function(success, error, data){ + if(success){ + req.user.examfollows.push(data); + if(count == req.user.followerCount ){ resolve(1); + }else{ + count++; } - }) - }); - - } else if(success && !data.length){ - resolve(1); - } - else{ //error - error = data; - resolve(1); - } + }else{ + resolve(1); + } + }) + }); + } else if(success && !data.length){ + resolve(1); + } + else{ //error + error = data; + resolve(1); + } }); }); @@ -134,6 +134,85 @@ router.get('/user_profile', loggedIn, isUser, function(req, res, next) { }); +/** + * Serve mail messages for page pageNum with messageCount messages as a JSON object + */ +router.get('/user_profile/inbox/:messageCount/:pageNum', loggedIn, isUser, function(req, res, next) { + + req.session.messages = null; + var inbox = []; + var error = null; + var pageNum = parseInt(req.params.pageNum); + var messageCount = parseInt(req.params.messageCount); + + function getMail(){ + return new Promise(function(resolve, reject) { + + // We assume that the messages stored in data are sorted by date + dbFile.checkMailbox(req.user.user_name, + function(success, error, data, message){ + + if (success) { + inbox = data; + req.user.messages = inbox.length; // Give user message length + console.log("Success: " + message); + + // needed to display in layout (used for modal popup classes) + var i = 0; + inbox.forEach(function(element) { + element.class = i; + i ++; + }); + + + resolve(1); + } else if (error) { + // Need to redirect to an error page instead. + console.log("Error: " + message); + resolve(1); + } + }); + }); + } + + + getMail().then(function (data) { + + var mail = []; + var index = (pageNum - 1) * messageCount; + var indexMax = index + messageCount; + var inboxLength = inbox.length; + var count = 0; + + /* Testing Purposes + var notification = "index = " + index + "\n" + + "indexMax = " + indexMax + "\n" + + "inboxLength = " + inbox.length + "\n"; + console.log(notification); */ + + // Find the 10 messages situated at page num in inbox, store in mail + if(index >= inboxLength){ + res.writeHead(400, {"Content-Type": "application/json"}); + console.log("Specified index is out of bounds"); + }else if (inboxLength <= messageCount) { + res.writeHead(200, {"Content-Type": "application/json"}); + mail = inbox; + }else { + res.writeHead(200, {"Content-Type": "application/json"}); + while((index < inboxLength) && (index < indexMax)){ + mail.push(inbox[index]); + index++; + count++; + console.log("in while: " + count); + } + } + + // return mail JSON object :: JSON.stringify({json}); + var mailJSON = JSON.stringify(mail); + res.end(mailJSON); + }); +}); + router.get('/resetPassword', loggedOut, function(req, res, next) { res.render('reset_password', {csrfToken: req.csrfToken(), success: req.session.success, errors: req.session.errors}); req.session.errors = null; diff --git a/test/data/messages.json b/test/data/messages.json new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/test/data/messages.json diff --git a/views/user_profile_alt.hbs b/views/user_profile_alt.hbs index 8353be9..8cc45b8 100644 --- a/views/user_profile_alt.hbs +++ b/views/user_profile_alt.hbs @@ -28,7 +28,7 @@ <!-- User name Header row --> <div class="u-name-header col-xs-12"> - <h2>ChrisHopper89</h2> + <h2>{{user.user_name}}</h2> </div> <!-- Main Content row --> @@ -37,36 +37,36 @@ <!-- User Image --> <img alt="User Pic" src="../assets/images/avatar.png" class="img-circle img-responsive"> - <h3>Jim Hopper</h3> - <h4>jhop@gmail.com</h4> + <h3>{{ user.f_name }} {{ user.l_name }}</h3> + <h4>{{ user.email }}</h4> <!-- Side Nav --> <nav> - <button class="nav-o"> + <button class="nav-o" id="nav-profile"> <div class="nav-o-container"> <h5><i class="fa fa-home" aria-hidden="true"></i> My Profile</h5> </div> </button> - <button class="nav-o"> + <button class="nav-o" id="nav-comments"> <div class="nav-o-container"> <h5><i class="fa fa-comment-o" aria-hidden="true"></i> Comments</h5> </div> </button> - <button class="nav-o"> + <button class="nav-o" id="nav-follows"> <div class="nav-o-container"> <h5><i class="fa fa-list" aria-hidden="true"></i> Follows</h5> </div> </button> - <button class="nav-o"> + <button class="nav-o" id="nav-inbox"> <div class="nav-o-container"> <h5><i class="fa fa-envelope" aria-hidden="true"></i> Inbox</h5> </div> </button> - <button class="nav-o"> + <button class="nav-o" id="nav-logout"> <div class="nav-o-container"> <h5><i class="fa fa-sign-out" aria-hidden="true"></i> Log Out</h5> </div> @@ -78,34 +78,110 @@ <!-- Content Pages (On Right) --> <article class="col-xs-12 col-sm-9 u-pages"> - <section class="u-profile"> + <section class="u-profile" id="profile-general"> <h4>Profile </h4> <hr> <h5>Personal Info</h5> - <p>Username: <em>ChiefHopper86</em></p> - <p>Name: <em>Jim Hopper</em></p> - <p>University: <em>University of Toronto</em></p> + <p>Username: <em>{{user.user_name}}</em></p> + <p>Name: <em>{{ user.f_name }} {{ user.l_name }}</em></p> + + + {{# if user.university }} + <p>University: <em>{{ user.university }}</em></p> + {{/if}} + {{# if user.department }} + <p>Department: <em>{{ user.department }}</em></p> + {{/if}} <hr> <h5>User Stats</h5> - <p>Answered: <em>20</em></p> - <p>Comments: <em>25</em></p> - <p>Score: <em>205</em></p> - + <p>Answered: <em>{{ user.answered }}</em></p> + <p>Comments: <em>{{ user.comments }}</em></p> + <p>Inbox: <em>{{ user.messages }}</em></p> + <!-- Need to add --> + <p>Score: <em>N/A</em></p> </section> - </article> + <!-- Add Script for paging --> + <section class="u-profile" id="profile-follows"> + <h4>Follows </h4> + + <hr> + <p>[PlaceHolder]</p> + </section> + + <section class="u-profile" id="profile-comments"> + <h4>Comments </h4> + <hr> + <p>[PlaceHolder]</p> + </section> + <!-- Inbox page --> + <section class="u-profile" id="profile-inbox"> + <h4>Inbox </h4> - <!-- Pages --> + <hr> + {{# if inbox }} + <div class="col-sm-12"> + <table class="table table-striped u-table"> + <thead> + <tr> + <th>Date</th> + <th>Subject</th> + <th>Sender</th> + </tr> + </thead> + <tbody> + {{# each inbox }} + <tr class="u-table-row" data-toggle="modal" data-target=".{{ this.class }}"> + <td>{{ this.date }}</td> + <td>{{ this.subject }}</td> + <td>{{ this.sender }}</td> + </tr> + {{/each}} + </tbody> + </table> + + + + {{# each inbox}} + <article class="modal fade {{this.class}}"> + <div class="modal-dialog"> + <div class="modal-content"> + + <section class="modal-header"> + <button type="button" class="close" data-dismiss="modal">×</button> + <h4 class="modal-title"><em>Subject: </em>{{ this.subject }}</h4> + <h5><em>From: </em>{{ this.sender }}</h5> + <em>Date: </em> {{this.date}} + </section> + + <section class="modal-body"> + {{ this.message }} + </section> + + <div class="modal-footer"> + + </div> + + </div> + </div> + </article> + {{/each}} + </div> + {{ else }} + <h3>No Messages.</h3> + {{/if}} + </section> + </article> </div> |
