diff --git a/app/src/main/java/com/bitchat/android/nostr/GeohashActivity.kt b/app/src/main/java/com/bitchat/android/nostr/GeohashActivity.kt index 674974658..22fa95e36 100644 --- a/app/src/main/java/com/bitchat/android/nostr/GeohashActivity.kt +++ b/app/src/main/java/com/bitchat/android/nostr/GeohashActivity.kt @@ -72,7 +72,9 @@ class GeohashActivityAggregator( private class Channel(val latitude: Double, val longitude: Double) { /** pubkey -> newest activity of any kind. */ val lastSeen = HashMap() - /** event id -> message, for chat events only. */ + /** pubkey -> newest chat message, kept apart from [messages] so the cap never drops a writer. */ + val lastChat = HashMap() + /** event id -> message, for chat events only; capped, so it bounds the message count only. */ val messages = LinkedHashMap() var lastActiveAt = 0L } @@ -118,6 +120,7 @@ class GeohashActivityAggregator( changed = true } if (chatEventId != null && chatEventId !in channel.messages) { + if (at > (channel.lastChat[key] ?: Long.MIN_VALUE)) channel.lastChat[key] = at channel.messages[chatEventId] = Message(key, at) if (channel.messages.size > MAX_MESSAGES_PER_CHANNEL) { channel.messages.remove(channel.messages.keys.first()) @@ -136,9 +139,9 @@ class GeohashActivityAggregator( while (iterator.hasNext()) { val channel = iterator.next() if (channel.messages.values.removeAll { it.createdAt < nowMillis - chatWindowMillis }) changed = true - val chatters = channel.messages.values.mapTo(HashSet()) { it.pubkey } + if (channel.lastChat.values.removeAll { it < nowMillis - chatWindowMillis }) changed = true if (channel.lastSeen.entries.removeAll { (pubkey, seen) -> - seen < nowMillis - presenceWindowMillis && pubkey !in chatters + seen < nowMillis - presenceWindowMillis && pubkey !in channel.lastChat } ) changed = true if (channel.lastSeen.isEmpty() && channel.messages.isEmpty()) { @@ -190,10 +193,13 @@ class GeohashActivityAggregator( /** The rule itself: online in the presence window, or wrote in the chat window. */ private fun tally(channel: Channel, nowMillis: Long, exclude: (String) -> Boolean = { false }): Tally { val chatters = HashSet() + for ((pubkey, wrote) in channel.lastChat) { + if (wrote < nowMillis - chatWindowMillis || exclude(pubkey)) continue + chatters.add(pubkey) + } var messages = 0 for (message in channel.messages.values) { if (message.createdAt < nowMillis - chatWindowMillis || exclude(message.pubkey)) continue - chatters.add(message.pubkey) messages++ } val people = HashSet(chatters) diff --git a/app/src/test/java/com/bitchat/android/nostr/GeohashActivityAggregatorTest.kt b/app/src/test/java/com/bitchat/android/nostr/GeohashActivityAggregatorTest.kt index 0546cde8e..f0bbb35e5 100644 --- a/app/src/test/java/com/bitchat/android/nostr/GeohashActivityAggregatorTest.kt +++ b/app/src/test/java/com/bitchat/android/nostr/GeohashActivityAggregatorTest.kt @@ -92,6 +92,19 @@ class GeohashActivityAggregatorTest { assertEquals(0, aggregator.snapshot(later).totalOnline) } + @Test + fun `a busy channel keeps earlier writers for the hour beyond the message cap`() { + chat("first", "alice", at = now - 600_000) + repeat(600) { chat("spam$it", "bob", at = now - 60_000) } + aggregator.prune(now) + + val channel = aggregator.snapshot(now).channel("u0")!! + + assertEquals(2, channel.people) + assertEquals(2, channel.chatters) + assertTrue("alice" in aggregator.activePeople("u0", now)) + } + @Test fun `excluded identities drop out of every count`() { chat("e1", "alice")