Handle db upgrades, start message archive caching.

This commit is contained in:
Lance Stout 2013-09-13 14:36:15 -07:00
parent 9449a00dda
commit 7d69314923
8 changed files with 86 additions and 5 deletions

View File

@ -157,6 +157,8 @@ module.exports = HumanModel.define({
this.lockedResource = undefined; this.lockedResource = undefined;
}, },
addMessage: function (message, notify) { addMessage: function (message, notify) {
message.owner = me.jid.bare;
if (notify && !this.activeContact && message.from.bare === this.jid) { if (notify && !this.activeContact && message.from.bare === this.jid) {
this.unreadCount++; this.unreadCount++;
app.notifier.show({ app.notifier.show({
@ -169,6 +171,8 @@ module.exports = HumanModel.define({
this.messages.add(message); this.messages.add(message);
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) {
this.lastInteraction = newInteraction; this.lastInteraction = newInteraction;

View File

@ -1,4 +1,4 @@
/*global me*/ /*global app, me*/
"use strict"; "use strict";
var HumanModel = require('human-model'); var HumanModel = require('human-model');
@ -10,6 +10,7 @@ module.exports = HumanModel.define({
}, },
type: 'message', type: 'message',
props: { props: {
owner: 'string',
id: ['string', true, ''], id: ['string', true, ''],
to: ['object', true], to: ['object', true],
from: ['object', true], from: ['object', true],
@ -75,6 +76,22 @@ module.exports = HumanModel.define({
this._created = Date.now(); this._created = Date.now();
this.edited = true; this.edited = true;
this.save();
return true; return true;
},
save: function () {
var data = {
archivedId: this.archivedId,
owner: this.owner,
to: this.to,
from: this.from,
created: this.created,
body: this.body,
type: this.type,
delay: this.delay,
edited: this.edited
};
app.storage.archive.add(data);
} }
}); });

View File

@ -1,3 +1,4 @@
/*global, IDBKeyRange*/
"use strict"; "use strict";
function ArchiveStorage(storage) { function ArchiveStorage(storage) {
@ -9,14 +10,61 @@ ArchiveStorage.prototype = {
value: ArchiveStorage value: ArchiveStorage
}, },
setup: function (db) { setup: function (db) {
db.createObjectStore('archive', { if (db.objectStoreNames.contains('archive')) {
keyPath: 'id' db.deleteObjectStore('archive');
}
var store = db.createObjectStore('archive', {
keyPath: 'archivedId'
}); });
store.createIndex("owner", "owner", {unique: false});
}, },
transaction: function (mode) { transaction: function (mode) {
var trans = this.storage.db.transaction('archive', mode); var trans = this.storage.db.transaction('archive', mode);
return trans.objectStore('archive'); return trans.objectStore('archive');
} },
add: function (message, cb) {
cb = cb || function () {};
var request = this.transaction('readwrite').put(message);
request.onsuccess = function () {
cb(false, message);
};
request.onerror = cb;
},
get: function (id, cb) {
cb = cb || function () {};
if (!id) {
return cb('not-found');
}
var request = this.transaction('readonly').get(id);
request.onsuccess = function (e) {
var res = request.result;
if (res === undefined) {
return cb('not-found');
}
request.result.acked = true;
cb(false, request.result);
};
request.onerror = cb;
},
getAll: function (owner, cb) {
cb = cb || function () {};
var results = [];
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) {
cursor.value.acked = true;
results.push(cursor.value);
cursor.continue();
} else {
cb(false, results);
}
};
request.onerror = cb;
},
}; };

View File

@ -14,6 +14,9 @@ AvatarStorage.prototype = {
value: AvatarStorage value: AvatarStorage
}, },
setup: function (db) { setup: function (db) {
if (db.objectStoreNames.contains('avatars')) {
db.deleteObjectStore('avatars');
}
db.createObjectStore('avatars', { db.createObjectStore('avatars', {
keyPath: 'id' keyPath: 'id'
}); });

View File

@ -9,6 +9,9 @@ DiscoStorage.prototype = {
value: DiscoStorage value: DiscoStorage
}, },
setup: function (db) { setup: function (db) {
if (db.objectStoreNames.contains('disco')) {
db.deleteObjectStore('disco');
}
db.createObjectStore('disco', { db.createObjectStore('disco', {
keyPath: 'ver' keyPath: 'ver'
}); });

View File

@ -22,7 +22,7 @@ Storage.prototype = {
constructor: { constructor: {
value: Storage value: Storage
}, },
version: 1, version: 2,
open: function (cb) { open: function (cb) {
cb = cb || function () {}; cb = cb || function () {};

View File

@ -18,6 +18,9 @@ RosterStorage.prototype = {
value: RosterStorage value: RosterStorage
}, },
setup: function (db) { setup: function (db) {
if (db.objectStoreNames.contains('roster')) {
db.deleteObjectStore('roster');
}
var store = db.createObjectStore('roster', { var store = db.createObjectStore('roster', {
keyPath: 'storageId' keyPath: 'storageId'
}); });

View File

@ -14,6 +14,9 @@ RosterVerStorage.prototype = {
value: RosterVerStorage value: RosterVerStorage
}, },
setup: function (db) { setup: function (db) {
if (db.objectStoreNames.contains('rosterver')) {
db.deleteObjectStore('rosterver');
}
db.createObjectStore('rosterver', { db.createObjectStore('rosterver', {
keyPath: 'jid' keyPath: 'jid'
}); });