Skip to content

Commit 0432a04

Browse files
committed
Only display max package count disclaimer when there's over 300 packages
1 parent c33da0b commit 0432a04

File tree

5 files changed

+24
-16
lines changed

5 files changed

+24
-16
lines changed

Sources/App/Controllers/KeywordController.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,24 @@ enum KeywordController {
6060
throw Abort(.notFound)
6161
}
6262
let query = try req.query.decode(Query.self)
63-
let page = try await Self.query(on: req.db, keyword: keyword, page: query.page, pageSize: query.pageSize)
6463

65-
guard !page.results.isEmpty else {
64+
async let page = Self.query(on: req.db, keyword: keyword, page: query.page, pageSize: query.pageSize)
65+
async let weightedKeywords = WeightedKeyword.query(on: req.db, keywords: [keyword])
66+
67+
let (pageResult, weightedKeywordsResult) = try await (page, weightedKeywords)
68+
69+
guard !pageResult.results.isEmpty else {
6670
throw Abort(.notFound)
6771
}
6872

69-
let packageInfo = page.results.compactMap(PackageInfo.init(package:))
73+
let packageInfo = pageResult.results.compactMap(PackageInfo.init(package:))
7074

7175
let model = KeywordShow.Model(
7276
keyword: keyword,
7377
packages: packageInfo,
7478
page: query.page,
75-
hasMoreResults: page.hasMoreResults
79+
hasMoreResults: pageResult.hasMoreResults,
80+
totalPackageCount: weightedKeywordsResult.weight(for: keyword)
7681
)
7782

7883
return KeywordShow.View(path: req.url.path, model: model).document()

Sources/App/Views/Keyword/KeywordShow+Model.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ extension KeywordShow {
1818
var packages: [PackageInfo]
1919
var page: Int
2020
var hasMoreResults: Bool
21+
let totalPackageCount: Int
2122

2223
internal init(keyword: String,
2324
packages: [PackageInfo],
2425
page: Int,
25-
hasMoreResults: Bool) {
26+
hasMoreResults: Bool,
27+
totalPackageCount: Int) {
2628
self.keyword = keyword
2729
self.packages = packages
2830
self.page = page
2931
self.hasMoreResults = hasMoreResults
32+
self.totalPackageCount = totalPackageCount
3033
}
3134
}
3235
}

Sources/App/Views/Keyword/KeywordShow+View.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,7 @@ enum KeywordShow {
5959
.copyableInputForm(buttonName: "Copy Package Collection URL",
6060
eventName: "Copy Package Collection URL Button",
6161
valueToCopy: SiteURL.packageCollectionKeyword(.value(model.keyword)).absoluteURL()),
62-
.p(
63-
.h6(
64-
.i("Note: Package collections are limited to a maximum of \(Constants.maxKeywordPackageCollectionCount) packages. If a keyword has more than \(Constants.maxKeywordPackageCollectionCount) packages, only the top \(Constants.maxKeywordPackageCollectionCount) packages will be included based on their package score.")
65-
)
66-
),
62+
showPackageLimitDisclaimer(),
6763
.hr(.class("minor")),
6864
.ul(
6965
.id("package-list"),
@@ -83,6 +79,15 @@ enum KeywordShow {
8379
)
8480
)
8581
}
82+
83+
private func showPackageLimitDisclaimer() -> Node<HTML.BodyContext> {
84+
guard model.totalPackageCount >= Constants.maxKeywordPackageCollectionCount else { return .empty }
85+
return .p(
86+
.h6(
87+
.i("Note: Package collections are limited to a maximum of \(Constants.maxKeywordPackageCollectionCount) packages. If a keyword has more than \(Constants.maxKeywordPackageCollectionCount) packages, only the top \(Constants.maxKeywordPackageCollectionCount) packages will be included based on their package score.")
88+
)
89+
)
90+
}
8691
}
8792
}
8893

Tests/AppTests/Mocks/KeywordShow+mock.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ extension KeywordShow.Model {
2929
lastActivityAt: .t0,
3030
hasDocs: true
3131
) }
32-
return .init(keyword: "networking", packages: packages, page: 1, hasMoreResults: false)
32+
return .init(keyword: "networking", packages: packages, page: 1, hasMoreResults: false, totalPackageCount: 10)
3333
}
3434
}

Tests/AppTests/__Snapshots__/WebpageSnapshotTests/test_KeywordShow.1.html

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,6 @@ <h2 class="trimmed">Packages for keyword “networking”</h2>
8989
<form class="copyable-input">
9090
<input type="text" data-button-name="Copy Package Collection URL" data-event-name="Copy Package Collection URL Button" value="http://localhost:8080/keywords/networking/collection.json" readonly/>
9191
</form>
92-
<p>
93-
<h6>
94-
<i>Note: Package collections are limited to a maximum of 300 packages. If a keyword has more than 300 packages, only the top 300 packages will be included based on their package score.</i>
95-
</h6>
96-
</p>
9792
<hr class="minor"/>
9893
<ul id="package-list">
9994
<li>

0 commit comments

Comments
 (0)