-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
17 changed files
with
191 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
adevspoon-domain/src/main/kotlin/com/adevspoon/domain/common/annotation/AdminMessageType.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.adevspoon.domain.common.annotation | ||
|
||
enum class AdminMessageType { | ||
REPORT | ||
} |
7 changes: 7 additions & 0 deletions
7
...n-domain/src/main/kotlin/com/adevspoon/domain/common/annotation/AdminNotificationEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.adevspoon.domain.common.annotation | ||
|
||
@Target(AnnotationTarget.FUNCTION) | ||
@Retention(AnnotationRetention.RUNTIME) | ||
annotation class AdminNotificationEvent( | ||
val type: AdminMessageType | ||
) |
39 changes: 39 additions & 0 deletions
39
adevspoon-domain/src/main/kotlin/com/adevspoon/domain/common/aop/AdminNotificationAdvisor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.adevspoon.domain.common.aop | ||
|
||
import com.adevspoon.domain.common.annotation.AdminMessageType | ||
import com.adevspoon.domain.common.annotation.AdminNotificationEvent | ||
import com.adevspoon.domain.common.event.ReportEvent | ||
import com.adevspoon.domain.common.exception.ReportEventInvalidReturnException | ||
import org.aspectj.lang.JoinPoint | ||
import org.aspectj.lang.annotation.AfterReturning | ||
import org.aspectj.lang.annotation.Aspect | ||
import org.aspectj.lang.reflect.MethodSignature | ||
import org.springframework.context.ApplicationEventPublisher | ||
import org.springframework.stereotype.Component | ||
|
||
|
||
@Aspect | ||
@Component | ||
class AdminNotificationAdvisor ( | ||
private val eventPublisher: ApplicationEventPublisher | ||
){ | ||
|
||
@AfterReturning(pointcut = "@annotation(com.adevspoon.domain.common.annotation.AdminNotificationEvent)", returning = "result") | ||
fun afterReturningAdvice(joinPoint: JoinPoint, result: Any?) { | ||
val notificationAnnotation = getAnnotation(joinPoint) | ||
when (notificationAnnotation.type) { | ||
AdminMessageType.REPORT -> bugReportEventPublish(result) | ||
} | ||
} | ||
|
||
private fun getAnnotation(joinPoint: JoinPoint) = | ||
(joinPoint.signature as MethodSignature).method | ||
.getAnnotation(AdminNotificationEvent::class.java) | ||
|
||
private fun bugReportEventPublish(result: Any?) { | ||
(result as? ReportEvent) | ||
?.let { | ||
eventPublisher.publishEvent(it) | ||
} ?: throw ReportEventInvalidReturnException() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
adevspoon-domain/src/main/kotlin/com/adevspoon/domain/common/event/ReportEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.adevspoon.domain.common.event | ||
|
||
import com.adevspoon.domain.common.entity.ReportEntity | ||
|
||
data class ReportEvent( | ||
val content: String, | ||
val report: ReportEntity | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
...on-domain/src/main/kotlin/com/adevspoon/domain/common/service/AdminNotificationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.adevspoon.domain.common.service | ||
|
||
import com.adevspoon.domain.common.annotation.DomainService | ||
import com.adevspoon.domain.common.event.ReportEvent | ||
import org.springframework.scheduling.annotation.Async | ||
import org.springframework.transaction.annotation.Propagation | ||
import org.springframework.transaction.annotation.Transactional | ||
import org.springframework.transaction.event.TransactionPhase | ||
import org.springframework.transaction.event.TransactionalEventListener | ||
|
||
@DomainService | ||
interface AdminNotificationService { | ||
@Async | ||
@TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT) | ||
@Transactional(propagation = Propagation.REQUIRES_NEW) | ||
fun sendReportNotification(event: ReportEvent) | ||
} |
53 changes: 53 additions & 0 deletions
53
...on-domain/src/main/kotlin/com/adevspoon/domain/common/service/SlackNotificationService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.adevspoon.domain.common.service | ||
|
||
import com.adevspoon.domain.common.event.ReportEvent | ||
import com.slack.api.Slack | ||
import com.slack.api.model.Attachment | ||
import com.slack.api.model.Field | ||
import com.slack.api.webhook.WebhookPayloads | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import java.io.IOException | ||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
|
||
class SlackNotificationService: AdminNotificationService{ | ||
|
||
@Value("\${slack.webhook.url}") | ||
private lateinit var SLACK_WEBHOOK_URL: String | ||
private val logger = LoggerFactory.getLogger(this.javaClass) | ||
|
||
override fun sendReportNotification(event: ReportEvent) { | ||
val slack = Slack.getInstance() | ||
try { | ||
slack.send(SLACK_WEBHOOK_URL, WebhookPayloads.payload { p -> | ||
p.text("*New Bug Report* :warning:") | ||
.attachments(listOf(generateSlackAttachment(event))) | ||
}) | ||
} catch (e: IOException) { | ||
logger.warn("IOException occurred when sending message to Slack", e) | ||
} | ||
} | ||
|
||
private fun generateSlackAttachment(event: ReportEvent): Attachment { | ||
val requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now()) | ||
val contentId = event.report.getContentId() | ||
return Attachment.builder() | ||
.color("fa8128") | ||
.title("$requestTime 발생 내용") | ||
.fields(listOf( | ||
generateSlackField("Report reason", event.report.reason.toString()), | ||
generateSlackField("Report Type", event.report.postType), | ||
generateSlackField("Target", "id: $contentId\ncontent: ${event.content}"), | ||
generateSlackField("Reported by", "id: ${event.report.user.id} nickname: ${event.report.user.nickname}"))) | ||
.build() | ||
} | ||
|
||
private fun generateSlackField(title: String, value: String): Field { | ||
return Field.builder() | ||
.title(title) | ||
.value(value) | ||
.valueShortEnough(false) | ||
.build() | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
adevspoon-domain/src/main/kotlin/com/adevspoon/domain/config/AdminNotificationConfig.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.adevspoon.domain.config | ||
|
||
import com.adevspoon.domain.common.service.AdminNotificationService | ||
import com.adevspoon.domain.common.service.SlackNotificationService | ||
import org.springframework.context.annotation.Bean | ||
import org.springframework.context.annotation.Configuration | ||
|
||
@Configuration | ||
class AdminNotificationConfig { | ||
|
||
@Bean | ||
fun notificationService(): AdminNotificationService { | ||
return SlackNotificationService() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters