forked from coracle/flotilla
Improve relay access checks, content loading
This commit is contained in:
+32
-23
@@ -189,39 +189,42 @@ export const setInboxRelayPolicy = (url: string, enabled: boolean) => {
|
||||
|
||||
// Relay access
|
||||
|
||||
export const requestRelayAccess = (url: string, claim = "") =>
|
||||
publishThunk({
|
||||
export const checkRelayAccess = async (url: string, claim = "") => {
|
||||
await ctx.net.pool.get(url).ensureAuth()
|
||||
|
||||
const result = await publishThunk({
|
||||
event: createEvent(28934, {tags: [["claim", claim]]}),
|
||||
relays: [url],
|
||||
})
|
||||
|
||||
export const attemptRelayAccess = async (url: string, claim = "") => {
|
||||
const relay = await loadRelay(url)
|
||||
|
||||
// Make sure the relay has a profile
|
||||
if (!relay?.profile) {
|
||||
return "Sorry, we weren't able to find that relay."
|
||||
}
|
||||
|
||||
const connection = ctx.net.pool.get(url)
|
||||
|
||||
// Check connection status
|
||||
await connection.ensureConnected()
|
||||
|
||||
if (![ConnectionStatus.Ok, ConnectionStatus.Slow].includes(connection.meta.getStatus())) {
|
||||
return `Failed to connect: "${connection.meta.getDescription()}"`
|
||||
}
|
||||
|
||||
// Attempt to publish a join request
|
||||
const result = await requestRelayAccess(url, claim)
|
||||
|
||||
if (result[url].status !== PublishStatus.Success) {
|
||||
const message = result[url].message?.replace(/^.*: /, '') || "join request rejected"
|
||||
|
||||
return `Failed to join relay: ${message}`
|
||||
}
|
||||
}
|
||||
|
||||
export const checkRelayProfile = async (url: string) => {
|
||||
const relay = await loadRelay(url)
|
||||
|
||||
if (!relay?.profile) {
|
||||
return "Sorry, we weren't able to find that relay."
|
||||
}
|
||||
}
|
||||
|
||||
export const checkRelayConnection = async (url: string) => {
|
||||
const connection = ctx.net.pool.get(url)
|
||||
|
||||
await connection.ensureConnected()
|
||||
|
||||
if (![ConnectionStatus.Ok, ConnectionStatus.Slow].includes(connection.meta.getStatus())) {
|
||||
return `Failed to connect: "${connection.meta.getDescription()}"`
|
||||
}
|
||||
}
|
||||
|
||||
export const checkRelayAuth = async (url: string) => {
|
||||
const connection = ctx.net.pool.get(url)
|
||||
|
||||
// Check auth status
|
||||
await connection.ensureAuth()
|
||||
|
||||
if (![AuthStatus.Ok, AuthStatus.Pending].includes(connection.meta.authStatus)) {
|
||||
@@ -229,6 +232,12 @@ export const attemptRelayAccess = async (url: string, claim = "") => {
|
||||
}
|
||||
}
|
||||
|
||||
export const attemptRelayAccess = async (url: string, claim = "") =>
|
||||
await checkRelayProfile(url) ||
|
||||
await checkRelayConnection(url) ||
|
||||
await checkRelayAccess(url, claim) ||
|
||||
await checkRelayAuth(url)
|
||||
|
||||
// Actions
|
||||
|
||||
export const sendWrapped = async ({
|
||||
|
||||
Reference in New Issue
Block a user