diff options
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() |
