Skip to content

Commit

Permalink
More tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
Willdotwhite committed Apr 17, 2024
1 parent 67e5859 commit 30dd264
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 40 deletions.
24 changes: 24 additions & 0 deletions api/src/main/kotlin/com/gmtkgamejam/ApplicationCallExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.gmtkgamejam

import com.auth0.jwt.JWT
import com.gmtkgamejam.models.auth.AuthTokenSet
import com.gmtkgamejam.services.AuthService
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.request.*
import io.ktor.server.response.*

fun ApplicationCall.getAuthTokenSet(authService: AuthService): AuthTokenSet? {
return this.request.header("Authorization")
?.substring(7)
?.let { JWT.decode(it) }?.getClaim("id")?.asString()
?.let { authService.getTokenSet(it) }
}

suspend fun ApplicationCall.respondJSON(text: String, status: HttpStatusCode? = null) {
if (status != null) {
response.status(status)
}

respond(mapOf("message" to text))
}

This file was deleted.

7 changes: 2 additions & 5 deletions api/src/main/kotlin/com/gmtkgamejam/routing/InfraRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package com.gmtkgamejam.routing

import com.gmtkgamejam.models.posts.PostItem
import com.gmtkgamejam.respondJSON
import com.gmtkgamejam.search.OpenSearch
import com.gmtkgamejam.search.OpensearchClusterConfigurer
import com.gmtkgamejam.search.SearchItem
import com.gmtkgamejam.services.PostService
import io.ktor.http.*
import io.ktor.server.application.*
Expand All @@ -20,9 +18,8 @@ fun Application.configureInfraRouting() {
route("/infra") {
route("/se") {
get("/reset") {
OpensearchClusterConfigurer.initCluster()
// TODO: Bulk uploading!
postService.getPosts(PostItem::deletedAt eq null).map { OpenSearch.index(SearchItem(it)) }
val posts = postService.getPosts(PostItem::deletedAt eq null)
OpensearchClusterConfigurer.initCluster(posts)
call.respondJSON("Search engine reset complete", HttpStatusCode.OK)
}
}
Expand Down
26 changes: 8 additions & 18 deletions api/src/main/kotlin/com/gmtkgamejam/routing/PostRoutes.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.gmtkgamejam.routing

import com.auth0.jwt.JWT
import com.gmtkgamejam.getAuthTokenSet
import com.gmtkgamejam.models.posts.PostItem
import com.gmtkgamejam.models.posts.dtos.PostItemCreateDto
import com.gmtkgamejam.models.posts.dtos.PostItemReportDto
Expand Down Expand Up @@ -37,13 +37,9 @@ fun Application.configurePostRouting() {
val posts = service.getPostsByOrderedIds(postIds)

// Set isFavourite on posts for this user if they're logged in
call.request.header("Authorization")?.substring(7)
?.let { JWT.decode(it) }?.getClaim("id")?.asString()
?.let { authService.getTokenSet(it) }
call.getAuthTokenSet(authService)
?.let { favouritesService.getFavouritesByUserId(it.discordId) }
?.let { favouritesList ->
posts.map { it.isFavourite = favouritesList.postIds.contains(it.id) }
}
?.let { favouritesList -> posts.map { it.isFavourite = favouritesList.postIds.contains(it.id) } }

call.respond(posts)
}
Expand All @@ -55,13 +51,9 @@ fun Application.configurePostRouting() {
}

// Set isFavourite on posts for this user if they're logged in
call.request.header("Authorization")?.substring(7)
?.let { JWT.decode(it) }?.getClaim("id")?.asString()
?.let { authService.getTokenSet(it) }
call.getAuthTokenSet(authService)
?.let { favouritesService.getFavouritesByUserId(it.discordId) }
?.let { favouritesList ->
post?.isFavourite = favouritesList.postIds.contains(post?.id)
}
?.let { favouritesList -> post?.isFavourite = favouritesList.postIds.contains(post?.id) }

post?.let { return@get call.respond(it) }
call.respondJSON("Post not found", status = HttpStatusCode.NotFound)
Expand Down Expand Up @@ -155,11 +147,9 @@ fun Application.configurePostRouting() {
delete {
authService.getTokenSet(call)
?.let { service.getPostByAuthorId(it.discordId) }
?.let {
service.deletePost(it)
OpenSearch.delete(it.id)
return@delete call.respondJSON("Post deleted", status = HttpStatusCode.OK)
}
?.also { service.deletePost(it) }
?.also { OpenSearch.delete(it.id) }
?.let { return@delete call.respondJSON("Post deleted", status = HttpStatusCode.OK) }

// TODO: Replace BadRequest with contextual response
call.respondJSON("Could not delete Post", status = HttpStatusCode.BadRequest)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gmtkgamejam.search

import com.gmtkgamejam.models.posts.PostItem
import org.apache.hc.core5.http.HttpHost
import org.opensearch.client.json.jackson.JacksonJsonpMapper
import org.opensearch.client.opensearch.OpenSearchClient
Expand Down Expand Up @@ -29,8 +30,9 @@ object OpensearchClusterConfigurer {
*
* Call to set up/refresh the cluster's `posts` index - all data will be lost
*/
fun initCluster() {
fun initCluster(posts: List<PostItem>) {
initIndices()
backfillPosts(posts)
}

private fun initIndices() {
Expand All @@ -44,4 +46,8 @@ object OpensearchClusterConfigurer {
client.indices().create(createPostsIndex)
}

private fun backfillPosts(posts: List<PostItem>) {
posts.map { OpenSearch.index(SearchItem(it)) }
}

}

0 comments on commit 30dd264

Please sign in to comment.