v1.18.x
barisusakli 11 years ago
parent cb08d4b04f
commit 81018d1305

@ -477,36 +477,55 @@ accountsController.getNotifications = function(req, res, next) {
}; };
accountsController.getChats = 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) { if (err) {
return next(err); return next(err);
} }
// Remove entries if they were already present as a followed contact // Remove entries if they were already present as a followed contact
if (res.locals.contacts && res.locals.contacts.length) { if (results.contacts && results.contacts.length) {
var contactUids = res.locals.contacts.map(function(contact) { var contactUids = results.contacts.map(function(contact) {
return parseInt(contact.uid, 10); return parseInt(contact.uid, 10);
}); });
chats = chats.filter(function(chatObj) { results.recentChats = results.recentChats.filter(function(chatObj) {
if (contactUids.indexOf(parseInt(chatObj.uid, 10)) !== -1) { return contactUids.indexOf(parseInt(chatObj.uid, 10)) === -1;
return false; });
} else {
return true;
} }
if (results.recentChats.length > 20) {
results.recentChats.length = 20;
}
if (!req.params.userslug) {
return res.render('chats', {
chats: results.recentChats,
contacts: results.contacts
}); });
} }
// Limit returned chats async.waterfall([
if (chats.length > 20) { async.apply(user.getUidByUserslug, req.params.userslug),
chats.length = 20; 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', { res.render('chats', {
meta: res.locals.chatData, chats: results.recentChats,
chats: chats, contacts: results.contacts,
contacts: res.locals.contacts, meta: data.toUser,
messages: res.locals.messages || undefined messages: data.messages
});
}); });
}); });
}; };

@ -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) { middleware.buildHeader = function(req, res, next) {
res.locals.renderHeader = true; res.locals.renderHeader = true;
async.parallel({ async.parallel({

@ -118,17 +118,14 @@ function accountRoutes(app, middleware, controllers) {
app.get('/notifications', middleware.buildHeader, middleware.authenticate, controllers.accounts.getNotifications); app.get('/notifications', middleware.buildHeader, middleware.authenticate, controllers.accounts.getNotifications);
app.get('/api/notifications', 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('/chats/:userslug?', middleware.buildHeader, middleware.authenticate, controllers.accounts.getChats);
app.get('/api/chats', middleware.authenticate, middleware.chat.getContactList, controllers.accounts.getChats); app.get('/api/chats/:userslug?', middleware.authenticate, 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) { function userRoutes(app, middleware, controllers) {
app.get('/users', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); app.get('/users', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers);
app.get('/api/users', 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('/users/online', middleware.buildHeader, middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers);
app.get('/api/users/online', middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers); app.get('/api/users/online', middleware.checkGlobalPrivacySettings, controllers.users.getOnlineUsers);

Loading…
Cancel
Save