1
0
mirror of https://github.com/moparisthebest/kaiwa synced 2024-11-10 19:45:02 -05:00
kaiwa/clientapp/models/me.js
2013-09-05 16:53:23 -07:00

64 lines
1.6 KiB
JavaScript

/*global app*/
"use strict";
var HumanModel = require('human-model');
var Contacts = require('./contacts');
var Contact = require('./contact');
module.exports = HumanModel.define({
session: {
jid: ['string', true, ''],
status: ['string', true, ''],
avatar: ['string', true, '']
},
derived: {
barejid: {
deps: ['jid'],
fn: function () {
var hasResource = this.jid.indexOf('/') > 0;
if (hasResource) {
return this.jid.slice(0, this.jid.indexOf('/'));
}
return this.jid;
}
}
},
collections: {
contacts: Contacts
},
getContact: function (jid, alt) {
if (this.isMe(jid)) {
jid = alt || jid;
}
var hasResource = jid.indexOf('/') > 0;
if (hasResource) {
jid = jid.slice(0, jid.indexOf('/'));
}
return this.contacts.get(jid);
},
setContact: function (data, create) {
var contact = this.getContact(data.jid);
if (contact) {
contact.set(data);
contact.save();
} else if (create) {
contact = new Contact(data);
contact.save();
this.contacts.add(contact);
}
},
removeContact: function (jid) {
this.contacts.remove(jid);
app.storage.roster.remove(jid);
},
isMe: function (jid) {
var hasResource = jid.indexOf('/') > 0;
if (hasResource) {
jid = jid.slice(0, jid.indexOf('/'));
}
return jid === this.barejid;
}
});