Requesting help demoting users with invalid email addresses

For historical reasons, my Google+ importer imported users at TL1 to give google+ refugees a head start. They are imported with .invalid email addresses, and we fix that when they log in and get hooked up to their existing user record by their google ID through google auth.

But now my trust_level_1 group is bloated with thousands of users who haven’t followed their content yet, and if they show up after a few years (as they sometimes do continue to do) there’s no particular reason to come in as Basic users; they should be New users. Bootstrapping is long past. I’d like to demote those never-logged-in users to TL0 until such time as they show up and look around, and just depend on the normal promotion process.

I tried this in the rails console:

User.where(trust_level: 1) do |u|
    u.change_trust_level!(TrustLevel[0]) if !Email.is_valid?(u.primary_email&.email)
    u.save!
end

It took minutes of consuming a full CPU, but did nothing. (I tried other variations as well on the way there, like if !u.primary_email.is_valid? and if !Email.is_valid?(u.primary_email) and I know the real problem is that I touch Ruby once a year or so…) I’m sure I’m missing something obvious to others. I’d appreciate a hint if anyone can come up with one!

1 Like

I was clearly missing the .each

User.where(trust_level: 1).each do |u|
    u.change_trust_level!(TrustLevel[0]) if !Email.is_valid?(u.primary_email&.email)
    u.save!
end

This brought my site down while it was running with lots of IO and still did not make any change. So I’m still doing something wrong and am not a little worried about continuing to try this on the live system. :relaxed:

I did work out that this isn’t what is_valid? means. So I’m now looking for the literal string instead, and I tested that by printing the email address. Also I see that change_trust_level already saves the user, so I don’t need to do that twice.

User.where(trust_level: 1).each do |u|
   if u.primary_email&.email.end_with?(".invalid")  
     u.change_trust_level!(TrustLevel[0])    
   end  
end  

But the number of users in trust_level_1 still doesn’t change, so I’m still missing something important.

Could you use the bulk admin task in this case?


Though I’ve just tested your second code out and it worked for me on my test user (email: test_thirteen@here.invalid, was TL1 and is now TL0).

User.where(trust_level: 1).each do |u|
   if u.primary_email&.email.end_with?(".invalid")  
     u.change_trust_level!(TrustLevel[0])    
   end  
end
3 Likes

I would start with the UserEmail model and find the ones with “invalid” in their email address and loop through those, but that’s not something I can figure out exactly on my phone.

If those addresses with invalid in them were truly invalid then they would not be able to be saved.

Thank you!

I’ve manually promoted some users who were previously well-known to the existing community members, so a bulk recalculation wouldn’t do what I want. (I had found that page while searching.)

That last was actually working, as it did for you. My problem was that I was looking at the /g groups overview page and refreshing it and looking for the trust_level_1 group count to decrease. It was not displaying a change in count while I was running it, so I stopped it with control-C. This morning the trust_level_1 group now has thousands of fewer members displaying on that page. So now I realize that the count there is cached, and I should have been clicking on that card and looking at the count on the /g/trust_level_1 page.

Now I understand that I should have followed up with Group.ensure_consistency! to repair the counts on that page.

Here’s the whole thing, expressed more compactly and idiomatically:

User.where(trust_level: 1).each do |u|
     u.change_trust_level!(TrustLevel[0]) if u.primary_email&.email.end_with?(".invalid")  
end
Group.ensure_consistency!

It happens that I have a record of a few users with only an invalid email with a record of having actually used the site and met TL1 requirements. I don’t know how that happened, but for now I don’t care. All I really care about is taking care of the bulk of thousands, without worrying about the few exceptions. If this happens to anyone else, this worked for me:

User.where(trust_level: 1).each do |u|
  begin
     u.change_trust_level!(TrustLevel[0]) if u.primary_email&.email.end_with?(".invalid")  
  rescue
  end
end
Group.ensure_consistency!
2 Likes

Here’s the background:

script/import_scripts/friendsmegplus.rb

  def import_google_user(id, name)
    if !@emails[id].present?
      google_user_info = UserAssociatedAccount.find_by(provider_name: 'google_oauth2', provider_u
id: id.to_i)
      if google_user_info.nil?
        # create new google user on system; expect this user to merge
        # when they later log in with google authentication
        # Note that because email address is not included in G+ data, we
        # don't know if they already have another account not yet associated
        # with google ooauth2. If they didn't log in, they'll have an
        # @gplus.invalid address associated with their account
        email = "#{id}@gplus.invalid"

This means that I only care about primary_email and don’t have to start with UserEmail. I started down that path before realizing that anything that changed that not to be the primary_email would make me not want to change them anyway. So here it’s very specifically users whose only (thus primary) email literally ends with .invalid — which is what I’ve done.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.