出于历史原因,我的 Google+ 导入器将用户导入到 TL1,以便为 Google+ 难民提供一个良好的开端。他们是使用 .invalid 电子邮件地址导入的, 当他们登录并通过 Google ID 连接到他们现有的用户记录时,我们会修复这个问题。
但现在我的 trust_level_1 群组充斥着数千名尚未关注其内容的未登录用户,如果他们几年后出现(他们有时会继续这样做),没有特别的理由让他们以 Basic 用户身份进入;他们应该成为 New 用户。引导期早已过去。我想将那些从未登录过的用户降级到 TL0,直到他们出现并查看为止,并仅依赖正常的晋升流程。
我在 rails 控制台中尝试过这个:
User.where(trust_level: 1) do |u|
u.change_trust_level!(TrustLevel[0]) if !Email.is_valid?(u.primary_email&.email)
u.save!
end
它花费了数分钟时间消耗了完整的 CPU,但什么也没做。(在此过程中,我还尝试了其他变体,例如 if !u.primary_email.is_valid? 和 if !Email.is_valid?(u.primary_email),我知道真正的问题是我一年才接触一次 Ruby……)我确信我遗漏了一些对其他人来说显而易见的东西。如果有人能想到一个提示,我将不胜感激!
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!
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!
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.