1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-08-13 17:03:51 -04:00
kaiwa/clientapp/models/me.js
2013-09-18 16:24:40 -07:00

133 lines
4.2 KiB
JavaScript

/*global app, client, XMPP*/
"use strict";
var HumanModel = require('human-model');
var Contacts = require('./contacts');
var Contact = require('./contact');
var MUCs = require('./mucs');
var MUC = require('./muc');
var uuid = require('node-uuid');
module.exports = HumanModel.define({
initialize: function () {
this.bind('change:jid', this.loadContacts, this);
this.bind('change:hasFocus', function () {
this.setActiveContact(this._activeContact);
}, this);
},
session: {
jid: ['object', true],
status: ['string', true, ''],
avatar: ['string', true, ''],
avatarID: ['string', true, ''],
connected: ['bool', true, false],
shouldAskForAlertsPermission: ['bool', true, false],
hasFocus: ['bool', true, false],
_activeContact: ['string', true, '']
},
collections: {
contacts: Contacts,
mucs: MUCs
},
setActiveContact: function (jid) {
var prev = this.getContact(this._activeContact);
if (prev) {
prev.activeContact = false;
}
var curr = this.getContact(jid);
if (curr) {
curr.activeContact = true;
curr.unreadCount = 0;
}
this._activeContact = jid;
},
setAvatar: function (id, type) {
var self = this;
if (!id) {
var gID = XMPP.crypto.createHash('md5').update(this.jid).digest('hex');
self.avatar = 'https://gravatar.com/avatar/' + gID + '?s=30&d=mm';
return;
}
app.storage.avatars.get(id, function (err, avatar) {
if (err) {
if (!type) {
// We can't find the ID, and we don't know the type, so fallback.
var gID = XMPP.crypto.createHash('md5').update(self.jid.bare).digest('hex');
self.avatar = 'https://gravatar.com/avatar/' + gID + '?s=30&d=mm';
return;
}
app.whenConnected(function () {
client.getAvatar(self.jid.bare, id, function (err, resp) {
if (err) return;
resp = resp.toJSON();
var avatarData = resp.pubsub.retrieve.item.avatarData;
var dataURI = 'data:' + type + ';base64,' + avatarData;
app.storage.avatars.add({id: id, uri: dataURI});
self.set({
avatar: dataURI,
avatarID: id
});
});
});
} else {
self.set({
avatar: avatar.uri,
avatarID: avatar.id
});
}
});
},
getContact: function (jid, alt) {
if (typeof jid === 'string') jid = new client.JID(jid);
if (typeof alt === 'string') alt = new client.JID(alt);
if (this.isMe(jid)) {
jid = alt || jid;
}
return this.contacts.get(jid.bare) || this.mucs.get(jid.bare) || undefined;
},
setContact: function (data, create) {
var contact = this.getContact(data.jid);
data.jid = data.jid.bare;
if (contact) {
contact.set(data);
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);
}
},
removeContact: function (jid) {
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;
}
});