From 6d0ae40c2b246626e952c279af6c9c17035079b6 Mon Sep 17 00:00:00 2001 From: Napster Date: Sun, 14 Jan 2024 13:45:57 +0100 Subject: [PATCH] Konvert AutoOuter & DiscordActivityListener to Kotlin --- .../domain/setup/lastactive/AutoOuter.java | 77 ------------------- .../domain/setup/lastactive/AutoOuter.kt | 68 ++++++++++++++++ .../lastactive/DiscordActivityListener.java | 52 ------------- .../lastactive/DiscordActivityListener.kt | 47 +++++++++++ 4 files changed, 115 insertions(+), 129 deletions(-) delete mode 100644 src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.java create mode 100644 src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.kt delete mode 100644 src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.java create mode 100644 src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.kt diff --git a/src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.java b/src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.java deleted file mode 100644 index c3f33762..00000000 --- a/src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.java +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2016-2023 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 . - */ - -package space.npstr.wolfia.domain.setup.lastactive; - -import io.lettuce.core.pubsub.RedisPubSubAdapter; -import java.util.Optional; -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 {@link ActivityService} and takes action - *

- * Since we could lose the redis connection, or restart, or , - * a "manual" check when starting the game should still be performed. - */ -@Component -public class AutoOuter extends RedisPubSubAdapter { - - private static final String EXPIRE_CHANNEL = "__keyevent@*__:expired"; - - private final RedisKeyParser redisKeyParser = new RedisKeyParser(); - - private final GameSetupService gameSetupService; - private final ShardManager shardManager; - - public AutoOuter(Redis redis, GameSetupService gameSetupService, ShardManager shardManager) { - this.gameSetupService = gameSetupService; - this.shardManager = shardManager; - - var pubSub = redis.getPubSub(); - pubSub.addListener(this); - pubSub.sync().psubscribe(EXPIRE_CHANNEL); - } - - @Override - public void message(String channel, String message) { - if (EXPIRE_CHANNEL.equals(channel)) { - this.expired(message); - } - } - - @Override - public void message(String pattern, String channel, String message) { - if (EXPIRE_CHANNEL.equals(pattern) || EXPIRE_CHANNEL.equals(channel)) { - this.expired(message); - } - } - - private void expired(String key) { - Optional parsed = Optional.ofNullable(redisKeyParser.fromKey(key)); - if (parsed.isEmpty()) { - return; - } - outUser(parsed.get()); - } - - private void outUser(long userId) { - this.gameSetupService.outUserDueToInactivity(userId, this.shardManager); - } -} diff --git a/src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.kt b/src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.kt new file mode 100644 index 00000000..187b21b6 --- /dev/null +++ b/src/main/java/space/npstr/wolfia/domain/setup/lastactive/AutoOuter.kt @@ -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 . + */ +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 , + * a "manual" check when starting the game should still be performed. + */ +@Component +class AutoOuter( + redis: Redis, + private val gameSetupService: GameSetupService, + private val shardManager: ShardManager, +) : RedisPubSubAdapter() { + + 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) + } +} diff --git a/src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.java b/src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.java deleted file mode 100644 index 524568bb..00000000 --- a/src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (C) 2016-2023 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 . - */ - -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 -public class DiscordActivityListener { - - private final ActivityService service; - - public DiscordActivityListener(ActivityService service) { - this.service = service; - } - - @Order(Ordered.HIGHEST_PRECEDENCE) - @EventListener - public void onUserTyping(UserTypingEvent event) { - active(event.getUser()); - } - - @Order(Ordered.HIGHEST_PRECEDENCE) - @EventListener - public void onMessageReceived(MessageReceivedEvent event) { - active(event.getAuthor()); - } - - private void active(User user) { - this.service.recordActivity(user); - } -} diff --git a/src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.kt b/src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.kt new file mode 100644 index 00000000..91e47c0e --- /dev/null +++ b/src/main/java/space/npstr/wolfia/domain/setup/lastactive/DiscordActivityListener.kt @@ -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 . + */ +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) + } +}