mirror of
https://github.com/hzrd149/nostrudel.git
synced 2025-09-25 11:13:30 +02:00
Merge pull request #161 from fr4nzap/event-publisher-custom-relay
Publish to custom relay
This commit is contained in:
@@ -22,12 +22,14 @@ type PublishContextType = {
|
|||||||
event: EventTemplate | NostrEvent,
|
event: EventTemplate | NostrEvent,
|
||||||
additionalRelays: Iterable<string> | undefined,
|
additionalRelays: Iterable<string> | undefined,
|
||||||
quite: false,
|
quite: false,
|
||||||
|
onlyAdditionalRelays: false
|
||||||
): Promise<NostrPublishAction>;
|
): Promise<NostrPublishAction>;
|
||||||
publishEvent(
|
publishEvent(
|
||||||
label: string,
|
label: string,
|
||||||
event: EventTemplate | NostrEvent,
|
event: EventTemplate | NostrEvent,
|
||||||
additionalRelays?: Iterable<string> | undefined,
|
additionalRelays?: Iterable<string> | undefined,
|
||||||
quite?: boolean,
|
quite?: boolean,
|
||||||
|
onlyAdditionalRelays?: boolean
|
||||||
): Promise<NostrPublishAction | undefined>;
|
): Promise<NostrPublishAction | undefined>;
|
||||||
};
|
};
|
||||||
export const PublishContext = createContext<PublishContextType>({
|
export const PublishContext = createContext<PublishContextType>({
|
||||||
@@ -47,14 +49,19 @@ export default function PublishProvider({ children }: PropsWithChildren) {
|
|||||||
const { requestSignature } = useSigningContext();
|
const { requestSignature } = useSigningContext();
|
||||||
|
|
||||||
const publishEvent = useCallback(
|
const publishEvent = useCallback(
|
||||||
async (label: string, event: DraftNostrEvent | NostrEvent, additionalRelays?: Iterable<string>, quite = true) => {
|
async (label: string, event: DraftNostrEvent | NostrEvent, additionalRelays?: Iterable<string>, quite = true, onlyAdditionalRelays = false) => {
|
||||||
try {
|
try {
|
||||||
const relays = RelaySet.from(
|
let relays;
|
||||||
clientRelaysService.writeRelays.value,
|
if (onlyAdditionalRelays) {
|
||||||
clientRelaysService.outbox,
|
relays = RelaySet.from(additionalRelays);
|
||||||
additionalRelays,
|
} else {
|
||||||
getAllRelayHints(event),
|
relays = RelaySet.from(
|
||||||
);
|
clientRelaysService.writeRelays.value,
|
||||||
|
clientRelaysService.outbox,
|
||||||
|
additionalRelays,
|
||||||
|
getAllRelayHints(event),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
let signed: NostrEvent;
|
let signed: NostrEvent;
|
||||||
if (!Object.hasOwn(event, "sig")) {
|
if (!Object.hasOwn(event, "sig")) {
|
||||||
|
@@ -12,6 +12,8 @@ import {
|
|||||||
ModalFooter,
|
ModalFooter,
|
||||||
ModalHeader,
|
ModalHeader,
|
||||||
ModalOverlay,
|
ModalOverlay,
|
||||||
|
Switch,
|
||||||
|
useDisclosure,
|
||||||
useToast,
|
useToast,
|
||||||
} from "@chakra-ui/react";
|
} from "@chakra-ui/react";
|
||||||
import VerticalPageLayout from "../../../components/vertical-page-layout";
|
import VerticalPageLayout from "../../../components/vertical-page-layout";
|
||||||
@@ -26,6 +28,7 @@ import { useSigningContext } from "../../../providers/global/signing-provider";
|
|||||||
import { usePublishEvent } from "../../../providers/global/publish-provider";
|
import { usePublishEvent } from "../../../providers/global/publish-provider";
|
||||||
import useCurrentAccount from "../../../hooks/use-current-account";
|
import useCurrentAccount from "../../../hooks/use-current-account";
|
||||||
import UserAvatar from "../../../components/user/user-avatar";
|
import UserAvatar from "../../../components/user/user-avatar";
|
||||||
|
import { RelayUrlInput } from "../../../components/relay-url-input";
|
||||||
|
|
||||||
export default function EventPublisherView() {
|
export default function EventPublisherView() {
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
@@ -33,6 +36,8 @@ export default function EventPublisherView() {
|
|||||||
const { requestSignature } = useSigningContext();
|
const { requestSignature } = useSigningContext();
|
||||||
const publish = usePublishEvent();
|
const publish = usePublishEvent();
|
||||||
const account = useCurrentAccount();
|
const account = useCurrentAccount();
|
||||||
|
const customRelay = useDisclosure();
|
||||||
|
const [customRelayURL, setCustomRelayURL] = useState("");
|
||||||
|
|
||||||
const defaultEvent = useMemo(
|
const defaultEvent = useMemo(
|
||||||
() =>
|
() =>
|
||||||
@@ -72,7 +77,11 @@ export default function EventPublisherView() {
|
|||||||
setLoading(true);
|
setLoading(true);
|
||||||
const valid = verifyEvent(draft as NostrEvent);
|
const valid = verifyEvent(draft as NostrEvent);
|
||||||
if (!valid) throw new Error("Invalid event");
|
if (!valid) throw new Error("Invalid event");
|
||||||
await publish("Custom Event", draft);
|
if (customRelayURL) {
|
||||||
|
await publish("Custom Event", draft, [customRelayURL], true, true);
|
||||||
|
} else {
|
||||||
|
await publish("Custom Event", draft);
|
||||||
|
}
|
||||||
setDraft(undefined);
|
setDraft(undefined);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Error) toast({ description: e.message, status: "error" });
|
if (e instanceof Error) toast({ description: e.message, status: "error" });
|
||||||
@@ -89,7 +98,11 @@ export default function EventPublisherView() {
|
|||||||
const event = await requestSignature(draft);
|
const event = await requestSignature(draft);
|
||||||
const valid = verifyEvent(event);
|
const valid = verifyEvent(event);
|
||||||
if (!valid) throw new Error("Invalid event");
|
if (!valid) throw new Error("Invalid event");
|
||||||
await publish("Custom Event", event);
|
if (customRelayURL) {
|
||||||
|
await publish("Custom Event", draft, [customRelayURL], true, true);
|
||||||
|
} else {
|
||||||
|
await publish("Custom Event", draft);
|
||||||
|
}
|
||||||
setDraft(undefined);
|
setDraft(undefined);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof Error) toast({ description: e.message, status: "error" });
|
if (e instanceof Error) toast({ description: e.message, status: "error" });
|
||||||
@@ -103,6 +116,18 @@ export default function EventPublisherView() {
|
|||||||
<Flex gap="2" alignItems="center" wrap="wrap">
|
<Flex gap="2" alignItems="center" wrap="wrap">
|
||||||
<BackButton size="sm" />
|
<BackButton size="sm" />
|
||||||
<Heading size="md">Event Publisher</Heading>
|
<Heading size="md">Event Publisher</Heading>
|
||||||
|
<Switch size="sm" checked={customRelay.isOpen} onChange={customRelay.onToggle}>
|
||||||
|
Publish to Relay
|
||||||
|
</Switch>
|
||||||
|
{customRelay.isOpen && (
|
||||||
|
<RelayUrlInput
|
||||||
|
size="sm"
|
||||||
|
borderRadius="md"
|
||||||
|
w="xs"
|
||||||
|
value={customRelayURL}
|
||||||
|
onChange={(e) => setCustomRelayURL(e.target.value)}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
<ButtonGroup ml="auto">
|
<ButtonGroup ml="auto">
|
||||||
{/* <IconButton icon={<HelpCircle />} aria-label="Help" title="Help" size="sm" onClick={helpModal.onOpen} /> */}
|
{/* <IconButton icon={<HelpCircle />} aria-label="Help" title="Help" size="sm" onClick={helpModal.onOpen} /> */}
|
||||||
<Button colorScheme="primary" onClick={submitEvent} leftIcon={<Play />} size="sm">
|
<Button colorScheme="primary" onClick={submitEvent} leftIcon={<Play />} size="sm">
|
||||||
|
Reference in New Issue
Block a user