2013-09-11 23:59:50 -04:00
|
|
|
/*global app, client*/
|
2013-08-29 23:38:28 -04:00
|
|
|
"use strict";
|
|
|
|
|
2013-09-03 22:18:31 -04:00
|
|
|
var HumanModel = require('human-model');
|
2013-08-20 13:45:06 -04:00
|
|
|
var Contacts = require('./contacts');
|
2013-08-29 23:38:28 -04:00
|
|
|
var Contact = require('./contact');
|
2013-09-16 19:12:00 -04:00
|
|
|
var MUCs = require('./mucs');
|
|
|
|
var MUC = require('./muc');
|
2013-09-11 17:58:39 -04:00
|
|
|
var uuid = require('node-uuid');
|
2013-06-03 18:51:30 -04:00
|
|
|
|
|
|
|
|
2013-09-05 19:53:23 -04:00
|
|
|
module.exports = HumanModel.define({
|
2013-09-11 17:58:39 -04:00
|
|
|
initialize: function () {
|
|
|
|
this.bind('change:jid', this.loadContacts, this);
|
2013-09-14 05:12:57 -04:00
|
|
|
this.bind('change:hasFocus', function () {
|
|
|
|
this.setActiveContact(this._activeContact);
|
|
|
|
}, this);
|
2013-09-11 17:58:39 -04:00
|
|
|
},
|
2013-06-03 18:51:30 -04:00
|
|
|
session: {
|
2013-09-10 03:57:01 -04:00
|
|
|
jid: ['object', true],
|
2013-08-20 13:45:06 -04:00
|
|
|
status: ['string', true, ''],
|
2013-09-11 23:59:50 -04:00
|
|
|
avatar: ['string', true, ''],
|
|
|
|
connected: ['bool', true, false],
|
2013-09-12 14:18:44 -04:00
|
|
|
shouldAskForAlertsPermission: ['bool', true, false],
|
2013-09-14 05:12:57 -04:00
|
|
|
hasFocus: ['bool', true, false],
|
2013-09-11 23:59:50 -04:00
|
|
|
_activeContact: ['string', true, '']
|
2013-06-03 18:51:30 -04:00
|
|
|
},
|
2013-08-20 13:45:06 -04:00
|
|
|
collections: {
|
2013-09-16 19:12:00 -04:00
|
|
|
contacts: Contacts,
|
|
|
|
mucs: MUCs
|
2013-08-20 13:45:06 -04:00
|
|
|
},
|
2013-09-11 23:59:50 -04:00
|
|
|
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;
|
|
|
|
},
|
2013-08-20 13:45:06 -04:00
|
|
|
getContact: function (jid, alt) {
|
2013-09-11 23:59:50 -04:00
|
|
|
if (typeof jid === 'string') jid = new client.JID(jid);
|
|
|
|
if (typeof alt === 'string') alt = new client.JID(alt);
|
|
|
|
|
2013-08-20 13:45:06 -04:00
|
|
|
if (this.isMe(jid)) {
|
|
|
|
jid = alt || jid;
|
|
|
|
}
|
2013-09-16 19:12:00 -04:00
|
|
|
return this.contacts.get(jid.bare) || this.mucs.get(jid.bare) || undefined;
|
2013-08-20 13:45:06 -04:00
|
|
|
},
|
2013-08-29 23:38:28 -04:00
|
|
|
setContact: function (data, create) {
|
|
|
|
var contact = this.getContact(data.jid);
|
2013-09-10 03:57:01 -04:00
|
|
|
data.jid = data.jid.bare;
|
|
|
|
|
2013-08-29 23:38:28 -04:00
|
|
|
if (contact) {
|
|
|
|
contact.set(data);
|
|
|
|
contact.save();
|
|
|
|
} else if (create) {
|
|
|
|
contact = new Contact(data);
|
2013-09-11 17:58:39 -04:00
|
|
|
contact.inRoster = true;
|
|
|
|
contact.owner = this.jid.bare;
|
|
|
|
contact.storageId = uuid.v4();
|
2013-08-29 23:38:28 -04:00
|
|
|
contact.save();
|
|
|
|
this.contacts.add(contact);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
removeContact: function (jid) {
|
2013-09-10 03:57:01 -04:00
|
|
|
this.contacts.remove(jid.bare);
|
|
|
|
app.storage.roster.remove(jid.bare);
|
2013-08-29 23:38:28 -04:00
|
|
|
},
|
2013-09-11 17:58:39 -04:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
2013-08-20 13:45:06 -04:00
|
|
|
isMe: function (jid) {
|
2013-09-10 03:57:01 -04:00
|
|
|
return jid.bare === this.jid.bare;
|
2013-06-03 18:51:30 -04:00
|
|
|
}
|
|
|
|
});
|