Update to new version of welshman, including new thunks and wrap manager

This commit is contained in:
Jon Staab
2025-10-17 10:19:21 -07:00
parent e0099141aa
commit 6ca74c21bf
21 changed files with 205 additions and 315 deletions
+3 -14
View File
@@ -90,6 +90,7 @@ import {
waitForThunkError,
getPubkeyRelays,
userBlossomServers,
shouldUnwrap,
} from "@welshman/app"
import {compressFile} from "@src/lib/html"
import type {SettingsValues, Alert} from "@app/core/state"
@@ -103,8 +104,6 @@ import {
DEFAULT_BLOSSOM_SERVERS,
userRoomsByUrl,
userSettingsValues,
canDecrypt,
ensureUnwrapped,
userInboxRelays,
getMembershipUrls,
} from "@app/core/state"
@@ -593,8 +592,8 @@ export const createAlert = async (params: CreateAlertParams): Promise<CreateAler
}
export const createDmAlert = async () => {
if (!get(canDecrypt)) {
enableGiftWraps()
if (!shouldUnwrap.get()) {
shouldUnwrap.set(true)
}
return createAlert({
@@ -658,16 +657,6 @@ export const payInvoice = async (invoice: string) => {
}
}
// Gift Wraps
export const enableGiftWraps = () => {
canDecrypt.set(true)
for (const event of repository.query([{kinds: [WRAP]}])) {
ensureUnwrapped(event)
}
}
// File upload
export const normalizeBlossomUrl = (url: string) => normalizeUrl(url.replace(/^ws/, "http"))
+6 -72
View File
@@ -27,14 +27,7 @@ import {
} from "@welshman/lib"
import type {Socket} from "@welshman/net"
import {Pool, load, AuthStateEvent, AuthStatus, SocketEvent, netContext} from "@welshman/net"
import {
collection,
custom,
deriveEvents,
deriveEventsMapped,
withGetter,
synced,
} from "@welshman/store"
import {collection, custom, deriveEvents, deriveEventsMapped, withGetter} from "@welshman/store"
import {isKindFeed, findFeed} from "@welshman/feeds"
import {
getIdFilters,
@@ -68,7 +61,6 @@ import {
getGroupTags,
getRelayTagValues,
getPubkeyTagValues,
isHashedEvent,
displayProfile,
readList,
getListTags,
@@ -82,8 +74,8 @@ import {
RelayMode,
getRelaysFromList,
} from "@welshman/util"
import type {TrustedEvent, SignedEvent, PublishedList, List, Filter} from "@welshman/util"
import {Nip59, decrypt} from "@welshman/signer"
import type {TrustedEvent, PublishedList, List, Filter} from "@welshman/util"
import {decrypt} from "@welshman/signer"
import {routerContext, Router} from "@welshman/router"
import {
pubkey,
@@ -92,14 +84,10 @@ import {
tracker,
makeTrackerStore,
makeRepositoryStore,
relay,
getSession,
getSigner,
createSearch,
userFollows,
ensurePlaintext,
thunks,
flattenThunks,
signer,
makeOutboxLoader,
appContext,
@@ -109,7 +97,6 @@ import {
userInboxRelaySelections,
} from "@welshman/app"
import type {Thunk, Relay} from "@welshman/app"
import {preferencesStorageProvider} from "@src/lib/storage"
export const fromCsv = (s: string) => (s || "").split(",").filter(identity)
@@ -195,46 +182,6 @@ export const defaultPubkeys = derived(userFollows, $userFollows => {
return userPubkeys.length > 5 ? userPubkeys : [...userPubkeys, ...appPubkeys]
})
const failedUnwraps = new Set()
export const ensureUnwrapped = async (event: TrustedEvent) => {
if (event.kind !== WRAP) {
return event
}
let rumor = repository.eventsByWrap.get(event.id)
if (rumor || failedUnwraps.has(event.id)) {
return rumor
}
for (const recipient of getPubkeyTagValues(event.tags)) {
const session = getSession(recipient)
const signer = getSigner(session)
if (signer) {
try {
rumor = await Nip59.fromSigner(signer).unwrap(event as SignedEvent)
break
} catch (e) {
// pass
}
}
}
if (rumor && isHashedEvent(rumor)) {
// Copy urls over to the rumor
tracker.copy(event.id, rumor.id)
// Send the rumor via our relay so listeners get updated
relay.send("EVENT", rumor)
} else {
failedUnwraps.add(event.id)
}
return rumor
}
export const trackerStore = makeTrackerStore()
export const repositoryStore = makeRepositoryStore()
@@ -262,7 +209,7 @@ export const getUrlsForEvent = derived([trackerStore, thunks], ([$tracker, $thun
const getThunksByEventId = memoize(() => {
const thunksByEventId = new Map<string, Thunk[]>()
for (const thunk of flattenThunks(Object.values($thunks))) {
for (const thunk of $thunks) {
pushToMapKey(thunksByEventId, thunk.event.id, thunk)
}
@@ -285,7 +232,7 @@ export const getUrlsForEvent = derived([trackerStore, thunks], ([$tracker, $thun
export const getEventsForUrl = (url: string, filters: Filter[]) => {
const ids = uniq([
...tracker.getIds(url),
...Array.from(flattenThunks(Object.values(get(thunks))))
...get(thunks)
.filter(t => t.options.relays.includes(url))
.map(t => t.event.id),
])
@@ -297,9 +244,7 @@ export const deriveEventsForUrl = (url: string, filters: Filter[]) =>
derived([trackerStore, thunks], ([$tracker, $thunks]) => {
const ids = uniq([
...$tracker.getIds(url),
...Array.from(flattenThunks(Object.values($thunks)))
.filter(t => t.options.relays.includes(url))
.map(t => t.event.id),
...$thunks.filter(t => t.options.relays.includes(url)).map(t => t.event.id),
])
return repository.query(filters.map(assoc("ids", ids)))
@@ -336,12 +281,6 @@ export const COMMENT_FILTER = makeCommentFilter(MESSAGE_KINDS)
// Settings
export const canDecrypt = synced({
key: "canDecrypt",
defaultValue: false,
storage: preferencesStorageProvider,
})
export const SETTINGS = "flotilla/settings"
export type SettingsValues = {
@@ -555,11 +494,6 @@ export const chats = derived(
const messagesByChatId = new Map<string, TrustedEvent[]>()
for (const message of $messages) {
// Filter out messages we sent but aren't addressed to the user
if (!getPubkeyTagValues(message.wrap?.tags || []).includes($pubkey!)) {
continue
}
const chatId = makeChatId(getPubkeyTagValues(message.tags).concat(message.pubkey))
pushToMapKey(messagesByChatId, chatId, message)
+8 -7
View File
@@ -39,6 +39,7 @@ import {
loadMutes,
loadProfile,
repository,
shouldUnwrap,
hasNegentropy,
} from "@welshman/app"
import {
@@ -46,7 +47,6 @@ import {
COMMENT_FILTER,
INDEXER_RELAYS,
REACTION_KINDS,
canDecrypt,
loadSettings,
userMembership,
defaultPubkeys,
@@ -334,14 +334,14 @@ const syncDMs = () => {
}
// When pubkey changes, re-sync
const unsubscribePubkey = derived([pubkey, canDecrypt], identity).subscribe(
([$pubkey, $canDecrypt]) => {
const unsubscribePubkey = derived([pubkey, shouldUnwrap], identity).subscribe(
([$pubkey, $shouldUnwrap]) => {
if ($pubkey !== currentPubkey) {
unsubscribeAll()
}
// If we have a pubkey, refresh our user's relay selections then sync our subscriptions
if ($pubkey && $canDecrypt) {
if ($pubkey && $shouldUnwrap) {
loadRelaySelections($pubkey)
.then(() => loadInboxRelaySelections($pubkey))
.then($l => subscribeAll($pubkey, getRelayTagValues(getListTags($l))))
@@ -352,11 +352,12 @@ const syncDMs = () => {
)
// When user inbox relays change, update synchronization
const unsubscribeSelections = userInboxRelaySelections.subscribe($l => {
const unsubscribeSelections = userInboxRelaySelections.subscribe($userInboxRelaySelections => {
const $pubkey = pubkey.get()
const $shouldUnwrap = shouldUnwrap.get()
if ($pubkey && $l) {
subscribeAll($pubkey, getRelayTagValues(getListTags($l)))
if ($pubkey && $shouldUnwrap) {
subscribeAll($pubkey, getRelayTagValues(getListTags($userInboxRelaySelections)))
}
})