How do you lock a post in the console?
Is it as simple as setting post.locked_by_id to a valid user id?
In a Dev category discussion titled ‘Locking a post in the console’, csmu asks how to lock a post via console, wondering if setting post.locked_by_id suffices. fhe initially confuses locking posts with closing topics, but clarifies that while setting locked_by_id works, it doesn’t log a staff action. csmu explains their use case: locking an old 2015 reply in a topic updated in 2018, since the question is outdated. They then ask fhe if a logged staff action can be created in the console. fhe confirms that using PostLocker from the console is the recommended approach, as it both locks the post and logs the staff action. Example code is provided: Post.where(id: <pid>).find_each { |post| PostLocker.new(post, Discourse.system_user).lock }. The discussion clarifies the distinction between locking posts and closing topics, and emphasizes proper logging for moderation actions.
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 ![]()
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