Skip to content

Commit

Permalink
Added message system - new message form, message lists and message vi…
Browse files Browse the repository at this point in the history
…ew, and proper inbox count in navbar. Closes #9.
  • Loading branch information
LordSputnik committed Feb 28, 2015
1 parent 9532c77 commit a90ae30
Show file tree
Hide file tree
Showing 7 changed files with 189 additions and 9 deletions.
1 change: 1 addition & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ app.use('/', require('./routes/entity/view/publication'));
app.use('/', require('./routes/editor'));
app.use('/', require('./routes/register'));
app.use('/', require('./routes/relationship/edit'));
app.use('/', require('./routes/message'));

// catch 404 and forward to error handler
app.use(function(req, res, next) {
Expand Down
25 changes: 19 additions & 6 deletions routes/login.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var express = require('express');
var router = express.Router();
var request = require('superagent');
var Promise = require('bluebird');
require('superagent-bluebird-promise');

/* GET users listing. */
Expand All @@ -26,12 +27,24 @@ router.post('/login/handler', function(req, res) {
'password': 'abc',
'grant_type': 'password'
}).promise().then(function(oauth) {
return request.get(ws + '/user/' + oauth.body.user_id)
.promise().then(function(user) {
req.session.oauth = oauth.body;
req.session.user = user.body;
res.redirect(303, '/');
});
// Get inbox, for message count
var inboxPromise = request.get(ws + '/message/inbox')
.set('Authorization', 'Bearer ' + oauth.body.access_token).promise()
.then(function(inboxResponse){
return inboxResponse.body;
});

var userPromise = request.get(ws + '/user/' + oauth.body.user_id).promise()
.then(function(userResponse) {
return userResponse.body;
});

Promise.join(userPromise, inboxPromise, function(user, inbox) {
req.session.oauth = oauth.body;
req.session.user = user;
req.session.inboxCount = inbox.objects.length;
res.redirect(303, '/');
});
}).catch(function(err) {
console.log(err);
var error = err.res.body.error_description;
Expand Down
76 changes: 76 additions & 0 deletions routes/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var express = require('express');
var router = express.Router();
var request = require('superagent');
require('superagent-bluebird-promise');

router.get('/messageForm', function(req, res) {
res.render('editor/messageForm', {session: req.session, error: req.query.error});
});

function renderMessageList(view, req, res) {
var ws = req.app.get('webservice');
request.get(ws + '/message/' + view)
.set('Authorization', 'Bearer ' + req.session.oauth.access_token).promise()
.then(function(listResponse) {
return listResponse.body;
})
.then(function(list) {
res.render('editor/messageList', {
session: req.session,
view: view,
messages: list
});
});
}

router.get('/inbox', function(req, res) {
renderMessageList('inbox', req, res);
});

router.get('/archive', function(req, res) {
renderMessageList('archive', req, res);
});

router.get('/sent', function(req, res) {
renderMessageList('sent', req, res);
});

router.get('/message/:id', function(req, res) {
var ws = req.app.get('webservice');
request.get(ws + '/message/' + req.params.id)
.set('Authorization', 'Bearer ' + req.session.oauth.access_token).promise()
.then(function(messageResponse) {
return messageResponse.body;
})
.then(function(message) {
res.render('editor/message', {
session: req.session,
message: message
});
})
});


router.post('/message/handler', function(req, res) {
// This function should post a new user to the /user endpoint of the ws.
var ws = req.app.get('webservice');

// Parse recipient ids
recipientIds = req.body.recipients.split(',').map(function(substr) {
return parseInt(substr);
});

request.post(ws + '/message/sent')
.set('Authorization', 'Bearer ' + req.session.oauth.access_token)
.send({
'recipient_ids': recipientIds,
'subject': req.body.subject,
'content': req.body.content,
}).promise()
.then(function() {
res.redirect(303, '/');
});
});


module.exports = router;
21 changes: 21 additions & 0 deletions views/editor/message.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
extends ../layout

block body
include ../navbar

.container
h1='Message #' + message.message_id

.row
.col-md-8.col-md-offset-2
.panel.panel-default
.panel-body
dl.dl-horizontal
dt Sender:
dd=message.sender.name
dt Subject:
dd=message.subject
dt Content:
dd=message.content

include ../footer
31 changes: 31 additions & 0 deletions views/editor/messageForm.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
extends ../layout

block body
include ../navbar

.container
h1 New Message
p.lead.
Use the form below to send a message to other BookBrainz users. These
messages may be viewed by moderators, so please do not include any
private information.
.row
.col-md-8.col-md-offset-2
form.whole-page-form.form-horizontal(action="/message/handler", method="post")
.form-group
label.col-md-2.control-label Recipients
.col-md-8
input.form-control(type="text", name="recipients" placeholder="Recipient ID[, Recipient ID...]")
.form-group
label.col-md-2.control-label Subject
.col-md-8
input.form-control(type="text", name="subject", placeholder="Subject")
.form-group
label.col-md-2.control-label Message
.col-md-8
textarea.form-control(name="content", rows="6")
.form-group
.col-md-4.col-md-offset-4
button.btn.btn-success.btn-block(type="submit") Submit

include ../footer
35 changes: 35 additions & 0 deletions views/editor/messageList.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
extends ../layout

block body
include ../navbar

.container
h1 My Messages

a.pull-right(href="/messageForm")
span.fa.fa-plus
|  New Message

ul.nav.nav-pills
li(class=(view === 'inbox' ? 'active' : undefined))
a(href="/inbox")
| Inbox 
span.badge=session.inboxCount
li(class=(view === 'archive' ? 'active' : undefined))
a(href="/archive") Archive
li(class=(view === 'sent' ? 'active' : undefined))
a(href="/sent") Sent

table.table.table-hover
thead
tr
th Sender
th Subject
tbody
for message in messages.objects
tr
td=message.sender.name
td
a(href='/message/'+message.message_id)=message.subject

include ../footer
9 changes: 6 additions & 3 deletions views/navbar.jade
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ nav.navbar.navbar-default.navbar-inverse.navbar-fixed-top(role='navigation')
a(href='/editor/'+session.user.id)= session.user.name

li
a(href='')
span.fa.fa-envelope(style='margin-right:0.2em;')
span.badge 1
a(href='/inbox')
if session.inboxCount === 0
span.fa.fa-envelope-o(style='margin-right:0.2em;')
else
span.fa.fa-envelope(style='margin-right:0.2em;')
span.badge=session.inboxCount

li
if session.user
Expand Down

0 comments on commit a90ae30

Please sign in to comment.