Skip to content
This repository was archived by the owner on Nov 21, 2024. It is now read-only.

Commit 6a096d0

Browse files
benlangfeldbklang
authored andcommitted
Avoid rendering messages with duplicate IDs
This is motivated by a race condition when loading archived messages (calling Candy.View.Pane.Message.show) at the same time as rendering an incoming message, such as when a 1-on-1 message arrives and auto-opens a new pane for the chat partner.
1 parent 7e43947 commit 6a096d0

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

src/view/pane/message.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,20 @@ Candy.View.Pane = (function(self, $) {
110110
timestamp = Candy.Util.iso8601toDate(timestamp);
111111
}
112112

113+
var messagePane = self.Room.getPane(roomJid, ".message-pane");
114+
var messageId;
115+
if (stanza) {
116+
messageId = $(stanza).attr('id');
117+
if (messageId) {
118+
var existingMessage = messagePane.find('[data-message-id="' + messageId + '"]');
119+
if (messageId && existingMessage.length > 0) {
120+
// This message ID has already been rendered
121+
return false;
122+
}
123+
}
124+
}
125+
113126
// Before we add the new message, check to see if we should be automatically scrolling or not.
114-
var messagePane = self.Room.getPane(roomJid, '.message-pane');
115127
var enableScroll = ((messagePane.scrollTop() + messagePane.outerHeight()) === messagePane.prop('scrollHeight')) || !$(messagePane).is(':visible');
116128
Candy.View.Pane.Chat.rooms[roomJid].enableScroll = enableScroll;
117129

@@ -158,7 +170,8 @@ Candy.View.Pane = (function(self, $) {
158170
time: Candy.Util.localizedTime(timestamp),
159171
timestamp: timestamp.toISOString(),
160172
roomjid: roomJid,
161-
from: from
173+
from: from,
174+
id: messageId
162175
},
163176
stanza: stanza
164177
};
@@ -246,6 +259,8 @@ Candy.View.Pane = (function(self, $) {
246259
* (String) message - Message text
247260
*/
248261
$(Candy).triggerHandler('candy:view.message.after-show', evtData);
262+
263+
return true;
249264
}
250265
};
251266

src/view/template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ Candy.View.Template = (function(self){
8989

9090
self.Message = {
9191
pane: '<div class="message-pane-wrapper"><ul class="message-pane"></ul></div>',
92-
item: '<li><small data-timestamp="{{timestamp}}">{{time}}</small><div>' +
92+
item: '<li data-message-id="{{id}}"><small data-timestamp="{{timestamp}}">{{time}}</small><div>' +
9393
'<a class="label" href="#" class="name">{{displayName}}</a>' +
9494
'<span class="spacer">▸</span>{{{message}}}</div></li>'
9595
};

0 commit comments

Comments
 (0)