Improve relay access checks, content loading

This commit is contained in:
Jon Staab
2024-10-10 17:09:01 -07:00
parent 83d892afc3
commit 597ebddf82
14 changed files with 127 additions and 104 deletions
+32 -23
View File
@@ -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 ({
+1 -1
View File
@@ -48,7 +48,7 @@
{@const following = getPubkeyTagValues(getListTags($userFollows)).includes(pubkey)}
<div class="divider" />
<Button class="chat chat-start" on:click={onClick}>
<div class="chat-bubble">
<div class="chat-bubble text-left bg-alt">
<Content hideMedia={!following} {event} />
<p class="text-right text-xs">{formatTimestamp(event.created_at)}</p>
</div>
+1 -1
View File
@@ -7,5 +7,5 @@
</script>
{#if $relay?.profile?.description}
<p>{$relay?.profile.description}</p>
<p class={$$props.class}>{$relay?.profile.description}</p>
{/if}
+1 -1
View File
@@ -58,7 +58,7 @@
<Icon icon="alt-arrow-left" />
Go back
</Button>
<Button type="submit" class="btn btn-primary">
<Button type="submit" class="btn btn-primary" disabled={loading}>
Go to Space
<Icon icon="alt-arrow-right" class="!bg-base-300" />
</Button>
+4 -4
View File
@@ -5,8 +5,8 @@
{#if $toast}
{@const theme = $toast.theme || "info"}
{#key $toast.id}
<div transition:fly class="toast z-toast">
<div transition:fly class="toast z-toast">
{#key $toast.id}
<div
role="alert"
class="alert flex justify-center"
@@ -14,6 +14,6 @@
class:alert-error={theme === "error"}>
{$toast.message}
</div>
</div>
{/key}
{/key}
</div>
{/if}
+10 -16
View File
@@ -209,6 +209,16 @@ export const deriveEvent = (idOrAddress: string, hints: string[] = []) => {
)
}
export const deriveEventsForUrl = (url: string, kinds: number[]) =>
derived(trackerStore, $tracker =>
sortBy(
e => -e.created_at,
Array.from($tracker.getIds(url))
.map(id => repository.eventsById.get(id)!)
.filter(e => kinds.includes(e?.kind)),
)
)
// Membership
export const getMembershipUrls = (list?: List) => sort(getRelayTagValues(getListTags(list)))
@@ -375,22 +385,6 @@ export const chatSearch = derived(chats, $chats =>
}),
)
// Calendar events
export const events = deriveEvents(repository, {filters: [{kinds: [EVENT_DATE, EVENT_TIME]}]})
export const eventsByUrl = derived([trackerStore, events], ([$tracker, $events]) => {
const eventsByUrl = new Map<string, TrustedEvent[]>()
for (const event of $events) {
for (const url of $tracker.getRelays(event.id)) {
pushToMapKey(eventsByUrl, url, event)
}
}
return eventsByUrl
})
// Rooms
export const roomsByUrl = derived(channels, $channels => {