Skip to content

Commit

Permalink
Limit user ability to leave server and still trigger contact
Browse files Browse the repository at this point in the history
  • Loading branch information
Willdotwhite committed Jun 16, 2024
1 parent b121712 commit fb21c88
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 29 deletions.
5 changes: 3 additions & 2 deletions api/src/main/kotlin/com/gmtkgamejam/bot/DiscordBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import org.slf4j.LoggerFactory
import kotlin.jvm.optionals.getOrElse

class DiscordBot {

private val logger: Logger = LoggerFactory.getLogger(javaClass)

private lateinit var api: DiscordApi
Expand Down Expand Up @@ -59,8 +58,10 @@ class DiscordBot {
val dmChannel = recipient.privateChannel.getOrElse { recipient.openPrivateChannel().get() }

val messageSendAttempt = if (messageBuilder.canBuildEmbedFromUser(sender)) {
logger.info("[CONTACT] [EMBED] ${sender.name} ($senderUserId) contacted ${recipient.name} ($recipientUserId)")
dmChannel.sendMessage(messageBuilder.embedMessage(recipient, sender))
} else {
logger.info("[CONTACT] [BASIC] ${sender.name} ($senderUserId) contacted ${recipient.name} ($recipientUserId)")
dmChannel.sendMessage(messageBuilder.basicMessage(recipient, sender))
}

Expand All @@ -78,6 +79,7 @@ class DiscordBot {
private suspend fun createFallbackChannelPingMessage(recipient: User, sender: User) {
val messageContents = "Hey ${recipient.mentionTag}, ${sender.mentionTag} wants to get in contact about your Team Finder post!"
// TODO: Validate message actually sent, give error otherwise
logger.info("[CONTACT] [PING] ${sender.name} (${sender.id}) contacted ${recipient.name} (${recipient.id})")
channel.sendMessage(messageContents).await()
}

Expand Down Expand Up @@ -137,5 +139,4 @@ class DiscordBot {
baseUserName
}
}

}
27 changes: 0 additions & 27 deletions api/src/main/kotlin/com/gmtkgamejam/routing/UserInfoRoutes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,14 @@ import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.coroutines.launch
import org.koin.ktor.ext.inject
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.time.LocalDateTime
import java.util.*

typealias UserId = String

fun Application.configureUserInfoRouting() {

val logger: Logger = LoggerFactory.getLogger(javaClass)

val bot: DiscordBot by inject()

val analyticsService = AnalyticsService()
val service = AuthService()

val shortLiveCache: MutableMap<UserId, Pair<LocalDateTime, UserInfo>> = mutableMapOf()

routing {
authenticate("auth-jwt") { // These routes go through the authentication middleware defined in Auth.kt
get("/hello") {
Expand All @@ -45,27 +35,11 @@ fun Application.configureUserInfoRouting() {
}

get("/userinfo") {
val currentTime = LocalDateTime.now()
val principal = call.principal<JWTPrincipal>()
val id = principal?.payload?.getClaim("id")?.asString()

service.getTokenSet(id!!)?.let {
val tokenSet = it

// Very short TTL cache to avoid unnecessary traffic for quick turnaround behaviour
// We don't expect the cache to expire during regular traffic
if (shortLiveCache.containsKey(tokenSet.discordId)) {
val (cacheSetTime, userInfo) = shortLiveCache[tokenSet.discordId]!!
shortLiveCache.remove(tokenSet.discordId)

// If the cache set was less than 5 minutes ago, don't hit discord again
if (currentTime < cacheSetTime.plusMinutes(5L)) {
// Refresh cache entry
shortLiveCache[userInfo.id] = Pair(LocalDateTime.now(), userInfo)
return@get call.respond(userInfo)
}
}

var accessToken = tokenSet.accessToken

// If access token has expired, try a dirty inline refresh
Expand Down Expand Up @@ -94,7 +68,6 @@ fun Application.configureUserInfoRouting() {
val isUserInGuild = bot.isUserInGuild(user.id)

val userInfo = UserInfo(user, displayName, isUserInGuild, hasPermissions)
shortLiveCache[user.id] = Pair(LocalDateTime.now(), userInfo)
return@get call.respond(userInfo)
}

Expand Down

0 comments on commit fb21c88

Please sign in to comment.