Closing a topic in console

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:

6 Likes

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')
1 Like