1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-25 18:52:20 -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) {
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;
}

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);
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;

View File

@ -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;
};

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

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