       initImageInternal id
       imagesInternal[id].count += 1
       checkImageInternal id
+    checkImage message._id
     ->
       for id of messageImages
         images[id].count -= 1

        messageFolded.set message._id, defaultFolded.get message._id
    , 0
  , [message._id]

  ## Fold referenced attached files by default on initial load.
  messageBodyRef = useRef()
  useEffect ->
    initImage message._id
    ## initImage calls updateFileQuery which will do this:
    #images[message._id].file = message.file
    #initImageInternal message.file if message.file?
    ## If message is naturally folded, don't count images it references.
    images[message._id].naturallyFolded = naturallyFolded message
    images[message._id].children = message.children?.length
    return if images[message._id].naturallyFolded
    messageImages = {}
    messageImagesInternal = {}
    return unless messageBodyRef.current?
    for elt in messageBodyRef.current.querySelectorAll """
      img[src^="#{fileUrlPrefix}"],
      img[src^="#{fileAbsoluteUrlPrefix}"],

  return unless id of images
  ## Image gets unnaturally folded if it's referenced at least once
  ## and doesn't have any children (don't want to hide children, and this
  ## can also lead to infinite loop if children has the image reference)
  ## and doesn't refer to any other images (which can also lead to bad loops).
  newDefault = images[id].naturallyFolded or
    (not images[id].children and
     not images[id].images and
     (images[id].count > 0 or
      (imagesInternal[images[id].file]? and
       imagesInternal[images[id].file].count > 0)))
  console.log id, defaultFolded.get(id), newDefault
  if newDefault != defaultFolded.get id
    defaultFolded.set id, newDefault
    messageFolded.set id, newDefault
