TLP: fails to register new thumbnail selection if image is in first post

Environment

Discourse Version: 2.4.0.beta9

Plugin Commit: Latest

Other Plugins
N/A

Steps to Reproduce

Set up a topic with images in 3 posts.

Select any thumbnail from post 2 or 3. SUCCESS

Select any thumbnail from post 1. FAILURE. You can’t seem to update the thumbnail with images from post 1.

Example

You cannot select the thumbnail from the first post

Logs

Started PUT "/t/swytch-e-bike-conversion-kit-reviews/479" for 79.66.40.168 at 2020-01-04 09:57:54 +0000
Processing by TopicsController#update as */*
  Parameters: {"image_url"=>"https://ichef.bbci.co.uk/news/1024/branded_news/C344/production/_110388994_gettyimages-1169549805-1.jpg", "thumbnail_post_id"=>1065, "slug"=>"swytch-e-bike-conversion-kit-reviews", "topic_id"=>"479", "topic"=>{"image_url"=>"https://ichef.bbci.co.uk/news/1024/branded_news/C344/production/_110388994_gettyimages-1169549805-1.jpg"}}

This is an example from the console. It seems to have all the correct information?!

Basically the PostRevisor update for Topic attribute image_url is failing when the image is selected from the first post. It is successful when selecting from any other post. The data going to the Controller appears to be correct and no errors are thrown.

Code is this:

  PostRevisor.class_eval do
    track_topic_field(:image_url) do |tc, image_url|
      tc.record_change('image_url', tc.topic.image_url, image_url)
      tc.topic.image_url = image_url

      topic_id = tc.topic.id
      thumbnail_post = nil
      thumbnail_post_id  = nil
      topic = Topic.find(topic_id)
      @posts = topic.posts

      @posts.each do |post|
          post_id = post.id
          doc = Nokogiri::HTML( post.cooked )
          @img_srcs = doc.css('img').map{ |i| i['src'] }
          @img_srcs << post.image_url if (!post.image_url.blank? && (!@img_srcs.include? post.image_url))
          @img_srcs.each do |image|
            if image == image_url
              thumbnail_post = post
              thumbnail_post_id = post_id
            end
          end
          break if thumbnail_post_id != nil
      end

      tc.record_change('thumbnail_from_post', tc.topic.custom_fields['thumbnail_from_post'], thumbnail_post_id)
      tc.topic.custom_fields['thumbnail_from_post'] = thumbnail_post_id
      byebug
      unless SiteSetting.topic_list_hotlink_thumbnails ||
                !SiteSetting.topic_list_previews_enabled
        if !thumbnail_post_id.nil?
          thumbnails = ListHelper.create_topic_thumbnails(thumbnail_post, image_url)
          if thumbnails[:id]
            tc.record_change('thumbnails', tc.topic.custom_fields['thumbnails'], thumbnails[:thumbnails])
            tc.topic.custom_fields['thumbnails'] = thumbnails[:thumbnails]
            ## ensure there is a post_upload record so the upload is not removed in the cleanup
            unless PostUpload.where(post_id: thumbnail_post_id).exists?
              PostUpload.create(post_id: thumbnail_post_id, upload_id: thumbnails[:id])
            end
          end
        end
      end
    end
  end

It wasn’t failing (I even tracked everything through the Discourse source code).

What was happening was the Cooked_Post_Processor was overwriting the value with nil.

I’ve released a change here, will test more in Prod before announcing

Marked as fixed.