Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load initial chat messages from most recent to oldest. #1322

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions js/models/chatmessagecollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
var ChatMessageCollection = Backbone.Collection.extend({

model: OCA.SpreedMe.Models.ChatMessage,
comparator: 'id',

initialize: function(models, options) {
if (options.token === undefined) {
Expand Down
36 changes: 33 additions & 3 deletions js/signaling.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
this.features = {};
this.pendingChatRequests = [];
this._lastChatMessagesFetch = null;
this.chatBatchSize = 10;
}

OCA.Talk.Signaling.Base = Base;
Expand Down Expand Up @@ -362,7 +363,8 @@
OCA.Talk.Signaling.Base.prototype._getChatRequestData = function(lastKnownMessageId) {
return {
lastKnownMessageId: lastKnownMessageId,
lookIntoFuture: 1
lookIntoFuture: 1,
limit: this.chatBatchSize
};
};

Expand Down Expand Up @@ -394,6 +396,14 @@
this.receiveMessagesAgain = true;
this.lastKnownMessageId = 0;

var room = OCA.SpreedMe.app.activeRoom;
if (room && room.attributes.lastMessage && room.attributes.lastMessage.id > this.chatBatchSize) {
this.lastKnownMessageId = room.attributes.lastMessage.id;
// Start loading at a multiple of the batch size to make sure we end up loading from "0" in the last request.
this.chatLoadingPosition = Math.max(0, this.lastKnownMessageId - (this.lastKnownMessageId % this.chatBatchSize));
} else {
this.chatLoadingPosition = -1;
}
this._receiveChatMessages();
};

Expand All @@ -410,7 +420,16 @@
return;
}

this.receiveChatMessages(this.lastKnownMessageId)
var position;
if (this.chatLoadingPosition >= 0) {
// Still loading initial messages (backwards).
position = this.chatLoadingPosition;
} else {
// All initial messages loaded, continue polling.
position = this.lastKnownMessageId;
}

this.receiveChatMessages(position)
.progress(this._messagesReceiveStart.bind(this))
.done(this._messagesReceiveSuccess.bind(this))
.fail(this._messagesReceiveError.bind(this));
Expand All @@ -421,9 +440,20 @@
};

OCA.Talk.Signaling.Base.prototype._messagesReceiveSuccess = function(messages, xhr) {
if (this.chatLoadingPosition >= 0) {
if (this.chatLoadingPosition > 0) {
// Still loading initial messages (backwards).
this.chatLoadingPosition = Math.max(0, this.chatLoadingPosition - this.chatBatchSize);
} else {
// Loaded last block of initial messages (backwards).
this.chatLoadingPosition = -1;
}
}

// Loading of new messages.
var lastKnownMessageId = xhr.getResponseHeader("X-Chat-Last-Given");
if (lastKnownMessageId !== null) {
this.lastKnownMessageId = lastKnownMessageId;
this.lastKnownMessageId = Math.max(this.lastKnownMessageId, lastKnownMessageId);
}

this._lastChatMessagesFetch = null;
Expand Down
2 changes: 1 addition & 1 deletion js/views/virtuallist.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@
// If the appended elements are not immediately after the last
// loaded element there is nothing to load now; they will be loaded
// as needed with the other pending elements.
if (this._$firstAppendedElement._previous !== this._$lastLoadedElement) {
if (!this._$firstAppendedElement || this._$firstAppendedElement._previous !== this._$lastLoadedElement) {
delete this._appendedElementsBuffer;

return;
Expand Down