1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-22 17:22:22 -05:00

Cache roster per account.

This commit is contained in:
Lance Stout 2013-09-11 14:58:39 -07:00
parent dd3f1a27da
commit 67fc48f9be
6 changed files with 73 additions and 31 deletions

View File

@ -208,7 +208,7 @@ module.exports = function (client, app) {
contact.messages.add(message); contact.messages.add(message);
if (!contact.lockedResource) { if (!contact.lockedResource) {
contact.lockedResource = msg.from; contact.lockedResource = msg.from.full;
} else if (msg.from !== contact.lockedResource) { } else if (msg.from !== contact.lockedResource) {
contact.lockedResource = undefined; contact.lockedResource = undefined;
} }
@ -261,7 +261,7 @@ module.exports = function (client, app) {
}); });
client.on('disco:caps', function (pres) { client.on('disco:caps', function (pres) {
if (pres.from !== client.jid && pres.caps.hash) { if (pres.caps.hash) {
log.debug('Caps from ' + pres.from + ' ver: ' + pres.caps.ver); log.debug('Caps from ' + pres.from + ' ver: ' + pres.caps.ver);
discoCapsQueue.push(pres); discoCapsQueue.push(pres);
} }

View File

@ -71,6 +71,8 @@ function Client(opts) {
WildEmitter.call(this); WildEmitter.call(this);
this.config = opts || {}; this.config = opts || {};
this.jid = new JID();
this._idPrefix = uuid.v4(); this._idPrefix = uuid.v4();
this._idCount = 0; this._idCount = 0;
@ -169,7 +171,7 @@ function Client(opts) {
}, function (err, resp) { }, function (err, resp) {
self.negotiatedFeatures.bind = true; self.negotiatedFeatures.bind = true;
self.emit('session:bound', resp.bind.jid); self.emit('session:bound', resp.bind.jid);
self.jid = resp.bind.jid; self.jid = new JID(resp.bind.jid);
if (!features._extensions.session) { if (!features._extensions.session) {
self.sessionStarted = true; self.sessionStarted = true;
self.emit('session:started', resp.bind.jid); self.emit('session:started', resp.bind.jid);
@ -372,10 +374,10 @@ Client.prototype.discoverBindings = function (server, cb) {
Client.prototype.getCredentials = function () { Client.prototype.getCredentials = function () {
var creds = this.config.credentials || {}; var creds = this.config.credentials || {};
var requestedJID = this.config.jid; var requestedJID = new JID(this.config.jid);
var username = creds.username || requestedJID.slice(0, requestedJID.indexOf('@')); var username = creds.username || requestedJID.local;
var server = creds.server || requestedJID.slice(requestedJID.indexOf('@') + 1); var server = creds.server || requestedJID.domain;
var defaultCreds = { var defaultCreds = {
username: username, username: username,
@ -463,6 +465,7 @@ Client.prototype.sendPresence = function (data) {
}; };
Client.prototype.sendIq = function (data, cb) { Client.prototype.sendIq = function (data, cb) {
var self = this;
data = data || {}; data = data || {};
cb = cb || function () {}; cb = cb || function () {};
if (!data.id) { if (!data.id) {
@ -481,14 +484,30 @@ Client.prototype.sendIq = function (data, cb) {
var timeoutCheck = this.timeoutMonitor.insure(function () { var timeoutCheck = this.timeoutMonitor.insure(function () {
rescb({type: 'error', error: {condition: 'timeout'}}, null); rescb({type: 'error', error: {condition: 'timeout'}}, null);
}); });
this.once('id:' + data.id, 'session', function (resp) {
var dest = new JID(data.to);
var allowed = {};
allowed[''] = true;
allowed[dest.full] = true;
allowed[dest.bare] = true;
allowed[dest.domain] = true;
allowed[self.jid.bare] = true;
allowed[self.jid.domain] = true;
var handler = function (resp) {
var source = resp.from;
if (!allowed[source.full]) return;
timeoutCheck.check_in(); timeoutCheck.check_in();
self.off('id:' + data.id, handler);
if (resp._extensions.error) { if (resp._extensions.error) {
rescb(resp, null); rescb(resp, null);
} else { } else {
rescb(null, resp); rescb(null, resp);
} }
}); };
this.on('id:' + data.id, 'session', handler);
} }
this.send(new Iq(data)); this.send(new Iq(data));
@ -556,8 +575,15 @@ module.exports = Client;
},{"../vendor/lodash":92,"./jid":3,"./stanza/bind":24,"./stanza/error":31,"./stanza/iq":34,"./stanza/message":36,"./stanza/presence":38,"./stanza/roster":42,"./stanza/sasl":44,"./stanza/session":45,"./stanza/sm":46,"./stanza/stream":47,"./stanza/streamError":48,"./stanza/streamFeatures":49,"./websocket":53,"async":54,"hostmeta":68,"node-uuid":77,"paddle":78,"sasl-anonymous":80,"sasl-digest-md5":82,"sasl-external":84,"sasl-plain":86,"sasl-scram-sha-1":88,"saslmechanisms":90,"wildemitter":91}],3:[function(require,module,exports){ },{"../vendor/lodash":92,"./jid":3,"./stanza/bind":24,"./stanza/error":31,"./stanza/iq":34,"./stanza/message":36,"./stanza/presence":38,"./stanza/roster":42,"./stanza/sasl":44,"./stanza/session":45,"./stanza/sm":46,"./stanza/stream":47,"./stanza/streamError":48,"./stanza/streamFeatures":49,"./websocket":53,"async":54,"hostmeta":68,"node-uuid":77,"paddle":78,"sasl-anonymous":80,"sasl-digest-md5":82,"sasl-external":84,"sasl-plain":86,"sasl-scram-sha-1":88,"saslmechanisms":90,"wildemitter":91}],3:[function(require,module,exports){
function JID(jid) { function JID(jid) {
this.jid = jid; jid = jid || '';
this.parts = {};
if (typeof jid === 'string') {
this.jid = jid;
this.parts = {};
} else {
this.jid = jid.jid;
this.parts = jid.parts;
}
} }
JID.prototype = { JID.prototype = {
@ -583,7 +609,7 @@ JID.prototype = {
} }
return this.parts.bare; return this.parts.bare;
}, },
get resource () { get resource() {
if (this.parts.resource) { if (this.parts.resource) {
return this.parts.resource; return this.parts.resource;
} }

View File

@ -24,6 +24,8 @@ module.exports = HumanModel.define({
type: 'contact', type: 'contact',
props: { props: {
inRoster: ['bool', true, false], inRoster: ['bool', true, false],
owner: ['string', true, ''],
storageId: ['string', true, ''],
jid: ['string', true], jid: ['string', true],
name: ['string', true, ''], name: ['string', true, ''],
subscription: ['string', true, 'none'], subscription: ['string', true, 'none'],
@ -195,6 +197,8 @@ module.exports = HumanModel.define({
if (!this.inRoster) return; if (!this.inRoster) return;
var data = { var data = {
storageId: this.storageId,
owner: this.owner,
jid: this.jid, jid: this.jid,
name: this.name, name: this.name,
groups: this.groups, groups: this.groups,

View File

@ -44,21 +44,6 @@ module.exports = BaseCollection.extend({
} }
}, },
initialize: function (model, options) { initialize: function (model, options) {
var self = this; this.bind('change', this.sort, this);
this.bind('change', this.orderChange, this);
app.storage.roster.getAll(function (err, contacts) {
if (err) return;
contacts.forEach(function (contact) {
contact = new Contact(contact);
contact.inRoster = true;
contact.save();
self.add(contact);
});
});
},
orderChange: function () {
this.sort();
} }
}); });

View File

@ -4,9 +4,13 @@
var HumanModel = require('human-model'); var HumanModel = require('human-model');
var Contacts = require('./contacts'); var Contacts = require('./contacts');
var Contact = require('./contact'); var Contact = require('./contact');
var uuid = require('node-uuid');
module.exports = HumanModel.define({ module.exports = HumanModel.define({
initialize: function () {
this.bind('change:jid', this.loadContacts, this);
},
session: { session: {
jid: ['object', true], jid: ['object', true],
status: ['string', true, ''], status: ['string', true, ''],
@ -30,6 +34,9 @@ module.exports = HumanModel.define({
contact.save(); contact.save();
} else if (create) { } else if (create) {
contact = new Contact(data); contact = new Contact(data);
contact.inRoster = true;
contact.owner = this.jid.bare;
contact.storageId = uuid.v4();
contact.save(); contact.save();
this.contacts.add(contact); this.contacts.add(contact);
} }
@ -38,6 +45,23 @@ module.exports = HumanModel.define({
this.contacts.remove(jid.bare); this.contacts.remove(jid.bare);
app.storage.roster.remove(jid.bare); app.storage.roster.remove(jid.bare);
}, },
loadContacts: function () {
if (!this.jid.bare) return;
var self = this;
app.storage.roster.getAll(this.jid.bare, function (err, contacts) {
if (err) return;
contacts.forEach(function (contact) {
contact = new Contact(contact);
contact.owner = self.jid.bare;
contact.inRoster = true;
contact.save();
self.contacts.add(contact);
});
});
},
isMe: function (jid) { isMe: function (jid) {
return jid.bare === this.jid.bare; return jid.bare === this.jid.bare;
} }

View File

@ -1,3 +1,4 @@
/*global, IDBKeyRange*/
"use strict"; "use strict";
// SCHEMA // SCHEMA
@ -17,9 +18,10 @@ RosterStorage.prototype = {
value: RosterStorage value: RosterStorage
}, },
setup: function (db) { setup: function (db) {
db.createObjectStore('roster', { var store = db.createObjectStore('roster', {
keyPath: 'jid' keyPath: 'storageId'
}); });
store.createIndex("owner", "owner", {unique: false});
}, },
transaction: function (mode) { transaction: function (mode) {
var trans = this.storage.db.transaction('roster', mode); var trans = this.storage.db.transaction('roster', mode);
@ -48,11 +50,12 @@ RosterStorage.prototype = {
}; };
request.onerror = cb; request.onerror = cb;
}, },
getAll: function (cb) { getAll: function (owner, cb) {
cb = cb || function () {}; cb = cb || function () {};
var results = []; var results = [];
var request = this.transaction('readonly').openCursor(); var store = this.transaction('readonly');
var request = store.index('owner').openCursor(IDBKeyRange.only(owner));
request.onsuccess = function (e) { request.onsuccess = function (e) {
var cursor = e.target.result; var cursor = e.target.result;
if (cursor) { if (cursor) {