Optimize feeds a bit more, push local relay url up a layer to avoid opting in when we don't want it
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
import {partition} from "@welshman/lib"
|
import {partition} from "@welshman/lib"
|
||||||
import {defaultOptimizeSubscriptions, getDefaultNetContext as originalGetDefaultNetContext} from "@welshman/net"
|
import {defaultOptimizeSubscriptions, getDefaultNetContext as originalGetDefaultNetContext} from "@welshman/net"
|
||||||
import type {Subscription, RelaysAndFilters, NetContext} from "@welshman/net"
|
import type {Subscription, RelaysAndFilters, NetContext} from "@welshman/net"
|
||||||
import {WRAP, isEphemeralKind, isDVMKind, unionFilters} from "@welshman/util"
|
import {WRAP, LOCAL_RELAY_URL, isEphemeralKind, isDVMKind, unionFilters} from "@welshman/util"
|
||||||
import type {TrustedEvent, StampedEvent} from "@welshman/util"
|
import type {TrustedEvent, StampedEvent} from "@welshman/util"
|
||||||
import {tracker, repository} from './core'
|
import {tracker, repository} from './core'
|
||||||
import {makeRouter, getFilterSelections} from './router'
|
import {makeRouter, getFilterSelections} from './router'
|
||||||
@@ -38,8 +38,10 @@ export const getDefaultNetContext = (overrides: Partial<NetContext> = {}) => ({
|
|||||||
const filters = unionFilters(withoutRelays.flatMap(sub => sub.request.filters))
|
const filters = unionFilters(withoutRelays.flatMap(sub => sub.request.filters))
|
||||||
const selections: RelaysAndFilters[] = defaultOptimizeSubscriptions(withRelays)
|
const selections: RelaysAndFilters[] = defaultOptimizeSubscriptions(withRelays)
|
||||||
|
|
||||||
|
selections.push({relays: [LOCAL_RELAY_URL], filters})
|
||||||
|
|
||||||
if (filters.length > 0) {
|
if (filters.length > 0) {
|
||||||
for (const selection of getFilterSelections(filters)) {
|
for (const selection of getFilterSelections(filters)) {
|
||||||
selections.push(selection)
|
selections.push(selection)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,10 +25,10 @@ import {
|
|||||||
RELAYS,
|
RELAYS,
|
||||||
INBOX_RELAYS,
|
INBOX_RELAYS,
|
||||||
FOLLOWS,
|
FOLLOWS,
|
||||||
LOCAL_RELAY_URL,
|
|
||||||
WRAP,
|
WRAP,
|
||||||
getAncestorTags,
|
getAncestorTags,
|
||||||
getPubkeyTagValues
|
getPubkeyTagValues,
|
||||||
|
normalizeRelayUrl,
|
||||||
} from "@welshman/util"
|
} from "@welshman/util"
|
||||||
import type {TrustedEvent, Filter} from "@welshman/util"
|
import type {TrustedEvent, Filter} from "@welshman/util"
|
||||||
import type {RelaysAndFilters} from "@welshman/net"
|
import type {RelaysAndFilters} from "@welshman/net"
|
||||||
@@ -103,7 +103,8 @@ export type Selection = {
|
|||||||
relays: string[],
|
relays: string[],
|
||||||
}
|
}
|
||||||
|
|
||||||
const makeSelection = (relays: string[], weight = 1): Selection => ({relays, weight})
|
const makeSelection = (relays: string[], weight = 1): Selection =>
|
||||||
|
({relays: relays.map(normalizeRelayUrl), weight})
|
||||||
|
|
||||||
// Fallback policies
|
// Fallback policies
|
||||||
|
|
||||||
@@ -427,7 +428,7 @@ export const defaultFilterSelectionRules = [
|
|||||||
|
|
||||||
export const getFilterSelections = (
|
export const getFilterSelections = (
|
||||||
filters: Filter[],
|
filters: Filter[],
|
||||||
rules: FilterSelectionRule[] = defaultFilterSelectionRules
|
rules: FilterSelectionRule[] = defaultFilterSelectionRules
|
||||||
): RelaysAndFilters[] => {
|
): RelaysAndFilters[] => {
|
||||||
const filtersById = new Map<string, Filter>()
|
const filtersById = new Map<string, Filter>()
|
||||||
const scenariosById = new Map<string, RouterScenario[]>()
|
const scenariosById = new Map<string, RouterScenario[]>()
|
||||||
@@ -445,9 +446,8 @@ export const getFilterSelections = (
|
|||||||
|
|
||||||
for (const [id, filter] of filtersById.entries()) {
|
for (const [id, filter] of filtersById.entries()) {
|
||||||
const scenario = ctx.app.router.merge(scenariosById.get(id) || [])
|
const scenario = ctx.app.router.merge(scenariosById.get(id) || [])
|
||||||
const relays = scenario.getUrls().concat(LOCAL_RELAY_URL)
|
|
||||||
|
|
||||||
result.push({filters: [filter], relays})
|
result.push({filters: [filter], relays: scenario.getUrls()})
|
||||||
}
|
}
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -81,11 +81,18 @@ export class FeedController {
|
|||||||
const minSince = sinces.length === filters.length ? min(sinces) : EPOCH
|
const minSince = sinces.length === filters.length ? min(sinces) : EPOCH
|
||||||
const initialDelta = guessFilterDelta(filters)
|
const initialDelta = guessFilterDelta(filters)
|
||||||
|
|
||||||
|
let loading = false
|
||||||
let delta = initialDelta
|
let delta = initialDelta
|
||||||
let since = useWindowing ? maxUntil - delta : minSince
|
let since = useWindowing ? maxUntil - delta : minSince
|
||||||
let until = maxUntil
|
let until = maxUntil
|
||||||
|
|
||||||
return async (limit: number) => {
|
return async (limit: number) => {
|
||||||
|
if (loading) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
loading = true
|
||||||
|
|
||||||
const requestFilters = filters!
|
const requestFilters = filters!
|
||||||
// Remove filters that don't fit our window
|
// Remove filters that don't fit our window
|
||||||
.filter((filter: Filter) => {
|
.filter((filter: Filter) => {
|
||||||
@@ -129,6 +136,8 @@ export class FeedController {
|
|||||||
} else if (count === 0) {
|
} else if (count === 0) {
|
||||||
onExhausted?.()
|
onExhausted?.()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
loading = false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -166,7 +166,9 @@ export const optimizeSubscriptions = (subs: Subscription[]) => {
|
|||||||
|
|
||||||
mergedSub.emitter.on(SubscriptionEvent.Event, (url: string, event: TrustedEvent) => {
|
mergedSub.emitter.on(SubscriptionEvent.Event, (url: string, event: TrustedEvent) => {
|
||||||
for (const sub of group) {
|
for (const sub of group) {
|
||||||
if (!sub.tracker.track(event.id, url) && matchFilters(sub.request.filters, event)) {
|
const seen = sub.tracker.track(event.id, url)
|
||||||
|
|
||||||
|
if (!seen && matchFilters(sub.request.filters, event)) {
|
||||||
sub.emitter.emit(SubscriptionEvent.Event, url, event)
|
sub.emitter.emit(SubscriptionEvent.Event, url, event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -226,7 +226,7 @@ export const getFilterGenerality = (filter: Filter) => {
|
|||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
export const guessFilterDelta = (filters: Filter[], max = 60 * 60 * 24 * 7) =>
|
export const guessFilterDelta = (filters: Filter[], max = 60 * 60 * 24) =>
|
||||||
Math.round(max * Math.max(0.01, 1 - avg(filters.map(getFilterGenerality))))
|
Math.round(max * Math.max(0.01, 1 - avg(filters.map(getFilterGenerality))))
|
||||||
|
|
||||||
// If a filter is specifying ids, we know how many results to expect
|
// If a filter is specifying ids, we know how many results to expect
|
||||||
|
|||||||
Reference in New Issue
Block a user