Skip to content

Commit

Permalink
Consolidate sitemap controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
finestructure committed Nov 9, 2023
1 parent d0a5b2f commit 0172a23
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 101 deletions.
8 changes: 4 additions & 4 deletions Sources/App/Controllers/PackageController+routes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,10 @@ enum PackageController {
[String]()
}

return try await SiteMapView.package(owner: packageResult.repository.owner,
repository: packageResult.repository.name,
lastActivityAt: packageResult.repository.lastActivityAt,
linkablePathUrls: urls)
return try await SiteMapController.package(owner: packageResult.repository.owner,
repository: packageResult.repository.name,
lastActivityAt: packageResult.repository.lastActivityAt,
linkablePathUrls: urls)
}

static func linkablePathUrls(client: Client, packageResult: PackageResult) async -> [String] {
Expand Down
82 changes: 80 additions & 2 deletions Sources/App/Controllers/SiteMapController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ import Plot

enum SiteMapController {

static var staticRoutes: [SiteURL] = [
.home,
.addAPackage,
.faq,
.supporters,
.buildMonitor,
.privacy
]

struct Package: Equatable, Decodable {
var owner: String
var repository: String
Expand All @@ -31,6 +40,9 @@ enum SiteMapController {
}
}

/// Generate a sitemap index page for a given request
/// - Parameter req: `Request`
/// - Returns: `SiteMapIndex`
static func index(req: Request) async throws -> SiteMapIndex {
guard let db = req.db as? SQLDatabase else {
fatalError("Database must be an SQLDatabase ('as? SQLDatabase' must succeed)")
Expand All @@ -46,10 +58,76 @@ enum SiteMapController {
.orderBy(Search.repoName)

let packages = try await query.all(decoding: Package.self)
return SiteMapView.index(packages: packages)
return index(packages: packages)
}

/// Generate a sitemap index page for a given list of packages
/// - Parameter packages: list of packages
/// - Returns: `SiteMapIndex`
static func index(packages: [SiteMapController.Package]) -> SiteMapIndex {
SiteMapIndex(
.sitemap(
.loc(SiteURL.siteMapStaticPages.absoluteURL()),
.lastmod(Current.date(), timeZone: .utc) // The home page updates every day.
),
.group(
packages.map { package -> Node<SiteMapIndex.SiteMapIndexContext> in
.sitemap(
.loc(SiteURL.package(.value(package.owner),
.value(package.repository),
.siteMap).absoluteURL()),
.unwrap(package.lastActivityAt, { .lastmod($0, timeZone: .utc) })
)
}
)
)
}

/// Generate a sitemap for a specific package
/// - Parameters:
/// - owner: package owner (author)
/// - repository: repository name
/// - lastActivityAt: last activity
/// - linkablePathUrls: list of linkable path urls
/// - Returns: `SiteMap`
static func package(owner: String?,
repository: String?,
lastActivityAt: Date?,
linkablePathUrls: [String]) async throws -> SiteMap {
guard let owner,
let repository
else {
// This should never happen, but we should return an empty
// sitemap instead of an incorrect one.
return SiteMap()
}

// See SwiftPackageIndex-Server#2485 for context on the performance implications of this
let lastmod: Node<SiteMap.URLContext> = lastActivityAt.map { .lastmod($0, timeZone: .utc) } ?? .empty

return SiteMap(
.url(
.loc(SiteURL.package(.value(owner),
.value(repository),
.none).absoluteURL()),
lastmod
),
.forEach(linkablePathUrls, { url in
.url(.loc(url), lastmod)
})
)
}

static func staticPages(req: Request) async throws -> SiteMap {
return SiteMapView.staticPages()
SiteMap(
.group(
staticRoutes.map { page -> Node<SiteMap.URLSetContext> in
.url(
.loc(page.absoluteURL())
)
}
)
)
}

}
87 changes: 0 additions & 87 deletions Sources/App/Views/SiteMap/SiteMapView.swift

This file was deleted.

16 changes: 8 additions & 8 deletions Tests/AppTests/SitemapTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,10 @@ class SitemapTests: SnapshotTestCase {
.query(on: app.db, owner: "owner", repository: "repo0")

// MUT
let sitemap = try await SiteMapView.package(owner: packageResult.repository.owner,
repository: packageResult.repository.name,
lastActivityAt: packageResult.repository.lastActivityAt,
linkablePathUrls: [])
let sitemap = try await SiteMapController.package(owner: packageResult.repository.owner,
repository: packageResult.repository.name,
lastActivityAt: packageResult.repository.lastActivityAt,
linkablePathUrls: [])
let xml = sitemap.render(indentedBy: .spaces(2))

// Validation
Expand All @@ -178,10 +178,10 @@ class SitemapTests: SnapshotTestCase {
]

// MUT
let sitemap = try await SiteMapView.package(owner: packageResult.repository.owner,
repository: packageResult.repository.name,
lastActivityAt: packageResult.repository.lastActivityAt,
linkablePathUrls: linkablePathUrls)
let sitemap = try await SiteMapController.package(owner: packageResult.repository.owner,
repository: packageResult.repository.name,
lastActivityAt: packageResult.repository.lastActivityAt,
linkablePathUrls: linkablePathUrls)
let xml = sitemap.render(indentedBy: .spaces(2))

// Validation
Expand Down

0 comments on commit 0172a23

Please sign in to comment.