Locking a post in the console

How do you lock a post in the console?

Is it as simple as setting post.locked_by_id to a valid user id?

I assume that you refer to closing a topic.

I’d either use the API for that or the corresponding method on the Topic type:

Topic.where(closed: false, category_id: <catid>, id: <tid>).find_each do |topic| 
  topic.update_status("closed", true, Discourse.system_user)
end

Edit: I just realize that locking posts is something different. You can set the post.locked_by_id but I guess you’re not creating a logged staff action in that case.

Thanks @fhe

That’s useful :slight_smile:

I’m looking how to lock a post at this stage.

Context. A topic that is created in 2015,edited in 2018. An old reply in 2015. The 2015 question was answered and it makes sense to lock it as the question is no longer in context the new version of the topic.

Can a logged staff action be created in the console?

Yes, I’ve just realized that and edited my post accordingly.

I think utilizing the PostLocker from the console would be good, it also creates the staff log action.

Post.where(id: <pid>).find_each do |post| 
  locker = PostLocker.new(post, Discourse.system_user)
  locker.lock
end
3 Likes