mirror of
https://github.com/aljazceru/rabbit.git
synced 2025-12-17 14:04:21 +01:00
update
This commit is contained in:
@@ -1,56 +1,89 @@
|
||||
import { getEventHash } from 'nostr-tools/event';
|
||||
import type { Event as NostrEvent } from 'nostr-tools/event';
|
||||
import type { Pub } from 'nostr-tools/relay';
|
||||
|
||||
import usePool from '@/clients/usePool';
|
||||
|
||||
type UseCommands = {
|
||||
publishTextNote: (props: {
|
||||
relayUrls: string[];
|
||||
pubkey: string;
|
||||
content: string;
|
||||
}) => Promise<void>;
|
||||
// NIP-20: Command Result
|
||||
const waitCommandResult = (pub: Pub): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
pub.on('ok', () => {
|
||||
console.log(`${relayUrl} has accepted our event`);
|
||||
resolve();
|
||||
});
|
||||
pub.on('failed', (reason: string) => {
|
||||
console.log(`failed to publish to ${relayUrl}: ${reason}`);
|
||||
reject(reason);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const useCommands = (): UseCommands => {
|
||||
const 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,
|
||||
};
|
||||
const publishEvent = async (relayUrls: string[], event: NostrEvent): Promise<Promise<void>[]> => {
|
||||
const preSignedEvent: NostrEvent = { ...event };
|
||||
preSignedEvent.id = getEventHash(preSignedEvent);
|
||||
const signedEvent = await window.nostr.signEvent(preSignedEvent);
|
||||
// TODO define window.nostr
|
||||
const signedEvent = (await window.nostr.signEvent(preSignedEvent)) as NostrEvent;
|
||||
|
||||
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 waitCommandResult(pub);
|
||||
});
|
||||
};
|
||||
|
||||
return { publishTextNote };
|
||||
return {
|
||||
// NIP-01
|
||||
publishTextNote({
|
||||
relayUrls,
|
||||
pubkey,
|
||||
content,
|
||||
}: {
|
||||
relayUrls: string[];
|
||||
pubkey: string;
|
||||
content: string;
|
||||
}): Promise<Promise<void>[]> {
|
||||
const preSignedEvent: NostrEvent = {
|
||||
kind: 1,
|
||||
pubkey,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
tags: [],
|
||||
content,
|
||||
};
|
||||
// TODO define window.nostr
|
||||
return publishEvent(relayUrls, preSignedEvent);
|
||||
},
|
||||
// NIP-25
|
||||
publishReaction({
|
||||
relayUrls,
|
||||
pubkey,
|
||||
content,
|
||||
eventId,
|
||||
notifyPubkey,
|
||||
}: {
|
||||
relayUrls: string[];
|
||||
pubkey: string;
|
||||
content: string;
|
||||
eventId: string;
|
||||
notifyPubkey: string;
|
||||
}): Promise<Promise<void>[]> {
|
||||
// TODO ensure that content is + or - or emoji.
|
||||
const preSignedEvent: NostrEvent = {
|
||||
kind: 7,
|
||||
pubkey,
|
||||
created_at: Math.floor(Date.now() / 1000),
|
||||
tags: [
|
||||
['e', eventId],
|
||||
['p', notifyPubkey],
|
||||
],
|
||||
content,
|
||||
};
|
||||
// TODO define window.nostr
|
||||
return publishEvent(relayUrls, preSignedEvent);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default useCommands;
|
||||
|
||||
Reference in New Issue
Block a user