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

View File

@ -1,4 +1,4 @@
/*global me*/
/*global app, me*/
"use strict";
var HumanModel = require('human-model');
@ -10,6 +10,7 @@ module.exports = HumanModel.define({
},
type: 'message',
props: {
owner: 'string',
id: ['string', true, ''],
to: ['object', true],
from: ['object', true],
@ -75,6 +76,22 @@ module.exports = HumanModel.define({
this._created = Date.now();
this.edited = true;
this.save();
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";
function ArchiveStorage(storage) {
@ -9,14 +10,61 @@ ArchiveStorage.prototype = {
value: ArchiveStorage
},
setup: function (db) {
db.createObjectStore('archive', {
keyPath: 'id'
if (db.objectStoreNames.contains('archive')) {
db.deleteObjectStore('archive');
}
var store = db.createObjectStore('archive', {
keyPath: 'archivedId'
});
store.createIndex("owner", "owner", {unique: false});
},
transaction: function (mode) {
var trans = this.storage.db.transaction('archive', mode);
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
},
setup: function (db) {
if (db.objectStoreNames.contains('avatars')) {
db.deleteObjectStore('avatars');
}
db.createObjectStore('avatars', {
keyPath: 'id'
});

View File

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

View File

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

View File

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

View File

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