From 5f49e9d0a689b1bdbaa96a9e2ca6bf857bb0576d Mon Sep 17 00:00:00 2001 From: Dennis Loose Date: Fri, 13 Dec 2024 16:23:29 +0100 Subject: [PATCH] feat: use `snap_name` in `Vote` and `Rating` --- .../lib/ratings/rated_category_model.dart | 3 +- .../app_center/lib/ratings/ratings_data.dart | 1 + .../app_center/lib/ratings/ratings_model.dart | 2 ++ .../app_center/lib/snapd/snapd_service.dart | 26 ---------------- packages/app_center/test/app_card_test.dart | 2 ++ packages/app_center/test/games_page_test.dart | 2 ++ .../app_center/test/ratings_model_test.dart | 14 ++++++--- .../app_center/test/ratings_service_test.dart | 31 ++++++++++++------- .../app_center/test/search_page_test.dart | 2 ++ packages/app_center/test/snap_page_test.dart | 2 ++ packages/app_center/test/test_utils.dart | 2 ++ .../lib/src/ratings.dart | 2 ++ .../lib/src/vote.dart | 2 ++ .../test/ratings_client_test.dart | 13 +++++++- 14 files changed, 60 insertions(+), 44 deletions(-) diff --git a/packages/app_center/lib/ratings/rated_category_model.dart b/packages/app_center/lib/ratings/rated_category_model.dart index 0aa6285d6..efd07d7a3 100644 --- a/packages/app_center/lib/ratings/rated_category_model.dart +++ b/packages/app_center/lib/ratings/rated_category_model.dart @@ -21,7 +21,8 @@ class RatedCategoryModel extends _$RatedCategoryModel { for (final category in categories) { final chart = await _ratings.getChart(category); for (var i = 0; snaps.length < numberOfSnaps && i < chart.length; i++) { - final snap = await _snapd.findById(chart[i].rating.snapId); + final snap = + (await _snapd.find(name: chart[i].rating.snapName)).singleOrNull; if (snap != null && snap.screenshotUrls.isNotEmpty) { snaps.add(snap); } diff --git a/packages/app_center/lib/ratings/ratings_data.dart b/packages/app_center/lib/ratings/ratings_data.dart index b5ffbadb0..4b55b7cd8 100644 --- a/packages/app_center/lib/ratings/ratings_data.dart +++ b/packages/app_center/lib/ratings/ratings_data.dart @@ -12,6 +12,7 @@ class RatingsData with _$RatingsData { required int snapRevision, required Rating? rating, required VoteStatus? voteStatus, + required String snapName, }) = _RatingsData; factory RatingsData.fromJson(Map json) => diff --git a/packages/app_center/lib/ratings/ratings_model.dart b/packages/app_center/lib/ratings/ratings_model.dart index c9824b014..06df81017 100644 --- a/packages/app_center/lib/ratings/ratings_model.dart +++ b/packages/app_center/lib/ratings/ratings_model.dart @@ -43,6 +43,7 @@ class RatingsModel extends _$RatingsModel { snapRevision: snap.revision, rating: rating, voteStatus: _getUserVote(snap.revision, votes), + snapName: snapName, ); cacheFile.writeRatingsDataSync(ratingsData); @@ -60,6 +61,7 @@ class RatingsModel extends _$RatingsModel { snapRevision: ratingsData.snapRevision, voteUp: voteUp, dateTime: clock.now(), + snapName: ratingsData.snapName, ); await _ratings.vote(vote); state = AsyncData(ratingsData.copyWith(voteStatus: voteStatus)); diff --git a/packages/app_center/lib/snapd/snapd_service.dart b/packages/app_center/lib/snapd/snapd_service.dart index f4f5afa26..49a42e737 100644 --- a/packages/app_center/lib/snapd/snapd_service.dart +++ b/packages/app_center/lib/snapd/snapd_service.dart @@ -1,34 +1,8 @@ import 'package:app_center/snapd/snapd_cache.dart'; import 'package:app_center/snapd/snapd_watcher.dart'; -import 'package:collection/collection.dart'; import 'package:snapd/snapd.dart'; class SnapdService extends SnapdClient with SnapdCache, SnapdWatcher { Future waitChange(String changeId) => watchChange(changeId).firstWhere((change) => change.ready); - - Future findById(String snapId) async { - final queryParams = { - 'series': '16', - 'remote': 'true', - 'snap-id': snapId, - }; - Map result; - try { - result = await getAssertions( - assertion: 'snap-declaration', - params: queryParams, - ); - } on Exception catch (_) { - return null; - } - - if (result.isEmpty) { - return null; - } - final declaration = SnapDeclaration.fromJson(result); - final findResult = await find(name: declaration.snapName); - return findResult - .singleWhereOrNull((element) => element.id == declaration.snapId); - } } diff --git a/packages/app_center/test/app_card_test.dart b/packages/app_center/test/app_card_test.dart index f81ae0d4a..6d626aa34 100644 --- a/packages/app_center/test/app_card_test.dart +++ b/packages/app_center/test/app_card_test.dart @@ -7,10 +7,12 @@ import 'package:ubuntu_service/ubuntu_service.dart'; import 'test_utils.dart'; const snapId = 'r4LxMVp7zWramXsJQAKdamxy6TAWlaDD'; +const snapName = 'signal-desktop'; const snapRating = Rating( snapId: snapId, totalVotes: 123, ratingsBand: RatingsBand.good, + snapName: snapName, ); final snap = createSnap( diff --git a/packages/app_center/test/games_page_test.dart b/packages/app_center/test/games_page_test.dart index 40fe95508..16d133e24 100644 --- a/packages/app_center/test/games_page_test.dart +++ b/packages/app_center/test/games_page_test.dart @@ -11,10 +11,12 @@ import 'package:ubuntu_service/ubuntu_service.dart'; import 'test_utils.dart'; const snapId = 'r4LxMVp7zWramXsJQAKdamxy6TAWlaDD'; +const snapName = 'signal-desktop'; const snapRating = Rating( snapId: snapId, totalVotes: 123, ratingsBand: RatingsBand.good, + snapName: snapName, ); void main() { diff --git a/packages/app_center/test/ratings_model_test.dart b/packages/app_center/test/ratings_model_test.dart index e51d6ca40..9a994ba28 100644 --- a/packages/app_center/test/ratings_model_test.dart +++ b/packages/app_center/test/ratings_model_test.dart @@ -10,7 +10,7 @@ import 'test_utils.dart'; void main() { final snap = createSnap( name: 'firefox', - id: 'firefox', + id: '1234', revision: 42, ); @@ -18,16 +18,18 @@ void main() { registerMockSnapdService(storeSnap: snap); registerMockRatingsService( rating: const Rating( - snapId: 'firefox', + snapId: '1234', totalVotes: 1337, ratingsBand: RatingsBand.veryGood, + snapName: 'firefox', ), snapVotes: [ Vote( - snapId: 'firefox', + snapId: '1234', snapRevision: 42, voteUp: true, dateTime: DateTime(1970), + snapName: 'firefox', ), ], ); @@ -43,9 +45,10 @@ void main() { ratingsData.rating, equals( const Rating( - snapId: 'firefox', + snapId: '1234', totalVotes: 1337, ratingsBand: RatingsBand.veryGood, + snapName: 'firefox', ), ), ); @@ -66,9 +69,10 @@ void main() { mockService.vote( Vote( dateTime: DateTime(1984), - snapId: 'firefox', + snapId: '1234', snapRevision: 42, voteUp: false, + snapName: 'firefox', ), ), ).called(1); diff --git a/packages/app_center/test/ratings_service_test.dart b/packages/app_center/test/ratings_service_test.dart index b90310a47..37dc57a01 100644 --- a/packages/app_center/test/ratings_service_test.dart +++ b/packages/app_center/test/ratings_service_test.dart @@ -11,9 +11,10 @@ void main() { final mockClient = createMockRatingsClient( token: 'jwt', rating: const Rating( - snapId: 'firefox', + snapId: '1234', totalVotes: 1337, ratingsBand: RatingsBand.veryGood, + snapName: 'firefox', ), ); final service = RatingsService(mockClient, id: 'myId'); @@ -24,9 +25,10 @@ void main() { rating, equals( const Rating( - snapId: 'firefox', + snapId: '1234', totalVotes: 1337, ratingsBand: RatingsBand.veryGood, + snapName: 'firefox', ), ), ); @@ -39,17 +41,19 @@ void main() { ChartData( rawRating: 117, rating: Rating( - snapId: 'john', + snapId: '4321', totalVotes: 117, ratingsBand: RatingsBand.veryGood, + snapName: 'john', ), ), ChartData( rawRating: 104, rating: Rating( - snapId: 'fred', + snapId: '5678', totalVotes: 104, ratingsBand: RatingsBand.veryGood, + snapName: 'fred', ), ), ], @@ -64,17 +68,19 @@ void main() { const ChartData( rawRating: 117, rating: Rating( - snapId: 'john', + snapId: '4321', totalVotes: 117, ratingsBand: RatingsBand.veryGood, + snapName: 'john', ), ), const ChartData( rawRating: 104, rating: Rating( - snapId: 'fred', + snapId: '5678', totalVotes: 104, ratingsBand: RatingsBand.veryGood, + snapName: 'fred', ), ), ]), @@ -87,14 +93,15 @@ void main() { await service.vote( Vote( - snapId: 'thunderbird', + snapId: '7890', snapRevision: 42, voteUp: true, dateTime: DateTime(1970), + snapName: 'thunderbird', ), ); verify(mockClient.authenticate('myId')).called(1); - verify(mockClient.vote('thunderbird', 42, true, 'jwt')).called(1); + verify(mockClient.vote('7890', 42, true, 'jwt')).called(1); }); test('delete', () async { @@ -111,26 +118,28 @@ void main() { token: 'jwt', snapVotes: [ Vote( - snapId: 'testsnap2', + snapId: '1111', snapRevision: 2, voteUp: true, dateTime: DateTime(1999), + snapName: 'testSnap2', ), ], ); final service = RatingsService(mockClient, id: 'myId'); - final votes = await service.getSnapVotes('testsnap2'); + final votes = await service.getSnapVotes('testSnap2'); verify(mockClient.authenticate('myId')).called(1); expect( votes, equals( [ Vote( - snapId: 'testsnap2', + snapId: '1111', snapRevision: 2, voteUp: true, dateTime: DateTime(1999), + snapName: 'testSnap2', ), ], ), diff --git a/packages/app_center/test/search_page_test.dart b/packages/app_center/test/search_page_test.dart index 175b6f761..363d41ffa 100644 --- a/packages/app_center/test/search_page_test.dart +++ b/packages/app_center/test/search_page_test.dart @@ -14,10 +14,12 @@ import 'test_utils.dart'; import 'test_utils.mocks.dart'; const snapId = 'r4LxMVp7zWramXsJQAKdamxy6TAWlaDD'; +const snapName = 'signal-desktop'; const snapRating = Rating( snapId: snapId, totalVotes: 123, ratingsBand: RatingsBand.good, + snapName: snapName, ); void main() { diff --git a/packages/app_center/test/snap_page_test.dart b/packages/app_center/test/snap_page_test.dart index 55ccb470f..bf1d5d8e5 100644 --- a/packages/app_center/test/snap_page_test.dart +++ b/packages/app_center/test/snap_page_test.dart @@ -17,10 +17,12 @@ import 'test_utils.dart'; import 'test_utils.mocks.dart'; const snapId = 'r4LxMVp7zWramXsJQAKdamxy6TAWlaDD'; +const snapName = 'signal-desktop'; const snapRating = Rating( snapId: snapId, totalVotes: 123, ratingsBand: RatingsBand.good, + snapName: snapName, ); final localSnap = createSnap( diff --git a/packages/app_center/test/test_utils.dart b/packages/app_center/test/test_utils.dart index 1b7a0bea3..0c2c2e1f7 100644 --- a/packages/app_center/test/test_utils.dart +++ b/packages/app_center/test/test_utils.dart @@ -304,6 +304,7 @@ MockRatingsService registerMockRatingsService({ snapId: '', totalVotes: 0, ratingsBand: RatingsBand.insufficientVotes, + snapName: '', ), ); when(service.getSnapVotes(any)).thenAnswer((_) async => snapVotes ?? []); @@ -328,6 +329,7 @@ MockRatingsClient createMockRatingsClient({ snapId: '', totalVotes: 0, ratingsBand: RatingsBand.insufficientVotes, + snapName: '', ), ); when(client.getSnapVotes(any, any)).thenAnswer((_) async => snapVotes ?? []); diff --git a/packages/app_center_ratings_client/lib/src/ratings.dart b/packages/app_center_ratings_client/lib/src/ratings.dart index dc71c3097..8a0e19aec 100644 --- a/packages/app_center_ratings_client/lib/src/ratings.dart +++ b/packages/app_center_ratings_client/lib/src/ratings.dart @@ -13,6 +13,7 @@ class Rating with _$Rating { required String snapId, required int totalVotes, required RatingsBand ratingsBand, + required String snapName, }) = _Rating; factory Rating.fromJson(Map json) => _$RatingFromJson(json); @@ -33,6 +34,7 @@ extension RatingFromDTO on pb.Rating { snapId: snapId, totalVotes: totalVotes.toInt(), ratingsBand: ratingsBand.fromDTO(), + snapName: snapName, ); } } diff --git a/packages/app_center_ratings_client/lib/src/vote.dart b/packages/app_center_ratings_client/lib/src/vote.dart index b878ce29d..53ac1512f 100644 --- a/packages/app_center_ratings_client/lib/src/vote.dart +++ b/packages/app_center_ratings_client/lib/src/vote.dart @@ -11,6 +11,7 @@ class Vote with _$Vote { required int snapRevision, required bool voteUp, required DateTime dateTime, + required String snapName, }) = _Vote; } @@ -21,6 +22,7 @@ extension VoteFromDTO on pb.Vote { snapRevision: snapRevision, voteUp: voteUp, dateTime: timestamp.toDateTime(), + snapName: snapName, ); } } diff --git a/packages/app_center_ratings_client/test/ratings_client_test.dart b/packages/app_center_ratings_client/test/ratings_client_test.dart index 8ac4ea5d6..58cdc32f0 100644 --- a/packages/app_center_ratings_client/test/ratings_client_test.dart +++ b/packages/app_center_ratings_client/test/ratings_client_test.dart @@ -33,6 +33,7 @@ void main() { const snapId = 'foobar'; const token = 'bar'; const timeframe = chart.Timeframe.month; + const snapName = 'foobarName'; final pbChartList = [ pb_chart.ChartData( rawRating: 3, @@ -40,6 +41,7 @@ void main() { snapId: snapId, totalVotes: Int64(105), ratingsBand: RatingsBand.NEUTRAL, + snapName: snapName, ), ), ]; @@ -51,6 +53,7 @@ void main() { snapId: snapId, totalVotes: 105, ratingsBand: ratings.RatingsBand.neutral, + snapName: snapName, ), ), ]; @@ -98,15 +101,18 @@ void main() { test('get rating', () async { const snapId = 'foo'; const token = 'bar'; + const snapName = 'fooName'; final pbRating = Rating( snapId: snapId, totalVotes: Int64(105), ratingsBand: RatingsBand.NEUTRAL, + snapName: snapName, ); const expectedResponse = ratings.Rating( snapId: snapId, totalVotes: 105, ratingsBand: ratings.RatingsBand.neutral, + snapName: snapName, ); final mockResponse = pb.GetRatingResponse(rating: pbRating); final request = pb.GetRatingRequest(snapId: snapId); @@ -198,8 +204,9 @@ void main() { }); test('user votes by snap id', () async { - const snapId = 'foo'; + const snapId = '123'; const token = 'bar'; + const snapName = 'foo'; final time = DateTime.now().toUtc(); final mockVotes = [ Vote( @@ -207,12 +214,14 @@ void main() { snapRevision: 1, voteUp: true, timestamp: Timestamp.fromDateTime(time), + snapName: snapName, ), Vote( snapId: snapId, snapRevision: 2, voteUp: false, timestamp: Timestamp.fromDateTime(time), + snapName: snapName, ), ]; final expectedResponse = [ @@ -221,12 +230,14 @@ void main() { snapRevision: 1, voteUp: true, dateTime: time, + snapName: snapName, ), user.Vote( snapId: snapId, snapRevision: 2, voteUp: false, dateTime: time, + snapName: snapName, ), ]; final mockResponse = GetSnapVotesResponse(votes: mockVotes);