From: TheSaminator Date: Sun, 20 Feb 2022 18:23:12 +0000 (-0500) Subject: Add embed data X-Git-Url: https://gitweb.starshipfights.net/?a=commitdiff_plain;h=7583f9cb04f945fe8c540d60e9a0c6a49cdd8fdc;p=starship-fights Add embed data --- diff --git a/src/jvmMain/kotlin/starshipfights/auth/providers.kt b/src/jvmMain/kotlin/starshipfights/auth/providers.kt index 56ded5a..b3599a1 100644 --- a/src/jvmMain/kotlin/starshipfights/auth/providers.kt +++ b/src/jvmMain/kotlin/starshipfights/auth/providers.kt @@ -438,9 +438,9 @@ object TestAuthProvider : AuthProvider { val errorMsg = call.request.queryParameters["error"] - call.respondHtml(HttpStatusCode.OK, page("Authentication Test", call.standardNavBar(), CustomSidebar { + call.respondHtml(HttpStatusCode.OK, call.page("Authentication Test", call.standardNavBar(), CustomSidebar { p { - +"This instance does not have Discord OAuth login set up. As a fallback, this authentication mode is used for testing." + +"This instance does not have Discord OAuth login set up. As a fallback, this authentication mode is used for testing only." } }) { section { @@ -516,7 +516,7 @@ class ProductionAuthProvider(private val discordLogin: DiscordLogin) : AuthProvi get("/login") { val errorMsg = call.request.queryParameters["error"] - call.respondHtml(HttpStatusCode.OK, page("Login with Discord", call.standardNavBar(), null) { + call.respondHtml(HttpStatusCode.OK, call.page("Login with Discord", call.standardNavBar()) { section { p { style = "text-align:center" diff --git a/src/jvmMain/kotlin/starshipfights/info/html_metadata.kt b/src/jvmMain/kotlin/starshipfights/info/html_metadata.kt new file mode 100644 index 0000000..2d96c7d --- /dev/null +++ b/src/jvmMain/kotlin/starshipfights/info/html_metadata.kt @@ -0,0 +1,47 @@ +package starshipfights.info + +import kotlinx.html.HEAD + +data class PageMetadata( + val title: String, + val description: String, + val type: PageMetadataType, +) { + companion object { + val default = PageMetadata( + title = "Starship Fights", + description = "Starship Fights is a space fleet battle game. Choose your allegiance, create your admiral, build up your fleet, and destroy your enemies' fleets with it!", + type = PageMetadataType.Website, + ) + } +} + +sealed class PageMetadataType { + object Website : PageMetadataType() + + data class Profile( + val name: String, + val isFemale: Boolean?, + ) : PageMetadataType() +} + +fun HEAD.metadata(pageMetadata: PageMetadata, url: String) { + when (pageMetadata.type) { + is PageMetadataType.Profile -> { + metaOG("og:type", "profile") + metaOG("og:profile:username", pageMetadata.type.name) + pageMetadata.type.isFemale?.let { + metaOG("og:profile:gender", if (it) "female" else "male") + } + } + PageMetadataType.Website -> { + metaOG("og:type", "website") + } + } + + metaOG("og:title", pageMetadata.title) + metaOG("og:description", pageMetadata.description) + metaOG("og:site_name", "Starship Fights") + metaOG("og:url", url) + metaOG("og:image", "https://starshipfights.net/static/images/embed-logo.png") +} diff --git a/src/jvmMain/kotlin/starshipfights/info/html_utils.kt b/src/jvmMain/kotlin/starshipfights/info/html_utils.kt index 881ff9e..17f6d6f 100644 --- a/src/jvmMain/kotlin/starshipfights/info/html_utils.kt +++ b/src/jvmMain/kotlin/starshipfights/info/html_utils.kt @@ -1,8 +1,6 @@ package starshipfights.info -import kotlinx.html.A -import kotlinx.html.FORM -import kotlinx.html.hiddenInput +import kotlinx.html.* import starshipfights.auth.CsrfProtector import starshipfights.data.Id import starshipfights.data.auth.UserSession @@ -24,3 +22,17 @@ fun FORM.csrfToken(cookie: Id) = hiddenInput { name = CsrfProtector.csrfInputName value = CsrfProtector.newNonce(cookie, this@csrfToken.action) } + +var META.property: String? + get() = attributes["property"] + set(value) { + if (value != null) + attributes["property"] = value + else + attributes.remove("property") + } + +fun HEAD.metaOG(property: String, content: String) = meta { + this.property = property + this.content = content +} diff --git a/src/jvmMain/kotlin/starshipfights/info/view_tpl.kt b/src/jvmMain/kotlin/starshipfights/info/view_tpl.kt index 8ebbd3b..aed4f31 100644 --- a/src/jvmMain/kotlin/starshipfights/info/view_tpl.kt +++ b/src/jvmMain/kotlin/starshipfights/info/view_tpl.kt @@ -1,11 +1,15 @@ package starshipfights.info +import io.ktor.application.* +import io.ktor.util.* import kotlinx.html.* -fun page(pageTitle: String?, navBar: List?, sidebar: Sidebar?, content: MAIN.() -> Unit): HTML.() -> Unit = { +fun ApplicationCall.page(pageTitle: String? = null, navBar: List? = null, sidebar: Sidebar? = null, pageData: PageMetadata = PageMetadata.default, content: MAIN.() -> Unit): HTML.() -> Unit = { head { meta(charset = "utf-8") + metadata(pageData, url()) + link(rel = "icon", type = "image/svg+xml", href = "/static/images/icon.svg") link(rel = "preconnect", href = "https://fonts.googleapis.com") link(rel = "preconnect", href = "https://fonts.gstatic.com") { attributes["crossorigin"] = "anonymous" } diff --git a/src/jvmMain/kotlin/starshipfights/info/views_error.kt b/src/jvmMain/kotlin/starshipfights/info/views_error.kt index 3625d5f..da89eeb 100644 --- a/src/jvmMain/kotlin/starshipfights/info/views_error.kt +++ b/src/jvmMain/kotlin/starshipfights/info/views_error.kt @@ -16,7 +16,7 @@ private fun MAIN.devModeCallId(callId: String?) { } } -suspend fun ApplicationCall.error400(): HTML.() -> Unit = page("Bad Request", standardNavBar(), IndexSidebar) { +suspend fun ApplicationCall.error400(): HTML.() -> Unit = page("Bad Request", standardNavBar()) { section { h1 { +"Bad Request" } p { +"The request your browser sent was improperly formatted." } @@ -24,7 +24,7 @@ suspend fun ApplicationCall.error400(): HTML.() -> Unit = page("Bad Request", st devModeCallId(callId) } -suspend fun ApplicationCall.error403(): HTML.() -> Unit = page("Not Allowed", standardNavBar(), IndexSidebar) { +suspend fun ApplicationCall.error403(): HTML.() -> Unit = page("Not Allowed", standardNavBar()) { section { h1 { +"Not Allowed" } p { +"You are not allowed to do that." } @@ -32,7 +32,7 @@ suspend fun ApplicationCall.error403(): HTML.() -> Unit = page("Not Allowed", st devModeCallId(callId) } -suspend fun ApplicationCall.error404(): HTML.() -> Unit = page("Not Found", standardNavBar(), IndexSidebar) { +suspend fun ApplicationCall.error404(): HTML.() -> Unit = page("Not Found", standardNavBar()) { section { h1 { +"Not Found" } p { +"Unfortunately, we could not find what you were looking for." } @@ -40,7 +40,7 @@ suspend fun ApplicationCall.error404(): HTML.() -> Unit = page("Not Found", stan devModeCallId(callId) } -suspend fun ApplicationCall.error503(): HTML.() -> Unit = page("Internal Error", standardNavBar(), IndexSidebar) { +suspend fun ApplicationCall.error503(): HTML.() -> Unit = page("Internal Error", standardNavBar()) { section { h1 { +"Internal Error" } p { +"The servers made a bit of a mistake. Please be patient while we fix our mess." } diff --git a/src/jvmMain/kotlin/starshipfights/info/views_main.kt b/src/jvmMain/kotlin/starshipfights/info/views_main.kt index 69b9bee..e50e483 100644 --- a/src/jvmMain/kotlin/starshipfights/info/views_main.kt +++ b/src/jvmMain/kotlin/starshipfights/info/views_main.kt @@ -129,7 +129,7 @@ suspend fun ApplicationCall.aboutPage(): HTML.() -> Unit { suspend fun ApplicationCall.newUsersPage(): HTML.() -> Unit { val newUsers = User.sorted(descending(User::registeredAt)).take(20).toList() - return page("New Users", standardNavBar(), IndexSidebar) { + return page("New Users", standardNavBar()) { section { h1 { +"New Users" } div { diff --git a/src/jvmMain/kotlin/starshipfights/info/views_ships.kt b/src/jvmMain/kotlin/starshipfights/info/views_ships.kt index 5673464..44c89fb 100644 --- a/src/jvmMain/kotlin/starshipfights/info/views_ships.kt +++ b/src/jvmMain/kotlin/starshipfights/info/views_ships.kt @@ -71,7 +71,16 @@ suspend fun ApplicationCall.shipsPage(): HTML.() -> Unit = page("Game Manual", s } } -suspend fun ApplicationCall.shipPage(shipType: ShipType): HTML.() -> Unit = page(shipType.fullerDisplayName, standardNavBar(), ShipViewSidebar(shipType)) { +suspend fun ApplicationCall.shipPage(shipType: ShipType): HTML.() -> Unit = page( + shipType.fullerDisplayName, + standardNavBar(), + ShipViewSidebar(shipType), + PageMetadata( + shipType.displayName + "-class Starship", + "A ${shipType.weightClass.displayName} of the ${shipType.faction.navyName}", + PageMetadataType.Website + ) +) { section { h1 { +shipType.fullDisplayName } diff --git a/src/jvmMain/kotlin/starshipfights/info/views_user.kt b/src/jvmMain/kotlin/starshipfights/info/views_user.kt index b337c4a..57957c8 100644 --- a/src/jvmMain/kotlin/starshipfights/info/views_user.kt +++ b/src/jvmMain/kotlin/starshipfights/info/views_user.kt @@ -98,7 +98,15 @@ suspend fun ApplicationCall.userPage(): HTML.() -> Unit { } } }*/ - } + }, + PageMetadata( + user.profileName, + user.profileBio, + PageMetadataType.Profile( + user.profileName, + null + ) + ) ) { section { h1 { +user.profileName } @@ -442,6 +450,14 @@ suspend fun ApplicationCall.admiralPage(): HTML.() -> Unit { NavLink("/admiral/${admiral.id}/manage", "Manage Admiral") ) else emptyList() + ), + PageMetadata( + admiral.name, + "${admiral.rank.getDisplayName(admiral.faction)} of the ${admiral.faction.navyName}", + PageMetadataType.Profile( + admiral.name, + admiral.isFemale + ) ) ) { section { diff --git a/src/jvmMain/kotlin/starshipfights/server_conf.kt b/src/jvmMain/kotlin/starshipfights/server_conf.kt index 59a6230..d84356f 100644 --- a/src/jvmMain/kotlin/starshipfights/server_conf.kt +++ b/src/jvmMain/kotlin/starshipfights/server_conf.kt @@ -24,6 +24,7 @@ data class DiscordLogin( val redirectUrlOrigin: String, val clientId: String, + //val clientPubKey: String, val clientSecret: String, val ownerId: String, diff --git a/src/jvmMain/resources/static/images/embed-logo.png b/src/jvmMain/resources/static/images/embed-logo.png new file mode 100644 index 0000000..b81c35f Binary files /dev/null and b/src/jvmMain/resources/static/images/embed-logo.png differ