Files
raucao 6750c61d87 Add Nostr photo feeds and wallpaper selection
Read the curated photo feeds from wss://nostr.kosmos.org, verify events, cache originals and thumbnails, and let users pick a wallpaper from the Photos section. Nature is the default feed; City Nights is opt-in. Adds the manual publishing scripts, real event fixtures, protocol documentation, and tests with stubbed network connections.
2026-11-08 21:51:23 +04:00

152 lines
3.6 KiB
JavaScript

import Service from '@ember/service';
import {
NOSTR_PHOTO_CURATOR,
NOSTR_QUERY_TIMEOUT,
NOSTR_RELAY_URL,
PHOTO_FEED_TAG,
PHOTO_ID_CHUNK_SIZE,
} from '../config/nostr';
import { verifyEvent } from '../utils/nostr-events';
let subscriptionCounter = 0;
// Minimal read-only Nostr relay client. Each query opens one WebSocket,
// subscribes, collects verified events until EOSE, and closes. Signing and
// publishing are deliberately out of scope for the app.
export default class NostrService extends Service {
url = NOSTR_RELAY_URL;
createSocket(url) {
return new WebSocket(url);
}
query(
filters,
{ timeout = NOSTR_QUERY_TIMEOUT, signal, verify = true } = {}
) {
const subscriptionId = `dashtab-${(subscriptionCounter += 1)}`;
const socket = this.createSocket(this.url);
const events = [];
return new Promise((resolve, reject) => {
let settled = false;
let timer;
const cleanup = () => {
clearTimeout(timer);
signal?.removeEventListener('abort', onAbort);
try {
socket.close();
} catch {
// The socket may already be closed.
}
};
const finish = (error) => {
if (settled) {
return;
}
settled = true;
cleanup();
if (error) {
reject(error);
} else {
resolve(events);
}
};
const onAbort = () => finish(new Error('Nostr query aborted'));
const onOpen = () => {
try {
socket.send(JSON.stringify(['REQ', subscriptionId, ...filters]));
} catch (error) {
finish(error);
}
};
const onMessage = (messageEvent) => {
let message;
try {
message = JSON.parse(messageEvent.data);
} catch {
return;
}
if (!Array.isArray(message)) {
return;
}
const [type, id] = message;
if (id !== subscriptionId) {
return;
}
if (type === 'EVENT') {
const event = message[2];
if (!verify || verifyEvent(event)) {
events.push(event);
}
} else if (type === 'EOSE') {
finish();
} else if (type === 'CLOSED') {
finish(new Error(message[2] || 'Nostr subscription closed'));
}
};
const onError = () => finish(new Error('Nostr relay connection failed'));
const onClose = () => finish(new Error('Nostr relay connection closed'));
socket.addEventListener('open', onOpen);
socket.addEventListener('message', onMessage);
socket.addEventListener('error', onError);
socket.addEventListener('close', onClose);
if (signal) {
if (signal.aborted) {
finish(new Error('Nostr query aborted'));
return;
}
signal.addEventListener('abort', onAbort, { once: true });
}
timer = setTimeout(
() => finish(new Error('Nostr query timed out')),
timeout
);
});
}
queryFeedEvents() {
return this.query([
{
kinds: [30006],
authors: [NOSTR_PHOTO_CURATOR],
'#t': [PHOTO_FEED_TAG],
limit: 20,
},
]);
}
async queryPhotos(photoIds) {
const uniqueIds = [...new Set(photoIds)];
const events = [];
for (
let index = 0;
index < uniqueIds.length;
index += PHOTO_ID_CHUNK_SIZE
) {
const ids = uniqueIds.slice(index, index + PHOTO_ID_CHUNK_SIZE);
const chunk = await this.query([{ kinds: [20], ids, limit: ids.length }]);
events.push(...chunk);
}
return events;
}
}