1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-12 04:25:05 -05:00

Save user profile (avatar id/status/etc)

This commit is contained in:
Lance Stout 2013-10-15 14:48:39 -07:00
parent 71e342a0a2
commit d611b9fdda
5 changed files with 121 additions and 25 deletions

View File

@ -32,6 +32,7 @@ module.exports = {
_.extend(this, Backbone.Events); _.extend(this, Backbone.Events);
var profile = {};
async.series([ async.series([
function (cb) { function (cb) {
app.notifications = new Notify(); app.notifications = new Notify();
@ -40,16 +41,17 @@ module.exports = {
app.storage.open(cb); app.storage.open(cb);
}, },
function (cb) { function (cb) {
app.storage.rosterver.get(config.jid, function (err, ver) { app.storage.profiles.get(config.jid, function (err, res) {
if (ver) { if (res) {
config.rosterVer = ver; profile = res;
config.rosterVer = res.rosterVer;
} }
cb(); cb();
}); });
}, },
function (cb) { function (cb) {
app.state = new AppState(); app.state = new AppState();
app.me = window.me = new MeModel(); app.me = window.me = new MeModel(profile);
window.onbeforeunload = function () { window.onbeforeunload = function () {
if (app.api.sessionStarted) { if (app.api.sessionStarted) {

View File

@ -107,7 +107,7 @@ module.exports = function (client, app) {
client.getRoster(function (err, resp) { client.getRoster(function (err, resp) {
resp = resp.toJSON(); resp = resp.toJSON();
app.storage.rosterver.set(me.jid.bare, resp.roster.ver); me.rosterVer = resp.roster.ver;
_.each(resp.roster.items, function (item) { _.each(resp.roster.items, function (item) {
me.setContact(item, true); me.setContact(item, true);
@ -116,6 +116,7 @@ module.exports = function (client, app) {
var caps = client.updateCaps(); var caps = client.updateCaps();
app.storage.disco.add(caps.ver, caps.discoInfo, function () { app.storage.disco.add(caps.ver, caps.discoInfo, function () {
client.sendPresence({ client.sendPresence({
status: me.status,
caps: client.disco.caps caps: client.disco.caps
}); });
client.enableCarbons(); client.enableCarbons();
@ -129,7 +130,7 @@ module.exports = function (client, app) {
iq = iq.toJSON(); iq = iq.toJSON();
var items = iq.roster.items; var items = iq.roster.items;
app.storage.rosterver.set(me.jid.bare, iq.roster.ver); me.rosterVer = iq.roster.ver;
_.each(items, function (item) { _.each(items, function (item) {
var contact = me.getContact(item.jid); var contact = me.getContact(item.jid);

View File

@ -11,20 +11,30 @@ var fetchAvatar = require('../helpers/fetchAvatar');
module.exports = HumanModel.define({ module.exports = HumanModel.define({
initialize: function () { initialize: function (opts) {
this.bind('change:jid', this.loadContacts, this); if (opts.avatarID) {
this.setAvatar(opts.avatarID);
}
this.bind('change:jid', this.load, this);
this.bind('change:hasFocus', function () { this.bind('change:hasFocus', function () {
this.setActiveContact(this._activeContact); this.setActiveContact(this._activeContact);
}, this); }, this);
this.bind('change:avatarID', this.save, this);
this.bind('change:status', this.save, this);
this.bind('change:rosterVer', this.save, this);
this.contacts.bind('change:unreadCount', this.updateUnreadCount, this); this.contacts.bind('change:unreadCount', this.updateUnreadCount, this);
app.state.bind('change:active', this.updateIdlePresence, this); app.state.bind('change:active', this.updateIdlePresence, this);
}, },
session: { props: {
jid: ['object', true], jid: ['object', true],
status: ['string', true, ''], status: ['string', true, ''],
avatar: ['string', true, ''],
avatarID: ['string', true, ''], avatarID: ['string', true, ''],
rosterVer: ['string', true, '']
},
session: {
avatar: ['string', true, ''],
connected: ['bool', true, false], connected: ['bool', true, false],
shouldAskForAlertsPermission: ['bool', true, false], shouldAskForAlertsPermission: ['bool', true, false],
hasFocus: ['bool', true, false], hasFocus: ['bool', true, false],
@ -83,22 +93,30 @@ 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 () { load: function () {
if (!this.jid.bare) return; if (!this.jid.bare) return;
var self = this; var self = this;
app.storage.roster.getAll(this.jid.bare, function (err, contacts) {
if (err) return;
contacts.forEach(function (contact) { app.storage.profiles.get(this.jid.bare, function (err, profile) {
contact = new Contact(contact); if (!err) {
contact.owner = self.jid.bare; self.status = profile.status;
contact.inRoster = true; self.avatarID = profile.avatarID;
contact.save(); }
self.contacts.add(contact); self.save();
app.storage.roster.getAll(self.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);
});
self.contacts.trigger('loaded');
}); });
self.contacts.trigger('loaded');
}); });
}, },
isMe: function (jid) { isMe: function (jid) {
@ -124,5 +142,14 @@ module.exports = HumanModel.define({
count = ''; count = '';
} }
app.state.badge = '' + count; app.state.badge = '' + count;
},
save: function () {
var data = {
jid: this.jid.bare,
avatarID: this.avatarID,
status: this.status,
rosterVer: this.rosterVer
};
app.storage.profiles.set(data);
} }
}); });

View File

@ -5,7 +5,7 @@ var AvatarStorage = require('./avatars');
var RosterStorage = require('./roster'); var RosterStorage = require('./roster');
var DiscoStorage = require('./disco'); var DiscoStorage = require('./disco');
var ArchiveStorage = require('./archive'); var ArchiveStorage = require('./archive');
var RosterVerStorage = require('./rosterver'); var ProfileStorage = require('./profile');
function Storage() { function Storage() {
@ -16,13 +16,13 @@ function Storage() {
this.roster = new RosterStorage(this); this.roster = new RosterStorage(this);
this.disco = new DiscoStorage(this); this.disco = new DiscoStorage(this);
this.archive = new ArchiveStorage(this); this.archive = new ArchiveStorage(this);
this.rosterver = new RosterVerStorage(this); this.profiles = new ProfileStorage(this);
} }
Storage.prototype = { Storage.prototype = {
constructor: { constructor: {
value: Storage value: Storage
}, },
version: 2, version: 3,
open: function (cb) { open: function (cb) {
cb = cb || function () {}; cb = cb || function () {};
@ -38,7 +38,7 @@ Storage.prototype = {
self.roster.setup(db); self.roster.setup(db);
self.disco.setup(db); self.disco.setup(db);
self.archive.setup(db); self.archive.setup(db);
self.rosterver.setup(db); self.profiles.setup(db);
}; };
request.onerror = cb; request.onerror = cb;
} }

View File

@ -0,0 +1,66 @@
/*global, IDBKeyRange*/
"use strict";
// SCHEMA
// jid: string
// name: string
// avatarID: string
// status: string
// rosterVer: string
function ProfileStorage(storage) {
this.storage = storage;
}
ProfileStorage.prototype = {
constructor: {
value: ProfileStorage
},
setup: function (db) {
if (db.objectStoreNames.contains('profiles')) {
db.deleteObjectStore('profiles');
}
var store = db.createObjectStore('profiles', {
keyPath: 'jid'
});
},
transaction: function (mode) {
var trans = this.storage.db.transaction('profiles', mode);
return trans.objectStore('profiles');
},
set: function (profile, cb) {
cb = cb || function () {};
var request = this.transaction('readwrite').put(profile);
request.onsuccess = function () {
cb(false, profile);
};
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');
}
cb(false, request.result);
};
request.onerror = cb;
},
remove: function (id, cb) {
cb = cb || function () {};
var request = this.transaction('readwrite')['delete'](id);
request.onsuccess = function (e) {
cb(false, request.result);
};
request.onerror = cb;
}
};
module.exports = ProfileStorage;