Skip to content

Extend my likes list with last published information about the package. #8872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions app/lib/frontend/handlers/account.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import 'package:_pub_shared/data/account_api.dart';
import 'package:clock/clock.dart';
import 'package:pub_dev/frontend/handlers/cache_control.dart';
import 'package:shelf/shelf.dart' as shelf;

import '../../account/backend.dart';
Expand All @@ -15,6 +14,7 @@ import '../../account/session_cookie.dart' as session_cookie;
import '../../audit/backend.dart';
import '../../frontend/request_context.dart';
import '../../package/backend.dart';
import '../../package/name_tracker.dart';
import '../../publisher/backend.dart';
import '../../publisher/models.dart';
import '../../scorecard/backend.dart';
Expand All @@ -26,6 +26,9 @@ import '../../shared/urls.dart' as urls;
import '../templates/admin.dart';
import '../templates/consent.dart';
import '../templates/misc.dart' show renderUnauthenticatedPage;
import '../templates/views/pkg/liked_package_list.dart';

import 'cache_control.dart';

/// Handles requests for /authorized
shelf.Response authorizedHandler(_) => htmlResponse(renderAuthorizedPage());
Expand Down Expand Up @@ -295,10 +298,18 @@ Future<shelf.Response> accountMyLikedPackagesPageHandler(
final user = (await accountBackend
.lookupUserById(requestContext.authenticatedUserId!))!;
final likes = await likeBackend.listPackageLikes(user);
// extending the like data with cached last-published timestamp
final combined = likes
.map((d) => LikeAndPackageData(
package: d.package!,
likeCreated: d.created!,
lastPublished: nameTracker.getPackage(d.package!)?.lastPublished,
))
.toList();
final html = renderMyLikedPackagesPage(
user: user,
userSessionData: requestContext.sessionData!,
likes: likes,
likes: combined,
);
return htmlResponse(html);
}
Expand Down
4 changes: 2 additions & 2 deletions app/lib/frontend/templates/admin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import 'package:_pub_shared/data/page_data.dart';

