From 28914dad11cc557d71f5dcd8f590e22aac0b472d Mon Sep 17 00:00:00 2001 From: Lance Stout Date: Wed, 18 Dec 2013 13:31:22 -0800 Subject: [PATCH] Get MUCs working with the mid changes --- clientapp/helpers/xmppEventHandlers.js | 12 +++++++----- clientapp/models/contact.js | 19 +++++++++++-------- clientapp/models/message.js | 21 +++++++++++++++++---- clientapp/models/muc.js | 4 +++- clientapp/pages/chat.js | 6 ++---- clientapp/pages/groupchat.js | 7 ++----- public/x-manifest.cache | 2 +- 7 files changed, 43 insertions(+), 28 deletions(-) diff --git a/clientapp/helpers/xmppEventHandlers.js b/clientapp/helpers/xmppEventHandlers.js index 774fbbf..78a4a39 100644 --- a/clientapp/helpers/xmppEventHandlers.js +++ b/clientapp/helpers/xmppEventHandlers.js @@ -251,7 +251,7 @@ module.exports = function (client, app) { client.on('chat', function (msg) { msg = msg.toJSON(); - msg.mid = msg.id || uuid.v4(); + msg.mid = msg.id; delete msg.id; var contact = me.getContact(msg.from, msg.to); @@ -276,7 +276,7 @@ module.exports = function (client, app) { client.on('groupchat', function (msg) { msg = msg.toJSON(); - msg.mid = msg.id || uuid.v4(); + msg.mid = msg.id; delete msg.id; var contact = me.getContact(msg.from, msg.to); @@ -289,11 +289,13 @@ module.exports = function (client, app) { client.on('replace', function (msg) { msg = msg.toJSON(); + msg.mid = msg.id; + delete msg.id; + var contact = me.getContact(msg.from, msg.to); if (!contact) return; - var id = msg.replace; - var original = HumanModel.registry.lookup('message', id, 'messages'); + var original = Message.idLookup(msg.from[msg.type === 'groupchat' ? 'full' : 'bare'], msg.replace); if (!original) return; @@ -343,7 +345,7 @@ module.exports = function (client, app) { if (stanza.body) { var contact = me.getContact(stanza.to, stanza.from); if (contact) { - var msg = HumanModel.registry.lookup('message', stanza.id, 'messages'); + var msg = Message.idLookup(me.jid.bare, stanza.id); if (msg) { msg.acked = true; } diff --git a/clientapp/models/contact.js b/clientapp/models/contact.js index 58dc699..3e42e98 100644 --- a/clientapp/models/contact.js +++ b/clientapp/models/contact.js @@ -222,9 +222,14 @@ module.exports = HumanModel.define({ }); } - this.messages.add(message); - - message.save(); + var existing = Message.idLookup(message.from[message.type == 'groupchat' ? 'full' : 'bare'], message.mid); + if (existing) { + existing.set(message); + existing.save(); + } else { + this.messages.add(message); + message.save(); + } var newInteraction = new Date(message.created); if (!this.lastInteraction || this.lastInteraction < newInteraction) { @@ -257,23 +262,21 @@ module.exports = HumanModel.define({ result = result.toJSON(); var msg = result.mam.forwarded.message; - if (!msg.id) { - msg.id = uuid.v4(); - } + msg.mid = msg.id; + delete msg.id; if (!msg.delay) { msg.delay = result.mam.forwarded.delay; } if (msg.replace) { - var original = self.messages.get(msg.replace); + var original = Message.idLookup(msg.from[msg.type == 'groupchat' ? 'full' : 'bare'], msg.replace); // Drop the message if editing a previous, but // keep it if it didn't actually change an // existing message. if (original && original.correct(msg)) return; } - var message = new Message(msg); message.archivedId = result.mam.id; message.acked = true; diff --git a/clientapp/models/message.js b/clientapp/models/message.js index f34fa1f..9481da9 100644 --- a/clientapp/models/message.js +++ b/clientapp/models/message.js @@ -6,13 +6,11 @@ var HumanModel = require('human-model'); var templates = require('../templates'); var htmlify = require('../helpers/htmlify'); +var ID_CACHE = {}; -module.exports = HumanModel.define({ +var Message = module.exports = HumanModel.define({ initialize: function (attrs) { this._created = new Date(Date.now()); - if (attrs.mid) { - HumanModel.registry._getCache('messages')['message' + attrs.mid] = this; - } }, type: 'message', props: { @@ -172,6 +170,11 @@ module.exports = HumanModel.define({ return true; }, save: function () { + if (this.mid) { + var from = this.type == 'groupchat' ? this.from.full : this.from.bare; + Message.idStore(from, this.mid, this); + } + var data = { archivedId: this.archivedId, owner: this.owner, @@ -193,3 +196,13 @@ module.exports = HumanModel.define({ } } }); + +Message.idLookup = function (jid, mid) { + var cache = ID_CACHE[jid] || (ID_CACHE[jid] = {}); + return cache[mid]; +}; + +Message.idStore = function (jid, mid, msg) { + var cache = ID_CACHE[jid] || (ID_CACHE[jid] = {}); + cache[mid] = msg; +}; diff --git a/clientapp/models/muc.js b/clientapp/models/muc.js index bcec2b4..8c34e90 100644 --- a/clientapp/models/muc.js +++ b/clientapp/models/muc.js @@ -83,12 +83,14 @@ module.exports = HumanModel.define({ } message.acked = true; + message.save(); - this.messages.add(message); if (mine) { this.lastSentMessage = message; } + this.messages.add(message); + var newInteraction = new Date(message.created); if (!this.lastInteraction || this.lastInteraction < newInteraction) { this.lastInteraction = newInteraction; diff --git a/clientapp/pages/chat.js b/clientapp/pages/chat.js index d34eb35..a05ea4b 100644 --- a/clientapp/pages/chat.js +++ b/clientapp/pages/chat.js @@ -19,9 +19,7 @@ module.exports = BasePage.extend(chatHelpers).extend({ this.listenTo(this, 'pageloaded', this.handlePageLoaded); this.listenTo(this, 'pageunloaded', this.handlePageUnloaded); - this.listenTo(this.model.messages, 'change:body', this.refreshModel); - this.listenTo(this.model.messages, 'change:edited', this.refreshModel); - this.listenTo(this.model.messages, 'change:pending', this.refreshModel); + this.listenTo(this.model.messages, 'change', this.refreshModel); this.render(); }, @@ -185,7 +183,7 @@ module.exports = BasePage.extend(chatHelpers).extend({ this.model.lastSentMessage.correct(message); } else { var msgModel = new MessageModel(message); - this.model.messages.add(msgModel); + this.model.addMessage(msgModel); this.model.lastSentMessage = msgModel; } } diff --git a/clientapp/pages/groupchat.js b/clientapp/pages/groupchat.js index 475021b..593b037 100644 --- a/clientapp/pages/groupchat.js +++ b/clientapp/pages/groupchat.js @@ -17,9 +17,7 @@ module.exports = BasePage.extend(chatHelpers).extend({ this.listenTo(this, 'pageloaded', this.handlePageLoaded); this.listenTo(this, 'pageunloaded', this.handlePageUnloaded); - this.listenTo(this.model.messages, 'change:body', this.refreshModel); - this.listenTo(this.model.messages, 'change:edited', this.refreshModel); - this.listenTo(this.model.messages, 'change:pending', this.refreshModel); + this.listenTo(this.model.messages, 'change', this.refreshModel); this.render(); }, @@ -193,8 +191,7 @@ module.exports = BasePage.extend(chatHelpers).extend({ this.model.lastSentMessage.correct(message); } else { var msgModel = new MessageModel(message); - msgModel.cid = id; - this.model.messages.add(msgModel); + msgModel.save(); this.model.lastSentMessage = msgModel; } } diff --git a/public/x-manifest.cache b/public/x-manifest.cache index 7256a26..0f532fa 100644 --- a/public/x-manifest.cache +++ b/public/x-manifest.cache @@ -1,5 +1,5 @@ CACHE MANIFEST -# 0.0.1 1387390012229 +# 0.0.1 1387399967854 CACHE: /app.js