That cleared the error. I assume that because it couldn’t find the Class before, it was treating it as a Constant? In any case, that’s solved it, brilliant, thanks so much! I’m unstuck!
So the reason I had this:
user.approved = true
user.approved_by_id = Discourse.system_user.id
before:
reviewable.perform(:approve, Discourse.system_user)
is because the addition to the review queue is asynchronous. In the job, the review is only created if approve is false from (discourse/app/jobs/regular/create_user_reviewable.rb at 888e68a1637ca784a7bf51a6bbb524dcf7413b13 · discourse/discourse · GitHub):
if user = User.find_by(id: args[:user_id])
return if user.approved?
@reviewable = ReviewableUser.needs_review!(
target: user,
created_by: Discourse.system_user,
reviewable_by_moderator: true,
payload: {
username: user.username,
name: user.name,
email: user.email
}
)
There’s a risk that the job may fire after you’ve tested for the review entry?
The result of that is the review entry doesn’t appear to exist but the job is just waiting to run. The job then runs and creates the review entry and you’ve missed the opportunity to clear it as your code to test this has already run.
It’s a potential race condition?
Make approved true
before you check for a review entry and you have solved the problem (because a review will never be created after this as its a dependency).
I ran into the problem testing my code - on dev it worked, then on production it failed as things ran in a different order.
NB I appreciate you have not written this to support this use case, but I think it’s important to allow plugins to be able to auto-approve new users for special circumstances (e.g. like the Discord plugin which does so if the user is a member of a trusted guild).