Re-adding global fetch.

This commit is contained in:
Stephen Vanderwarker 2025-01-29 17:12:15 -05:00
parent 435200ccb3
commit c3f521f5b0

@ -14,6 +14,7 @@ let {
bech32,
bech32m
} = require('bech32');
let letsCID;
let signedEvent;
@ -77,9 +78,57 @@ if (fs.existsSync(configPath)) {
xmpp.send(from, 'https://wiki.vanderwarker.family/doku.php?id=code:nostrsms:commands');
debug();
// reauth (I am not sure this is possible so...)
} else if (message === "!auth") {
xmpp.send(from, "https://auth.nostrsms.com");
// get latest 10 posts from global
} else if (message === "!g") {
xmpp.send(from, `Here are the latest 10 posts from global on ${config.relays.read}`);
async function getGlobalEvents() {
// use read relays in config.ini
const relay = nostr.relayInit(config.relays.read, WebSocket);
// connect to read relay
relay.on('connect', () => {
console.log(`connected to ${relay.url}`);
});
relay.on('error', () => {
console.log(`failed to connect to ${relay.url}`);
});
await relay.connect();
// create subscription to get events
let sub = relay.sub([{
kinds: [1],
limit: 10
}, ])
// we've go an event! time to parse!
sub.on('event', event => {
console.log('got event:', event);
// Check if the event is already an object
const globalEvents = typeof event === 'string' ? JSON.parse(event) : event;
// if globalEvents.* is set, set to const for later use
if (globalEvents && globalEvents.content && globalEvents.pubkey && globalEvents.created_at) {
const content = globalEvents.content;
const author = globalEvents.pubkey;
const createdAt = globalEvents.created_at;
// WIP move to debug
console.log('Content:', content);
console.log('Author:', author);
console.log('Created At:', createdAt);
// send the latest posts to the user whom requested them
xmpp.send(from, `\"${content}\", - ${author} @ ${createdAt}`);
relay.close();
} else {
console.error('Invalid event structure or missing required properties.');
}
});
}
// let's run the above code to get our events and send them on XMPP/SMS! :)
getGlobalEvents().catch((error) => {
console.error(error);
});
// Well they didn't use a command, so let's get to the NOSTR STUFF!!1!
} else {