changed the chat route to /chats/:roomid?

v1.18.x
barisusakli 9 years ago
parent 58d05f7c79
commit 5d1169e686

@ -1,6 +1,6 @@
'use strict';
/* globals define, config, app, ajaxify, utils, socket, templates */
/* globals define, config, app, ajaxify, utils, socket, templates, Mousetrap, bootbox */
define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', 'translator'], function(components, S, sounds, infinitescroll, translator) {
var Chats = {
@ -43,8 +43,8 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
};
Chats.addEventListeners = function() {
components.get('chat/recent').on('click', 'li', function(e) {
Chats.switchChat(parseInt($(this).attr('data-uid'), 10), $(this).attr('data-username'));
$('[component="chat/recent"]').on('click', '[component="chat/recent/room"]', function() {
Chats.switchChat($(this).attr('data-roomid'));
});
Chats.addSendHandlers(Chats.getRecipientUid(), $('.chat-input'), $('.expanded-chat button[data-action="send"]'));
@ -96,7 +96,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
prev = activeContact.prev();
if (prev.length) {
Chats.switchChat(parseInt(prev.attr('data-uid'), 10), prev.attr('data-username'));
Chats.switchChat(prev.attr('data-roomid'));
}
});
Mousetrap.bind('ctrl+down', function() {
@ -104,7 +104,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
next = activeContact.next();
if (next.length) {
Chats.switchChat(parseInt(next.attr('data-uid'), 10), next.attr('data-username'));
Chats.switchChat(next.attr('data-roomid'));
}
});
Mousetrap.bind('up', function(e) {
@ -181,40 +181,15 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
$(this).attr('data-typing', val);
});
sendEl.off('click').on('click', function(e) {
sendEl.off('click').on('click', function() {
Chats.sendMessage(toUid, inputEl);
inputEl.focus();
return false;
});
};
Chats.switchChat = function(uid, username) {
if (!$('#content [component="chat/messages"]').length) {
return ajaxify.go('chats/' + utils.slugify(username));
}
var contactEl = $('.chats-list [data-uid="' + uid + '"]');
Chats.loadChatSince(uid, $('.chat-content'), 'recent');
Chats.addSendHandlers(uid, components.get('chat/input'), $('[data-action="send"]'));
contactEl
.removeClass('unread')
.addClass('bg-primary')
.siblings().removeClass('bg-primary');
components.get('chat/title').text(username);
components.get('chat/messages').attr('data-uid', uid).attr('data-username', username);
components.get('breadcrumb/current').text(username);
components.get('chat/input').focus();
if (window.history && window.history.pushState) {
var url = 'chats/' + utils.slugify(username);
window.history.pushState({
url: url
}, url, RELATIVE_PATH + '/' + url);
}
Chats.switchChat = function(roomid) {
ajaxify.go('chats/' + roomid);
};
Chats.loadChatSince = function(toUid, chatContentEl, since) {
@ -320,13 +295,12 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
});
socket.on('event:chats.edit', function(data) {
var message;
data.messages.forEach(function(message) {
templates.parse('partials/chat_message', {
messages: message
}, function(html) {
body = components.get('chat/message', message.messageId);
var body = components.get('chat/message', message.messageId);
if (body.length) {
body.replaceWith(html);
components.get('chat/message', message.messageId).find('.timeago').timeago();

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

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

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

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

Loading…
Cancel
Save