Clean up user data a tad
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import {Scope, FeedController, FeedControllerOptions, Feed} from "@welshman/feeds"
|
||||
import {pubkey, signer} from "./session.js"
|
||||
import {wotGraph, maxWot, getFollows, getNetwork, getFollowers} from "./wot.js"
|
||||
import {getWotGraph, getMaxWot, getFollows, getNetwork, getFollowers} from "./wot.js"
|
||||
|
||||
export const getPubkeysForScope = (scope: string) => {
|
||||
const $pubkey = pubkey.get()
|
||||
@@ -25,10 +25,11 @@ export const getPubkeysForScope = (scope: string) => {
|
||||
|
||||
export const getPubkeysForWOTRange = (min: number, max: number) => {
|
||||
const pubkeys = []
|
||||
const thresholdMin = maxWot.get() * min
|
||||
const thresholdMax = maxWot.get() * max
|
||||
const $maxWot = getMaxWot()
|
||||
const thresholdMin = $maxWot * min
|
||||
const thresholdMax = $maxWot * max
|
||||
|
||||
for (const [tpk, score] of wotGraph.get().entries()) {
|
||||
for (const [tpk, score] of getWotGraph().entries()) {
|
||||
if (score >= thresholdMin && score <= thresholdMax) {
|
||||
pubkeys.push(tpk)
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ import {PROFILE, PublishedProfile, RelayProfile} from "@welshman/util"
|
||||
import {load} from "@welshman/net"
|
||||
import {throttled} from "@welshman/store"
|
||||
import {Router} from "@welshman/router"
|
||||
import {wotGraph, maxWot} from "./wot.js"
|
||||
import {getWotGraph, getMaxWot} from "./wot.js"
|
||||
import {profiles} from "./profiles.js"
|
||||
import {topics, Topic} from "./topics.js"
|
||||
import {relays} from "./relays.js"
|
||||
@@ -75,9 +75,9 @@ export const profileSearch = derived(
|
||||
onSearch: searchProfiles,
|
||||
getValue: (profile: PublishedProfile) => profile.event.pubkey,
|
||||
sortFn: ({score = 1, item}) => {
|
||||
const wotScore = wotGraph.get().get(item.event.pubkey) || 0
|
||||
const wotScore = getWotGraph().get(item.event.pubkey) || 0
|
||||
|
||||
return dec(score) * inc(wotScore / maxWot.get())
|
||||
return dec(score) * inc(wotScore / getMaxWot())
|
||||
},
|
||||
fuseOptions: {
|
||||
keys: [
|
||||
|
||||
+25
-58
@@ -1,5 +1,5 @@
|
||||
import {derived, Readable} from "svelte/store"
|
||||
import {withGetter, memoized} from "@welshman/store"
|
||||
import {ItemsByKey} from "@welshman/store"
|
||||
import {pubkey} from "./session.js"
|
||||
import {profilesByPubkey, forceLoadProfile, loadProfile} from "./profiles.js"
|
||||
import {followListsByPubkey, forceLoadFollowList, loadFollowList} from "./follows.js"
|
||||
@@ -16,94 +16,61 @@ import {
|
||||
forceLoadMessagingRelayList,
|
||||
loadMessagingRelayList,
|
||||
} from "./messagingRelayLists.js"
|
||||
import {wotGraph} from "./wot.js"
|
||||
import {wotGraph, getWotGraph} from "./wot.js"
|
||||
|
||||
export type UserDataLoader = (pubkey: string, relays?: string[], force?: boolean) => unknown
|
||||
export const makeUserData = <T>(
|
||||
itemsByKey: Readable<ItemsByKey<T>>,
|
||||
onDerive?: (key: string, ...args: any[]) => void,
|
||||
) =>
|
||||
derived([itemsByKey, pubkey], ([$itemsByKey, $pubkey]) => {
|
||||
if (!$pubkey) return undefined
|
||||
|
||||
export type MakeUserDataOptions<T> = {
|
||||
mapStore: Readable<Map<string, T>>
|
||||
loadItem: UserDataLoader
|
||||
}
|
||||
onDerive?.($pubkey)
|
||||
|
||||
export const makeUserData = <T>({mapStore, loadItem}: MakeUserDataOptions<T>) =>
|
||||
withGetter(
|
||||
memoized(
|
||||
derived([mapStore, pubkey], ([$mapStore, $pubkey]) => {
|
||||
if (!$pubkey) return undefined
|
||||
|
||||
loadItem($pubkey)
|
||||
|
||||
return $mapStore.get($pubkey)
|
||||
}),
|
||||
),
|
||||
)
|
||||
return $itemsByKey.get($pubkey)
|
||||
})
|
||||
|
||||
export const makeUserLoader =
|
||||
(loadItem: UserDataLoader) =>
|
||||
async (relays: string[] = [], force = false) => {
|
||||
(loadItem: (key: string, ...args: any[]) => void) =>
|
||||
async (...args: any[]) => {
|
||||
const $pubkey = pubkey.get()
|
||||
|
||||
if ($pubkey) {
|
||||
await loadItem($pubkey, relays, force)
|
||||
await loadItem($pubkey, ...args)
|
||||
}
|
||||
}
|
||||
|
||||
export const userProfile = makeUserData({
|
||||
mapStore: profilesByPubkey,
|
||||
loadItem: loadProfile,
|
||||
})
|
||||
|
||||
export const userProfile = makeUserData(profilesByPubkey, loadProfile)
|
||||
export const forceLoadUserProfile = makeUserLoader(forceLoadProfile)
|
||||
export const loadUserProfile = makeUserLoader(loadProfile)
|
||||
|
||||
export const userFollowList = makeUserData({
|
||||
mapStore: followListsByPubkey,
|
||||
loadItem: loadFollowList,
|
||||
})
|
||||
|
||||
export const userFollowList = makeUserData(followListsByPubkey, loadFollowList)
|
||||
export const forceLoadUserFollowList = makeUserLoader(forceLoadFollowList)
|
||||
export const loadUserFollowList = makeUserLoader(loadFollowList)
|
||||
|
||||
export const userMuteList = makeUserData({
|
||||
mapStore: muteListsByPubkey,
|
||||
loadItem: loadMuteList,
|
||||
})
|
||||
|
||||
export const userMuteList = makeUserData(muteListsByPubkey, loadMuteList)
|
||||
export const forceLoadUserMuteList = makeUserLoader(forceLoadMuteList)
|
||||
export const loadUserMuteList = makeUserLoader(loadMuteList)
|
||||
|
||||
export const userPinList = makeUserData({
|
||||
mapStore: pinListsByPubkey,
|
||||
loadItem: loadPinList,
|
||||
})
|
||||
|
||||
export const userPinList = makeUserData(pinListsByPubkey, loadPinList)
|
||||
export const forceLoadUserPinList = makeUserLoader(forceLoadPinList)
|
||||
export const loadUserPinList = makeUserLoader(loadPinList)
|
||||
|
||||
export const userRelayList = makeUserData({
|
||||
mapStore: relayListsByPubkey,
|
||||
loadItem: loadRelayList,
|
||||
})
|
||||
|
||||
export const userRelayList = makeUserData(relayListsByPubkey, loadRelayList)
|
||||
export const forceLoadUserRelayList = makeUserLoader(forceLoadRelayList)
|
||||
export const loadUserRelayList = makeUserLoader(loadRelayList)
|
||||
|
||||
export const userMessagingRelayList = makeUserData({
|
||||
mapStore: messagingRelayListsByPubkey,
|
||||
loadItem: loadMessagingRelayList,
|
||||
})
|
||||
|
||||
export const userMessagingRelayList = makeUserData(
|
||||
messagingRelayListsByPubkey,
|
||||
loadMessagingRelayList,
|
||||
)
|
||||
export const forceLoadUserMessagingRelayList = makeUserLoader(forceLoadMessagingRelayList)
|
||||
export const loadUserMessagingRelayList = makeUserLoader(loadMessagingRelayList)
|
||||
|
||||
export const userBlossomServerList = makeUserData({
|
||||
mapStore: blossomServerListsByPubkey,
|
||||
loadItem: loadBlossomServerList,
|
||||
})
|
||||
|
||||
export const userBlossomServerList = makeUserData(blossomServerListsByPubkey, loadBlossomServerList)
|
||||
export const forceLoadUserBlossomServerList = makeUserLoader(forceLoadBlossomServerList)
|
||||
export const loadUserBlossomServerList = makeUserLoader(loadBlossomServerList)
|
||||
|
||||
export const getUserWotScore = (tpk: string) => wotGraph.get().get(tpk) || 0
|
||||
export const getUserWotScore = (tpk: string) => getWotGraph().get(tpk) || 0
|
||||
|
||||
export const deriveUserWotScore = (tpk: string) => derived(wotGraph, $g => $g.get(tpk) || 0)
|
||||
|
||||
+29
-26
@@ -1,7 +1,7 @@
|
||||
import {derived, writable} from "svelte/store"
|
||||
import {max, throttle, addToMapKey, inc, dec} from "@welshman/lib"
|
||||
import {getListTags, getPubkeyTagValues} from "@welshman/util"
|
||||
import {throttled, withGetter} from "@welshman/store"
|
||||
import {throttled, getter} from "@welshman/store"
|
||||
import {pubkey} from "./session.js"
|
||||
import {followLists, getFollowListsByPubkey, getFollowList} from "./follows.js"
|
||||
import {muteLists, getMuteList} from "./mutes.js"
|
||||
@@ -25,38 +25,37 @@ export const getNetwork = (pubkey: string) => {
|
||||
return Array.from(network)
|
||||
}
|
||||
|
||||
export const followersByPubkey = withGetter(
|
||||
derived(throttled(1000, followLists), lists => {
|
||||
const $followersByPubkey = new Map<string, Set<string>>()
|
||||
export const followersByPubkey = derived(throttled(1000, followLists), lists => {
|
||||
const $followersByPubkey = new Map<string, Set<string>>()
|
||||
|
||||
for (const list of lists) {
|
||||
for (const pubkey of getPubkeyTagValues(getListTags(list))) {
|
||||
addToMapKey($followersByPubkey, pubkey, list.event.pubkey)
|
||||
}
|
||||
for (const list of lists) {
|
||||
for (const pubkey of getPubkeyTagValues(getListTags(list))) {
|
||||
addToMapKey($followersByPubkey, pubkey, list.event.pubkey)
|
||||
}
|
||||
}
|
||||
|
||||
return $followersByPubkey
|
||||
}),
|
||||
)
|
||||
return $followersByPubkey
|
||||
})
|
||||
|
||||
export const mutersByPubkey = withGetter(
|
||||
derived(throttled(1000, muteLists), lists => {
|
||||
const $mutersByPubkey = new Map<string, Set<string>>()
|
||||
export const getFollowersByPubkey = getter(followersByPubkey)
|
||||
|
||||
for (const list of lists) {
|
||||
for (const pubkey of getPubkeyTagValues(getListTags(list))) {
|
||||
addToMapKey($mutersByPubkey, pubkey, list.event.pubkey)
|
||||
}
|
||||
export const mutersByPubkey = derived(throttled(1000, muteLists), lists => {
|
||||
const $mutersByPubkey = new Map<string, Set<string>>()
|
||||
|
||||
for (const list of lists) {
|
||||
for (const pubkey of getPubkeyTagValues(getListTags(list))) {
|
||||
addToMapKey($mutersByPubkey, pubkey, list.event.pubkey)
|
||||
}
|
||||
}
|
||||
|
||||
return $mutersByPubkey
|
||||
}),
|
||||
)
|
||||
return $mutersByPubkey
|
||||
})
|
||||
|
||||
export const getFollowers = (pubkey: string) =>
|
||||
Array.from(followersByPubkey.get().get(pubkey) || [])
|
||||
export const getMutersByPubkey = getter(mutersByPubkey)
|
||||
|
||||
export const getMuters = (pubkey: string) => Array.from(mutersByPubkey.get().get(pubkey) || [])
|
||||
export const getFollowers = (pubkey: string) => Array.from(getFollowersByPubkey().get(pubkey) || [])
|
||||
|
||||
export const getMuters = (pubkey: string) => Array.from(getMutersByPubkey().get(pubkey) || [])
|
||||
|
||||
export const getFollowsWhoFollow = (pubkey: string, target: string) =>
|
||||
getFollows(pubkey).filter(other => getFollows(other).includes(target))
|
||||
@@ -64,9 +63,13 @@ export const getFollowsWhoFollow = (pubkey: string, target: string) =>
|
||||
export const getFollowsWhoMute = (pubkey: string, target: string) =>
|
||||
getFollows(pubkey).filter(other => getMutes(other).includes(target))
|
||||
|
||||
export const wotGraph = withGetter(writable(new Map<string, number>()))
|
||||
export const wotGraph = writable(new Map<string, number>())
|
||||
|
||||
export const maxWot = withGetter(derived(wotGraph, $g => max(Array.from($g.values()))))
|
||||
export const getWotGraph = getter(wotGraph)
|
||||
|
||||
export const maxWot = derived(wotGraph, $g => max(Array.from($g.values())))
|
||||
|
||||
export const getMaxWot = getter(maxWot)
|
||||
|
||||
const buildGraph = throttle(1000, () => {
|
||||
const $pubkey = pubkey.get()
|
||||
|
||||
Reference in New Issue
Block a user