Fix relay selections

This commit is contained in:
Jon Staab
2024-10-29 15:40:36 -07:00
parent a25adeb235
commit da3379bca3
2 changed files with 7 additions and 15 deletions
+5 -4
View File
@@ -1,6 +1,6 @@
import {
intersection, first, switcher, throttleWithValue, clamp, last, splitAt, identity, sortBy, uniq, shuffle,
pushToMapKey, now, assoc, ctx,
pushToMapKey, now, assoc, ctx, sample,
} from '@welshman/lib'
import {
Tags, getFilterId, unionFilters, isShareableRelayUrl, isCommunityAddress, isGroupAddress, isContextAddress,
@@ -527,7 +527,7 @@ export const getFilterSelectionsForContext = (state: FilterSelectionRuleState) =
}
export const getFilterSelectionsForWraps = (state: FilterSelectionRuleState) => {
if (state.filter.kinds?.includes(WRAP) || !state.filter.authors) return false
if (!state.filter.kinds?.includes(WRAP) || state.filter.authors) return false
const id = getFilterId({...state.filter, kinds: [WRAP]})
const scenario = ctx.app.router.InboxRelays().update(assoc('value', id))
@@ -553,10 +553,11 @@ export const getFilterSelectionsForIndexedKinds = (state: FilterSelectionRuleSta
export const getFilterSelectionsForAuthors = (state: FilterSelectionRuleState) => {
// If we have a ton of authors, just use our indexers
if (!state.filter.authors || state.filter.authors.length > 30) return false
if (!state.filter.authors) return false
const id = getFilterId(state.filter)
const scenario = ctx.app.router.FromPubkeys(state.filter.authors!).update(assoc('value', id))
const pubkeys = sample(50, state.filter.authors!)
const scenario = ctx.app.router.FromPubkeys(pubkeys).update(assoc('value', id))
state.selections.push(makeFilterSelection(id, state.filter, scenario))
+2 -11
View File
@@ -282,6 +282,8 @@ export const choice = <T>(xs: T[]): T => xs[Math.floor(xs.length * Math.random()
export const shuffle = <T>(xs: Iterable<T>): T[] => Array.from(xs).sort(() => Math.random() > 0.5 ? 1 : -1)
export const sample = <T>(n: number, xs: T[]) => shuffle(xs).slice(0, n)
export const isIterable = (x: any) => Symbol.iterator in Object(x)
export const toIterable = (x: any) => isIterable(x) ? x : [x]
@@ -377,17 +379,6 @@ export const indexBy = <T, K>(f: (x: T) => K, xs: T[]) => {
return r
}
export const sample = <T>(n: number, xs: T[]) => {
const result: T[] = []
const limit = Math.min(n, xs.length)
for (let i = 0; i < limit; i++) {
result.push(xs.splice(Math.floor(xs.length * Math.random()), 1)[0])
}
return result
}
export const initArray = <T>(n: number, f: () => T) => {
const result = []