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) {
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
});
});
});
};

@ -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({

@ -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);

Loading…
Cancel
Save