Use some welshman stuff

This commit is contained in:
Jon Staab 2024-08-15 15:54:30 -07:00
parent fe73ae81c6
commit eba1753d12
7 changed files with 36 additions and 104 deletions

View File

@ -60,7 +60,6 @@
"@welshman/signer": "^0.0.2",
"@welshman/store": "^0.0.2",
"@welshman/util": "^0.0.25",
"@welshman/domain": "^0.0.1",
"bowser": "^2.11.0",
"classnames": "^2.5.1",
"compressorjs": "^1.2.1",

View File

@ -18,8 +18,7 @@
export let filter
export let group = null
const calendarEvents = deriveEventsMapped({
repository,
const calendarEvents = deriveEventsMapped(repository, {
filters: [filter],
itemToEvent: (item: any) => item.event,
eventToItem: (event: TrustedEvent) => {

View File

@ -38,7 +38,7 @@
const loadFeed = () => router.at("notes").cx({feed}).push()
$: isFavorite = getSingletonValues("a", $userFeedFavorites).has(address)
$: isFavorite = getSingletonValues("a", $userFeedFavorites).includes(address)
$: favoritedPubkeys = remove(
$pubkey,
($feedFavoritesByAddress.get(address) || []).map(s => s.event.pubkey),

View File

@ -1,4 +1,4 @@
import {last} from '@welshman/lib'
import {last} from "@welshman/lib"
export type Handle = {
pubkey: string

View File

@ -1,4 +1,11 @@
export type {Profile, PublishedProfile, DecryptedEvent} from "@welshman/domain"
export type {
Profile,
PublishedProfile,
DecryptedEvent,
ListParams as SingletonParams,
List as Singleton,
PublishedList as PublishedSingleton,
} from "@welshman/util"
export {
Encryptable,
makeProfile,
@ -10,7 +17,12 @@ export {
profileHasName,
isPublishedProfile,
asDecryptedEvent,
} from "@welshman/domain"
makeList as makeSingleton,
readList as readSingleton,
createList as createSingleton,
editList as editSingleton,
getListValues as getSingletonValues,
} from "@welshman/util"
export * from "./collection"
export * from "./feed"
@ -20,4 +32,3 @@ export * from "./handler"
export * from "./kind"
export * from "./list"
export * from "./relay"
export * from "./singleton"

View File

@ -1,68 +0,0 @@
import {addToKey} from "@welshman/lib"
import {Address, isShareableRelayUrl} from "@welshman/util"
import {Encryptable} from "@welshman/domain"
import type {DecryptedEvent} from "@welshman/domain"
import {parseJson} from "src/util/misc"
export type SingletonParams = {
kind: number
}
export type IndexableSingleton = SingletonParams & {
publicTags: string[][]
privateTags: string[][]
event?: DecryptedEvent
}
export type Singleton = IndexableSingleton & {
valuesByKey: Record<string, Set<string>>
}
export type PublishedSingleton = Omit<Singleton, "event"> & {
event: DecryptedEvent
}
export const indexSingleton = (singleton: IndexableSingleton): Singleton => {
const valuesByKey = {}
for (const [k, v] of [...singleton.publicTags, ...singleton.privateTags]) {
if (v) {
addToKey(valuesByKey, k, v)
}
}
return {...singleton, valuesByKey}
}
export const makeSingleton = (singleton: SingletonParams & Partial<Singleton>): Singleton =>
indexSingleton({publicTags: [], privateTags: [], ...singleton})
const isValidTag = (tag: string[]) => {
if (tag[0] === "p") return tag[1]?.length === 64
if (tag[0] === "e") return tag[1]?.length === 64
if (tag[0] === "a") return Address.isAddress(tag[1] || "")
if (tag[0] === "t") return tag[1]?.length > 0
if (tag[0] === "r") return isShareableRelayUrl(tag[1])
if (tag[0] === "relay") return isShareableRelayUrl(tag[1])
return true
}
export const readSingleton = (event: DecryptedEvent) => {
const getTags = tags => (Array.isArray(tags) ? tags.filter(isValidTag) : [])
const privateTags = getTags(parseJson(event.plaintext?.content))
const publicTags = getTags(event.tags)
return indexSingleton({event, kind: event.kind, publicTags, privateTags}) as PublishedSingleton
}
export const createSingleton = ({kind, publicTags = [], privateTags = []}: Singleton) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
export const editSingleton = ({kind, publicTags = [], privateTags = []}: PublishedSingleton) =>
new Encryptable({kind, tags: publicTags}, {content: JSON.stringify(privateTags)})
export const getSingletonValues = (
tagName: string,
singleton: Singleton | undefined,
): Set<string> => singleton?.valuesByKey?.[tagName] || new Set<string>()

View File

@ -423,8 +423,7 @@ export const dufflepud = (path: string) => {
// Profiles
export const profiles = deriveEventsMapped<PublishedProfile>({
repository,
export const profiles = deriveEventsMapped<PublishedProfile>(repository, {
filters: [{kinds: [PROFILE]}],
eventToItem: readProfile,
itemToEvent: prop("event"),
@ -541,10 +540,10 @@ export const getFollowList = (pk: string) =>
export const deriveFollowList = (pk: string) =>
derived(followListsByPubkey, m => m.get(pk) as PublishedSingleton | undefined)
export const getFollows = (pk: string) => getSingletonValues("p", getFollowList(pk))
export const getFollows = (pk: string) => new Set(getSingletonValues("p", getFollowList(pk)))
export const deriveFollows = (pk: string) =>
derived(followListsByPubkey, m => getSingletonValues("p", m.get(pk)))
derived(followListsByPubkey, m => new Set(getSingletonValues("p", m.get(pk))))
export const isFollowing = (pk: string, tpk: string) => getFollows(pk).has(tpk)
@ -572,10 +571,10 @@ export const getMuteList = (pk: string) =>
export const deriveMuteList = (pk: string) =>
derived(muteListsByPubkey, m => m.get(pk) as PublishedSingleton | undefined)
export const getMutes = (pk: string) => getSingletonValues("p", getMuteList(pk))
export const getMutes = (pk: string) => new Set(getSingletonValues("p", getMuteList(pk)))
export const deriveMutes = (pk: string) =>
derived(muteListsByPubkey, m => getSingletonValues("p", m.get(pk)))
derived(muteListsByPubkey, m => new Set(getSingletonValues("p", m.get(pk))))
export const isMuting = (pk, tpk) => getMutes(pk).has(tpk)
@ -601,7 +600,7 @@ export const getFollowers = simpleCache(
new Set(
followLists
.get()
.filter(l => l.valuesByKey.p?.has(pk))
.filter(l => getSingletonValues("p", l).includes(pk))
.map(l => l.event.pubkey),
),
)
@ -665,13 +664,13 @@ export const userFollowList = derived(
},
)
export const userFollows = derived(userFollowList, l => getSingletonValues("p", l))
export const userFollows = derived(userFollowList, l => new Set(getSingletonValues("p", l)))
export const userNetwork = derived(userFollowList, l => getNetwork(l.event.pubkey))
export const userMuteList = derived([muteListsByPubkey, pubkey], ([$m, $pk]) => $m.get($pk))
export const userMutes = derived(userMuteList, l => getSingletonValues("p", l))
export const userMutes = derived(userMuteList, l => new Set(getSingletonValues("p", l)))
// Communities
@ -711,15 +710,14 @@ export const getCommunityList = (pk: string) =>
export const deriveCommunityList = (pk: string) =>
derived(communityListsByPubkey, m => m.get(pk) as PublishedSingleton | undefined)
export const getCommunities = (pk: string) => getSingletonValues("a", getCommunityList(pk))
export const getCommunities = (pk: string) => new Set(getSingletonValues("a", getCommunityList(pk)))
export const deriveCommunities = (pk: string) =>
derived(communityListsByPubkey, m => getSingletonValues("a", m.get(pk)))
derived(communityListsByPubkey, m => new Set(getSingletonValues("a", m.get(pk))))
// Groups
export const groupMeta = deriveEventsMapped<PublishedGroupMeta>({
repository,
export const groupMeta = deriveEventsMapped<PublishedGroupMeta>(repository, {
filters: [{kinds: [GROUP, COMMUNITY]}],
itemToEvent: prop("event"),
eventToItem: readGroupMeta,
@ -1223,8 +1221,7 @@ export const derivePubkeysWithoutInbox = (pubkeys: string[]) =>
)
export const legacyRelayLists = withGetter(
deriveEventsMapped<{event: TrustedEvent; policy: RelayPolicy[]}>({
repository,
deriveEventsMapped<{event: TrustedEvent; policy: RelayPolicy[]}>(repository, {
filters: [{kinds: [FOLLOWS]}],
itemToEvent: prop("event"),
eventToItem: event => {
@ -1413,8 +1410,7 @@ export const searchTopicNames = searchTopics.derived(search => term => pluck("na
// Lists
export const lists = deriveEventsMapped<PublishedList>({
repository,
export const lists = deriveEventsMapped<PublishedList>(repository, {
filters: [{kinds: EDITABLE_LIST_KINDS}],
eventToItem: (event: TrustedEvent) => (event.tags.length > 1 ? readList(event) : null),
itemToEvent: prop("event"),
@ -1431,8 +1427,7 @@ export const listSearch = derived(lists, $lists => new ListSearch($lists))
// Feeds
export const feeds = deriveEventsMapped<PublishedFeed>({
repository,
export const feeds = deriveEventsMapped<PublishedFeed>(repository, {
filters: [{kinds: [FEED]}],
itemToEvent: prop("event"),
eventToItem: readFeed,
@ -1477,10 +1472,7 @@ export const userFeedFavorites = derived(
)
export const userFavoritedFeeds = derived(userFeedFavorites, $singleton =>
Array.from(getSingletonValues("a", $singleton))
.map(repository.getEvent)
.filter(identity)
.map(readFeed),
getSingletonValues("a", $singleton).map(repository.getEvent).filter(identity).map(readFeed),
)
export class FeedSearch extends SearchHelper<PublishedFeed, string> {
@ -1514,8 +1506,7 @@ export class FeedSearch extends SearchHelper<PublishedFeed, string> {
export const feedSearch = derived(feeds, $feeds => new FeedSearch($feeds))
export const listFeeds = deriveEventsMapped<PublishedListFeed>({
repository,
export const listFeeds = deriveEventsMapped<PublishedListFeed>(repository, {
filters: [{kinds: [NAMED_BOOKMARKS]}],
eventToItem: (event: TrustedEvent) =>
event.tags.length > 1 ? mapListToFeed(readList(event)) : undefined,
@ -2217,6 +2208,8 @@ class IndexedDBAdapter {
const newRecords = current.filter(r => !prevIds.has(r[key]))
const removedRecords = prev.filter(r => !currentIds.has(r[key]))
prev = current
if (newRecords.length > 0) {
await storage.bulkPut(name, newRecords)
}
@ -2234,8 +2227,6 @@ class IndexedDBAdapter {
if (current.length > limit * 1.5) {
set((sort ? sort(current) : current).slice(0, limit))
}
prev = current
}),
)
}
@ -2369,5 +2360,5 @@ export const storage = new Storage(15, [
sort: sortBy(prop("created_at")),
}),
collectionAdapter("groupAdminKeys", "pubkey", groupAdminKeys, {limit: 1000}),
collectionAdapter("repository", "id", events, {limit: 100000, sort: sortBy(scoreEvent)}),
collectionAdapter("repository", "id", events, {limit: 30000, sort: sortBy(scoreEvent)}),
])