diff --git a/app/lib/account/consent_backend.dart b/app/lib/account/consent_backend.dart index 69e09c7c6a..a79cc518ce 100644 --- a/app/lib/account/consent_backend.dart +++ b/app/lib/account/consent_backend.dart @@ -46,10 +46,10 @@ abstract class ConsentKind { /// Represents the backend for the consent handling and authentication. class ConsentBackend { final DatastoreDB _db; - final _actions = { - ConsentKind.packageUploader: _PackageUploaderAction(), - ConsentKind.publisherContact: _PublisherContactAction(), - ConsentKind.publisherMember: _PublisherMemberAction(), + late final _actions = { + ConsentKind.packageUploader: _PackageUploaderAction(_db), + ConsentKind.publisherContact: _PublisherContactAction(_db), + ConsentKind.publisherMember: _PublisherMemberAction(_db), }; ConsentBackend(this._db); @@ -316,6 +316,9 @@ abstract class ConsentAction { /// Callbacks for package uploader consents. class _PackageUploaderAction extends ConsentAction { + final DatastoreDB _db; + _PackageUploaderAction(this._db); + @override Future onAccept(Consent consent) async { final packageName = consent.args![0]; @@ -335,7 +338,7 @@ class _PackageUploaderAction extends ConsentAction { @override Future onReject(Consent consent, User? user) async { final packageName = consent.args![0]; - await withRetryTransaction(dbService, (tx) async { + await withRetryTransaction(_db, (tx) async { tx.insert(await AuditLogRecord.uploaderInviteRejected( fromAgent: consent.fromAgent, package: packageName, @@ -348,7 +351,7 @@ class _PackageUploaderAction extends ConsentAction { @override Future onExpire(Consent consent) async { final packageName = consent.args![0]; - await withRetryTransaction(dbService, (tx) async { + await withRetryTransaction(_db, (tx) async { tx.insert(await AuditLogRecord.uploaderInviteExpired( fromAgent: consent.fromAgent, package: packageName, @@ -386,6 +389,9 @@ class _PackageUploaderAction extends ConsentAction { /// Callbacks for requesting permission to use e-mail as publisher contact. class _PublisherContactAction extends ConsentAction { + final DatastoreDB _db; + _PublisherContactAction(this._db); + @override Future onAccept(Consent consent) async { final publisherId = consent.args![0]; @@ -400,7 +406,7 @@ class _PublisherContactAction extends ConsentAction { @override Future onReject(Consent consent, User? user) async { final publisherId = consent.args![0]; - await withRetryTransaction(dbService, (tx) async { + await withRetryTransaction(_db, (tx) async { tx.insert(await AuditLogRecord.publisherContactInviteRejected( fromAgent: consent.fromAgent, publisherId: publisherId, @@ -414,7 +420,7 @@ class _PublisherContactAction extends ConsentAction { @override Future onExpire(Consent consent) async { final publisherId = consent.args![0]; - await withRetryTransaction(dbService, (tx) async { + await withRetryTransaction(_db, (tx) async { tx.insert(await AuditLogRecord.publisherContactInviteExpired( fromAgent: consent.fromAgent, publisherId: publisherId, @@ -462,6 +468,9 @@ class _PublisherContactAction extends ConsentAction { /// Callbacks for publisher member consents. class _PublisherMemberAction extends ConsentAction { + final DatastoreDB _db; + _PublisherMemberAction(this._db); + @override Future onAccept(Consent consent) async { final publisherId = consent.args![0]; @@ -479,7 +488,7 @@ class _PublisherMemberAction extends ConsentAction { @override Future onReject(Consent consent, User? user) async { final publisherId = consent.args![0]; - await withRetryTransaction(dbService, (tx) async { + await withRetryTransaction(_db, (tx) async { tx.insert(await AuditLogRecord.publisherMemberInviteRejected( fromAgent: consent.fromAgent, publisherId: publisherId, @@ -492,7 +501,7 @@ class _PublisherMemberAction extends ConsentAction { @override Future onExpire(Consent consent) async { final publisherId = consent.args![0]; - await withRetryTransaction(dbService, (tx) async { + await withRetryTransaction(_db, (tx) async { tx.insert(await AuditLogRecord.publisherMemberInviteExpired( fromAgent: consent.fromAgent, publisherId: publisherId, diff --git a/app/lib/audit/backend.dart b/app/lib/audit/backend.dart index 4c70feae62..6c2a0c2f63 100644 --- a/app/lib/audit/backend.dart +++ b/app/lib/audit/backend.dart @@ -179,7 +179,7 @@ class AuditBackend { window = Duration(minutes: 2); } - final query = dbService.query() + final query = _db.query() ..filter('created >', now.subtract(window)); final current = await query.run().toList(); diff --git a/app/lib/package/backend.dart b/app/lib/package/backend.dart index 9651b9cbb8..27631a5b5d 100644 --- a/app/lib/package/backend.dart +++ b/app/lib/package/backend.dart @@ -117,8 +117,10 @@ class PackageBackend { }))!; } + Stream allPackages() => db.query().run(); + Stream allPackageNames() { - return db.query().run().map((p) => p.name!); + return allPackages().map((p) => p.name!); } /// Retrieves the packages that need to be included in sitemap.txt. diff --git a/app/lib/search/backend.dart b/app/lib/search/backend.dart index c3fca24f6d..3239dff2c6 100644 --- a/app/lib/search/backend.dart +++ b/app/lib/search/backend.dart @@ -180,7 +180,7 @@ class SearchBackend { // initial scan of packages final pool = Pool(concurrency); final futures = []; - await for (final package in dbService.query().run()) { + await for (final package in packageBackend.allPackages()) { if (package.isNotVisible) { continue; } diff --git a/app/lib/shared/integrity.dart b/app/lib/shared/integrity.dart index 72d9344905..593f85d4b8 100644 --- a/app/lib/shared/integrity.dart +++ b/app/lib/shared/integrity.dart @@ -907,8 +907,8 @@ class IntegrityChecker { // TODO: verify fields once the other PR lands if (mc.appealedCaseId != null) { - final appealed = await dbService.lookupOrNull( - dbService.emptyKey.append(ModerationCase, id: mc.appealedCaseId!)); + final appealed = await _db.lookupOrNull( + _db.emptyKey.append(ModerationCase, id: mc.appealedCaseId!)); if (appealed == null) { yield 'ModerationCase "${mc.caseId}" references an appealed case that does not exists.'; } diff --git a/app/lib/task/backend.dart b/app/lib/task/backend.dart index f54f54d074..7be584283a 100644 --- a/app/lib/task/backend.dart +++ b/app/lib/task/backend.dart @@ -1005,7 +1005,7 @@ class TaskBackend { final status = await cache.taskPackageStatus(package).get(() async { for (final rt in acceptedRuntimeVersions) { final key = PackageState.createKey(_db, rt, package); - final state = await dbService.lookupOrNull(key); + final state = await _db.lookupOrNull(key); // skip states where the entry was created, but no analysis has not finished yet if (state == null || state.hasNeverFinished) { continue; @@ -1038,11 +1038,11 @@ class TaskBackend { Future Function(Payload payload) processPayload, ) async { await backfillTrackingState(); - await for (final state in dbService.query().run()) { + await for (final state in _db.query().run()) { final zone = taskWorkerCloudCompute.zones.first; // ignore: invalid_use_of_visible_for_testing_member final payload = await updatePackageStateWithPendingVersions( - dbService, + _db, state, zone, taskWorkerCloudCompute.generateInstanceName(), @@ -1077,7 +1077,7 @@ class TaskBackend { await cache.latestFinishedVersion(package).get(() async { for (final rt in acceptedRuntimeVersions) { final key = PackageState.createKey(_db, rt, package); - final state = await dbService.lookupOrNull(key); + final state = await _db.lookupOrNull(key); // skip states where the entry was created, but no analysis has not finished yet if (state == null || state.hasNeverFinished) { continue; @@ -1119,7 +1119,7 @@ class TaskBackend { final semanticVersion = Version.parse(version); for (final rt in acceptedRuntimeVersions) { final key = PackageState.createKey(_db, rt, package); - final state = await dbService.lookupOrNull(key); + final state = await _db.lookupOrNull(key); // Skip states where the entry was created, but the analysis has not finished yet. if (state == null || state.hasNeverFinished) { continue;