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

Refactor transmitted-messages code #110

Merged
merged 1 commit into from
Mar 17, 2016
Merged
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
55 changes: 12 additions & 43 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,10 @@ function MatrixClient(opts) {
var self = this;
this.scheduler.setProcessFunction(function(eventToSend) {
var room = self.getRoom(eventToSend.getRoomId());
_updateLocalEchoStatus(room, eventToSend, EventStatus.SENDING);
if (eventToSend.status !== EventStatus.SENDING) {
_updatePendingEventStatus(room, eventToSend,
EventStatus.SENDING);
}
return _sendEventHttpRequest(self, eventToSend);
});
}
Expand Down Expand Up @@ -718,7 +721,7 @@ MatrixClient.prototype.joinRoom = function(roomIdOrAlias, opts, callback) {
* @return {module:http-api.MatrixError} Rejects: with an error response.
*/
MatrixClient.prototype.resendEvent = function(event, room) {
_updateLocalEchoStatus(room, event, EventStatus.SENDING);
_updatePendingEventStatus(room, event, EventStatus.SENDING);
return _sendEvent(this, room, event);
};

Expand Down Expand Up @@ -928,11 +931,11 @@ MatrixClient.prototype.sendEvent = function(roomId, eventType, content, txnId,
content: content
});
localEvent._txnId = txnId;
localEvent.status = EventStatus.SENDING;

// add this event immediately to the local store as 'sending'.
if (room) {
localEvent.status = EventStatus.SENDING;
room.addEventsToTimeline([localEvent]);
room.addPendingEvent(localEvent, txnId);
}

if (eventType === "m.room.message" && this.sessionStore && CRYPTO_ENABLED) {
Expand Down Expand Up @@ -1140,11 +1143,6 @@ function _badEncryptedMessage(event, reason) {
}

function _sendEvent(client, room, event, callback) {
// cache the local event ID here because if /sync returns before /send then
// event.getId() will return a REAL event ID which we will then incorrectly
// remove!
var localEventId = event.getId();

var defer = q.defer();
var promise;
// this event may be queued
Expand All @@ -1157,7 +1155,7 @@ function _sendEvent(client, room, event, callback) {
if (promise && client.scheduler.getQueueForEvent(event).length > 1) {
// event is processed FIFO so if the length is 2 or more we know
// this event is stuck behind an earlier event.
_updateLocalEchoStatus(room, event, EventStatus.QUEUED);
_updatePendingEventStatus(room, event, EventStatus.QUEUED);
}
}

Expand All @@ -1167,52 +1165,23 @@ function _sendEvent(client, room, event, callback) {

promise.done(function(res) { // the request was sent OK
if (room) {
var eventId = res.event_id;

// FIXME: This manipulation of the room should probably be done
// inside the room class, not by the client.
var timeline = room.getTimelineForEvent(eventId);
if (!timeline) {
// we haven't yet received the event from the stream; we
// need to update the fake event with the right event id.
//
// best way to make sure the room timeline structures are updated
// correctly is to remove the event and add it again with the right
// ID.
//
// This will also make us synthesize our own read receipt for the
// sent message.
var oldStatus = event.status;
room.removeEvents([localEventId]);
event.event.event_id = res.event_id;
// TODO: at this point, we're still expecting the remote echo
// to come back and update the server-generated fields for
// us. We should probably set the status to some distinct value
// so that the client app can figure out what is going on.
event.status = null;
room.addEventsToTimeline([event]);

// FIXME: doing this here is a horrible fudge, but this all
// needs unpicking, which will touch the crypto code.
room.emit("Room.localEchoUpdated", event, room, localEventId,
oldStatus);
}
room.updatePendingEvent(event, EventStatus.SENT, res.event_id);
}

_resolve(callback, defer, res);
}, function(err) {
// the request failed to send.
_updateLocalEchoStatus(room, event, EventStatus.NOT_SENT);
_updatePendingEventStatus(room, event, EventStatus.NOT_SENT);

_reject(callback, defer, err);
});

return defer.promise;
}

function _updateLocalEchoStatus(room, event, newStatus) {
function _updatePendingEventStatus(room, event, newStatus) {
if (room) {
room.updateLocalEchoStatus(event, newStatus);
room.updatePendingEvent(event, newStatus);
} else {
event.status = newStatus;
}
Expand Down
5 changes: 4 additions & 1 deletion lib/models/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ module.exports.EventStatus = {
/** The event is in the process of being sent. */
SENDING: "sending",
/** The event is in a queue waiting to be sent. */
QUEUED: "queued"
QUEUED: "queued",
/** The event has been sent to the server, but we have not yet received the
* echo. */
SENT: "sent",
};

/**
Expand Down
Loading