import '../../account/models.dart' show LikeData, User, SessionData;
import '../../account/models.dart' show User, SessionData;
import '../../audit/models.dart';
import '../../frontend/templates/views/account/activity_log_table.dart';
import '../../package/models.dart';
Expand Down Expand Up @@ -96,7 +96,7 @@ String renderAccountPackagesPage({
String renderMyLikedPackagesPage({
required User user,
required SessionData userSessionData,
required List<LikeData> likes,
required List<LikeAndPackageData> likes,
}) {
final resultCount = likes.isNotEmpty
? d.p(
Expand Down
27 changes: 22 additions & 5 deletions app/lib/frontend/templates/views/pkg/liked_package_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@
// 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 '../../../../account/models.dart';
import '../../../../shared/urls.dart' as urls;

import '../../../dom/dom.dart' as d;
import '../../../dom/material.dart' as material;
import '../../../static_files.dart' show staticUrls;

/// Describes the combined like data that will be rendered.
class LikeAndPackageData {
final String package;
final DateTime likeCreated;
final DateTime? lastPublished;

LikeAndPackageData({
required this.package,
required this.likeCreated,
required this.lastPublished,
});
}

/// Renders the package list of /my-liked-packages page.
d.Node likedPackageListNode(List<LikeData> likes) {
d.Node likedPackageListNode(List<LikeAndPackageData> likes) {
final thumbUpOutlinedUrl =
staticUrls.getAssetUrl('/static/img/thumb-up-24px.svg');
final thumbUpFilledUrl =
Expand All @@ -27,7 +39,7 @@ d.Node likedPackageListNode(List<LikeData> likes) {
d.h3(
classes: ['packages-title'],
child: d.a(
href: urls.pkgPageUrl(like.package!),
href: urls.pkgPageUrl(like.package),
text: like.package,
),
),
Expand All @@ -37,7 +49,7 @@ d.Node likedPackageListNode(List<LikeData> likes) {
unelevated: true,
customTypeClass: '-pub-like-button',
attributes: {
'data-package': like.package!,
'data-package': like.package,
'data-thumb_up_outlined': thumbUpOutlinedUrl,
'data-thumb_up_filled': thumbUpFilledUrl,
},
Expand All @@ -56,7 +68,12 @@ d.Node likedPackageListNode(List<LikeData> likes) {
classes: ['packages-metadata'],
children: [
d.text(' Liked '),
d.xAgoTimestamp(like.created!, datePrefix: 'on'),
d.xAgoTimestamp(like.likeCreated, datePrefix: 'on'),
if (like.lastPublished != null) ...[
d.br(),
d.text(' Published '),
d.xAgoTimestamp(like.lastPublished!, datePrefix: 'on'),
],
],
),
],
Expand Down
10 changes: 10 additions & 0 deletions app/lib/package/name_tracker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,16 @@ class NameTracker {
/// Whether the name tracker has a record of the package.
bool hasPackage(String name) => _data._hasPackage(name);

/// Returns the tracked data of a package, or `null` if [name] is
/// not a visible package.
TrackedPackage? getPackage(String name) {
final p = _data._packages[name];
if (p != null && p.isVisible) {
return p;
}
return null;
}

/// Scans the Datastore and populates the tracker.
@visibleForTesting
Future<void> reloadFromDatastore() async {
Expand Down
6 changes: 6 additions & 0 deletions app/test/frontend/golden/my_liked_packages.html
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@ <h3 class="packages-title">
<p class="packages-metadata">
Liked
<a class="-x-ago" href="" title="on %%liked1-date%%" role="button" data-timestamp="%%millis%%">%%x-ago%%</a>
<br/>
Published
<a class="-x-ago" href="" title="on %%published1-date%%" role="button" data-timestamp="%%millis%%">%%x-ago%%</a>
</p>
</div>
<div class="packages-item">
Expand All @@ -214,6 +217,9 @@ <h3 class="packages-title">
<p class="packages-metadata">
Liked
<a class="-x-ago" href="" title="on %%liked1-date%%" role="button" data-timestamp="%%millis%%">%%x-ago%%</a>
<br/>
Published
<a class="-x-ago" href="" title="on %%published2-date%%" role="button" data-timestamp="%%millis%%">%%x-ago%%</a>
</p>
</div>
</div>
Expand Down
18 changes: 15 additions & 3 deletions app/test/frontend/templates_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:_pub_shared/validation/html/html_validation.dart';
import 'package:clock/clock.dart';
import 'package:html/parser.dart';
import 'package:pub_dev/account/backend.dart';
import 'package:pub_dev/account/models.dart';
import 'package:pub_dev/admin/models.dart';
import 'package:pub_dev/audit/backend.dart';
import 'package:pub_dev/audit/models.dart';
Expand All @@ -28,6 +27,7 @@ import 'package:pub_dev/frontend/templates/package.dart';
import 'package:pub_dev/frontend/templates/package_admin.dart';
import 'package:pub_dev/frontend/templates/publisher.dart';
import 'package:pub_dev/frontend/templates/report.dart';
import 'package:pub_dev/frontend/templates/views/pkg/liked_package_list.dart';
import 'package:pub_dev/frontend/templates/views/pkg/score_tab.dart';
import 'package:pub_dev/package/backend.dart';
import 'package:pub_dev/package/models.dart';
Expand Down Expand Up @@ -701,19 +701,31 @@ void main() {
final authenticatedUser = await requireAuthenticatedWebUser();
final user = authenticatedUser.user;
final liked1 = DateTime.fromMillisecondsSinceEpoch(1574423824000);
final published1 = liked1.add(Duration(days: 10));
final liked2 = DateTime.fromMillisecondsSinceEpoch(1574423824000);
final published2 = liked2.subtract(Duration(days: 10));
final html = renderMyLikedPackagesPage(
user: user,
userSessionData: requestContext.sessionData!,
likes: [
LikeData(package: 'super_package', created: liked1),
LikeData(package: 'another_package', created: liked2)
LikeAndPackageData(
package: 'super_package',
likeCreated: liked1,
lastPublished: published1,
),
LikeAndPackageData(
package: 'another_package',
likeCreated: liked2,
lastPublished: published2,
),
],
);
expectGoldenFile(html, 'my_liked_packages.html', timestamps: {
'user-created': user.created,
'liked1': liked1,
'published1': published1,
'liked2': liked2,
'published2': published2,
});
});
});
Expand Down