feat(platform): search ranking merge and device link pairSwarm hook (v0.8.91)
mergeSearchHits accepts query for ranked enrichment; DB fallback uses rankAndEnrichSearchHits. linkDevicePairCode accepts an external Hyperswarm for smoke/live pairing without destroying the guest swarm early. Co-authored-by: Cursor <[email protected]>
This commit is contained in:
@@ -913,14 +913,17 @@ class PearcordPlatform extends EventEmitter {
|
||||
const hostPublicKey = b4a.toString(this._devicePairSwarm.keyPair.publicKey, 'hex')
|
||||
const invite = await this.deviceSync.createPairInvite({ deviceLabel, ttlMs, hostPublicKey })
|
||||
await this.deviceSync.hostSession({ topic: invite.topic })
|
||||
await this._devicePairSwarm.flush().catch(() => {})
|
||||
return invite
|
||||
}
|
||||
|
||||
async linkDevicePairCode (code, { deviceLabel } = {}) {
|
||||
async linkDevicePairCode (code, { deviceLabel, pairSwarm: externalSwarm, guestPublicKey } = {}) {
|
||||
if (this.onboarded) throw new Error('already onboarded — use a fresh install')
|
||||
const crypto = require('hypercore-crypto')
|
||||
const pairKey = crypto.keyPair()
|
||||
const pairSwarm = new Hyperswarm({ keyPair: pairKey })
|
||||
const pairKey = externalSwarm ? null : crypto.keyPair()
|
||||
const pairSwarm = externalSwarm || new Hyperswarm({ keyPair: pairKey })
|
||||
const guestPkHex = guestPublicKey ||
|
||||
b4a.toString((pairKey || pairSwarm.keyPair).publicKey, 'hex')
|
||||
const deviceSync = new PearcordDeviceSync({
|
||||
storagePath: this.storagePath,
|
||||
getIdentityBundle: async () => null
|
||||
@@ -931,10 +934,10 @@ class PearcordPlatform extends EventEmitter {
|
||||
try {
|
||||
bundle = await deviceSync.acceptPairCode(code, {
|
||||
deviceLabel,
|
||||
guestPublicKey: b4a.toString(pairKey.publicKey, 'hex')
|
||||
guestPublicKey: guestPkHex
|
||||
})
|
||||
} finally {
|
||||
await pairSwarm.destroy().catch(() => {})
|
||||
if (!externalSwarm) await pairSwarm.destroy().catch(() => {})
|
||||
}
|
||||
const user = await this.identity.importFromBundle({
|
||||
user: bundle.user,
|
||||
@@ -1242,7 +1245,7 @@ class PearcordPlatform extends EventEmitter {
|
||||
const idx = await this._ensureGuildSearchIndex(guildId)
|
||||
const indexed = idx.search(q, { limit })
|
||||
if (indexed.length) {
|
||||
return indexed.map((m) => enrichMessageRow({ ...m, channelName: m.channelName }))
|
||||
return indexed.map((m) => enrichMessageRow(m))
|
||||
}
|
||||
if (opts.indexOnly) return []
|
||||
const channels = await this.db.find(COLLECTIONS.CHANNELS, { guildId })
|
||||
@@ -1279,12 +1282,13 @@ class PearcordPlatform extends EventEmitter {
|
||||
.toLowerCase()
|
||||
return searchable.has(m.channelId) && hay.includes(q)
|
||||
})
|
||||
.sort((a, b) => b.createdAt - a.createdAt)
|
||||
.slice(0, limit)
|
||||
return hits.map((m) => ({
|
||||
.slice(0, limit * 2)
|
||||
const { rankAndEnrichSearchHits } = require('pearcord-search')
|
||||
const mapped = hits.map((m) => ({
|
||||
...enrichMessageRow(m),
|
||||
channelName: channelById.get(m.channelId)?.name || m.channelId.slice(0, 8)
|
||||
}))
|
||||
return rankAndEnrichSearchHits(mapped, q, { limit })
|
||||
}
|
||||
|
||||
getGuildSearchIndexMeta (guildId) {
|
||||
@@ -1309,7 +1313,10 @@ class PearcordPlatform extends EventEmitter {
|
||||
}
|
||||
try {
|
||||
const meshHits = await this._fetchGuildSearchFromMeshOnce(guildId, query, opts)
|
||||
const merged = mergeSearchHits(local, meshHits, { limit: opts.limit || 50 })
|
||||
const merged = mergeSearchHits(local, meshHits, {
|
||||
limit: opts.limit || 50,
|
||||
query: String(query || '').trim()
|
||||
})
|
||||
return {
|
||||
hits: merged.map((m) => enrichMessageRow(m)),
|
||||
mesh: true,
|
||||
|
||||
Reference in New Issue
Block a user