changed the chat route to /chats/:roomid?

This commit is contained in:
barisusakli
2015-12-15 19:05:32 +02:00
parent 58d05f7c79
commit 5d1169e686
5 changed files with 63 additions and 101 deletions

View File

@@ -1,13 +1,11 @@
'use strict';
var async = require('async'),
nconf = require('nconf'),
var async = require('async');
var messaging = require('../../messaging');
var meta = require('../../meta');
var helpers = require('../helpers');
user = require('../../user'),
messaging = require('../../messaging'),
meta = require('../../meta'),
helpers = require('../helpers'),
utils = require('../../../public/src/utils');
var chatsController = {};
@@ -16,35 +14,15 @@ chatsController.get = function(req, res, callback) {
return callback();
}
// In case a userNAME is passed in instead of a slug, the route should not 404
var slugified = utils.slugify(req.params.userslug);
if (req.params.userslug && req.params.userslug !== slugified) {
return helpers.redirect(res, '/chats/' + slugified);
}
async.parallel({
contacts: async.apply(user.getFollowing, req.user.uid, 0, 199),
recentChats: async.apply(messaging.getRecentChats, req.user.uid, 0, 19)
}, function(err, results) {
messaging.getRecentChats(req.user.uid, 0, 19, function(err, recentChats) {
if (err) {
return callback(err);
}
if (results.recentChats.users && results.recentChats.users.length) {
var contactUids = results.recentChats.users.map(function(chatObj) {
return parseInt(chatObj.uid, 10);
});
results.contacts = results.contacts.filter(function(contact) {
return contactUids.indexOf(parseInt(contact.uid, 10)) === -1;
});
}
if (!req.params.userslug) {
if (!req.params.roomid) {
return res.render('chats', {
chats: results.recentChats.users,
nextStart: results.recentChats.nextStart,
contacts: results.contacts,
rooms: recentChats.rooms,
nextStart: recentChats.nextStart,
allowed: true,
title: '[[pages:chats]]',
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:chats]]'}])
@@ -52,21 +30,23 @@ chatsController.get = function(req, res, callback) {
}
async.waterfall([
async.apply(user.getUidByUserslug, req.params.userslug),
function(toUid, next) {
if (!toUid || parseInt(toUid, 10) === parseInt(req.user.uid, 10)) {
function (next) {
messaging.isUserInRoom(req.uid, req.params.roomid, next);
},
function (inRoom, next) {
if (!inRoom) {
return callback();
}
async.parallel({
toUser: async.apply(user.getUserFields, toUid, ['uid', 'username']),
users: async.apply(messaging.getUsersInRoom, req.params.roomid, 0, -1),
messages: async.apply(messaging.getMessages, {
fromuid: req.user.uid,
touid: toUid,
uid: req.user.uid,
roomId: req.params.roomid,
since: 'recent',
isNew: false
}),
allowed: async.apply(messaging.canMessage, req.user.uid, toUid)
allowed: async.apply(messaging.canMessage, req.user.uid, req.params.roomid)
}, next);
}
], function(err, data) {
@@ -74,15 +54,20 @@ chatsController.get = function(req, res, callback) {
return callback(err);
}
var usernames = data.users.map(function(user) {
return user && user.username;
}).join(', ');
res.render('chats', {
chats: results.recentChats.users,
nextStart: results.recentChats.nextStart,
contacts: results.contacts,
meta: data.toUser,
roomId: req.params.roomid,
rooms: recentChats.rooms,
nextStart: recentChats.nextStart,
users: data.users,
usernames: usernames,
messages: data.messages,
allowed: data.allowed,
title: '[[pages:chat, ' + data.toUser.username + ']]',
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:chats]]', url: '/chats'}, {text: data.toUser.username}])
title: '[[pages:chat, ' + usernames + ']]',
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:chats]]', url: '/chats'}, {text: usernames}])
});
});
});

View File

@@ -260,7 +260,7 @@ var async = require('async'),
if (err) {
return next(err);
}
uids = uids.filter(function(value, index, array) {
uids = uids.filter(function(value) {
return value && parseInt(value, 10) !== parseInt(uid, 10);
});
user.getUsersFields(uids, ['uid', 'username', 'picture', 'status', 'lastonline'] , next);
@@ -348,20 +348,20 @@ var async = require('async'),
}, 1000*60); // wait 60s before sending
};
Messaging.canMessage = function(fromUid, toUid, callback) {
if (parseInt(meta.config.disableChat) === 1 || !fromUid || toUid === fromUid) {
Messaging.canMessage = function(uid, roomId, callback) {
if (parseInt(meta.config.disableChat) === 1 || !uid) {
return callback(null, false);
}
async.waterfall([
function (next) {
user.exists(toUid, next);
Messaging.roomExists(roomId, next);
},
function (exists, next) {
if (!exists) {
function (roomExists, next) {
if (!roomExists) {
return callback(null, false);
}
user.getUserFields(fromUid, ['banned', 'email:confirmed'], next);
user.getUserFields(uid, ['banned', 'email:confirmed'], next);
},
function (userData, next) {
if (parseInt(userData.banned, 10) === 1) {
@@ -372,20 +372,7 @@ var async = require('async'),
return callback(null, false);
}
user.getSettings(toUid, next);
},
function(settings, next) {
if (!settings.restrictChat) {
return callback(null, true);
}
user.isAdministrator(fromUid, next);
},
function(isAdmin, next) {
if (isAdmin) {
return callback(null, true);
}
user.isFollowing(toUid, fromUid, next);
next(null, true);
}
], callback);
};

View File

@@ -3,9 +3,14 @@
var async = require('async');
var db = require('../database');
var user = require('../user');
module.exports = function(Messaging) {
Messaging.isUserInRoom = function(uid, roomId, callback) {
db.isSortedSetMember('chat:room:' + roomId + ':uids', uid, callback);
};
Messaging.roomExists = function(roomId, callback) {
db.exists('chat:room:' + roomId + ':uids', callback);
};
@@ -44,4 +49,15 @@ module.exports = function(Messaging) {
db.getSortedSetRange('chat:room:' + roomId + ':uids', start, stop, callback);
};
Messaging.getUsersInRoom = function(roomId, start, stop, callback) {
async.waterfall([
function (next) {
Messaging.getUidsInRoom(roomId, start, stop, next);
},
function (uids, next) {
user.getUsersFields(uids, ['username', 'uid', 'picture', 'status'], next);
}
], callback);
};
};

View File

@@ -23,5 +23,5 @@ module.exports = function (app, middleware, controllers) {
setupPageRoute(app, '/user/:userslug/settings', middleware, accountMiddlewares, controllers.accounts.settings.get);
setupPageRoute(app, '/notifications', middleware, [middleware.authenticate], controllers.accounts.notifications.get);
setupPageRoute(app, '/chats/:userslug?', middleware, [middleware.authenticate], controllers.accounts.chats.get);
setupPageRoute(app, '/chats/:roomid?', middleware, [middleware.authenticate], controllers.accounts.chats.get);
};