From 9e827fc8158e0aba5889e8aa8828176a9b36115a Mon Sep 17 00:00:00 2001 From: Istvan Soos Date: Thu, 9 Jan 2025 13:31:02 +0100 Subject: [PATCH] Migrate User.isBlocked uses before removing the field. --- app/lib/account/backend.dart | 25 ++-------------- app/lib/account/models.dart | 3 +- .../tool/backfill/backfill_new_fields.dart | 29 ++----------------- .../maintenance/migrate_isblocked_test.dart | 23 --------------- 4 files changed, 6 insertions(+), 74 deletions(-) delete mode 100644 app/test/tool/maintenance/migrate_isblocked_test.dart diff --git a/app/lib/account/backend.dart b/app/lib/account/backend.dart index 7884d17cff..f2f52d5929 100644 --- a/app/lib/account/backend.dart +++ b/app/lib/account/backend.dart @@ -138,7 +138,7 @@ Future _requireAuthenticatedAgent() async { if (user == null) { throw AuthenticationException.failed(); } - if (user.isBlocked || user.isModerated) { + if (user.isModerated) { throw AuthorizationException.blocked(); } if (user.isDeleted) { @@ -458,7 +458,7 @@ class AccountBackend { final info = await authProvider.callTokenInfoWithAccessToken( accessToken: profile.accessToken ?? ''); final user = await _lookupOrCreateUserByOauthUserId(profile); - if (user == null || user.isBlocked || user.isModerated || user.isDeleted) { + if (user == null || user.isModerated || user.isDeleted) { throw AuthenticationException.failed(); } final data = await withRetryTransaction(_db, (tx) async { @@ -535,7 +535,7 @@ class AccountBackend { } final user = await lookupUserById(session.userId!); - if (user == null || user.isBlocked || user.isModerated || user.isDeleted) { + if (user == null || user.isModerated || user.isDeleted) { return null; } return AuthenticatedUser(user, @@ -613,25 +613,6 @@ class AccountBackend { _logger.info('Deleted ${count.deleted} UserSession entries.'); } - /// Updates the blocked status of a user. - Future updateBlockedFlag(String userId, bool isBlocked) async { - var expireSessions = false; - await withRetryTransaction(_db, (tx) async { - final user = - await tx.lookupOrNull(_db.emptyKey.append(User, id: userId)); - if (user == null) throw NotFoundException.resource('User:$userId'); - - if (user.isBlocked == isBlocked) return; - user.isBlocked = isBlocked; - tx.insert(user); - expireSessions = isBlocked; - }); - - if (expireSessions) { - await _expireAllSessions(userId); - } - } - /// Updates the moderated status of a user. /// /// Expires all existing user sessions. diff --git a/app/lib/account/models.dart b/app/lib/account/models.dart index 14a203a062..abf1c81251 100644 --- a/app/lib/account/models.dart +++ b/app/lib/account/models.dart @@ -71,12 +71,11 @@ class User extends db.ExpandoModel { User(); User.init() { - isBlocked = false; isDeleted = false; isModerated = false; } - late final isVisible = !isBlocked && !isModerated && !isDeleted; + late final isVisible = !isModerated && !isDeleted; bool get isNotVisible => !isVisible; void updateIsModerated({ diff --git a/app/lib/tool/backfill/backfill_new_fields.dart b/app/lib/tool/backfill/backfill_new_fields.dart index 9a025ae243..ac1a2011bc 100644 --- a/app/lib/tool/backfill/backfill_new_fields.dart +++ b/app/lib/tool/backfill/backfill_new_fields.dart @@ -2,10 +2,7 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -import 'package:clock/clock.dart'; import 'package:logging/logging.dart'; -import 'package:meta/meta.dart'; -import 'package:pub_dev/account/models.dart'; import 'package:pub_dev/package/models.dart'; import 'package:pub_dev/shared/datastore.dart'; @@ -17,34 +14,11 @@ final _logger = Logger('backfill_new_fields'); /// CHANGELOG.md must be updated with the new fields, and the next /// release could remove the backfill from here. Future backfillNewFields() async { - await migrateIsBlocked(); await _removeKnownUnmappedFields(); } -/// Migrates entities from the `isBlocked` fields to the new `isModerated` instead. -@visibleForTesting -Future migrateIsBlocked() async { - _logger.info('Migrating isBlocked...'); - final userQuery = dbService.query()..filter('isBlocked =', true); - await for (final entity in userQuery.run()) { - await withRetryTransaction(dbService, (tx) async { - final user = await tx.lookupValue(entity.key); - // sanity check - if (!user.isBlocked) { - return; - } - user - ..isModerated = true - ..moderatedAt = user.moderatedAt ?? clock.now() - ..isBlocked = false; - tx.insert(user); - }); - } - - _logger.info('isBlocked migration completed.'); -} - Future _removeKnownUnmappedFields() async { + _logger.info('Removing unmapped fields...'); await for (final p in dbService.query().run()) { if (p.additionalProperties.isEmpty) continue; if (p.additionalProperties.containsKey('automatedPublishingJson') || @@ -59,4 +33,5 @@ Future _removeKnownUnmappedFields() async { }); } } + _logger.info('Removing unmapped fields completed.'); } diff --git a/app/test/tool/maintenance/migrate_isblocked_test.dart b/app/test/tool/maintenance/migrate_isblocked_test.dart deleted file mode 100644 index 8abd9509af..0000000000 --- a/app/test/tool/maintenance/migrate_isblocked_test.dart +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -import 'package:pub_dev/account/backend.dart'; -import 'package:pub_dev/shared/datastore.dart'; -import 'package:pub_dev/tool/backfill/backfill_new_fields.dart'; -import 'package:test/test.dart'; - -import '../../shared/test_services.dart'; - -void main() { - group('Migrate isBlocked', () { - testWithProfile('user', fn: () async { - final u1 = await accountBackend.lookupUserByEmail('user@pub.dev'); - await dbService.commit(inserts: [u1..isBlocked = true]); - await migrateIsBlocked(); - - final u2 = await accountBackend.lookupUserByEmail('user@pub.dev'); - expect(u2.isModerated, true); - }); - }); -}