Skip to content

Commit

Permalink
feat: use snap_name in Vote and Rating
Browse files Browse the repository at this point in the history
  • Loading branch information
d-loose committed Dec 13, 2024
1 parent 2440d3a commit c18afd1
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 44 deletions.
3 changes: 2 additions & 1 deletion packages/app_center/lib/ratings/rated_category_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions packages/app_center/lib/ratings/ratings_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> json) =>
Expand Down
2 changes: 2 additions & 0 deletions packages/app_center/lib/ratings/ratings_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class RatingsModel extends _$RatingsModel {
snapRevision: snap.revision,
rating: rating,
voteStatus: _getUserVote(snap.revision, votes),
snapName: snapName,
);

cacheFile.writeRatingsDataSync(ratingsData);
Expand All @@ -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));
Expand Down
26 changes: 0 additions & 26 deletions packages/app_center/lib/snapd/snapd_service.dart
Original file line number Diff line number Diff line change
@@ -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<void> waitChange(String changeId) =>
watchChange(changeId).firstWhere((change) => change.ready);

Future<Snap?> findById(String snapId) async {
final queryParams = {
'series': '16',
'remote': 'true',
'snap-id': snapId,
};
Map<String, dynamic> 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);
}
}
2 changes: 2 additions & 0 deletions packages/app_center/test/app_card_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions packages/app_center/test/games_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
14 changes: 9 additions & 5 deletions packages/app_center/test/ratings_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,26 @@ import 'test_utils.dart';
void main() {
final snap = createSnap(
name: 'firefox',
id: 'firefox',
id: '1234',
revision: 42,
);

setUp(() {
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',
),
],
);
Expand All @@ -43,9 +45,10 @@ void main() {
ratingsData.rating,
equals(
const Rating(
snapId: 'firefox',
snapId: '1234',
totalVotes: 1337,
ratingsBand: RatingsBand.veryGood,
snapName: 'firefox',
),
),
);
Expand All @@ -66,9 +69,10 @@ void main() {
mockService.vote(
Vote(
dateTime: DateTime(1984),
snapId: 'firefox',
snapId: '1234',
snapRevision: 42,
voteUp: false,
snapName: 'firefox',
),
),
).called(1);
Expand Down
31 changes: 20 additions & 11 deletions packages/app_center/test/ratings_service_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -24,9 +25,10 @@ void main() {
rating,
equals(
const Rating(
snapId: 'firefox',
snapId: '1234',
totalVotes: 1337,
ratingsBand: RatingsBand.veryGood,
snapName: 'firefox',
),
),
);
Expand All @@ -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',
),
),
],
Expand All @@ -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',
),
),
]),
Expand All @@ -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 {
Expand All @@ -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',
),
],
),
Expand Down
2 changes: 2 additions & 0 deletions packages/app_center/test/search_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions packages/app_center/test/snap_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions packages/app_center/test/test_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ MockRatingsService registerMockRatingsService({
snapId: '',
totalVotes: 0,
ratingsBand: RatingsBand.insufficientVotes,
snapName: '',
),
);
when(service.getSnapVotes(any)).thenAnswer((_) async => snapVotes ?? []);
Expand All @@ -328,6 +329,7 @@ MockRatingsClient createMockRatingsClient({
snapId: '',
totalVotes: 0,
ratingsBand: RatingsBand.insufficientVotes,
snapName: '',
),
);
when(client.getSnapVotes(any, any)).thenAnswer((_) async => snapVotes ?? []);
Expand Down
2 changes: 2 additions & 0 deletions packages/app_center_ratings_client/lib/src/ratings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> json) => _$RatingFromJson(json);
Expand All @@ -33,6 +34,7 @@ extension RatingFromDTO on pb.Rating {
snapId: snapId,
totalVotes: totalVotes.toInt(),
ratingsBand: ratingsBand.fromDTO(),
snapName: snapName,
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/app_center_ratings_client/lib/src/vote.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Vote with _$Vote {
required int snapRevision,
required bool voteUp,
required DateTime dateTime,
required String snapName,
}) = _Vote;
}

Expand All @@ -21,6 +22,7 @@ extension VoteFromDTO on pb.Vote {
snapRevision: snapRevision,
voteUp: voteUp,
dateTime: timestamp.toDateTime(),
snapName: snapName,
);
}
}
Loading

0 comments on commit c18afd1

Please sign in to comment.