mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-08 06:55:46 +01:00
changed the chat route to /chats/:roomid?
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
'use strict';
|
'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) {
|
define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll', 'translator'], function(components, S, sounds, infinitescroll, translator) {
|
||||||
var Chats = {
|
var Chats = {
|
||||||
@@ -43,8 +43,8 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
|
|||||||
};
|
};
|
||||||
|
|
||||||
Chats.addEventListeners = function() {
|
Chats.addEventListeners = function() {
|
||||||
components.get('chat/recent').on('click', 'li', function(e) {
|
$('[component="chat/recent"]').on('click', '[component="chat/recent/room"]', function() {
|
||||||
Chats.switchChat(parseInt($(this).attr('data-uid'), 10), $(this).attr('data-username'));
|
Chats.switchChat($(this).attr('data-roomid'));
|
||||||
});
|
});
|
||||||
|
|
||||||
Chats.addSendHandlers(Chats.getRecipientUid(), $('.chat-input'), $('.expanded-chat button[data-action="send"]'));
|
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();
|
prev = activeContact.prev();
|
||||||
|
|
||||||
if (prev.length) {
|
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() {
|
Mousetrap.bind('ctrl+down', function() {
|
||||||
@@ -104,7 +104,7 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
|
|||||||
next = activeContact.next();
|
next = activeContact.next();
|
||||||
|
|
||||||
if (next.length) {
|
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) {
|
Mousetrap.bind('up', function(e) {
|
||||||
@@ -181,40 +181,15 @@ define('forum/chats', ['components', 'string', 'sounds', 'forum/infinitescroll',
|
|||||||
$(this).attr('data-typing', val);
|
$(this).attr('data-typing', val);
|
||||||
});
|
});
|
||||||
|
|
||||||
sendEl.off('click').on('click', function(e) {
|
sendEl.off('click').on('click', function() {
|
||||||
Chats.sendMessage(toUid, inputEl);
|
Chats.sendMessage(toUid, inputEl);
|
||||||
inputEl.focus();
|
inputEl.focus();
|
||||||
return false;
|
return false;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
Chats.switchChat = function(uid, username) {
|
Chats.switchChat = function(roomid) {
|
||||||
if (!$('#content [component="chat/messages"]').length) {
|
ajaxify.go('chats/' + roomid);
|
||||||
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.loadChatSince = function(toUid, chatContentEl, since) {
|
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) {
|
socket.on('event:chats.edit', function(data) {
|
||||||
var message;
|
|
||||||
|
|
||||||
data.messages.forEach(function(message) {
|
data.messages.forEach(function(message) {
|
||||||
templates.parse('partials/chat_message', {
|
templates.parse('partials/chat_message', {
|
||||||
messages: message
|
messages: message
|
||||||
}, function(html) {
|
}, function(html) {
|
||||||
body = components.get('chat/message', message.messageId);
|
var body = components.get('chat/message', message.messageId);
|
||||||
if (body.length) {
|
if (body.length) {
|
||||||
body.replaceWith(html);
|
body.replaceWith(html);
|
||||||
components.get('chat/message', message.messageId).find('.timeago').timeago();
|
components.get('chat/message', message.messageId).find('.timeago').timeago();
|
||||||
|
|||||||
@@ -1,13 +1,11 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
var async = require('async'),
|
var async = require('async');
|
||||||
nconf = require('nconf'),
|
|
||||||
|
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 = {};
|
var chatsController = {};
|
||||||
|
|
||||||
@@ -16,35 +14,15 @@ chatsController.get = function(req, res, callback) {
|
|||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
// In case a userNAME is passed in instead of a slug, the route should not 404
|
messaging.getRecentChats(req.user.uid, 0, 19, function(err, recentChats) {
|
||||||
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) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (results.recentChats.users && results.recentChats.users.length) {
|
if (!req.params.roomid) {
|
||||||
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) {
|
|
||||||
return res.render('chats', {
|
return res.render('chats', {
|
||||||
chats: results.recentChats.users,
|
rooms: recentChats.rooms,
|
||||||
nextStart: results.recentChats.nextStart,
|
nextStart: recentChats.nextStart,
|
||||||
contacts: results.contacts,
|
|
||||||
allowed: true,
|
allowed: true,
|
||||||
title: '[[pages:chats]]',
|
title: '[[pages:chats]]',
|
||||||
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:chats]]'}])
|
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:chats]]'}])
|
||||||
@@ -52,21 +30,23 @@ chatsController.get = function(req, res, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
async.apply(user.getUidByUserslug, req.params.userslug),
|
function (next) {
|
||||||
function(toUid, next) {
|
messaging.isUserInRoom(req.uid, req.params.roomid, next);
|
||||||
if (!toUid || parseInt(toUid, 10) === parseInt(req.user.uid, 10)) {
|
},
|
||||||
|
function (inRoom, next) {
|
||||||
|
if (!inRoom) {
|
||||||
return callback();
|
return callback();
|
||||||
}
|
}
|
||||||
|
|
||||||
async.parallel({
|
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, {
|
messages: async.apply(messaging.getMessages, {
|
||||||
fromuid: req.user.uid,
|
uid: req.user.uid,
|
||||||
touid: toUid,
|
roomId: req.params.roomid,
|
||||||
since: 'recent',
|
since: 'recent',
|
||||||
isNew: false
|
isNew: false
|
||||||
}),
|
}),
|
||||||
allowed: async.apply(messaging.canMessage, req.user.uid, toUid)
|
allowed: async.apply(messaging.canMessage, req.user.uid, req.params.roomid)
|
||||||
}, next);
|
}, next);
|
||||||
}
|
}
|
||||||
], function(err, data) {
|
], function(err, data) {
|
||||||
@@ -74,15 +54,20 @@ chatsController.get = function(req, res, callback) {
|
|||||||
return callback(err);
|
return callback(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var usernames = data.users.map(function(user) {
|
||||||
|
return user && user.username;
|
||||||
|
}).join(', ');
|
||||||
|
|
||||||
res.render('chats', {
|
res.render('chats', {
|
||||||
chats: results.recentChats.users,
|
roomId: req.params.roomid,
|
||||||
nextStart: results.recentChats.nextStart,
|
rooms: recentChats.rooms,
|
||||||
contacts: results.contacts,
|
nextStart: recentChats.nextStart,
|
||||||
meta: data.toUser,
|
users: data.users,
|
||||||
|
usernames: usernames,
|
||||||
messages: data.messages,
|
messages: data.messages,
|
||||||
allowed: data.allowed,
|
allowed: data.allowed,
|
||||||
title: '[[pages:chat, ' + data.toUser.username + ']]',
|
title: '[[pages:chat, ' + usernames + ']]',
|
||||||
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:chats]]', url: '/chats'}, {text: data.toUser.username}])
|
breadcrumbs: helpers.buildBreadcrumbs([{text: '[[pages:chats]]', url: '/chats'}, {text: usernames}])
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -260,7 +260,7 @@ var async = require('async'),
|
|||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
}
|
}
|
||||||
uids = uids.filter(function(value, index, array) {
|
uids = uids.filter(function(value) {
|
||||||
return value && parseInt(value, 10) !== parseInt(uid, 10);
|
return value && parseInt(value, 10) !== parseInt(uid, 10);
|
||||||
});
|
});
|
||||||
user.getUsersFields(uids, ['uid', 'username', 'picture', 'status', 'lastonline'] , next);
|
user.getUsersFields(uids, ['uid', 'username', 'picture', 'status', 'lastonline'] , next);
|
||||||
@@ -348,20 +348,20 @@ var async = require('async'),
|
|||||||
}, 1000*60); // wait 60s before sending
|
}, 1000*60); // wait 60s before sending
|
||||||
};
|
};
|
||||||
|
|
||||||
Messaging.canMessage = function(fromUid, toUid, callback) {
|
Messaging.canMessage = function(uid, roomId, callback) {
|
||||||
if (parseInt(meta.config.disableChat) === 1 || !fromUid || toUid === fromUid) {
|
if (parseInt(meta.config.disableChat) === 1 || !uid) {
|
||||||
return callback(null, false);
|
return callback(null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
async.waterfall([
|
async.waterfall([
|
||||||
function (next) {
|
function (next) {
|
||||||
user.exists(toUid, next);
|
Messaging.roomExists(roomId, next);
|
||||||
},
|
},
|
||||||
function (exists, next) {
|
function (roomExists, next) {
|
||||||
if (!exists) {
|
if (!roomExists) {
|
||||||
return callback(null, false);
|
return callback(null, false);
|
||||||
}
|
}
|
||||||
user.getUserFields(fromUid, ['banned', 'email:confirmed'], next);
|
user.getUserFields(uid, ['banned', 'email:confirmed'], next);
|
||||||
},
|
},
|
||||||
function (userData, next) {
|
function (userData, next) {
|
||||||
if (parseInt(userData.banned, 10) === 1) {
|
if (parseInt(userData.banned, 10) === 1) {
|
||||||
@@ -372,20 +372,7 @@ var async = require('async'),
|
|||||||
return callback(null, false);
|
return callback(null, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
user.getSettings(toUid, next);
|
next(null, true);
|
||||||
},
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
], callback);
|
], callback);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -3,9 +3,14 @@
|
|||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
|
||||||
var db = require('../database');
|
var db = require('../database');
|
||||||
|
var user = require('../user');
|
||||||
|
|
||||||
module.exports = function(Messaging) {
|
module.exports = function(Messaging) {
|
||||||
|
|
||||||
|
Messaging.isUserInRoom = function(uid, roomId, callback) {
|
||||||
|
db.isSortedSetMember('chat:room:' + roomId + ':uids', uid, callback);
|
||||||
|
};
|
||||||
|
|
||||||
Messaging.roomExists = function(roomId, callback) {
|
Messaging.roomExists = function(roomId, callback) {
|
||||||
db.exists('chat:room:' + roomId + ':uids', 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);
|
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, '/user/:userslug/settings', middleware, accountMiddlewares, controllers.accounts.settings.get);
|
||||||
|
|
||||||
setupPageRoute(app, '/notifications', middleware, [middleware.authenticate], controllers.accounts.notifications.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);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user