1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-26 03:02:25 -05:00

Get MUCs working with the mid changes

This commit is contained in:
Lance Stout 2013-12-18 13:31:22 -08:00
parent 7dc2964d41
commit 28914dad11
7 changed files with 43 additions and 28 deletions

View File

@ -251,7 +251,7 @@ module.exports = function (client, app) {
client.on('chat', function (msg) { client.on('chat', function (msg) {
msg = msg.toJSON(); msg = msg.toJSON();
msg.mid = msg.id || uuid.v4(); msg.mid = msg.id;
delete msg.id; delete msg.id;
var contact = me.getContact(msg.from, msg.to); var contact = me.getContact(msg.from, msg.to);
@ -276,7 +276,7 @@ module.exports = function (client, app) {
client.on('groupchat', function (msg) { client.on('groupchat', function (msg) {
msg = msg.toJSON(); msg = msg.toJSON();
msg.mid = msg.id || uuid.v4(); msg.mid = msg.id;
delete msg.id; delete msg.id;
var contact = me.getContact(msg.from, msg.to); var contact = me.getContact(msg.from, msg.to);
@ -289,11 +289,13 @@ module.exports = function (client, app) {
client.on('replace', function (msg) { client.on('replace', function (msg) {
msg = msg.toJSON(); msg = msg.toJSON();
msg.mid = msg.id;
delete msg.id;
var contact = me.getContact(msg.from, msg.to); var contact = me.getContact(msg.from, msg.to);
if (!contact) return; if (!contact) return;
var id = msg.replace; var original = Message.idLookup(msg.from[msg.type === 'groupchat' ? 'full' : 'bare'], msg.replace);
var original = HumanModel.registry.lookup('message', id, 'messages');
if (!original) return; if (!original) return;
@ -343,7 +345,7 @@ module.exports = function (client, app) {
if (stanza.body) { if (stanza.body) {
var contact = me.getContact(stanza.to, stanza.from); var contact = me.getContact(stanza.to, stanza.from);
if (contact) { if (contact) {
var msg = HumanModel.registry.lookup('message', stanza.id, 'messages'); var msg = Message.idLookup(me.jid.bare, stanza.id);
if (msg) { if (msg) {
msg.acked = true; msg.acked = true;
} }

View File

@ -222,9 +222,14 @@ module.exports = HumanModel.define({
}); });
} }
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); this.messages.add(message);
message.save(); message.save();
}
var newInteraction = new Date(message.created); var newInteraction = new Date(message.created);
if (!this.lastInteraction || this.lastInteraction < newInteraction) { if (!this.lastInteraction || this.lastInteraction < newInteraction) {
@ -257,23 +262,21 @@ module.exports = HumanModel.define({
result = result.toJSON(); result = result.toJSON();
var msg = result.mam.forwarded.message; var msg = result.mam.forwarded.message;
if (!msg.id) { msg.mid = msg.id;
msg.id = uuid.v4(); delete msg.id;
}
if (!msg.delay) { if (!msg.delay) {
msg.delay = result.mam.forwarded.delay; msg.delay = result.mam.forwarded.delay;
} }
if (msg.replace) { 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 // Drop the message if editing a previous, but
// keep it if it didn't actually change an // keep it if it didn't actually change an
// existing message. // existing message.
if (original && original.correct(msg)) return; if (original && original.correct(msg)) return;
} }
var message = new Message(msg); var message = new Message(msg);
message.archivedId = result.mam.id; message.archivedId = result.mam.id;
message.acked = true; message.acked = true;

View File

@ -6,13 +6,11 @@ var HumanModel = require('human-model');
var templates = require('../templates'); var templates = require('../templates');
var htmlify = require('../helpers/htmlify'); var htmlify = require('../helpers/htmlify');
var ID_CACHE = {};
module.exports = HumanModel.define({ var Message = module.exports = HumanModel.define({
initialize: function (attrs) { initialize: function (attrs) {
this._created = new Date(Date.now()); this._created = new Date(Date.now());
if (attrs.mid) {
HumanModel.registry._getCache('messages')['message' + attrs.mid] = this;
}
}, },
type: 'message', type: 'message',
props: { props: {
@ -172,6 +170,11 @@ module.exports = HumanModel.define({
return true; return true;
}, },
save: function () { save: function () {
if (this.mid) {
var from = this.type == 'groupchat' ? this.from.full : this.from.bare;
Message.idStore(from, this.mid, this);
}
var data = { var data = {
archivedId: this.archivedId, archivedId: this.archivedId,
owner: this.owner, 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;
};

View File

@ -83,12 +83,14 @@ module.exports = HumanModel.define({
} }
message.acked = true; message.acked = true;
message.save();
this.messages.add(message);
if (mine) { if (mine) {
this.lastSentMessage = message; this.lastSentMessage = message;
} }
this.messages.add(message);
var newInteraction = new Date(message.created); var newInteraction = new Date(message.created);
if (!this.lastInteraction || this.lastInteraction < newInteraction) { if (!this.lastInteraction || this.lastInteraction < newInteraction) {
this.lastInteraction = newInteraction; this.lastInteraction = newInteraction;

View File

@ -19,9 +19,7 @@ module.exports = BasePage.extend(chatHelpers).extend({
this.listenTo(this, 'pageloaded', this.handlePageLoaded); this.listenTo(this, 'pageloaded', this.handlePageLoaded);
this.listenTo(this, 'pageunloaded', this.handlePageUnloaded); this.listenTo(this, 'pageunloaded', this.handlePageUnloaded);
this.listenTo(this.model.messages, 'change:body', this.refreshModel); this.listenTo(this.model.messages, 'change', this.refreshModel);
this.listenTo(this.model.messages, 'change:edited', this.refreshModel);
this.listenTo(this.model.messages, 'change:pending', this.refreshModel);
this.render(); this.render();
}, },
@ -185,7 +183,7 @@ module.exports = BasePage.extend(chatHelpers).extend({
this.model.lastSentMessage.correct(message); this.model.lastSentMessage.correct(message);
} else { } else {
var msgModel = new MessageModel(message); var msgModel = new MessageModel(message);
this.model.messages.add(msgModel); this.model.addMessage(msgModel);
this.model.lastSentMessage = msgModel; this.model.lastSentMessage = msgModel;
} }
} }

View File

@ -17,9 +17,7 @@ module.exports = BasePage.extend(chatHelpers).extend({
this.listenTo(this, 'pageloaded', this.handlePageLoaded); this.listenTo(this, 'pageloaded', this.handlePageLoaded);
this.listenTo(this, 'pageunloaded', this.handlePageUnloaded); this.listenTo(this, 'pageunloaded', this.handlePageUnloaded);
this.listenTo(this.model.messages, 'change:body', this.refreshModel); this.listenTo(this.model.messages, 'change', this.refreshModel);
this.listenTo(this.model.messages, 'change:edited', this.refreshModel);
this.listenTo(this.model.messages, 'change:pending', this.refreshModel);
this.render(); this.render();
}, },
@ -193,8 +191,7 @@ module.exports = BasePage.extend(chatHelpers).extend({
this.model.lastSentMessage.correct(message); this.model.lastSentMessage.correct(message);
} else { } else {
var msgModel = new MessageModel(message); var msgModel = new MessageModel(message);
msgModel.cid = id; msgModel.save();
this.model.messages.add(msgModel);
this.model.lastSentMessage = msgModel; this.model.lastSentMessage = msgModel;
} }
} }

View File

@ -1,5 +1,5 @@
CACHE MANIFEST CACHE MANIFEST
# 0.0.1 1387390012229 # 0.0.1 1387399967854
CACHE: CACHE:
/app.js /app.js