inf scroll in chat-content

v1.18.x
barisusakli 8 years ago
parent 6c7fe6b5cf
commit 55396ca442

@ -81,6 +81,39 @@ define('forum/chats', [
Chats.addSinceHandler(ajaxify.data.roomId, $('.expanded-chat .chat-content'), $('.expanded-chat [data-since]')); Chats.addSinceHandler(ajaxify.data.roomId, $('.expanded-chat .chat-content'), $('.expanded-chat [data-since]'));
Chats.addRenameHandler(ajaxify.data.roomId, $('[component="chat/room/name"]')); Chats.addRenameHandler(ajaxify.data.roomId, $('[component="chat/room/name"]'));
Chats.addScrollHandler(ajaxify.data.roomId, ajaxify.data.uid, $('.chat-content'));
};
Chats.addScrollHandler = function(roomId, uid, el) {
var loading = false;
el.off('scroll').on('scroll', function() {
if (loading) {
return;
}
var top = (el[0].scrollHeight - el.height()) * 0.1;
if (el.scrollTop() >= top) {
return;
}
loading = true;
socket.emit('modules.chats.getMessages', {roomId: roomId, uid: uid, start: $('.chat-content').children('[data-index]').first().attr('data-index')}, function(err, data) {
if (err) {
return app.alertError(err.message);
}
messages.parseMessage(data, function(html) {
var currentScrollTop = el.scrollTop();
var previousHeight = el[0].scrollHeight;
html = $(html);
el.prepend(html);
html.find('.timeago').timeago();
html.find('img:not(.not-responsive)').addClass('img-responsive');
el.scrollTop((el[0].scrollHeight - previousHeight) + currentScrollTop);
loading = false;
});
});
});
}; };
Chats.addEditDeleteHandler = function(element, roomId) { Chats.addEditDeleteHandler = function(element, roomId) {

@ -104,7 +104,7 @@ define('chat', [
}; };
module.loadChatsDropdown = function(chatsListEl) { module.loadChatsDropdown = function(chatsListEl) {
socket.emit('modules.chats.getRecentChats', {after: 0}, function(err, data) { socket.emit('modules.chats.getRecentChats', {uid: app.user.uid, after: 0}, function(err, data) {
if (err) { if (err) {
return app.alertError(err.message); return app.alertError(err.message);
} }
@ -261,6 +261,8 @@ define('chat', [
Chats.loadChatSince(chatModal.attr('roomId'), chatModal.find('.chat-content'), 'recent'); Chats.loadChatSince(chatModal.attr('roomId'), chatModal.find('.chat-content'), 'recent');
Chats.addScrollHandler(chatModal.attr('roomId'), app.user.uid, chatModal.find('.chat-content'));
checkStatus(chatModal); checkStatus(chatModal);
taskbar.push('chat', chatModal.attr('UUID'), { taskbar.push('chat', chatModal.attr('UUID'), {

@ -49,21 +49,22 @@ var async = require('async'),
}; };
Messaging.getMessages = function(params, callback) { Messaging.getMessages = function(params, callback) {
var uid = params.uid, var uid = params.uid;
roomId = params.roomId, var roomId = params.roomId;
since = params.since, var since = params.since;
isNew = params.isNew, var isNew = params.isNew;
count = params.count || 250, var start = params.hasOwnProperty('start') ? params.start : 0;
markRead = params.markRead || true; var count = params.count || 250;
var markRead = params.markRead || true;
var min = params.count ? 0 : Date.now() - (terms[since] || terms.day); var min = params.count ? 0 : Date.now() - (terms[since] || terms.day);
if (since === 'recent') { if (since === 'recent') {
count = 49; count = 50;
min = 0; min = 0;
} }
db.getSortedSetRevRangeByScore('uid:' + uid + ':chat:room:' + roomId + ':mids', 0, count, '+inf', min, function(err, mids) { db.getSortedSetRevRangeByScore('uid:' + uid + ':chat:room:' + roomId + ':mids', start, count, '+inf', min, function(err, mids) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
@ -71,10 +72,24 @@ var async = require('async'),
if (!Array.isArray(mids) || !mids.length) { if (!Array.isArray(mids) || !mids.length) {
return callback(null, []); return callback(null, []);
} }
var indices = {};
mids.forEach(function(mid, index) {
indices[mid] = start + index;
});
mids.reverse(); mids.reverse();
Messaging.getMessagesData(mids, uid, roomId, isNew, callback); Messaging.getMessagesData(mids, uid, roomId, isNew, function(err, messageData) {
if (err) {
return callback(err);
}
for(var i=0; i<messageData.length; i++) {
messageData[i].index = indices[messageData[i].messageId.toString()];
}
callback(null, messageData);
});
}); });
if (markRead) { if (markRead) {

@ -316,6 +316,28 @@ SocketModules.chats.hasPrivateChat = function(socket, uid, callback) {
Messaging.hasPrivateChat(socket.uid, uid, callback); Messaging.hasPrivateChat(socket.uid, uid, callback);
}; };
SocketModules.chats.getMessages = function(socket, data, callback) {
if (!socket.uid || !data.uid || !data.roomId) {
return callback(new Error('[[error:invalid-data]]'));
}
var params = {
uid: data.uid,
roomId: data.roomId,
start: parseInt(data.start, 10) + 1,
count: 50,
markRead: false
};
if (socket.uid === parseInt(data.uid, 10)) {
return Messaging.getMessages(params, callback);
}
user.isAdminOrGlobalMod(socket.uid, function(err, isAdminOrGlobalMod) {
if (err || !isAdminOrGlobalMod) {
return callback(err || new Error('[[error:no-privileges]]'));
}
Messaging.getMessages(params, callback);
});
};
/* Sounds */ /* Sounds */
SocketModules.sounds.getSounds = function(socket, data, callback) { SocketModules.sounds.getSounds = function(socket, data, callback) {
// Read sounds from local directory // Read sounds from local directory

Loading…
Cancel
Save