Closing a topic in console

AI-generated summary

The discussion on closing a topic in the Rails console begins with user csmu asking how to close a topic. tshenry initially shares a link to a Discourse API example but clarifies they misunderstood the request, then provides a detailed Ruby function for closing topics in the Rails console. csmu thanks tshenry and shares the function they wrote, which reads a JSON file containing closed topics, maps imported IDs to internal Discourse IDs, and updates the topic status to ‘closed’ using the system user. The function includes error handling and logs the process, demonstrating how to automate closing topics based on imported data.

How do you close a topic in the Rails console?

This should be what you are looking for:

Edit: I read your post too quickly, you were looking for a rails console option. here’s an example you should be able to build off of:

Thanks

Here’s the function I wrote.

	def close_sabai_topics(path)
    
	   if File.exists?(path) then
     
	     import_id_user_id_hash = UserCustomField.where(name: 'import_id').pluck(:value, :user_id).to_h
	     import_id_post_id_hash = PostCustomField.where(name: 'import_id').pluck(:value, :post_id).to_h
     
	     post_id_topic_id_hash = Post.all.pluck(:id,:topic_id).to_h
     
	     posts = Post.all
	     posts_hash = Hash.new
	     posts.each do |post|
	         posts_hash[post.id] = post
	     end
     
	     topics = Topic.all
	     topics_hash = Hash.new
	     topics.each do |topic|
	       topics_hash[topic.id] = topic
	     end
     
	     json = JSON.load File.new(path)     
	     data = json['data']
     
	     data.each do |closed|
	       imported_user_id = "#{closed['user_id']}"
	       imported_post_id = "sabai:#{closed['post_id']}"
	       closed_at = closed['closed_at']
	       user_id = import_id_user_id_hash[imported_user_id]
	       if user_id != nil then
	         post_id = import_id_post_id_hash[imported_post_id]
	         puts "imported_post_id: #{imported_post_id} post_id: #{post_id}"
	         if post_id != nil then
	           post = posts_hash[post_id]
	           if post != nil then
	             topic = topics_hash[post.topic_id]
	             if topic != nil then
	               puts "#{topic.slug}"
	               topic.update_status("closed", true, Discourse.system_user)
	             else
	                raise "Unexpected issue with finding topic.id: #{post.topic_id}"
	             end
	           else
	               # ignore 
	               # puts "Unexpected issue with finding post.id: #{post_id}"
	           end # if post != nil then
	         else
	           # ignore return import_id_post_id_hash
	           # puts "Unexpected issue with finding post_custom_field.value: #{imported_post_id}"
	         end # if post_id != nil then         
	       else
	         raise "Unexpected issue with finding user_custom_field.value: #{imported_user_id}"
	       end # if user_id != nil then
	     end # data.each do |closed|
	   else
	     raise "Unexpected issue with finding path: #{path}"
	   end # if File.exists?(path) then
   
	end

	error = close_sabai_topics('/shared/sabai/questions-closed.json')