From 81018d1305cb3dc21fa4c13a3470c6f67c24fbbc Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 21 Jul 2014 15:03:01 -0400 Subject: [PATCH] closes #1863 --- src/controllers/accounts.js | 53 ++++++++++++++++++++++++------------ src/middleware/middleware.js | 41 +--------------------------- src/routes/index.js | 7 ++--- 3 files changed, 39 insertions(+), 62 deletions(-) diff --git a/src/controllers/accounts.js b/src/controllers/accounts.js index 4afb9ded93..6fd9393ba0 100644 --- a/src/controllers/accounts.js +++ b/src/controllers/accounts.js @@ -477,36 +477,55 @@ accountsController.getNotifications = function(req, res, next) { }; accountsController.getChats = function(req, res, next) { - messaging.getRecentChats(req.user.uid, 0, -1, function(err, chats) { + async.parallel({ + contacts: async.apply(user.getFollowing, req.user.uid), + recentChats: async.apply(messaging.getRecentChats, req.user.uid, 0, -1) + }, function(err, results) { if (err) { return next(err); } // Remove entries if they were already present as a followed contact - if (res.locals.contacts && res.locals.contacts.length) { - var contactUids = res.locals.contacts.map(function(contact) { + if (results.contacts && results.contacts.length) { + var contactUids = results.contacts.map(function(contact) { return parseInt(contact.uid, 10); }); - chats = chats.filter(function(chatObj) { - if (contactUids.indexOf(parseInt(chatObj.uid, 10)) !== -1) { - return false; - } else { - return true; - } + results.recentChats = results.recentChats.filter(function(chatObj) { + return contactUids.indexOf(parseInt(chatObj.uid, 10)) === -1; }); } - // Limit returned chats - if (chats.length > 20) { - chats.length = 20; + if (results.recentChats.length > 20) { + results.recentChats.length = 20; } - res.render('chats', { - meta: res.locals.chatData, - chats: chats, - contacts: res.locals.contacts, - messages: res.locals.messages || undefined + if (!req.params.userslug) { + return res.render('chats', { + chats: results.recentChats, + contacts: results.contacts + }); + } + + async.waterfall([ + async.apply(user.getUidByUserslug, req.params.userslug), + function(toUid, next) { + async.parallel({ + toUser: async.apply(user.getUserFields, toUid, ['uid', 'username']), + messages: async.apply(messaging.getMessages, req.user.uid, toUid, false) + }, next); + } + ], function(err, data) { + if (err) { + return next(err); + } + + res.render('chats', { + chats: results.recentChats, + contacts: results.contacts, + meta: data.toUser, + messages: data.messages + }); }); }); }; diff --git a/src/middleware/middleware.js b/src/middleware/middleware.js index 017242e684..738ada5cd2 100644 --- a/src/middleware/middleware.js +++ b/src/middleware/middleware.js @@ -108,7 +108,7 @@ middleware.checkTopicIndex = function(req, res, next) { var topicIndex = parseInt(req.params.topic_index, 10); topicCount = parseInt(topicCount, 10) + 1; var url = ''; - + if (topicIndex > topicCount) { url = '/category/' + req.params.category_id + '/' + req.params.slug + '/' + topicCount; return res.locals.isAPI ? res.json(302, url) : res.redirect(url); @@ -190,45 +190,6 @@ middleware.checkAccountPermissions = function(req, res, 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(); - } -}; - -/* End Chat Middlewares */ - 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 27f7da64c3..1128bccc8f 100644 --- a/src/routes/index.js +++ b/src/routes/index.js @@ -118,17 +118,14 @@ function accountRoutes(app, middleware, controllers) { app.get('/notifications', middleware.buildHeader, middleware.authenticate, controllers.accounts.getNotifications); app.get('/api/notifications', middleware.authenticate, controllers.accounts.getNotifications); - app.get('/chats', middleware.buildHeader, middleware.authenticate, middleware.chat.getContactList, controllers.accounts.getChats); - app.get('/api/chats', middleware.authenticate, middleware.chat.getContactList, 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); + app.get('/chats/:userslug?', middleware.buildHeader, middleware.authenticate, controllers.accounts.getChats); + app.get('/api/chats/:userslug?', middleware.authenticate, controllers.accounts.getChats); } function userRoutes(app, middleware, controllers) { app.get('/users', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); app.get('/api/users', middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); - // was this duped by accident or purpose? app.get('/users/online', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); app.get('/api/users/online', middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers);