mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-17 14:04:21 +01:00
initial
This commit is contained in:
52
src/clients/useCommands.ts
Normal file
52
src/clients/useCommands.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { getEventHash } from 'nostr-tools/event';
|
||||
import type { Event as NostrEvent } from 'nostr-tools/event';
|
||||
|
||||
import usePool from '@/clients/usePool';
|
||||
|
||||
type UseCommands = {
|
||||
publishTextNote: ({ content }: { content: string }) => Promise<void>;
|
||||
};
|
||||
|
||||
const useCommands = (): UseCommands => {
|
||||
const pool = usePool();
|
||||
|
||||
const publishTextNote = async ({
|
||||
relayUrls,
|
||||
pubkey,
|
||||
content,
|
||||
}: {
|
||||
relayUrls: string[];
|
||||
pubkey: string;
|
||||
content: string;
|
||||
}) => {
|
||||
const preSignedEvent: NostrEvent = {
|
||||
kind: 1,
|
||||
pubkey,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
tags: [],
|
||||
content,
|
||||
};
|
||||
preSignedEvent.id = getEventHash(preSignedEvent);
|
||||
const signedEvent = await window.nostr.signEvent(preSignedEvent);
|
||||
|
||||
return relayUrls.map(async (relayUrl) => {
|
||||
const relay = await pool().ensureRelay(relayUrl);
|
||||
const pub = relay.publish(signedEvent);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
pub.on('ok', () => {
|
||||
console.log(`${relayUrl} has accepted our event`);
|
||||
resolve(null);
|
||||
});
|
||||
pub.on('failed', (reason: any) => {
|
||||
console.log(`failed to publish to ${relayUrl}: ${reason}`);
|
||||
reject(reason);
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
return { publishTextNote };
|
||||
};
|
||||
|
||||
export default useCommands;
|
||||
Reference in New Issue
Block a user