From b03bb3a32b44d35ccb12d4a07537850965b79d0c Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 4 Jul 2014 19:54:47 -0400 Subject: [PATCH] issue #1788 - start --- public/language/en_GB/global.json | 1 - public/language/en_GB/modules.json | 2 ++ src/controllers/accounts.js | 5 ++++- src/messaging.js | 3 ++- src/middleware/middleware.js | 15 +++++++++++++++ src/routes/index.js | 2 ++ 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/public/language/en_GB/global.json b/public/language/en_GB/global.json index dab7fd1df6..83dd0517f5 100644 --- a/public/language/en_GB/global.json +++ b/public/language/en_GB/global.json @@ -32,7 +32,6 @@ "header.tags": "Tags", "header.popular": "Popular", "header.users": "Users", - "header.chats": "Chats", "header.notifications": "Notifications", "header.search": "Search", "header.profile": "Profile", diff --git a/public/language/en_GB/modules.json b/public/language/en_GB/modules.json index 5355c97f7b..3e56487a90 100644 --- a/public/language/en_GB/modules.json +++ b/public/language/en_GB/modules.json @@ -6,6 +6,8 @@ "chat.user_typing": "%1 is typing ...", "chat.user_has_messaged_you": "%1 has messaged you.", "chat.see_all": "See all Chats", + "chat.no-messages": "Please select a recipient to view chat message history", + "chat.recent-chats": "Recent Chats", "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 312c6fc038..2269877dcb 100644 --- a/src/controllers/accounts.js +++ b/src/controllers/accounts.js @@ -486,8 +486,11 @@ accountsController.getChats = function(req, res, next) { return next(err); } + console.log(res.locals.messages); + res.render('chats', { - chats: chats + chats: chats, + messages: res.locals.messages || undefined }); }); }; diff --git a/src/messaging.js b/src/messaging.js index 25bdeb9c51..2d5b3ea272 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -77,9 +77,10 @@ var db = require('./database'), } async.map(messages, function(message, next) { - var self = parseInt(message.fromuid, 10) === parseInt(fromuid) + var self = parseInt(message.fromuid, 10) === parseInt(fromuid, 10); message.fromUser = self ? userData[0] : userData[1]; message.toUser = self ? userData[1] : userData[0]; + message.timestampISO = new Date(parseInt(message.timestamp, 10)).toISOString(); Messaging.parse(message.content, message.fromuid, fromuid, userData[1], userData[0], isNew, function(result) { message.content = result; diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 20594ac309..c1a71ed8eb 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -15,6 +15,7 @@ var app, db = require('./../database'), categories = require('./../categories'), topics = require('./../topics'), + messaging = require('../messaging'), controllers = { api: require('./../controllers/api') @@ -189,6 +190,20 @@ 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(); + } + }); +}; + 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 bea9e769dd..cfbe194630 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -121,6 +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); } function userRoutes(app, middleware, controllers) {