app/assets/javascripts/backbone/models/contact.js.coffee
ff94bdbf
 class Xmpp.Models.Contact extends Xmpp.Models.Model
   namespace: 'app.roster'
 
   defaults:
52ef7688
     jid: ''
a7decc88
     preferredJid: null
52ef7688
     name: ''
     status: 'offline'
     message: ''
1f685c13
     avatar: 'assets/avatar.png'
635339a3
     belongsTo: []
7a817e66
     usingMyApp: false
ff94bdbf
 
   initialize: ->
b27f285a
     _.bindAll(this)
ff94bdbf
 
52ef7688
     if ! @get('name')
       @set(name: @get('jid'))
 
c6c526f1
     @set('id', @get('jid'))
 
6a6ccd01
   firstname: ->
     @get('name').split(' ')[0]
52ef7688
 
7a817e66
   setUsingMyApp: ->
     @set('usingMyApp', true)
 
a7decc88
   setPreferredResource: (jid) ->
     @set('preferredJid', jid)
 
   getPreferredResource: ->
     @get('preferredJid') || @get('jid')
 
ff94bdbf
 class Xmpp.Collections.ContactsCollection extends Backbone.Collection
   model: Xmpp.Models.Contact
b27f285a
 
   initialize: ->
     _.bindAll(this)
e2e90aca
 
b27f285a
     @friendsList = new Xmpp.Views.Contacts.ListView(collection: this, attributes: {title: 'chat.roster.friends', id: 'js-inactive-friends'})
e2e90aca
     @friendsList.createListContainer()
 
     @activeList  = new Xmpp.Views.Contacts.ListView(collection: this, attributes: {title: 'chat.roster.chat-group', id: 'js-active-friends'})
     @activeList.setAsActiveChatGroup()
b27f285a
 
bcce524b
     @on('add', @appendContact)
635339a3
     @on('change:belongsTo', @mergeContacts)
bcce524b
 
     Backbone.Events.on('openChat', (chat) =>
       @moveToInactiveList('all')
b1c6b359
 #      App.debug chat
bcce524b
       @moveToActiveList(chat.get('withWhom'))
3a34499e
       Backbone.Events.trigger('resizeWorkspace')
bcce524b
     )
 
b27f285a
   appendContact: (contact) ->
635339a3
     App.debug ['apppend', contact]
52ef7688
     @friendsList.appendContact(contact)
3a34499e
     Backbone.Events.trigger('resizeWorkspace')
52ef7688
 
635339a3
   mergeContacts: (contact) ->
     App.debug ['merging contacts', contact]
     #TODO: ak ten isty clovek je vo viacerych rosteroch, treba nastavit spravne belongsTo
 
     existingContact = @get(contact.get('id'))
     if existingContact?
       alreadyBelongsTo = existingContact.get('belongsTo')
       willBelongTo = contact.get('belongsTo')
       existingContact.set(belongsTo: _.union(alreadyBelongsTo, willBelongTo))
     else
       @add(contact)
 
eb420b38
   moveToActiveList: (contacts) ->
     App.debug ['move to active list', contacts];
     contacts = [contacts] unless _.isArray(contacts)
 
     _.each(contacts, (contact) =>
       @_switchContactBelongingList(@get(contact), @friendsList, @activeList) && @activeList.reOrder()
     )
e2e90aca
 
   moveToInactiveList: (contact) ->
     if contact == 'all'
       _.each(@activeList.contactViews, (view) =>
         @_switchContactBelongingList(view.model, @activeList, @friendsList)
       )
624379a8
       @friendsList.reOrder()
e2e90aca
     else
624379a8
       @_switchContactBelongingList(@get(contact), @activeList, @friendsList) && @friendsList.reOrder()
e2e90aca
 
52ef7688
   updateStatus: (response) ->
a7decc88
     contact = @get(response.jid)
     return unless contact
 
     contact.set(message: response.status.message, status: response.status.status)
     if response.status.status == 'offline'
       contact.setPreferredResource(null)
52ef7688
 
   udpateVcard: (response) ->
e2e90aca
     @get(response.jid).set(name: response.vcard.name, avatar: response.vcard.avatar)
 
635339a3
   subscriptionChanged: (newSubscription) ->
     if (newSubscription.action == 'subscribed')
       App.debug ['subscribed', newSubscription]
 
       newContact = new Xmpp.Models.Contact(
         id: newSubscription.jid
         jid: newSubscription.jid
         belongsTo: [newSubscription.belongsTo]
       )
       @mergeContacts(newContact)
     else if (newSubscription.action == 'unsubscribed')
       # TODO: show info and ask if user want to remove him from roster
       App.debug ['unsubscribed', newSubscription]
 
       contact = @get(newSubscription.jid)
       App.Com.removeContactRemote(contact.get('jid'), newSubscription.belongsTo)
 
       nowBelongsTo = _.without(contact.get('belongsTo'), newSubscription.belongsTo)
       if (nowBelongsTo.length == 0)
         Backbone.Events.trigger('removeContact', contact)
         @remove(contact)
 
     # ignore 'unsubscribe' type of action
3a34499e
     Backbone.Events.trigger('resizeWorkspace')
635339a3
 
1f685c13
   # toto sem asi nepatri, ale asi do views. Nic to v skutocnosti nerobi s modelmi.
43899b3a
   filter: (searchTerm) ->
     @friendsList.filter(searchTerm)
     @activeList.filter(searchTerm)
3a34499e
     Backbone.Events.trigger('resizeWorkspace')
43899b3a
 
1f685c13
   findByJid: (jid) ->
     @get(jid)
 
e2e90aca
   _switchContactBelongingList: (contact, fromList, toList) ->
     if !contact?
       false
 
     fromList.detachContact(contact) && toList.appendContact(contact)
 
     true