1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-22 09:12:19 -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);
if (!contact.lockedResource) {
contact.lockedResource = msg.from;
contact.lockedResource = msg.from.full;
} else if (msg.from !== contact.lockedResource) {
contact.lockedResource = undefined;
}
@ -261,7 +261,7 @@ module.exports = function (client, app) {
});
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);
discoCapsQueue.push(pres);
}

View File

@ -71,6 +71,8 @@ function Client(opts) {
WildEmitter.call(this);
this.config = opts || {};
this.jid = new JID();
this._idPrefix = uuid.v4();
this._idCount = 0;
@ -169,7 +171,7 @@ function Client(opts) {
}, function (err, resp) {
self.negotiatedFeatures.bind = true;
self.emit('session:bound', resp.bind.jid);
self.jid = resp.bind.jid;
self.jid = new JID(resp.bind.jid);
if (!features._extensions.session) {
self.sessionStarted = true;
self.emit('session:started', resp.bind.jid);
@ -372,10 +374,10 @@ Client.prototype.discoverBindings = function (server, cb) {
Client.prototype.getCredentials = function () {
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 server = creds.server || requestedJID.slice(requestedJID.indexOf('@') + 1);
var username = creds.username || requestedJID.local;
var server = creds.server || requestedJID.domain;
var defaultCreds = {
username: username,
@ -463,6 +465,7 @@ Client.prototype.sendPresence = function (data) {
};
Client.prototype.sendIq = function (data, cb) {
var self = this;
data = data || {};
cb = cb || function () {};
if (!data.id) {
@ -481,14 +484,30 @@ Client.prototype.sendIq = function (data, cb) {
var timeoutCheck = this.timeoutMonitor.insure(function () {
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();
self.off('id:' + data.id, handler);
if (resp._extensions.error) {
rescb(resp, null);
} else {
rescb(null, resp);
}
});
};
this.on('id:' + data.id, 'session', handler);
}
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){
function JID(jid) {
jid = jid || '';
if (typeof jid === 'string') {
this.jid = jid;
this.parts = {};
} else {
this.jid = jid.jid;
this.parts = jid.parts;
}
}
JID.prototype = {

View File

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

View File

@ -44,21 +44,6 @@ module.exports = BaseCollection.extend({
}
},
initialize: function (model, options) {
var self = 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();
this.bind('change', this.sort, this);
}
});

View File

@ -4,9 +4,13 @@
var HumanModel = require('human-model');
var Contacts = require('./contacts');
var Contact = require('./contact');
var uuid = require('node-uuid');
module.exports = HumanModel.define({
initialize: function () {
this.bind('change:jid', this.loadContacts, this);
},
session: {
jid: ['object', true],
status: ['string', true, ''],
@ -30,6 +34,9 @@ module.exports = HumanModel.define({
contact.save();
} else if (create) {
contact = new Contact(data);
contact.inRoster = true;
contact.owner = this.jid.bare;
contact.storageId = uuid.v4();
contact.save();
this.contacts.add(contact);
}
@ -38,6 +45,23 @@ module.exports = HumanModel.define({
this.contacts.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) {
return jid.bare === this.jid.bare;
}

View File

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