Skip to content

Commit

Permalink
Move loading of chat messages to signaling class.
Browse files Browse the repository at this point in the history
No functional changes yet, but this will allow later to use the standalone
signaling backend to notify clients about new chat messages without having
to poll (see #624).

Signed-off-by: Joachim Bauch <bauch@struktur.de>
  • Loading branch information
fancycode authored and nickvergessen committed Jul 12, 2018
1 parent bd3165e commit 18c090f
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 49 deletions.
62 changes: 13 additions & 49 deletions js/models/chatmessagecollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@
throw 'Missing parameter token';
}

this._lastFetch = null;

this._handler = this._messagesReceived.bind(this);
this.setRoomToken(options.token);
},

Expand All @@ -84,14 +83,10 @@

this.token = token;

this.lastKnownMessageId = 0;

this._waitTimeUntilRetry = 1;

if (token !== null) {
this.url = OC.linkToOCS('apps/spreed/api/v1/chat', 2) + token;
this.signaling = OCA.SpreedMe.app.signaling;
} else {
this.url = null;
this.signaling = null;
}

this.reset();
Expand All @@ -101,52 +96,21 @@
this.invoke('updateGuestName', {sessionId: sessionId, displayName: newDisplayName});
},

receiveMessages: function() {
this.receiveMessagesAgain = true;

this._lastFetch = this.fetch({
data: {
lastKnownMessageId: this.lastKnownMessageId,
lookIntoFuture: 1
},
success: _.bind(this._successfulFetch, this),
error: _.bind(this._failedFetch, this)
});
},

stopReceivingMessages: function() {
this.receiveMessagesAgain = false;

if (this._lastFetch !== null) {
this._lastFetch.abort();
}
_messagesReceived: function(messages) {
this.set(messages);
},

_successfulFetch: function(collection, response, options) {
var lastKnownMessageId = options.xhr.getResponseHeader("X-Chat-Last-Given");
if (lastKnownMessageId !== null) {
this.lastKnownMessageId = lastKnownMessageId;
}

this._lastFetch = null;

this._waitTimeUntilRetry = 1;

if (this.receiveMessagesAgain) {
this.receiveMessages();
receiveMessages: function() {
if (this.signaling) {
this.signaling.on("chatMessagesReceived", this._handler);
this.signaling.startReceiveMessages();
}
},

_failedFetch: function() {
this._lastFetch = null;

if (this.receiveMessagesAgain) {
_.delay(_.bind(this.receiveMessages, this), this._waitTimeUntilRetry * 1000);

// Increase the wait time until retry to at most 64 seconds.
if (this._waitTimeUntilRetry < 64) {
this._waitTimeUntilRetry *= 2;
}
stopReceivingMessages: function() {
if (this.signaling) {
this.signaling.off("chatMessagesReceived", this._handler);
this.signaling.stopReceiveMessages();
}
}

Expand Down
122 changes: 122 additions & 0 deletions js/signaling.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
this.currentCallToken = null;
this.handlers = {};
this.features = {};
this.pendingChatRequests = [];
}

OCA.Talk.Signaling.Base = Base;
Expand All @@ -56,6 +57,18 @@
}
};

OCA.Talk.Signaling.Base.prototype.off = function(ev, handler) {
if (!this.handlers.hasOwnProperty(ev)) {
return;
}

var pos = this.handlers[ev].indexOf(handler);
while (pos !== -1) {
this.handlers[ev].splice(pos, 1);
pos = this.handlers[ev].indexOf(handler);
}
};

OCA.Talk.Signaling.Base.prototype._trigger = function(ev, args) {
var handlers = this.handlers[ev];
if (!handlers) {
Expand Down Expand Up @@ -169,6 +182,7 @@
console.log("Joined", result);
this.currentRoomToken = token;
this._trigger('joinRoom', [token]);
this._runPendingChatRequests();
this._joinRoomSuccess(token, result.ocs.data.sessionId);
}.bind(this),
error: function (result) {
Expand Down Expand Up @@ -276,6 +290,50 @@
});
};

OCA.Talk.Signaling.Base.prototype._runPendingChatRequests = function() {
while (this.pendingChatRequests.length) {
var item = this.pendingChatRequests.shift();
this._doReceiveChatMessages.apply(this, item);
}
};

OCA.Talk.Signaling.Base.prototype.receiveChatMessages = function(lastKnownMessageId) {
var defer = $.Deferred();
if (!this.currentRoomToken) {
// Not in a room yet, defer loading of messages.
this.pendingChatRequests.push([defer, lastKnownMessageId]);
return defer;
}

return this._doReceiveChatMessages(defer, lastKnownMessageId);
};

OCA.Talk.Signaling.Base.prototype._doReceiveChatMessages = function(defer, lastKnownMessageId) {
$.ajax({
url: OC.linkToOCS('apps/spreed/api/v1/chat', 2) + this.currentRoomToken,
method: 'GET',
data: {
lastKnownMessageId: lastKnownMessageId,
lookIntoFuture: 1
},
beforeSend: function (request) {
defer.notify(request);
request.setRequestHeader('Accept', 'application/json');
},
success: function (data, status, request) {
if (status === "notmodified") {
defer.resolve(null, request);
} else {
defer.resolve(data.ocs.data, request);
}
}.bind(this),
error: function (result) {
defer.reject(result);
}
});
return defer;
};

// Connection to the internal signaling server provided by the app.
function Internal(/*settings*/) {
OCA.Talk.Signaling.Base.prototype.constructor.apply(this, arguments);
Expand All @@ -287,6 +345,10 @@

this.pullMessagesRequest = null;

// TODO(fancycode): Move to "Internal" class once an implementation
// exists for the standalone signaling server.
this._lastChatMessagesFetch = null;

this.sendInterval = window.setInterval(function(){
this.sendPendingMessages();
}.bind(this), 500);
Expand Down Expand Up @@ -528,6 +590,66 @@
}.bind(this));
};

