summaryrefslogtreecommitdiff
path: root/platform-paper/src
diff options
context:
space:
mode:
authorSho Sakuma <me@m1sk9.dev>2026-03-18 13:53:48 +0900
committerSho Sakuma <me@m1sk9.dev>2026-03-18 13:58:07 +0900
commita048f9c9f558778a7abaab4187b98702f65c3319 (patch)
treea68deb9d5e69535ef088ff08f0af273266abbc90 /platform-paper/src
parent2be902e6858d59bf02aa72a6702e55e6b371fbc9 (diff)
downloadLunaticChat-a048f9c9f558778a7abaab4187b98702f65c3319.tar.gz
LunaticChat-a048f9c9f558778a7abaab4187b98702f65c3319.tar.bz2
LunaticChat-a048f9c9f558778a7abaab4187b98702f65c3319.zip
fix: Remove state active channels
Diffstat (limited to 'platform-paper/src')
-rw-r--r--platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManager.kt10
-rw-r--r--platform-paper/src/test/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManagerTest.kt38
2 files changed, 46 insertions, 2 deletions
diff --git a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManager.kt b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManager.kt
index 7e28872..7305266 100644
--- a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManager.kt
+++ b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManager.kt
@@ -436,8 +436,14 @@ class ChannelManager(
*/
fun getPlayerChannelContext(playerId: UUID): ChannelContext? {
val channelId = activeChannels[playerId] ?: return null
- val channel = channelsCache[channelId] ?: return null
- val members = membersCache[channelId]?.toList() ?: return null
+ val channel = channelsCache[channelId]
+ val members = membersCache[channelId]?.toList()
+
+ if (channel == null || members == null) {
+ activeChannels.remove(playerId)
+ saveToStorage()
+ return null
+ }
return ChannelContext(
channelId = channelId,
diff --git a/platform-paper/src/test/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManagerTest.kt b/platform-paper/src/test/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManagerTest.kt
index 239da52..d5ae373 100644
--- a/platform-paper/src/test/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManagerTest.kt
+++ b/platform-paper/src/test/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelManagerTest.kt
@@ -503,6 +503,44 @@ class ChannelManagerTest {
}
@Test
+ fun `getPlayerChannelContext should clear stale active channel when channel is deleted`() {
+ val (manager, storage, _) = createManager()
+ manager.initialize()
+
+ val ownerId = createTestUUID(1)
+ manager.createChannel(createTestChannel(id = "stale-ch", name = "Stale", ownerId = ownerId))
+ assertNotNull(manager.getPlayerChannelContext(ownerId))
+
+ // Delete the channel, which clears activeChannels for affected players
+ manager.deleteChannel("stale-ch", ownerId)
+
+ // Active channel should be cleared
+ assertNull(manager.getPlayerChannel(ownerId))
+ assertNull(manager.getPlayerChannelContext(ownerId))
+ }
+
+ @Test
+ fun `getPlayerChannelContext should clear stale active channel pointing to nonexistent channel`() {
+ // Simulate a stale activeChannels entry pointing to a channel that no longer exists
+ val playerId = createTestUUID(1)
+ val data =
+ ChannelData(
+ activeChannels = mapOf(playerId.toString() to "deleted-ch"),
+ )
+
+ val (manager, storage, _) = createManager(initialData = data)
+ manager.initialize()
+
+ // activeChannels has an entry but the channel doesn't exist
+ assertEquals("deleted-ch", manager.getPlayerChannel(playerId))
+
+ // getPlayerChannelContext should detect the stale entry and clean it up
+ assertNull(manager.getPlayerChannelContext(playerId))
+ assertNull(manager.getPlayerChannel(playerId))
+ verify { storage.queueAsyncSave(any()) }
+ }
+
+ @Test
fun `restorePlayerChannel should restore from membership when no active channel`() {
val (manager, _, _) = createManager()
manager.initialize()