Add support for receiving messages in random order.
Needed for ejabberd support. updates #306
This commit is contained in:
parent
8d84d5ed03
commit
268c4b9389
71
converse.js
71
converse.js
@ -1380,6 +1380,19 @@
|
|||||||
}));
|
}));
|
||||||
},
|
},
|
||||||
|
|
||||||
|
appendMessage: function (attrs) {
|
||||||
|
/* Helper method which appends a message to the end of the chat
|
||||||
|
* box's content area.
|
||||||
|
*
|
||||||
|
* Parameters:
|
||||||
|
* (Object) attrs: An object containing the message attributes.
|
||||||
|
*/
|
||||||
|
_.compose(
|
||||||
|
_.debounce(this.scrollDown.bind(this), 50),
|
||||||
|
this.$content.append.bind(this.$content)
|
||||||
|
)(this.renderMessage(attrs));
|
||||||
|
},
|
||||||
|
|
||||||
showMessage: function (attrs) {
|
showMessage: function (attrs) {
|
||||||
/* Inserts a chat message into the content area of the chat box.
|
/* Inserts a chat message into the content area of the chat box.
|
||||||
* Will also insert a new day indicator if the message is on a
|
* Will also insert a new day indicator if the message is on a
|
||||||
@ -1391,15 +1404,34 @@
|
|||||||
* Parameters:
|
* Parameters:
|
||||||
* (Object) attrs: An object containing the message attributes.
|
* (Object) attrs: An object containing the message attributes.
|
||||||
*/
|
*/
|
||||||
var current_msg_date = moment(attrs.time) || moment,
|
var $first_msg = this.$content.children('.chat-message:first'),
|
||||||
$first_msg = this.$content.children('.chat-message:first'),
|
|
||||||
first_msg_date = $first_msg.data('isodate'),
|
first_msg_date = $first_msg.data('isodate'),
|
||||||
last_msg_date = this.$content.children(':last').data('isodate'),
|
last_msg_date, current_msg_date, day_date, $msgs, msg_dates, idx;
|
||||||
day_date;
|
if (typeof first_msg_date === "undefined") {
|
||||||
|
this.appendMessage(attrs);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
current_msg_date = moment(attrs.time) || moment;
|
||||||
|
last_msg_date = this.$content.children(':last').data('isodate');
|
||||||
|
|
||||||
|
if (typeof last_msg_date !== "undefined" && (current_msg_date.isAfter(last_msg_date) || current_msg_date.isSame(last_msg_date))) {
|
||||||
|
// The new message is after the last message
|
||||||
|
if (current_msg_date.isAfter(last_msg_date, 'day')) {
|
||||||
|
// Append a new day indicator
|
||||||
|
day_date = moment(current_msg_date).startOf('day');
|
||||||
|
this.$content.append(converse.templates.new_day({
|
||||||
|
isodate: current_msg_date.format(),
|
||||||
|
datestring: current_msg_date.format("dddd MMM Do YYYY")
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
this.appendMessage(attrs);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof first_msg_date !== "undefined" &&
|
if (typeof first_msg_date !== "undefined" &&
|
||||||
(current_msg_date.isBefore(first_msg_date) ||
|
(current_msg_date.isBefore(first_msg_date) ||
|
||||||
(current_msg_date.isSame(first_msg_date) && !current_msg_date.isSame(last_msg_date)))) {
|
(current_msg_date.isSame(first_msg_date) && !current_msg_date.isSame(last_msg_date)))) {
|
||||||
|
// The new message is before the first message
|
||||||
|
|
||||||
if ($first_msg.prev().length === 0) {
|
if ($first_msg.prev().length === 0) {
|
||||||
// There's no day indicator before the first message, so we prepend one.
|
// There's no day indicator before the first message, so we prepend one.
|
||||||
@ -1427,19 +1459,24 @@
|
|||||||
}
|
}
|
||||||
)(this.renderMessage(attrs));
|
)(this.renderMessage(attrs));
|
||||||
}
|
}
|
||||||
return;
|
} else {
|
||||||
} else if (current_msg_date.isAfter(last_msg_date, 'day')) {
|
// We need to find the correct place to position the message
|
||||||
// Append a new day indicator
|
current_msg_date = current_msg_date.format();
|
||||||
day_date = moment(current_msg_date).startOf('day');
|
$msgs = this.$content.children('.chat-message');
|
||||||
this.$content.append(converse.templates.new_day({
|
msg_dates = _.map($msgs, function (el) {
|
||||||
isodate: current_msg_date.format(),
|
return $(el).data('isodate');
|
||||||
datestring: current_msg_date.format("dddd MMM Do YYYY")
|
});
|
||||||
}));
|
msg_dates.push(current_msg_date);
|
||||||
|
msg_dates.sort();
|
||||||
|
idx = msg_dates.indexOf(current_msg_date)-1;
|
||||||
|
_.compose(
|
||||||
|
this.scrollDownMessageHeight.bind(this),
|
||||||
|
function ($el) {
|
||||||
|
$el.insertAfter(this.$content.find('.chat-message[data-isodate="'+msg_dates[idx]+'"]'));
|
||||||
|
return $el;
|
||||||
|
}.bind(this)
|
||||||
|
)(this.renderMessage(attrs));
|
||||||
}
|
}
|
||||||
_.compose(
|
|
||||||
_.debounce(this.scrollDown.bind(this), 50),
|
|
||||||
this.$content.append.bind(this.$content)
|
|
||||||
)(this.renderMessage(attrs));
|
|
||||||
},
|
},
|
||||||
|
|
||||||
renderMessage: function (attrs) {
|
renderMessage: function (attrs) {
|
||||||
|
Loading…
Reference in New Issue
Block a user