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

124 lines
3.0 KiB
JavaScript

import Service from '@ember/service';
import { sha256 } from '@noble/hashes/sha2.js';
import { bytesToHex } from '@noble/hashes/utils.js';
import { PHOTO_CACHE_NAME, PHOTO_CACHE_ORIGIN } from '../config/nostr';
// Cache Storage wrapper for full-size originals and thumbnails. Originals are
// keyed by their content hash; thumbnails are keyed by their Blossom URL.
// Object URLs are reused for the lifetime of the service.
export default class PhotoCacheService extends Service {
objectUrls = new Map();
get cacheStorage() {
return typeof caches === 'undefined' ? null : caches;
}
async openCache() {
if (!this.cacheStorage) {
throw new Error('Cache Storage is unavailable');
}
return this.cacheStorage.open(PHOTO_CACHE_NAME);
}
originalKey(photo) {
return `${PHOTO_CACHE_ORIGIN}/photos/${photo.sha256 ?? photo.id}`;
}
thumbKey(photo) {
return photo.thumbUrl;
}
async cachedResponse(key) {
try {
const cache = await this.openCache();
return await cache.match(key);
} catch {
return null;
}
}
async put(url, key, { expectedSha256 } = {}) {
const response = await fetch(url, { credentials: 'omit' });
if (!response.ok) {
throw new Error(`Failed to fetch photo: HTTP ${response.status}`);
}
const blob = await response.clone().blob();
if (expectedSha256) {
const actual = bytesToHex(
sha256(new Uint8Array(await blob.arrayBuffer()))
);
if (actual !== expectedSha256) {
throw new Error('Photo hash mismatch');
}
}
const cache = await this.openCache();
await cache.put(key, response.clone());
return blob;
}
async objectUrlFor(key) {
const existing = this.objectUrls.get(key);
if (existing) {
return existing;
}
const response = await this.cachedResponse(key);
if (!response) {
return null;
}
const blob = await response.blob();
const url = URL.createObjectURL(blob);
this.objectUrls.set(key, url);
return url;
}
originalUrl(photo) {
return this.objectUrlFor(this.originalKey(photo));
}
thumbUrl(photo) {
return this.objectUrlFor(this.thumbKey(photo));
}
async prefetchOriginal(photo) {
const key = this.originalKey(photo);
const cached = await this.objectUrlFor(key);
if (cached) {
return cached;
}
const blob = await this.put(photo.url, key, {
expectedSha256: photo.sha256,
});
const url = URL.createObjectURL(blob);
this.objectUrls.set(key, url);
return url;
}
async prefetchThumb(photo) {
const key = this.thumbKey(photo);
const cached = await this.objectUrlFor(key);
if (cached) {
return cached;
}
const blob = await this.put(photo.thumbUrl, key);
const url = URL.createObjectURL(blob);
this.objectUrls.set(key, url);
return url;
}
willDestroy() {
for (const url of this.objectUrls.values()) {
URL.revokeObjectURL(url);
}
this.objectUrls.clear();
super.willDestroy(...arguments);
}
}