Add add/remove relay commands, thunk status utilities, raw on parsed
This commit is contained in:
@@ -1,20 +1,78 @@
|
||||
import {get} from "svelte/store"
|
||||
import {uniq} from "@welshman/lib"
|
||||
import {uniq, nthNe, removeNil, nthEq} from "@welshman/lib"
|
||||
import {
|
||||
addToListPublicly,
|
||||
EventTemplate,
|
||||
removeFromList,
|
||||
getListTags,
|
||||
getRelayTags,
|
||||
makeList,
|
||||
RelayMode,
|
||||
INBOX_RELAYS,
|
||||
FOLLOWS,
|
||||
RELAYS,
|
||||
MUTES,
|
||||
PINS,
|
||||
} from "@welshman/util"
|
||||
import {Nip59, stamp} from "@welshman/signer"
|
||||
import {Router, addMaximalFallbacks} from "@welshman/router"
|
||||
import {userFollows, userMutes, userPins} from "./user.js"
|
||||
import {
|
||||
userRelaySelections,
|
||||
userInboxRelaySelections,
|
||||
userFollows,
|
||||
userMutes,
|
||||
userPins,
|
||||
} from "./user.js"
|
||||
import {nip44EncryptToSelf, signer} from "./session.js"
|
||||
import {ThunkOptions, MergedThunk, publishThunk} from "./thunk.js"
|
||||
|
||||
export const removeRelay = async (url: string, mode: RelayMode) => {
|
||||
const list = get(userRelaySelections) || makeList({kind: RELAYS})
|
||||
const dup = getRelayTags(getListTags(list)).find(nthEq(1, url))
|
||||
const alt = mode === RelayMode.Read ? RelayMode.Write : RelayMode.Read
|
||||
const tags = list.publicTags.filter(nthNe(1, url))
|
||||
|
||||
// If we had a duplicate that was used as the alt mode, keep the alt
|
||||
if (dup && (!dup[2] || dup[2] === alt)) {
|
||||
tags.push(["r", url, alt])
|
||||
}
|
||||
|
||||
const event = {kind: list.kind, content: list.event?.content || "", tags}
|
||||
const relays = Router.get().FromUser().policy(addMaximalFallbacks).getUrls()
|
||||
|
||||
// Make sure to notify the old relay too
|
||||
relays.push(url)
|
||||
|
||||
return publishThunk({event, relays})
|
||||
}
|
||||
|
||||
export const addRelay = async (url: string, mode: RelayMode) => {
|
||||
const list = get(userRelaySelections) || makeList({kind: RELAYS})
|
||||
const dup = getRelayTags(getListTags(list)).find(nthEq(1, url))
|
||||
const tag = removeNil(["r", url, dup && dup[2] !== mode ? undefined : mode])
|
||||
const tags = [...list.publicTags.filter(nthNe(1, url)), tag]
|
||||
const event = {kind: list.kind, content: list.event?.content || "", tags}
|
||||
const relays = Router.get().FromUser().policy(addMaximalFallbacks).getUrls()
|
||||
|
||||
return publishThunk({event, relays})
|
||||
}
|
||||
|
||||
export const removeInboxRelay = async (url: string) => {
|
||||
const list = get(userInboxRelaySelections) || makeList({kind: INBOX_RELAYS})
|
||||
const event = await removeFromList(list, url).reconcile(nip44EncryptToSelf)
|
||||
const relays = Router.get().FromUser().policy(addMaximalFallbacks).getUrls()
|
||||
|
||||
return publishThunk({event, relays})
|
||||
}
|
||||
|
||||
export const addInboxRelay = async (url: string) => {
|
||||
const list = get(userInboxRelaySelections) || makeList({kind: INBOX_RELAYS})
|
||||
const event = await addToListPublicly(list, ["relay", url]).reconcile(nip44EncryptToSelf)
|
||||
const relays = Router.get().FromUser().policy(addMaximalFallbacks).getUrls()
|
||||
|
||||
return publishThunk({event, relays})
|
||||
}
|
||||
|
||||
export const unfollow = async (value: string) => {
|
||||
const list = get(userFollows) || makeList({kind: FOLLOWS})
|
||||
const event = await removeFromList(list, value).reconcile(nip44EncryptToSelf)
|
||||
|
||||
@@ -270,6 +270,45 @@ export const thunkIncompleteUrls = (thunk: AbstractThunk) => {
|
||||
|
||||
export const thunkIsComplete = (thunk: AbstractThunk) => thunkCompleteUrls(thunk).length > 0
|
||||
|
||||
export const getThunkError = (thunk: Thunk) =>
|
||||
new Promise<string>(resolve => {
|
||||
thunk.subscribe($thunk => {
|
||||
for (const [relay, status] of Object.entries($thunk.status)) {
|
||||
if (status === PublishStatus.Failure) {
|
||||
resolve($thunk.details[relay])
|
||||
}
|
||||
}
|
||||
|
||||
if (thunkIsComplete($thunk)) {
|
||||
resolve("")
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
export const waitForThunkStatus = (thunk: Thunk, status: PublishStatus) =>
|
||||
new Promise<boolean>(resolve => {
|
||||
thunk.subscribe($thunk => {
|
||||
for (const [_, s] of Object.entries($thunk.status)) {
|
||||
if (s === status) {
|
||||
resolve(true)
|
||||
}
|
||||
}
|
||||
|
||||
if (thunkIsComplete($thunk)) {
|
||||
resolve(false)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
export const waitForThunkCompletion = (thunk: Thunk) =>
|
||||
new Promise<void>(resolve => {
|
||||
thunk.subscribe($thunk => {
|
||||
if (thunkIsComplete($thunk)) {
|
||||
resolve()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
export function* walkThunks(thunks: AbstractThunk[]): Iterable<Thunk> {
|
||||
for (const thunk of thunks) {
|
||||
if (thunk instanceof MergedThunk) {
|
||||
|
||||
Reference in New Issue
Block a user