Keep geohash chat writers counted beyond the message cap

Chatters were derived from the retained messages, which are capped at
500 per channel, so in a busy channel someone who wrote ten minutes ago
dropped out of the headcount and roster once 500 newer messages arrived,
and prune then forgot their last-seen time. Track each writer's newest
chat time separately; the cap now bounds only the message count.
This commit is contained in:
callebtc committed 2026-10-27 17:44:49 +01:00
1 parent 53ea6a2e90
commit 19a6474eae
2 files changed
+23 -4

No files matched your search

@@ -72,7 +72,9 @@ class GeohashActivityAggregator(
private class Channel(val latitude: Double, val longitude: Double) {
/** pubkey -> newest activity of any kind. */
val lastSeen = HashMap<String, Long>()
/** 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<String, Long>()
/** event id -> message, for chat events only; capped, so it bounds the message count only. */
val messages = LinkedHashMap<String, Message>()
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<String>()
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<String>(chatters)
@@ -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")