FIX: User titles from translated badge names were automatically revoked

It also cleans up the denormalized data about badge titles in the user_profiles table.
This commit is contained in:
Gerhard Schlager
2020-08-19 23:23:31 +02:00
committed by Gerhard Schlager
parent 11647b79f7
commit cff68ef0dd
2 changed files with 96 additions and 23 deletions

View File

@@ -397,21 +397,33 @@ class BadgeGranter
def self.revoke_ungranted_titles!
DB.exec <<~SQL
UPDATE users SET title = ''
WHERE NOT title IS NULL AND
title <> '' AND
EXISTS (
SELECT 1
FROM user_profiles
WHERE user_id = users.id AND badge_granted_title
) AND
title NOT IN (
SELECT name
FROM badges
WHERE allow_title AND enabled AND
badges.id IN (SELECT badge_id FROM user_badges ub where ub.user_id = users.id)
UPDATE users u
SET title = ''
FROM user_profiles up
WHERE u.title IS NOT NULL
AND u.title <> ''
AND up.user_id = u.id
AND up.badge_granted_title
AND up.granted_title_badge_id IS NOT NULL
AND NOT EXISTS(
SELECT 1
FROM badges b
JOIN user_badges ub ON ub.user_id = u.id AND ub.badge_id = b.id
WHERE b.id = up.granted_title_badge_id
AND b.allow_title
AND b.enabled
)
SQL
DB.exec <<~SQL
UPDATE user_profiles up
SET badge_granted_title = FALSE,
granted_title_badge_id = NULL
FROM users u
WHERE up.user_id = u.id
AND (u.title IS NULL OR u.title = '')
AND (up.badge_granted_title OR up.granted_title_badge_id IS NOT NULL)
SQL
end
def self.notification_locale(locale)