forked from coracle/flotilla
Add support for blocked relays
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
* Fix memory leak, notification badge not showing
|
||||
* Improve space join flow
|
||||
* Fix opening images in fullscreen dialog
|
||||
* Add support for blocked relays
|
||||
|
||||
# 1.6.2
|
||||
|
||||
|
||||
@@ -52,10 +52,8 @@ import {
|
||||
removeFromListByPredicate,
|
||||
getTag,
|
||||
getListTags,
|
||||
getRelayTags,
|
||||
getRelayTagValues,
|
||||
toNostrURI,
|
||||
getRelaysFromList,
|
||||
RelayMode,
|
||||
getAddress,
|
||||
getTagValue,
|
||||
@@ -80,8 +78,6 @@ import {
|
||||
publishThunk,
|
||||
tagEvent,
|
||||
tagEventForReaction,
|
||||
userRelayList,
|
||||
userMessagingRelayList,
|
||||
nip44EncryptToSelf,
|
||||
dropSession,
|
||||
tagEventForComment,
|
||||
@@ -208,42 +204,6 @@ export const removeRoomMembership = async (url: string, h: string) => {
|
||||
return publishThunk({event, relays})
|
||||
}
|
||||
|
||||
export const setRelayPolicy = (url: string, read: boolean, write: boolean) => {
|
||||
const list = get(userRelayList) || makeList({kind: RELAYS})
|
||||
const tags = getRelayTags(getListTags(list)).filter(t => normalizeRelayUrl(t[1]) !== url)
|
||||
|
||||
if (read && write) {
|
||||
tags.push(["r", url])
|
||||
} else if (read) {
|
||||
tags.push(["r", url, "read"])
|
||||
} else if (write) {
|
||||
tags.push(["r", url, "write"])
|
||||
}
|
||||
|
||||
return publishThunk({
|
||||
event: makeEvent(list.kind, {tags}),
|
||||
relays: [url, ...INDEXER_RELAYS, ...Router.get().FromUser().getUrls(), ...get(userSpaceUrls)],
|
||||
})
|
||||
}
|
||||
|
||||
export const setMessagingRelayPolicy = (url: string, enabled: boolean) => {
|
||||
const list = get(userMessagingRelayList) || makeList({kind: MESSAGING_RELAYS})
|
||||
|
||||
// Only update messaging policies if they already exist or we're adding them
|
||||
if (enabled || getRelaysFromList(list).includes(url)) {
|
||||
const tags = getRelayTags(getListTags(list)).filter(t => normalizeRelayUrl(t[1]) !== url)
|
||||
|
||||
if (enabled) {
|
||||
tags.push(["relay", url])
|
||||
}
|
||||
|
||||
return publishThunk({
|
||||
event: makeEvent(list.kind, {tags}),
|
||||
relays: [...INDEXER_RELAYS, ...Router.get().FromUser().getUrls(), ...get(userSpaceUrls)],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Relay access
|
||||
|
||||
export const canEnforceNip70 = async (url: string) => {
|
||||
|
||||
@@ -41,6 +41,7 @@ import {
|
||||
loadRelayList,
|
||||
loadMessagingRelayList,
|
||||
loadBlossomServerList,
|
||||
loadBlockedRelayList,
|
||||
loadFollowList,
|
||||
loadMuteList,
|
||||
loadProfile,
|
||||
@@ -215,6 +216,7 @@ const syncUserData = () => {
|
||||
loadAlerts($userRelayList.event.pubkey)
|
||||
loadAlertStatuses($userRelayList.event.pubkey)
|
||||
loadBlossomServerList($userRelayList.event.pubkey)
|
||||
loadBlockedRelayList($userRelayList.event.pubkey)
|
||||
loadFollowList($userRelayList.event.pubkey)
|
||||
loadGroupList($userRelayList.event.pubkey)
|
||||
loadMuteList($userRelayList.event.pubkey)
|
||||
@@ -229,13 +231,13 @@ const syncUserData = () => {
|
||||
await sleep(1000)
|
||||
|
||||
await Promise.all(
|
||||
pubkeys.map(async pk => {
|
||||
await loadRelayList(pk)
|
||||
await loadGroupList(pk)
|
||||
await loadProfile(pk)
|
||||
await loadFollowList(pk)
|
||||
await loadMuteList(pk)
|
||||
}),
|
||||
pubkeys.flatMap(pk => [
|
||||
loadRelayList(pk),
|
||||
loadGroupList(pk),
|
||||
loadProfile(pk),
|
||||
loadFollowList(pk),
|
||||
loadMuteList(pk),
|
||||
]),
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {on, always, call, dissoc, assoc, uniq} from "@welshman/lib"
|
||||
import {RelayMode} from "@welshman/util"
|
||||
import type {Socket, RelayMessage, ClientMessage} from "@welshman/net"
|
||||
import {
|
||||
makeSocketPolicyAuth,
|
||||
@@ -13,7 +14,7 @@ import {
|
||||
isClientNegOpen,
|
||||
isClientNegClose,
|
||||
} from "@welshman/net"
|
||||
import {sign} from "@welshman/app"
|
||||
import {sign, pubkey, getPubkeyRelays} from "@welshman/app"
|
||||
import {
|
||||
userSettingsValues,
|
||||
getSetting,
|
||||
@@ -23,6 +24,22 @@ import {
|
||||
|
||||
export const authPolicy = makeSocketPolicyAuth({sign, shouldAuth: always(true)})
|
||||
|
||||
export const blockPolicy = (socket: Socket) => {
|
||||
const previousOpen = socket.open
|
||||
|
||||
socket.open = () => {
|
||||
const $pubkey = pubkey.get()
|
||||
|
||||
if (!$pubkey || !getPubkeyRelays($pubkey, RelayMode.Blocked).includes(socket.url)) {
|
||||
return previousOpen()
|
||||
}
|
||||
}
|
||||
|
||||
return () => {
|
||||
socket.open = previousOpen
|
||||
}
|
||||
}
|
||||
|
||||
export const trustPolicy = (socket: Socket) => {
|
||||
const buffer: RelayMessage[] = []
|
||||
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
import {setupHistory} from "@app/util/history"
|
||||
import {setupTracking} from "@app/util/tracking"
|
||||
import {setupAnalytics} from "@app/util/analytics"
|
||||
import {authPolicy, trustPolicy, mostlyRestrictedPolicy} from "@app/util/policies"
|
||||
import {authPolicy, blockPolicy, trustPolicy, mostlyRestrictedPolicy} from "@app/util/policies"
|
||||
import {kv, db} from "@app/core/storage"
|
||||
import {userSettingsValues} from "@app/core/state"
|
||||
import {syncApplicationData} from "@app/core/sync"
|
||||
@@ -43,7 +43,7 @@
|
||||
|
||||
const {children} = $props()
|
||||
|
||||
const policies = [authPolicy, trustPolicy, mostlyRestrictedPolicy]
|
||||
const policies = [authPolicy, blockPolicy, trustPolicy, mostlyRestrictedPolicy]
|
||||
|
||||
// Add stuff to window for convenience
|
||||
Object.assign(window, {
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
<script lang="ts">
|
||||
import {onMount} from "svelte"
|
||||
import {pubkey, getRelayLists, getMessagingRelayLists, derivePubkeyRelays} from "@welshman/app"
|
||||
import {
|
||||
pubkey,
|
||||
getRelayLists,
|
||||
derivePubkeyRelays,
|
||||
addRelay,
|
||||
removeRelay,
|
||||
addBlockedRelay,
|
||||
removeBlockedRelay,
|
||||
addMessagingRelay,
|
||||
removeMessagingRelay,
|
||||
} from "@welshman/app"
|
||||
import {RelayMode} from "@welshman/util"
|
||||
import Icon from "@lib/components/Icon.svelte"
|
||||
import Button from "@lib/components/Button.svelte"
|
||||
@@ -9,43 +19,42 @@
|
||||
import RelayAdd from "@app/components/RelayAdd.svelte"
|
||||
import {pushModal} from "@app/util/modal"
|
||||
import {discoverRelays} from "@app/core/requests"
|
||||
import {setRelayPolicy, setMessagingRelayPolicy} from "@app/core/commands"
|
||||
import Globus from "@assets/icons/globus.svg?dataurl"
|
||||
import Inbox from "@assets/icons/inbox.svg?dataurl"
|
||||
import Mailbox from "@assets/icons/mailbox.svg?dataurl"
|
||||
import ForbiddenCircle from "@assets/icons/forbidden-circle.svg?dataurl"
|
||||
import CloseCircle from "@assets/icons/close-circle.svg?dataurl"
|
||||
import AddCircle from "@assets/icons/add-circle.svg?dataurl"
|
||||
|
||||
const readRelayUrls = derivePubkeyRelays($pubkey!, RelayMode.Read)
|
||||
const writeRelayUrls = derivePubkeyRelays($pubkey!, RelayMode.Write)
|
||||
const blockedRelayUrls = derivePubkeyRelays($pubkey!, RelayMode.Blocked)
|
||||
const messagingRelayUrls = derivePubkeyRelays($pubkey!, RelayMode.Messaging)
|
||||
|
||||
const addReadRelay = () =>
|
||||
const addReadRelays = () =>
|
||||
pushModal(RelayAdd, {
|
||||
relays: readRelayUrls,
|
||||
addRelay: (url: string) => setRelayPolicy(url, true, $writeRelayUrls.includes(url)),
|
||||
addRelay: (url: string) => addRelay(url, RelayMode.Read),
|
||||
})
|
||||
|
||||
const addWriteRelay = () =>
|
||||
const addWriteRelays = () =>
|
||||
pushModal(RelayAdd, {
|
||||
relays: writeRelayUrls,
|
||||
addRelay: (url: string) => setRelayPolicy(url, $readRelayUrls.includes(url), true),
|
||||
addRelay: (url: string) => addRelay(url, RelayMode.Write),
|
||||
})
|
||||
|
||||
const addMessagingRelay = () =>
|
||||
pushModal(RelayAdd, {
|
||||
relays: messagingRelayUrls,
|
||||
addRelay: (url: string) => setMessagingRelayPolicy(url, true),
|
||||
})
|
||||
const addBlockedRelays = () =>
|
||||
pushModal(RelayAdd, {relays: blockedRelayUrls, addRelay: addBlockedRelay})
|
||||
|
||||
const removeReadRelay = (url: string) => setRelayPolicy(url, false, $writeRelayUrls.includes(url))
|
||||
const addMessagingRelays = () =>
|
||||
pushModal(RelayAdd, {relays: messagingRelayUrls, addRelay: addMessagingRelay})
|
||||
|
||||
const removeWriteRelay = (url: string) => setRelayPolicy(url, $readRelayUrls.includes(url), false)
|
||||
const removeReadRelay = (url: string) => removeRelay(url, RelayMode.Read)
|
||||
|
||||
const removeMessagingRelay = (url: string) => setMessagingRelayPolicy(url, false)
|
||||
const removeWriteRelay = (url: string) => removeRelay(url, RelayMode.Write)
|
||||
|
||||
onMount(() => {
|
||||
discoverRelays([...getRelayLists(), ...getMessagingRelayLists()])
|
||||
discoverRelays(getRelayLists())
|
||||
})
|
||||
</script>
|
||||
|
||||
@@ -77,7 +86,7 @@
|
||||
{:else}
|
||||
<p class="text-center text-sm">No relays found</p>
|
||||
{/each}
|
||||
<Button class="btn btn-primary mt-2" onclick={addWriteRelay}>
|
||||
<Button class="btn btn-primary mt-2" onclick={addWriteRelays}>
|
||||
<Icon icon={AddCircle} />
|
||||
Add Relay
|
||||
</Button>
|
||||
@@ -109,7 +118,7 @@
|
||||
{:else}
|
||||
<p class="text-center text-sm">No relays found</p>
|
||||
{/each}
|
||||
<Button class="btn btn-primary mt-2" onclick={addReadRelay}>
|
||||
<Button class="btn btn-primary mt-2" onclick={addReadRelays}>
|
||||
<Icon icon={AddCircle} />
|
||||
Add Relay
|
||||
</Button>
|
||||
@@ -142,7 +151,38 @@
|
||||
{:else}
|
||||
<p class="text-center text-sm">No relays found</p>
|
||||
{/each}
|
||||
<Button class="btn btn-primary mt-2" onclick={addMessagingRelay}>
|
||||
<Button class="btn btn-primary mt-2" onclick={addMessagingRelays}>
|
||||
<Icon icon={AddCircle} />
|
||||
Add Relay
|
||||
</Button>
|
||||
</div>
|
||||
</Collapse>
|
||||
<Collapse class="card2 bg-alt column gap-4 shadow-md">
|
||||
{#snippet title()}
|
||||
<h2 class="flex items-center gap-3 text-xl">
|
||||
<Icon icon={ForbiddenCircle} />
|
||||
Blocked Relays
|
||||
</h2>
|
||||
{/snippet}
|
||||
{#snippet description()}
|
||||
<p class="text-sm">
|
||||
These relays will never be connected to by clients supporting this policy.
|
||||
</p>
|
||||
{/snippet}
|
||||
<div class="column gap-2">
|
||||
{#each $blockedRelayUrls.sort() as url (url)}
|
||||
<RelayItem {url}>
|
||||
<Button
|
||||
class="tooltip flex items-center"
|
||||
data-tip="Stop using this relay"
|
||||
onclick={() => removeBlockedRelay(url)}>
|
||||
<Icon icon={CloseCircle} />
|
||||
</Button>
|
||||
</RelayItem>
|
||||
{:else}
|
||||
<p class="text-center text-sm">No relays found</p>
|
||||
{/each}
|
||||
<Button class="btn btn-primary mt-2" onclick={addBlockedRelays}>
|
||||
<Icon icon={AddCircle} />
|
||||
Add Relay
|
||||
</Button>
|
||||
|
||||
Reference in New Issue
Block a user