refactor: type subscriptions.ts

typed subscribe functions in subscriptions.ts
pull/72/head
OFF0 1 year ago
parent 309367852a
commit 35b8baef92
Signed by: offbyn
GPG Key ID: 94A2F643C51F37FA

@ -1,5 +1,6 @@
import {generatePrivateKey, getEventHash, getPublicKey, nip19, signEvent} from 'nostr-tools';
import {publish, sub, unsubAll} from './relays';
import {sub24hFeed, subNote, subProfile} from './subscriptions'
import {publish} from './relays';
import {bounce} from './utils.js';
import {zeroLeadingBitsCount} from './cryptoutils.js';
import {elem, parseTextContent} from './domutil.js';
@ -35,59 +36,6 @@ let pubkey = localStorage.getItem('pub_key') || (() => {
return pubkey;
})();
function sub24hFeed() {
sub({
cb: onEvent,
filter: {
kinds: [0, 1, 2, 7],
// until: Math.floor(Date.now() * 0.001),
since: Math.floor((Date.now() * 0.001) - (24 * 60 * 60)),
limit: 50,
}
});
}
function subNoteAndProfile(id) {
// view(`/${id}`); // assume text note
subTextNote(id);
subProfile(id);
}
function subTextNote(eventId) {
sub({
cb: onEvent,
filter: {
ids: [eventId],
kinds: [1],
limit: 1,
}
});
}
function subProfile(pubkey) {
sub({
cb: (evt, relay) => {
console.log('found profile, unsub subTextNote somehow')
// renderProfile(evt, relay);
// view('/[profile]');
},
filter: {
authors: [pubkey],
kinds: [0],
limit: 1,
}
});
// get notes for profile
sub({
cb: onEvent,
filter: {
authors: [pubkey],
kinds: [1],
limit: 50,
}
});
}
const containers = [
// {
// id: '/00527c2b28ea78446c148cb40cc6e442ea3d0945ff5a8b71076483398525b54d',
@ -836,19 +784,18 @@ function view(route) {
// subscribe and change view
function route(path) {
unsubAll();
if (path === '/') {
sub24hFeed();
sub24hFeed(onEvent);
view('/');
} else if (path.length === 64 && path.match(/^\/[0-9a-z]+$/)) {
const {type, data} = nip19.decode(path.slice(1));
switch(type) {
case 'note':
subNote(data);
subNote(data, onEvent);
view(path);
break;
case 'npub':
subProfile(data);
subProfile(data, onEvent);
view(path);
break;
default:

@ -0,0 +1,69 @@
import {Event} from 'nostr-tools';
import {sub, unsubAll} from './relays';
type SubCallback = (
event: Event,
relay: string,
) => void;
/** subscribe to global feed */
export const sub24hFeed = (onEvent: SubCallback) => {
unsubAll();
sub({
cb: onEvent,
filter: {
kinds: [0, 1, 2, 7],
// until: Math.floor(Date.now() * 0.001),
since: Math.floor((Date.now() * 0.001) - (24 * 60 * 60)),
limit: 50,
}
});
};
/** subscribe to a note id (nip-19) */
export const subNote = (
eventId: string,
onEvent: SubCallback,
) => {
unsubAll();
sub({
cb: onEvent,
filter: {
ids: [eventId],
kinds: [1],
limit: 1,
}
});
sub({
cb: onEvent,
filter: {
'#e': [eventId],
kinds: [1, 7],
}
});
};
/** subscribe to npub key (nip-19) */
export const subProfile = (
pubkey: string,
onEvent: SubCallback,
) => {
unsubAll();
sub({
cb: onEvent,
filter: {
authors: [pubkey],
kinds: [0],
limit: 1,
}
});
// get notes for profile
sub({
cb: onEvent,
filter: {
authors: [pubkey],
kinds: [1],
limit: 50,
}
});
};
Loading…
Cancel
Save