Skip to content

Commit 68aeb5b

Browse files
authored
Migrate User.isBlocked uses before removing the field. (#8451)
1 parent 25e5c9c commit 68aeb5b

File tree

4 files changed

+6
-74
lines changed

4 files changed

+6
-74
lines changed

app/lib/account/backend.dart

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Future<AuthenticatedAgent> _requireAuthenticatedAgent() async {
138138
if (user == null) {
139139
throw AuthenticationException.failed();
140140
}
141-
if (user.isBlocked || user.isModerated) {
141+
if (user.isModerated) {
142142
throw AuthorizationException.blocked();
143143
}
144144
if (user.isDeleted) {
@@ -458,7 +458,7 @@ class AccountBackend {
458458
final info = await authProvider.callTokenInfoWithAccessToken(
459459
accessToken: profile.accessToken ?? '');
460460
final user = await _lookupOrCreateUserByOauthUserId(profile);
461-
if (user == null || user.isBlocked || user.isModerated || user.isDeleted) {
461+
if (user == null || user.isModerated || user.isDeleted) {
462462
throw AuthenticationException.failed();
463463
}
464464
final data = await withRetryTransaction(_db, (tx) async {
@@ -535,7 +535,7 @@ class AccountBackend {
535535
}
536536

537537
final user = await lookupUserById(session.userId!);
538-
if (user == null || user.isBlocked || user.isModerated || user.isDeleted) {
538+
if (user == null || user.isModerated || user.isDeleted) {
539539
return null;
540540
}
541541
return AuthenticatedUser(user,
@@ -613,25 +613,6 @@ class AccountBackend {
613613
_logger.info('Deleted ${count.deleted} UserSession entries.');
614614
}
615615

616-
/// Updates the blocked status of a user.
617-
Future<void> updateBlockedFlag(String userId, bool isBlocked) async {
618-
var expireSessions = false;
619-
await withRetryTransaction(_db, (tx) async {
620-
final user =
621-
await tx.lookupOrNull<User>(_db.emptyKey.append(User, id: userId));
622-
if (user == null) throw NotFoundException.resource('User:$userId');
623-
624-
if (user.isBlocked == isBlocked) return;
625-
user.isBlocked = isBlocked;
626-
tx.insert(user);
627-
expireSessions = isBlocked;
628-
});
629-
630-
if (expireSessions) {
631-
await _expireAllSessions(userId);
632-
}
633-
}
634-
635616
/// Updates the moderated status of a user.
636617
///
637618
/// Expires all existing user sessions.

app/lib/account/models.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,11 @@ class User extends db.ExpandoModel<String> {
7171

7272
User();
7373
User.init() {
74-
isBlocked = false;
7574
isDeleted = false;
7675
isModerated = false;
7776
}
7877

79-
late final isVisible = !isBlocked && !isModerated && !isDeleted;
78+
late final isVisible = !isModerated && !isDeleted;
8079
bool get isNotVisible => !isVisible;
8180

8281
void updateIsModerated({

app/lib/tool/backfill/backfill_new_fields.dart

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:clock/clock.dart';
65
import 'package:logging/logging.dart';
7-
import 'package:meta/meta.dart';
8-
import 'package:pub_dev/account/models.dart';
96
import 'package:pub_dev/package/models.dart';
107
import 'package:pub_dev/shared/datastore.dart';
118

@@ -17,34 +14,11 @@ final _logger = Logger('backfill_new_fields');
1714
/// CHANGELOG.md must be updated with the new fields, and the next
1815
/// release could remove the backfill from here.
1916
Future<void> backfillNewFields() async {
20-
await migrateIsBlocked();
2117
await _removeKnownUnmappedFields();
2218
}
2319

24-
/// Migrates entities from the `isBlocked` fields to the new `isModerated` instead.
25-
@visibleForTesting
26-
Future<void> migrateIsBlocked() async {
27-
_logger.info('Migrating isBlocked...');
28-
final userQuery = dbService.query<User>()..filter('isBlocked =', true);
29-
await for (final entity in userQuery.run()) {
30-
await withRetryTransaction(dbService, (tx) async {
31-
final user = await tx.lookupValue<User>(entity.key);
32-
// sanity check
33-
if (!user.isBlocked) {
34-
return;
35-
}
36-
user
37-
..isModerated = true
38-
..moderatedAt = user.moderatedAt ?? clock.now()
39-
..isBlocked = false;
40-
tx.insert(user);
41-
});
42-
}
43-
44-
_logger.info('isBlocked migration completed.');
45-
}
46-
4720
Future<void> _removeKnownUnmappedFields() async {
21+
_logger.info('Removing unmapped fields...');
4822
await for (final p in dbService.query<Package>().run()) {
4923
if (p.additionalProperties.isEmpty) continue;
5024
if (p.additionalProperties.containsKey('automatedPublishingJson') ||
@@ -59,4 +33,5 @@ Future<void> _removeKnownUnmappedFields() async {
5933
});
6034
}
6135
}
36+
_logger.info('Removing unmapped fields completed.');
6237
}

app/test/tool/maintenance/migrate_isblocked_test.dart

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)