// TODO(fancycode): Move chat functions to "Internal" class once an
// implementation exists for the standalone signaling server.
OCA.Talk.Signaling.Base.prototype.startReceiveMessages = function() {
this._waitTimeUntilRetry = 1;
this.receiveMessagesAgain = true;
this.lastKnownMessageId = 0;

this._receiveChatMessages();
};

OCA.Talk.Signaling.Base.prototype.stopReceiveMessages = function() {
this.receiveMessagesAgain = false;
if (this._lastChatMessagesFetch !== null) {
this._lastChatMessagesFetch.abort();
}
};

OCA.Talk.Signaling.Base.prototype._receiveChatMessages = function() {
this.receiveChatMessages(this.lastKnownMessageId)
.progress(this._messagesReceiveStart.bind(this))
.done(this._messagesReceiveSuccess.bind(this))
.fail(this._messagesReceiveError.bind(this));
};

OCA.Talk.Signaling.Base.prototype._messagesReceiveStart = function(xhr) {
this._lastChatMessagesFetch = xhr;
};

OCA.Talk.Signaling.Base.prototype._messagesReceiveSuccess = function(messages, xhr) {
var lastKnownMessageId = xhr.getResponseHeader("X-Chat-Last-Given");
if (lastKnownMessageId !== null) {
this.lastKnownMessageId = lastKnownMessageId;
}

this._lastChatMessagesFetch = null;

this._waitTimeUntilRetry = 1;

if (this.receiveMessagesAgain) {
this._receiveChatMessages();
}

if (messages && messages.length) {
this._trigger("chatMessagesReceived", [messages]);
}
};

OCA.Talk.Signaling.Base.prototype._messagesReceiveError = function(/* result */) {
this._lastChatMessagesFetch = null;

if (this.receiveMessagesAgain) {
_.delay(_.bind(this._receiveChatMessages, this), this._waitTimeUntilRetry * 1000);

// Increase the wait time until retry to at most 64 seconds.
if (this._waitTimeUntilRetry < 64) {
this._waitTimeUntilRetry *= 2;
}
}
};

function Standalone(settings, urls) {
OCA.Talk.Signaling.Base.prototype.constructor.apply(this, arguments);
if (typeof(urls) === "string") {
Expand Down

0 comments on commit 18c090f

Please sign in to comment.