           initImageInternal images[id].file, id
           checkImageInternal images[id].file
 , 250
-
-initImage = (id) ->
-  if id not of images
-    images[id] =
-      count: 0
-      file: null
-    updateFileQuery()
-
-initImageInternal = (id, image = null) ->
-  #console.log "initImageInternal #{id}, #{image}"
-  if id not of imagesInternal
-    imagesInternal[id] =
-      count: 0
-      image: image
-  else if image?
-    imagesInternal[id].image = image
-
-checkImage = (id) ->
-  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
-  ## No longer care about this image if it's not referenced and doesn't have
-  ## a rendered template.
-  if images[id].count == 0 and id not of id2dom
-    delete images[id]
-    updateFileQuery()
-
-checkImageInternal = (id) ->
-  image = imagesInternal[id].image
-  #console.log "#{id} corresponds to #{image}"
-  checkImage image if image?
 
 messageOnDragStart = (message) -> (e) ->
   #url = "coauthor:#{message._id}"

threadAuthors = {}
threadMentions = {}

images = {}
imagesInternal = {}
id2dom = {}
scrollToLater = null
fileQuery = null

  , [message._id]
  ###

  ## Count images referenced by this message, and
  ## update default folded status of this message.
  messageBodyRef = useRef()
  ## Depend on references to this message (if it's a file).
  useTracker ->
    [
      imageRefCount.equals 0, message._id
      imageInternalRefCount.equals 0, message.file if message.file?
    ]
  , [message._id, message.file]
  useEffect ->
    refCount = 0
    ## If message is naturally folded, don't count images it references.
    natural = naturallyFolded message
    if not natural and messageBodyRef.current?
      messageImages = {}
      messageImagesInternal = {}
      for elt in messageBodyRef.current.querySelectorAll """
        img[src^="#{fileUrlPrefix}"],
        img[src^="#{fileAbsoluteUrlPrefix}"],
        img[src^="#{internalFileUrlPrefix}"],
        img[src^="#{internalFileAbsoluteUrlPrefix}"],
        video source[src^="#{fileUrlPrefix}"],
        video source[src^="#{fileAbsoluteUrlPrefix}"],
        video source[src^="#{internalFileUrlPrefix}"],
        video source[src^="#{internalFileAbsoluteUrlPrefix}"]
      """
        refCount += 1
        src = elt.getAttribute 'src'
        if 0 <= src.indexOf 'gridfs'
          messageImagesInternal[url2internalFile src] = true
        else
          messageImages[url2file src] = true
      for id of messageImages
        imageRefCount.set id, (imageRefCount.get(id) ? 0) + 1
      for id of messageImagesInternal
        imageInternalRefCount.set id, (imageInternalRefCount.get(id) ? 0) + 1
      uncount = ->
        for id of messageImages
          imageRefCount.set id, (imageRefCount.get(id) ? 0) - 1
        for id of messageImagesInternal
          imageInternalRefCount.set id, (imageInternalRefCount.get(id) ? 0) - 1
    ## 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 = natural or
      (not message.children?.length and
       not refCount and
       (imageRefCount.get(message._id) or
        imageInternalRefCount.get(message.file)))
    console.log message._id, defaultFolded.get(message._id), newDefault, natural, 
      not message.children?.length,
      not refCount,
      imageRefCount.get(message._id),
      imageInternalRefCount.get(message.file)
    if newDefault != defaultFolded.get message._id
      defaultFolded.set message._id, newDefault
      messageFolded.set message._id, newDefault
    uncount
  # too many dependencies to list

  ## Transform images
