-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Konvert AutoOuter & DiscordActivityListener to Kotlin
- Loading branch information
1 parent
98032a8
commit 6d0ae40
Showing
4 changed files
with
115 additions
and
129 deletions.
There are no files selected for viewing
77 changes: 0 additions & 77 deletions
77
src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.java
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.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,68 @@ | ||
/* | ||
* Copyright (C) 2016-2024 the original author or authors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package space.npstr.wolfia.domain.setup.lastactive | ||
|
||
import io.lettuce.core.pubsub.RedisPubSubAdapter | ||
import net.dv8tion.jda.api.sharding.ShardManager | ||
import org.springframework.stereotype.Component | ||
import space.npstr.wolfia.domain.setup.GameSetupService | ||
import space.npstr.wolfia.system.redis.Redis | ||
|
||
/** | ||
* This component listens the keys expiring from [ActivityService] and takes action | ||
* | ||
* | ||
* Since we could lose the redis connection, or restart, or <insert any other reason to miss expiry events>, | ||
* a "manual" check when starting the game should still be performed. | ||
</insert> */ | ||
@Component | ||
class AutoOuter( | ||
redis: Redis, | ||
private val gameSetupService: GameSetupService, | ||
private val shardManager: ShardManager, | ||
) : RedisPubSubAdapter<String, String>() { | ||
|
||
private val expireChannel = "__keyevent@*__:expired" | ||
private val redisKeyParser = RedisKeyParser() | ||
|
||
init { | ||
redis.pubSub.let { | ||
it.addListener(this) | ||
it.sync().psubscribe(expireChannel) | ||
} | ||
} | ||
|
||
override fun message(channel: String, message: String) { | ||
if (expireChannel == channel) { | ||
this.expired(message) | ||
} | ||
} | ||
|
||
override fun message(pattern: String, channel: String, message: String) { | ||
if (expireChannel == pattern || expireChannel == channel) { | ||
this.expired(message) | ||
} | ||
} | ||
|
||
private fun expired(key: String) { | ||
redisKeyParser.fromKey(key)?.let { outUser(it) } | ||
} | ||
|
||
private fun outUser(userId: Long) { | ||
gameSetupService.outUserDueToInactivity(userId, this.shardManager) | ||
} | ||
} |
52 changes: 0 additions & 52 deletions
52
src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.java
This file was deleted.
Oops, something went wrong.
47 changes: 47 additions & 0 deletions
47
src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.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,47 @@ | ||
/* | ||
* Copyright (C) 2016-2024 the original author or authors | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package space.npstr.wolfia.domain.setup.lastactive | ||
|
||
import net.dv8tion.jda.api.entities.User | ||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent | ||
import net.dv8tion.jda.api.events.user.UserTypingEvent | ||
import org.springframework.context.event.EventListener | ||
import org.springframework.core.Ordered | ||
import org.springframework.core.annotation.Order | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class DiscordActivityListener( | ||
private val service: ActivityService | ||
) { | ||
|
||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@EventListener | ||
fun onUserTyping(event: UserTypingEvent) { | ||
active(event.user) | ||
} | ||
|
||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
@EventListener | ||
fun onMessageReceived(event: MessageReceivedEvent) { | ||
active(event.author) | ||
} | ||
|
||
private fun active(user: User) { | ||
service.recordActivity(user) | ||
} | ||
} |