Skip to content

Commit afaa2c5

Browse files
authored
release: 1.5.2 (#296)
2 parents c74e2cf + 3fa5f10 commit afaa2c5

File tree

10 files changed

+180
-86
lines changed

10 files changed

+180
-86
lines changed

โ€Žsrc/main/kotlin/org/gitanimals/rank/app/GivePointToGuildFacade.ktโ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class GivePointToGuildFacade(
5151

5252
private fun givePointToLeader(guild: GuildApi.GuildResponse, point: Int) {
5353
runCatching {
54-
identityApi.increaseUserPointsByUserId(
54+
identityApi.increaseUserPointsByUsername(
5555
username = guild.leader.name,
5656
point = point,
5757
idempotencyKey = IdGenerator.generate().toString(),
@@ -64,7 +64,7 @@ class GivePointToGuildFacade(
6464
private fun givePointToMembers(guild: GuildApi.GuildResponse, point: Int) {
6565
guild.members.forEach { member ->
6666
runCatching {
67-
identityApi.increaseUserPointsByUserId(
67+
identityApi.increaseUserPointsByUsername(
6868
member.name,
6969
point = point,
7070
idempotencyKey = IdGenerator.generate().toString(),

โ€Žsrc/main/kotlin/org/gitanimals/rank/app/GivePointToUsersFacade.ktโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class GivePointToUsersFacade(
3636
else -> 0
3737
}
3838

39-
identityApi.increaseUserPointsByUserId(
39+
identityApi.increaseUserPointsByUsername(
4040
username = rankResponse.name,
4141
point = point,
4242
idempotencyKey = IdGenerator.generate().toString(),

โ€Žsrc/main/kotlin/org/gitanimals/rank/app/IdentityApi.ktโ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface IdentityApi {
1111
fun getUserByName(@PathVariable("name") name: String): UserResponse
1212

1313
@PostExchange("/internals/users/points/increases/by-username/{username}")
14-
fun increaseUserPointsByUserId(
14+
fun increaseUserPointsByUsername(
1515
@PathVariable("username") username: String,
1616
@RequestParam("point") point: Int,
1717
@RequestParam("idempotency-key") idempotencyKey: String,

โ€Žsrc/main/kotlin/org/gitanimals/render/app/AnimationFacade.ktโ€Ž

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import org.gitanimals.render.domain.UserService
66
import org.gitanimals.render.domain.event.NewUserCreated
77
import org.gitanimals.render.domain.event.Visited
88
import org.rooftop.netx.api.SagaManager
9+
import org.springframework.context.ApplicationEventPublisher
910
import org.springframework.stereotype.Service
1011
import org.springframework.web.client.RestClientException
1112

@@ -14,13 +15,14 @@ class AnimationFacade(
1415
private val userService: UserService,
1516
private val contributionApi: ContributionApi,
1617
private val sagaManager: SagaManager,
18+
private val eventPublisher: ApplicationEventPublisher,
1719
) {
1820

1921
fun getFarmAnimation(username: String): String {
2022
return when (userService.existsByName(username)) {
2123
true -> {
2224
val svgAnimation = userService.getFarmAnimationByUsername(username)
23-
sagaManager.startSync(Visited(username))
25+
eventPublisher.publishEvent(Visited(username))
2426
svgAnimation
2527
}
2628

@@ -36,7 +38,7 @@ class AnimationFacade(
3638
return when (userService.existsByName(username)) {
3739
true -> {
3840
val svgAnimation = userService.getLineAnimationByUsername(username, personaId, mode)
39-
sagaManager.startSync(Visited(username))
41+
eventPublisher.publishEvent(Visited(username))
4042
svgAnimation
4143
}
4244

โ€Žsrc/main/kotlin/org/gitanimals/render/app/IdentityApi.ktโ€Ž

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
package org.gitanimals.render.app
22

3-
fun interface IdentityApi {
3+
import org.springframework.http.HttpHeaders
4+
import org.springframework.web.bind.annotation.PathVariable
5+
import org.springframework.web.bind.annotation.RequestHeader
6+
import org.springframework.web.bind.annotation.RequestParam
7+
import org.springframework.web.service.annotation.GetExchange
8+
import org.springframework.web.service.annotation.PostExchange
49

5-
fun getUserByToken(token: String): UserResponse
10+
interface IdentityApi {
11+
12+
@GetExchange("/users")
13+
fun getUserByToken(@RequestHeader(HttpHeaders.AUTHORIZATION) token: String): UserResponse
14+
15+
@PostExchange("/internals/users/points/increases/by-username/{username}")
16+
fun increaseUserPointsByUsername(
17+
@PathVariable("username") username: String,
18+
@RequestParam("point") point: Int,
19+
@RequestParam("idempotency-key") idempotencyKey: String,
20+
)
621

722
data class UserResponse(
823
val id: String,
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.gitanimals.render.infra
2+
3+
import org.gitanimals.core.AuthorizationException
4+
import org.springframework.http.HttpStatus
5+
import org.springframework.http.client.ClientHttpResponse
6+
import org.springframework.web.client.ResponseErrorHandler
7+
8+
9+
class HttpClientErrorHandler : ResponseErrorHandler {
10+
11+
override fun hasError(response: ClientHttpResponse): Boolean {
12+
return response.statusCode.isError
13+
}
14+
15+
override fun handleError(response: ClientHttpResponse) {
16+
val body = response.body.bufferedReader().use { it.readText() }
17+
when {
18+
response.statusCode.isSameCodeAs(HttpStatus.UNAUTHORIZED) ->
19+
throw AuthorizationException(body)
20+
21+
response.statusCode.is4xxClientError ->
22+
throw IllegalArgumentException(body)
23+
24+
response.statusCode.is5xxServerError ->
25+
throw IllegalStateException(body)
26+
}
27+
}
28+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.gitanimals.render.infra
2+
3+
import org.gitanimals.core.filter.MDCFilter
4+
import org.gitanimals.rank.infra.HttpClientErrorHandler
5+
import org.gitanimals.render.app.IdentityApi
6+
import org.slf4j.MDC
7+
import org.springframework.beans.factory.annotation.Value
8+
import org.springframework.context.annotation.Bean
9+
import org.springframework.context.annotation.Configuration
10+
import org.springframework.context.annotation.Profile
11+
import org.springframework.web.client.RestClient
12+
import org.springframework.web.client.support.RestClientAdapter
13+
import org.springframework.web.service.invoker.HttpServiceProxyFactory
14+
15+
@Configuration
16+
@Profile("!test")
17+
class RenderHttpClientConfigurer(
18+
@Value("\${internal.secret}") private val internalSecret: String,
19+
) {
20+
21+
@Bean
22+
fun renderIdentityApiHttpClient(): IdentityApi {
23+
val restClient = RestClient
24+
.builder()
25+
.requestInterceptor { request, body, execution ->
26+
request.headers.add(MDCFilter.TRACE_ID, MDC.get(MDCFilter.TRACE_ID))
27+
if (request.uri.path.startsWith("/internals")) {
28+
request.headers.add(INTERNAL_SECRET_KEY, internalSecret)
29+
}
30+
execution.execute(request, body)
31+
}
32+
.defaultStatusHandler(renderHttpClientErrorHandler())
33+
.baseUrl("https://api.gitanimals.org")
34+
.build()
35+
36+
val httpServiceProxyFactory = HttpServiceProxyFactory
37+
.builderFor(RestClientAdapter.create(restClient))
38+
.build()
39+
40+
return httpServiceProxyFactory.createClient(IdentityApi::class.java)
41+
}
42+
43+
@Bean
44+
fun renderHttpClientErrorHandler(): HttpClientErrorHandler = HttpClientErrorHandler()
45+
46+
private companion object {
47+
private const val INTERNAL_SECRET_KEY = "Internal-Secret"
48+
}
49+
}
50+
51+
52+
@Configuration
53+
@Profile("test")
54+
class RankTestHttpClientConfigurer {
55+
56+
@Bean
57+
fun rankIdentityApiHttpClient(): IdentityApi {
58+
val restClient = RestClient
59+
.builder()
60+
.defaultStatusHandler(renderHttpClientErrorHandler())
61+
.baseUrl("http://localhost:8080")
62+
.build()
63+
64+
val httpServiceProxyFactory = HttpServiceProxyFactory
65+
.builderFor(RestClientAdapter.create(restClient))
66+
.build()
67+
68+
return httpServiceProxyFactory.createClient(IdentityApi::class.java)
69+
}
70+
71+
@Bean
72+
fun renderHttpClientErrorHandler(): HttpClientErrorHandler = HttpClientErrorHandler()
73+
}

โ€Žsrc/main/kotlin/org/gitanimals/render/infra/RestIdentityApi.ktโ€Ž

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package org.gitanimals.render.infra
2+
3+
import org.gitanimals.core.IdGenerator
4+
import org.gitanimals.render.app.ContributionApi
5+
import org.gitanimals.render.app.IdentityApi
6+
import org.gitanimals.render.domain.UserService
7+
import org.gitanimals.render.domain.event.Visited
8+
import org.rooftop.netx.api.*
9+
import org.rooftop.netx.meta.SagaHandler
10+
import org.slf4j.LoggerFactory
11+
import org.springframework.context.event.EventListener
12+
import org.springframework.scheduling.annotation.Async
13+
import java.time.ZoneId
14+
import java.time.ZonedDateTime
15+
16+
@SagaHandler
17+
class VisitedEventListener(
18+
private val userService: UserService,
19+
private val contributionApi: ContributionApi,
20+
private val identityApi: IdentityApi,
21+
) {
22+
23+
private val logger = LoggerFactory.getLogger(this::class.simpleName)
24+
25+
@Async
26+
@EventListener(Visited::class)
27+
fun increaseUserVisited(visited: Visited) {
28+
runCatching {
29+
val username = visited.username
30+
userService.increaseVisit(username)
31+
32+
logger.info("Increase visit to user. username: \"$username\"")
33+
34+
if (!userService.isContributionUpdatedBeforeOneHour(username)) {
35+
return
36+
}
37+
38+
val currentYear = ZonedDateTime.now(ZoneId.of("UTC")).year
39+
val contribution =
40+
contributionApi.getContributionCount(username, listOf(currentYear))[currentYear]
41+
?: throw NullPointerException("Empty contribution current year \"$currentYear\"")
42+
43+
val increaseContributionCount = userService.updateContributions(username, contribution)
44+
identityApi.increaseUserPointsByUsername(
45+
username = username,
46+
point = increaseContributionCount * 100,
47+
idempotencyKey = IdGenerator.generate().toString(),
48+
)
49+
logger.info("Increase point to user. username: \"$username\", point:\"${increaseContributionCount * 100}\"")
50+
}.onFailure {
51+
logger.error("Cannot increase visit or point to user. username: \"${visited.username}\"", it)
52+
}
53+
}
54+
}

โ€Žsrc/main/kotlin/org/gitanimals/render/saga/VisitedSagaHandlers.ktโ€Ž

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
ย (0)