How do you rebuild the post or reaction count

If I want to rebuild the reaction count and summary, is there a way to do it?

Like:bundle exec rake reactions:recount

Would this be covered by the jobs::EnsureDbConsistency background job?

You can manually trigger it from your /sidekiq/scheduler page if you want to speed it up and see if that rebuilds your counts for you.

Though if you can go into a little more detail about why you need to recalculate them it may add some extra context that could be important.

2 Likes

Thank you for your response. I was doing the test import of reactions from Xenforo to Discourse. The data was correctly imported but not showing up on posts with username and all.

Have managed to do it now.

Used this script

RAILS_ENV=production bundle exec rails runner "

  counts = DiscourseReactions::ReactionUser.group(:reaction_id).count
  
  DiscourseReactions::Reaction.where(id: counts.keys).find_each do |reaction|
    reaction.update_column(:reaction_users_count, counts[reaction.id])
  end

  posts_to_update = Post.where(id: DiscourseReactions::Reaction.select(:post_id).distinct)
  
  posts_to_update.find_each do |post|
    post_reactions = post.reactions.where('reaction_users_count > 0')

    custom_field_value = post_reactions.map do |reaction|
      {
        id: reaction.reaction_value,
        type: reaction.reaction_type,
        count: reaction.reaction_users_count
      }
    end

    if custom_field_value.present?
      post.custom_fields['discourse_reactions_reactions'] = custom_field_value.to_json
    else
      post.custom_fields.delete('discourse_reactions_reactions')
    end
    
    post.save_custom_fields(true)
  end

"
2 Likes