From 84e046185b082c30ecb03bb398822ad44d65259a Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Sat, 5 Jul 2014 15:01:25 -0400 Subject: [PATCH] WIP --- public/language/en_GB/modules.json | 2 ++ src/controllers/accounts.js | 4 ++- src/middleware/middleware.js | 43 +++++++++++++++++++++++------- src/routes/index.js | 4 +-- 4 files changed, 40 insertions(+), 13 deletions(-) diff --git a/public/language/en_GB/modules.json b/public/language/en_GB/modules.json index 3e56487a90..f1902d0e21 100644 --- a/public/language/en_GB/modules.json +++ b/public/language/en_GB/modules.json @@ -8,6 +8,8 @@ "chat.see_all": "See all Chats", "chat.no-messages": "Please select a recipient to view chat message history", "chat.recent-chats": "Recent Chats", + "chat.contacts": "Contacts", + "chat.message-history": "Message History", "composer.user_said_in": "%1 said in %2:\n", "composer.user_said": "%1 said:\n", diff --git a/src/controllers/accounts.js b/src/controllers/accounts.js index 2269877dcb..fd55dc7dc9 100644 --- a/src/controllers/accounts.js +++ b/src/controllers/accounts.js @@ -486,10 +486,12 @@ accountsController.getChats = function(req, res, next) { return next(err); } - console.log(res.locals.messages); + console.log(res.locals); res.render('chats', { + meta: res.locals.chatData, chats: chats, + contacts: res.locals.contacts, messages: res.locals.messages || undefined }); }); diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index c1a71ed8eb..3a7833c029 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -190,20 +190,43 @@ middleware.checkAccountPermissions = function(req, res, next) { }); }; -middleware.getChatMessages = function(req, res, next) { - user.getUidByUserslug(req.params.userslug, function(err, toUid) { - if (!err && toUid) { - messaging.getMessages(req.user.uid, toUid, false, function(err, messages) { - res.locals.messages = messages; - next(); - }); - } else { - res.locals.messages = []; - next(); +/* Chat related middlewares */ + +middleware.chat = {}; +middleware.chat.getMetadata = function(req, res, next) { + async.waterfall([ + async.apply(user.getUidByUserslug, req.params.userslug), + function(toUid, next) { + user.getUserFields(toUid, ['uid', 'username'], next); } + ], function(err, chatData) { + if (!err) { + res.locals.chatData = chatData; + } + + next(); }); }; +middleware.chat.getContactList = function(req, res, next) { + user.getFollowing(req.user.uid, function(err, contacts) { + res.locals.contacts = contacts; + next(); + }); +}; + +middleware.chat.getMessages = function(req, res, next) { + if (res.locals.chatData) { + messaging.getMessages(req.user.uid, res.locals.chatData.uid, false, function(err, messages) { + res.locals.messages = messages; + next(); + }); + } else { + res.locals.messages = []; + next(); + } +}; + middleware.buildHeader = function(req, res, next) { res.locals.renderHeader = true; async.parallel({ diff --git a/src/routes/index.js b/src/routes/index.js index 6bbeca5e8f..ea1ee05ccb 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -121,8 +121,8 @@ function accountRoutes(app, middleware, controllers) { app.get('/chats', middleware.buildHeader, middleware.authenticate, controllers.accounts.getChats); app.get('/api/chats', middleware.authenticate, controllers.accounts.getChats); - app.get('/chats/:userslug', middleware.buildHeader, middleware.authenticate, middleware.getChatMessages, controllers.accounts.getChats); - app.get('/api/chats/:userslug', middleware.authenticate, middleware.getChatMessages, controllers.accounts.getChats); + app.get('/chats/:userslug', middleware.buildHeader, middleware.authenticate, middleware.chat.getMetadata, middleware.chat.getContactList, middleware.chat.getMessages, controllers.accounts.getChats); + app.get('/api/chats/:userslug', middleware.authenticate, middleware.chat.getMetadata, middleware.chat.getContactList, middleware.chat.getMessages, controllers.accounts.getChats); } function userRoutes(app, middleware, controllers) {