112:f4a4878c99a3
Anton Shestakov <av6@dwimlabs.net>, Thu, 07 Apr 2016 22:56:16 +0800
index: check if roster has the item before removing it Sometimes we get events from contacts that are not in user's roster, trying to remove such contacts used to fail before this patch.

next change 191:d2453b933c21
previous change 104:f5614e27bbf1

coffee/messages.coffee

Permissions: -rw-r--r--

Other formats: Feeds:
class Tram.Message extends Backbone.Model
initialize: ->
@on 'add change:stamp', ->
if @has('stamp')
@set('d/mstamp', moment(@get('stamp')))
else
@unset('d/mstamp')
class Tram.Messages extends Backbone.Collection
model: Tram.Message
splitThreshold: 30 * 60 * 1000
foldThreshold: 60 * 1000
comparator: (model) ->
model.get('stamp').valueOf()
initialize: ->
@on('add', @onAdd)
onAdd: (model) ->
mi = @indexOf(model)
prev = @models[mi - 1]
@_splitOrFold(prev, model)
next = @models[mi + 1]
@_splitOrFold(model, next)
_splitOrFold: (m1, m2) ->
if not m2?
return
if not m1?
m2.unset('d/split')
m2.unset('d/fold')
return
if @_splittable(m1, m2)
m2.set('d/split', true)
else
m2.unset('d/split')
if @_foldable(m1, m2)
m2.set('d/fold', true)
else
m2.unset('d/fold')
_splittable: (m1, m2) ->
Math.abs(m1.get('stamp').valueOf() - m2.get('stamp').valueOf()) > @splitThreshold
_foldable: (m1, m2) ->
# check stamp, type, etc
m1.get('from') is m2.get('from') and Math.abs(m1.get('stamp').valueOf() - m2.get('stamp').valueOf()) < @foldThreshold
class Tram.MessageView extends Backbone.View
tagName: 'div'
className: 'message'
template: $('#message-template').html()
initialize: ->
@$el.attr('data-id', @model.get('id'))
@$el.html(@template)
@$avatarColumn = @$('.avatar-column')
@bind()
bind: ->
@listenTo(@model, 'change:contact', @updateContact)
updateContact: ->
if not @model.previous('contact')? and @model.get('contact')?
av = new Tram.AvatarView(model: contact)
@$avatarColumn.prepend(av.render().el)
getHandle: ->
@model.get('contact')?.get('d/handle') or @model.get('from')
render: (model) ->
@rivet = rivets.bind(@el, model: @model, view: this)
contact = @model.get('contact')
if contact
av = new Tram.AvatarView(model: contact)
@$avatarColumn.prepend(av.render().el)
@
remove: ->
@rivet.unbind()
super
class Tram.LogApp extends Backbone.View
tagName: 'div'
className: 'log'
initialize: ->
@listenTo(@collection, 'add', @onAdd)
onAdd: (model, collection) ->
mi = collection.indexOf(model)
view = new Tram.MessageView(model: model)
el = view.render().el
if mi is 0
@$el.prepend(el)
else
@$el.children().eq(mi - 1).after(el)
if @bottomed
@scroll()
bottomed: ->
@$el.scrollTop() + @$el.height() == @$el.get(0).scrollHeight
scroll: ->
@$el.scrollTop(@$el.get(0).scrollHeight)