app/assets/javascripts/backbone/models/chat.js.coffee
6a6ccd01
 class Xmpp.Models.Chat extends Xmpp.Models.Model
   namespace: 'app.chat'
 
   defaults:
     who: null
     withWhom: null
2a61cdcc
     chatId: null
94bd8368
     isMultiChat: false
6a6ccd01
 
   initialize: ->
     _.bindAll(this)
 
94bd8368
     if (@get('isMultiChat'))
       @set('withWhom', [])
 
   appendWithWhom: (newPerson) ->
     allWithWhom = @get('withWhom');
 
     exists = _.find(allWithWhom, (withWhom) ->
       withWhom == newPerson
     )?
 
     if not exists
       allWithWhom.push(newPerson)
 
2a61cdcc
   setChatId: (id) ->
94bd8368
     App.debug ['multichat has id', id]
2a61cdcc
     @set('chatId', id)
 
dc7e418a
   isMeOwner: ->
     @get('who') == App.Models.me
 
   isAttending: (contact) ->
     attendants = @get('withWhom')
     if _.isArray(attendants)
       return _.find(attendants, (somebody) -> somebody == contact)
     else
       return contact == attendants
 
c6c526f1
   syncContacts: (contacts, owner) ->
     contactsWithoutMe = _.filter(contacts, (jid) ->
       jid != App.Models.me.get('jid')
     )
 
a0c73ec7
     if owner and owner != App.Models.me.get('jid')
       contactsWithoutMe = contactsWithoutMe.concat([owner])
eb420b38
 
c6c526f1
     attendants = _.map(contactsWithoutMe, (jid) =>
       contact = App.Collections.contacts.findByJid(jid)
       if not contact
e68b2617
         contact = new Xmpp.Models.Contact(
           id: App.stripJid(jid)
           jid: App.stripJid(jid)
           belongsTo: [App.Models.me.get('jid')]
           usingMyApp: true
         )
         App.Collections.contacts.add(contact)
c6c526f1
 
       contact
     )
 
     @set('withWhom', attendants);
 
     if owner
       ownerContact = App.Collections.contacts.findByJid(owner)
       @set('who', ownerContact)
 
b1c6b359
     App.debug ['importing contacts to chat', this, this.get('chatId')]
c6c526f1
 
 
6a6ccd01
 class Xmpp.Collections.ChatsCollection extends Backbone.Collection
   model: Xmpp.Models.Chat
dc7e418a
   activeChat: null
6a6ccd01
 
   initialize: ->
     _.bindAll(this)
 
aa2c1028
     Backbone.Events.on('closeChat', (chat, ignoreCheckIfMultichat) =>
bcce524b
       @removeChat(chat)
aa2c1028
       if not ignoreCheckIfMultichat
         if (chat.get('isMultiChat'))
           App.Com.iClosedMultichat(chat.get('chatId'), App.Models.me.get('jid'))
bcce524b
     )
6a6ccd01
 
dc7e418a
     Backbone.Events.on('openChat', (chat) =>
       @activeChat = chat
     )
 
6a6ccd01
   find: (who, withWhom) ->
fcce5d56
     _.find(@models, (chat) ->
0da0afa5
       chat.get('who') == who and chat.get('withWhom') == withWhom and not chat.get('isMultiChat')
6a6ccd01
     )
 
1f685c13
   findById: (id) ->
b1c6b359
     _.find(@models, (chat) ->
       chat.get('chatId') == id
     )
1f685c13
 
6a6ccd01
   removeChat: (chat) ->
aa2c1028
     @activeChat = null
c6c526f1
     @models = _.without(@openedChats, chat)
 
   createTempContact: (jid) ->
     newTempContact = new Xmpp.Models.Contact(jid: jid, belongsTo: [App.Models.me])
     newTempContact