Skip to content

Commit

Permalink
isModerated flags + backfill in Package, PackageVersion, Publisher an…
Browse files Browse the repository at this point in the history
…d User (#7538)
  • Loading branch information
isoos committed Mar 7, 2024
1 parent 675cf67 commit d71e009
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ AppEngine version, listed here to ease deployment and troubleshooting.
* Upgraded stable Flutter analysis SDK to `3.19.2`.
* Upgraded dartdoc to `8.0.6`.
* Note: preview analysis SDKs are downloaded dynamically.
* Note: started to backfill `isModerated` flags in `Package`, `PackageVersion`, `Publisher` and `User`.
* Resyncing all `SecurityAdvisory` entities to get `pub_display_url` in the
`database_specific` field backfilled.

Expand Down
6 changes: 2 additions & 4 deletions app/lib/account/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -411,14 +411,12 @@ class AccountBackend {
}

// Create new user with oauth2 user_id mapping
final user = User()
final user = User.init()
..parentKey = emptyKey
..id = createUuid()
..oauthUserId = auth.oauthUserId
..email = auth.email
..created = clock.now().toUtc()
..isBlocked = false
..isDeleted = false;
..created = clock.now().toUtc();

tx.insert(user);
tx.insert(
Expand Down
16 changes: 16 additions & 0 deletions app/lib/account/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ class User extends db.ExpandoModel<String> {
/// to perform any action.
@db.BoolProperty(required: true)
bool isBlocked = false;

/// `true` if user was moderated (pending moderation or deletion).
/// TODO: set this to true after backfill and all versions use it
@db.BoolProperty(required: false)
bool? isModerated;

/// The timestamp when the user was moderated.
@db.DateTimeProperty()
DateTime? moderatedAt;

User();
User.init() {
isBlocked = false;
isDeleted = false;
isModerated = false;
}
}

/// Maps Oauth user_id to User.id
Expand Down
5 changes: 2 additions & 3 deletions app/lib/package/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,7 @@ Future<_UploadEntities> _createUploadEntities(
final packageKey = db.emptyKey.append(Package, id: pubspec.name);
final versionString = canonicalizeVersion(pubspec.nonCanonicalVersion);

final version = PackageVersion()
final version = PackageVersion.init()
..id = versionString
..parentKey = packageKey
..version = versionString
Expand All @@ -1763,8 +1763,7 @@ Future<_UploadEntities> _createUploadEntities(
..pubspec = pubspec
..libraries = archive.libraries
..uploader = agent.agentId
..sha256 = sha256Hash
..isRetracted = false;
..sha256 = sha256Hash;

final derived = derivePackageVersionEntities(
archive: archive,
Expand Down
26 changes: 26 additions & 0 deletions app/lib/package/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ class Package extends db.ExpandoModel<String> {
@db.DateTimeProperty()
DateTime? blocked;

/// `true` if package was moderated (pending moderation or deletion).
/// TODO: set this to true after backfill and all versions use it
@db.BoolProperty(required: false)
bool? isModerated;

/// The timestamp when the package was moderated.
@db.DateTimeProperty()
DateTime? moderatedAt;

/// Tags that are assigned to this package.
///
/// The permissions required to assign a tag typically depends on the tag.
Expand Down Expand Up @@ -187,6 +196,7 @@ class Package extends db.ExpandoModel<String> {
..isDiscontinued = false
..isUnlisted = false
..isBlocked = false
..isModerated = false
..assignedTags = []
..deletedVersions = [];
}
Expand Down Expand Up @@ -579,6 +589,22 @@ class PackageVersion extends db.ExpandoModel<String> {
@db.DateTimeProperty()
DateTime? retracted;

/// `true` if package version was moderated (pending moderation or deletion).
/// TODO: set this to true after backfill and all versions use it
@db.BoolProperty(required: false)
bool? isModerated;

/// The timestamp when the package version was moderated.
@db.DateTimeProperty()
DateTime? moderatedAt;

PackageVersion();

PackageVersion.init() {
isModerated = false;
isRetracted = false;
}

// Convenience Fields:

late final semanticVersion = Version.parse(version!);
Expand Down
10 changes: 10 additions & 0 deletions app/lib/publisher/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@ class Publisher extends db.ExpandoModel<String> {
@db.BoolProperty(required: true)
bool isBlocked = false;

/// `true` if publisher was moderated (pending moderation or deletion).
/// TODO: set this to true after backfill and all versions use it
@db.BoolProperty(required: false)
bool? isModerated;

/// The timestamp when the publisher was moderated.
@db.DateTimeProperty()
DateTime? moderatedAt;

Publisher();

Publisher.init({
Expand All @@ -80,6 +89,7 @@ class Publisher extends db.ExpandoModel<String> {
websiteUrl = defaultPublisherWebsite(publisherId);
isAbandoned = false;
isBlocked = false;
isModerated = false;
}

/// Clears most properties on the entity and sets the [isBlocked] flag.
Expand Down
39 changes: 39 additions & 0 deletions app/lib/tool/backfill/backfill_new_fields.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:logging/logging.dart';
import 'package:pub_dev/account/models.dart';
import 'package:pub_dev/package/models.dart';
import 'package:pub_dev/publisher/models.dart';
import 'package:pub_dev/service/security_advisories/sync_security_advisories.dart';
import 'package:pub_dev/shared/datastore.dart';

final _logger = Logger('backfill_new_fields');

Expand All @@ -16,4 +20,39 @@ Future<void> backfillNewFields() async {
_logger.info('Resyncing all security advisories...');
// This will backfill the `pub_display_url` in the `database_specific` field on the `SecurityAdvisory` entity.
await syncSecurityAdvisories(resync: true);

_logger.info('Backfilling isModerated fileds...');
await for (final e in dbService.query<Package>().run()) {
if (e.isModerated != null) continue;
await withRetryTransaction(dbService, (tx) async {
final o = await tx.lookupValue<Package>(e.key);
o.isModerated = false;
tx.insert(o);
});
}
await for (final e in dbService.query<PackageVersion>().run()) {
if (e.isModerated != null) continue;
await withRetryTransaction(dbService, (tx) async {
final o = await tx.lookupValue<PackageVersion>(e.key);
o.isModerated = false;
tx.insert(o);
});
}
await for (final e in dbService.query<User>().run()) {
if (e.isModerated != null) continue;
await withRetryTransaction(dbService, (tx) async {
final o = await tx.lookupValue<User>(e.key);
o.isModerated = false;
tx.insert(o);
});
}
await for (final e in dbService.query<Publisher>().run()) {
if (e.isModerated != null) continue;
await withRetryTransaction(dbService, (tx) async {
final o = await tx.lookupValue<Publisher>(e.key);
o.isModerated = false;
tx.insert(o);
});
}
_logger.info('Backfill completed.');
}
2 changes: 1 addition & 1 deletion app/test/audit/backend_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void main() {
package: 'pkg',
version: '1.0.0',
uploader: AuthenticatedUser(
User()
User.init()
..id = 'user-id'
..email = 'user@pub.dev',
audience: 'fake-client-audience',
Expand Down
2 changes: 1 addition & 1 deletion app/test/package/models_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ class _PublishSequence {

void publish(String version, {int sdk = 0}) {
final minSdk = sdk > 0 ? _futureSdk : (sdk < 0 ? _pastSdk : _currentSdk);
final pv = PackageVersion()
final pv = PackageVersion.init()
..parentKey = _p.key
..id = version
..version = version
Expand Down

0 comments on commit d71e009

Please sign in to comment.