 Collections.copy Events.getCollection(), collection,
   beforeInsert: (event) ->
     readAllDate = UserEventStats.get()?.readAllDate
+    # TODO(aramk) This isn't triggered since the event doc is published first.
     dateRead = UserEvents.getDateRead(eventId: event._id)
     if dateRead
       event.dateRead = dateRead

    collection.update id, $set: dateRead: new Date()
    if doc.eventId then UserEvents.read(eventId: doc.eventId)

  getDocLabel: (doc) -> @_options.getDocLabel(doc)

  open: (doc) ->

    delete event.doc
    event.eventId = event._id

onUserEventChange = (doc) ->
  dateRead = UserEvents.getDateRead eventId: doc.eventId
  return unless dateRead?
  notification = collection.findOne eventId: doc.eventId
  Notifications.read(notification._id) if notification?

# Reading individual events should mark their notification has read.
Collections.observe UserEvents.getCollection(),
  added: onUserEventChange
  changed: onUserEventChange
  removed: (doc) ->
    notification = collection.findOne eventId: doc.eventId
    Notifications.unread(notification._id) if notification?

# Updating the read all date marks all event notifications as read.
Tracker.autorun ->
