Skip to content

Commit

Permalink
Migrate User.isBlocked uses before removing the field. (dart-lang#8451)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Jan 9, 2025
1 parent 25e5c9c commit 68aeb5b
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 74 deletions.
25 changes: 3 additions & 22 deletions app/lib/account/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ Future<AuthenticatedAgent> _requireAuthenticatedAgent() async {
if (user == null) {
throw AuthenticationException.failed();
}
if (user.isBlocked || user.isModerated) {
if (user.isModerated) {
throw AuthorizationException.blocked();
}
if (user.isDeleted) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -613,25 +613,6 @@ class AccountBackend {
_logger.info('Deleted ${count.deleted} UserSession entries.');
}

/// Updates the blocked status of a user.
Future<void> updateBlockedFlag(String userId, bool isBlocked) async {
var expireSessions = false;
await withRetryTransaction(_db, (tx) async {
final user =
await tx.lookupOrNull<User>(_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.
Expand Down
3 changes: 1 addition & 2 deletions app/lib/account/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,11 @@ class User extends db.ExpandoModel<String> {

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({
Expand Down
29 changes: 2 additions & 27 deletions app/lib/tool/backfill/backfill_new_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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<void> backfillNewFields() async {
await migrateIsBlocked();
await _removeKnownUnmappedFields();
}

/// Migrates entities from the `isBlocked` fields to the new `isModerated` instead.
@visibleForTesting
Future<void> migrateIsBlocked() async {
_logger.info('Migrating isBlocked...');
final userQuery = dbService.query<User>()..filter('isBlocked =', true);
await for (final entity in userQuery.run()) {
await withRetryTransaction(dbService, (tx) async {
final user = await tx.lookupValue<User>(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<void> _removeKnownUnmappedFields() async {
_logger.info('Removing unmapped fields...');
await for (final p in dbService.query<Package>().run()) {
if (p.additionalProperties.isEmpty) continue;
if (p.additionalProperties.containsKey('automatedPublishingJson') ||
Expand All @@ -59,4 +33,5 @@ Future<void> _removeKnownUnmappedFields() async {
});
}
}
_logger.info('Removing unmapped fields completed.');
}
23 changes: 0 additions & 23 deletions app/test/tool/maintenance/migrate_isblocked_test.dart

This file was deleted.

0 comments on commit 68aeb5b

Please sign in to comment.