diff --git a/clientapp/models/contact.js b/clientapp/models/contact.js index 152ba86..e457841 100644 --- a/clientapp/models/contact.js +++ b/clientapp/models/contact.js @@ -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; diff --git a/clientapp/models/message.js b/clientapp/models/message.js index 079422e..560edcc 100644 --- a/clientapp/models/message.js +++ b/clientapp/models/message.js @@ -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); } }); diff --git a/clientapp/storage/archive.js b/clientapp/storage/archive.js index 90b8f09..b773d09 100644 --- a/clientapp/storage/archive.js +++ b/clientapp/storage/archive.js @@ -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; + }, + }; diff --git a/clientapp/storage/avatars.js b/clientapp/storage/avatars.js index cd2dda9..51869ef 100644 --- a/clientapp/storage/avatars.js +++ b/clientapp/storage/avatars.js @@ -14,6 +14,9 @@ AvatarStorage.prototype = { value: AvatarStorage }, setup: function (db) { + if (db.objectStoreNames.contains('avatars')) { + db.deleteObjectStore('avatars'); + } db.createObjectStore('avatars', { keyPath: 'id' }); diff --git a/clientapp/storage/disco.js b/clientapp/storage/disco.js index c7dee51..36895d1 100644 --- a/clientapp/storage/disco.js +++ b/clientapp/storage/disco.js @@ -9,6 +9,9 @@ DiscoStorage.prototype = { value: DiscoStorage }, setup: function (db) { + if (db.objectStoreNames.contains('disco')) { + db.deleteObjectStore('disco'); + } db.createObjectStore('disco', { keyPath: 'ver' }); diff --git a/clientapp/storage/index.js b/clientapp/storage/index.js index 46d905c..de83d3d 100644 --- a/clientapp/storage/index.js +++ b/clientapp/storage/index.js @@ -22,7 +22,7 @@ Storage.prototype = { constructor: { value: Storage }, - version: 1, + version: 2, open: function (cb) { cb = cb || function () {}; diff --git a/clientapp/storage/roster.js b/clientapp/storage/roster.js index 001ded6..e82ae5c 100644 --- a/clientapp/storage/roster.js +++ b/clientapp/storage/roster.js @@ -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' }); diff --git a/clientapp/storage/rosterver.js b/clientapp/storage/rosterver.js index 0d603ad..d60d9f0 100644 --- a/clientapp/storage/rosterver.js +++ b/clientapp/storage/rosterver.js @@ -14,6 +14,9 @@ RosterVerStorage.prototype = { value: RosterVerStorage }, setup: function (db) { + if (db.objectStoreNames.contains('rosterver')) { + db.deleteObjectStore('rosterver'); + } db.createObjectStore('rosterver', { keyPath: 'jid' });