fix(discovery): show new public guilds on remote Explore immediately

Invalidate cached Explore listings when DIRECTORY_LISTING arrives, publish
new guilds to the discovery mesh after create, and re-sync on peer connect.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
Raven Scott
2026-06-01 15:41:53 -04:00
co-authored by Cursor
parent 428dfa5804
commit 580ea5a478
+45 -6
View File
@@ -754,10 +754,12 @@ class PearcordPlatform extends EventEmitter {
if (!this.discovery || this._discoveryWired) return
this._discoveryWired = true
this.discovery.on('listing', (row) => {
this._invalidateDiscoveryListingsCache()
this._noteDiscoveryListingDelta(row)
this.emit('discovery')
})
this.discovery.on('listing-prune', ({ removed }) => {
this._invalidateDiscoveryListingsCache()
if (removed > 0) {
this._discoveryDiffHint = `${removed} stale listing${removed === 1 ? '' : 's'} pruned`
this._discoveryDiffUntil = Date.now() + 8000
@@ -782,6 +784,7 @@ class PearcordPlatform extends EventEmitter {
})
})
this.discovery.on('listing-revoke', (payload) => {
this._invalidateDiscoveryListingsCache()
const guildId = payload?.guildId || payload?.id
if (!guildId) return
this.db
@@ -819,12 +822,21 @@ class PearcordPlatform extends EventEmitter {
const members = await this.db.find(COLLECTIONS.MEMBERS, { guildId: g.id })
const prev = publicRows.find((r) => r.id === g.id)
const tags = await this.discovery.getGuildDiscoveryTags(g.id)
let inviteCode = prev?.inviteCode || null
if (!inviteCode && this.guild?.guild?.id === g.id) {
const inv = await this.createInvite().catch(() => null)
inviteCode = inv?.shareCode || inv?.code || null
}
await this.discovery.announceGuild(g, {
inviteCode: prev?.inviteCode || null,
inviteCode,
memberCount: members.length || 1,
tags
})
}
if (this.discovery._meshJoined) {
await this.discovery.syncCachedListingsToMesh().catch(() => {})
}
this._invalidateDiscoveryListingsCache()
}
async _leaveMeshes () {
@@ -2451,6 +2463,25 @@ class PearcordPlatform extends EventEmitter {
})
}
_invalidateDiscoveryListingsCache () {
this._cachedPublicListings = null
this._cachedPublicListingsAt = 0
}
async _publishGuildToDiscoveryMesh (guild, opts = {}) {
if (!this.discovery || !guild?.id) return null
await this._joinDiscoveryMesh().catch(() => {})
const listed = await this.discovery.isGuildPublicListing(guild.id)
if (!listed) return null
const listing = await this.discovery.announceGuild(guild, opts)
if (listing && this.discovery._meshJoined) {
await this.discovery.syncCachedListingsToMesh().catch(() => {})
}
this._invalidateDiscoveryListingsCache()
this.emit('discovery')
return listing
}
_noteDiscoveryListingDelta (row) {
if (!row?.id) {
this._discoveryDiffHint = 'Discovery listings updated'
@@ -2472,6 +2503,7 @@ class PearcordPlatform extends EventEmitter {
const pruned = await this.discovery.pruneStaleListings()
const synced = await this.discovery.syncCachedListingsToMesh()
this._discoveryMeshRefreshAt = Date.now()
this._invalidateDiscoveryListingsCache()
this.emit('discovery')
return { synced, pruned: pruned.removed || 0 }
}
@@ -2494,10 +2526,15 @@ class PearcordPlatform extends EventEmitter {
inviteCode = prev?.inviteCode || null
memberCount = prev?.memberCount || 1
}
await this.discovery.announceGuild(guild, { inviteCode, memberCount, tags: row.discoveryTags })
await this._publishGuildToDiscoveryMesh(guild, {
inviteCode,
memberCount,
tags: row.discoveryTags
})
}
} else {
this.emit('discovery')
}
this.emit('discovery')
return { guildId, tags: row.discoveryTags }
}
@@ -7875,7 +7912,7 @@ class PearcordPlatform extends EventEmitter {
}
const inv = await this.createInvite().catch(() => null)
if (listOnDiscovery) {
await this.discovery?.announceGuild(result.guild, {
await this._publishGuildToDiscoveryMesh(result.guild, {
inviteCode: inv?.shareCode || inv?.code || null,
memberCount: 1
}).catch(() => {})
@@ -9023,9 +9060,11 @@ class PearcordPlatform extends EventEmitter {
inviteCode = inv.shareCode || inv.code
memberCount = (await this.guild.listMembers()).length
}
await this.discovery.announceGuild(guild, { inviteCode, memberCount })
await this._publishGuildToDiscoveryMesh(guild, { inviteCode, memberCount })
} else {
this._invalidateDiscoveryListingsCache()
this.emit('discovery')
}
this.emit('discovery')
span.end({
spanKind: 'discovery.publish',
guildId,