36 changed files with 949 additions and 1092 deletions
@ -1,63 +1,69 @@ |
|||||||
html, body { |
html, |
||||||
position: relative; |
body { |
||||||
width: 100%; |
position: relative; |
||||||
height: 100%; |
width: 100%; |
||||||
|
height: 100%; |
||||||
} |
} |
||||||
|
|
||||||
body { |
body { |
||||||
color: #333; |
color: #333; |
||||||
margin: 0; |
margin: 0; |
||||||
padding: 8px; |
padding: 8px; |
||||||
box-sizing: border-box; |
box-sizing: border-box; |
||||||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; |
font-family: |
||||||
|
-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, |
||||||
|
Cantarell, "Helvetica Neue", sans-serif; |
||||||
} |
} |
||||||
|
|
||||||
a { |
a { |
||||||
color: rgb(0,100,200); |
color: rgb(0, 100, 200); |
||||||
text-decoration: none; |
text-decoration: none; |
||||||
} |
} |
||||||
|
|
||||||
a:hover { |
a:hover { |
||||||
text-decoration: underline; |
text-decoration: underline; |
||||||
} |
} |
||||||
|
|
||||||
a:visited { |
a:visited { |
||||||
color: rgb(0,80,160); |
color: rgb(0, 80, 160); |
||||||
} |
} |
||||||
|
|
||||||
label { |
label { |
||||||
display: block; |
display: block; |
||||||
} |
} |
||||||
|
|
||||||
input, button, select, textarea { |
input, |
||||||
font-family: inherit; |
button, |
||||||
font-size: inherit; |
select, |
||||||
-webkit-padding: 0.4em 0; |
textarea { |
||||||
padding: 0.4em; |
font-family: inherit; |
||||||
margin: 0 0 0.5em 0; |
font-size: inherit; |
||||||
box-sizing: border-box; |
-webkit-padding: 0.4em 0; |
||||||
border: 1px solid #ccc; |
padding: 0.4em; |
||||||
border-radius: 2px; |
margin: 0 0 0.5em 0; |
||||||
|
box-sizing: border-box; |
||||||
|
border: 1px solid #ccc; |
||||||
|
border-radius: 2px; |
||||||
} |
} |
||||||
|
|
||||||
input:disabled { |
input:disabled { |
||||||
color: #ccc; |
color: #ccc; |
||||||
} |
} |
||||||
|
|
||||||
button { |
button { |
||||||
color: #333; |
color: #333; |
||||||
background-color: #f4f4f4; |
background-color: #f4f4f4; |
||||||
outline: none; |
outline: none; |
||||||
} |
} |
||||||
|
|
||||||
button:disabled { |
button:disabled { |
||||||
color: #999; |
color: #999; |
||||||
} |
} |
||||||
|
|
||||||
button:not(:disabled):active { |
button:not(:disabled):active { |
||||||
background-color: #ddd; |
background-color: #ddd; |
||||||
} |
} |
||||||
|
|
||||||
button:focus { |
button:focus { |
||||||
border-color: #666; |
border-color: #666; |
||||||
} |
} |
||||||
|
|||||||
@ -1,18 +1,17 @@ |
|||||||
<!DOCTYPE html> |
<!doctype html> |
||||||
<html lang="en"> |
<html lang="en"> |
||||||
<head> |
<head> |
||||||
<meta charset='utf-8'> |
<meta charset="utf-8" /> |
||||||
<meta name='viewport' content='width=device-width,initial-scale=1'> |
<meta name="viewport" content="width=device-width,initial-scale=1" /> |
||||||
|
|
||||||
<title>ORLY?</title> |
<title>ORLY?</title> |
||||||
|
|
||||||
<link rel='icon' type='image/png' href='/orly.png'> |
<link rel="icon" type="image/png" href="/orly.png" /> |
||||||
<link rel='stylesheet' href='/global.css'> |
<link rel="stylesheet" href="/global.css" /> |
||||||
<link rel='stylesheet' href='/build/bundle.css'> |
<link rel="stylesheet" href="/build/bundle.css" /> |
||||||
|
|
||||||
<script defer src='/build/bundle.js'></script> |
<script defer src="/build/bundle.js"></script> |
||||||
</head> |
</head> |
||||||
|
|
||||||
<body> |
<body></body> |
||||||
</body> |
|
||||||
</html> |
</html> |
||||||
|
|||||||
@ -1,78 +1,78 @@ |
|||||||
import { spawn } from 'child_process'; |
import { spawn } from "child_process"; |
||||||
import svelte from 'rollup-plugin-svelte'; |
import svelte from "rollup-plugin-svelte"; |
||||||
import commonjs from '@rollup/plugin-commonjs'; |
import commonjs from "@rollup/plugin-commonjs"; |
||||||
import terser from '@rollup/plugin-terser'; |
import terser from "@rollup/plugin-terser"; |
||||||
import resolve from '@rollup/plugin-node-resolve'; |
import resolve from "@rollup/plugin-node-resolve"; |
||||||
import livereload from 'rollup-plugin-livereload'; |
import livereload from "rollup-plugin-livereload"; |
||||||
import css from 'rollup-plugin-css-only'; |
import css from "rollup-plugin-css-only"; |
||||||
|
|
||||||
const production = !process.env.ROLLUP_WATCH; |
const production = !process.env.ROLLUP_WATCH; |
||||||
|
|
||||||
function serve() { |
function serve() { |
||||||
let server; |
let server; |
||||||
|
|
||||||
function toExit() { |
function toExit() { |
||||||
if (server) server.kill(0); |
if (server) server.kill(0); |
||||||
} |
} |
||||||
|
|
||||||
return { |
return { |
||||||
writeBundle() { |
writeBundle() { |
||||||
if (server) return; |
if (server) return; |
||||||
server = spawn('npm', ['run', 'start', '--', '--dev'], { |
server = spawn("npm", ["run", "start", "--", "--dev"], { |
||||||
stdio: ['ignore', 'inherit', 'inherit'], |
stdio: ["ignore", "inherit", "inherit"], |
||||||
shell: true |
shell: true, |
||||||
}); |
}); |
||||||
|
|
||||||
process.on('SIGTERM', toExit); |
process.on("SIGTERM", toExit); |
||||||
process.on('exit', toExit); |
process.on("exit", toExit); |
||||||
} |
}, |
||||||
}; |
}; |
||||||
} |
} |
||||||
|
|
||||||
export default { |
export default { |
||||||
input: 'src/main.js', |
input: "src/main.js", |
||||||
output: { |
output: { |
||||||
sourcemap: true, |
sourcemap: true, |
||||||
format: 'iife', |
format: "iife", |
||||||
name: 'app', |
name: "app", |
||||||
file: 'dist/bundle.js' |
file: "dist/bundle.js", |
||||||
}, |
}, |
||||||
plugins: [ |
plugins: [ |
||||||
svelte({ |
svelte({ |
||||||
compilerOptions: { |
compilerOptions: { |
||||||
// enable run-time checks when not in production
|
// enable run-time checks when not in production
|
||||||
dev: !production |
dev: !production, |
||||||
} |
}, |
||||||
}), |
}), |
||||||
// we'll extract any component CSS out into
|
// we'll extract any component CSS out into
|
||||||
// a separate file - better for performance
|
// a separate file - better for performance
|
||||||
css({ output: 'bundle.css' }), |
css({ output: "bundle.css" }), |
||||||
|
|
||||||
// If you have external dependencies installed from
|
// If you have external dependencies installed from
|
||||||
// npm, you'll most likely need these plugins. In
|
// npm, you'll most likely need these plugins. In
|
||||||
// some cases you'll need additional configuration -
|
// some cases you'll need additional configuration -
|
||||||
// consult the documentation for details:
|
// consult the documentation for details:
|
||||||
// https://github.com/rollup/plugins/tree/master/packages/commonjs
|
// https://github.com/rollup/plugins/tree/master/packages/commonjs
|
||||||
resolve({ |
resolve({ |
||||||
browser: true, |
browser: true, |
||||||
dedupe: ['svelte'], |
dedupe: ["svelte"], |
||||||
exportConditions: ['svelte'] |
exportConditions: ["svelte"], |
||||||
}), |
}), |
||||||
commonjs(), |
commonjs(), |
||||||
|
|
||||||
// In dev mode, call `npm run start` once
|
// In dev mode, call `npm run start` once
|
||||||
// the bundle has been generated
|
// the bundle has been generated
|
||||||
!production && serve(), |
!production && serve(), |
||||||
|
|
||||||
// Watch the `public` directory and refresh the
|
// Watch the `public` directory and refresh the
|
||||||
// browser on changes when not in production
|
// browser on changes when not in production
|
||||||
!production && livereload('public'), |
!production && livereload("public"), |
||||||
|
|
||||||
// If we're building for production (npm run build
|
// If we're building for production (npm run build
|
||||||
// instead of npm run dev), minify
|
// instead of npm run dev), minify
|
||||||
production && terser() |
production && terser(), |
||||||
], |
], |
||||||
watch: { |
watch: { |
||||||
clearScreen: false |
clearScreen: false, |
||||||
} |
}, |
||||||
}; |
}; |
||||||
|
|||||||
@ -1,11 +1,11 @@ |
|||||||
// Default Nostr relays for searching
|
// Default Nostr relays for searching
|
||||||
export const DEFAULT_RELAYS = [ |
export const DEFAULT_RELAYS = [ |
||||||
'wss://relay.damus.io', |
"wss://relay.damus.io", |
||||||
'wss://relay.nostr.band', |
"wss://relay.nostr.band", |
||||||
'wss://nos.lol', |
"wss://nos.lol", |
||||||
'wss://relay.nostr.net', |
"wss://relay.nostr.net", |
||||||
'wss://relay.minibits.cash', |
"wss://relay.minibits.cash", |
||||||
'wss://relay.coinos.io/', |
"wss://relay.coinos.io/", |
||||||
'wss://nwc.primal.net', |
"wss://nwc.primal.net", |
||||||
'wss://relay.orly.dev', |
"wss://relay.orly.dev", |
||||||
]; |
]; |
||||||
|
|||||||
@ -1,11 +1,11 @@ |
|||||||
import App from './App.svelte'; |
import App from "./App.svelte"; |
||||||
import '../public/global.css'; |
import "../public/global.css"; |
||||||
|
|
||||||
const app = new App({ |
const app = new App({ |
||||||
target: document.body, |
target: document.body, |
||||||
props: { |
props: { |
||||||
name: 'world' |
name: "world", |
||||||
} |
}, |
||||||
}); |
}); |
||||||
|
|
||||||
export default app; |
export default app; |
||||||
|
|||||||
@ -1,316 +1,359 @@ |
|||||||
import { DEFAULT_RELAYS } from './constants.js'; |
import { DEFAULT_RELAYS } from "./constants.js"; |
||||||
|
|
||||||
// Simple WebSocket relay manager
|
// Simple WebSocket relay manager
|
||||||
class NostrClient { |
class NostrClient { |
||||||
constructor() { |
constructor() { |
||||||
this.relays = new Map(); |
this.relays = new Map(); |
||||||
this.subscriptions = new Map(); |
this.subscriptions = new Map(); |
||||||
} |
} |
||||||
|
|
||||||
async connect() { |
async connect() { |
||||||
console.log('Starting connection to', DEFAULT_RELAYS.length, 'relays...'); |
console.log("Starting connection to", DEFAULT_RELAYS.length, "relays..."); |
||||||
|
|
||||||
const connectionPromises = DEFAULT_RELAYS.map(relayUrl => { |
const connectionPromises = DEFAULT_RELAYS.map((relayUrl) => { |
||||||
return new Promise((resolve) => { |
return new Promise((resolve) => { |
||||||
try { |
try { |
||||||
console.log(`Attempting to connect to ${relayUrl}`); |
console.log(`Attempting to connect to ${relayUrl}`); |
||||||
const ws = new WebSocket(relayUrl); |
const ws = new WebSocket(relayUrl); |
||||||
|
|
||||||
ws.onopen = () => { |
ws.onopen = () => { |
||||||
console.log(`✓ Successfully connected to ${relayUrl}`); |
console.log(`✓ Successfully connected to ${relayUrl}`); |
||||||
resolve(true); |
resolve(true); |
||||||
}; |
}; |
||||||
|
|
||||||
ws.onerror = (error) => { |
ws.onerror = (error) => { |
||||||
console.error(`✗ Error connecting to ${relayUrl}:`, error); |
console.error(`✗ Error connecting to ${relayUrl}:`, error); |
||||||
resolve(false); |
resolve(false); |
||||||
}; |
}; |
||||||
|
|
||||||
ws.onclose = (event) => { |
ws.onclose = (event) => { |
||||||
console.warn(`Connection closed to ${relayUrl}:`, event.code, event.reason); |
console.warn( |
||||||
}; |
`Connection closed to ${relayUrl}:`, |
||||||
|
event.code, |
||||||
ws.onmessage = (event) => { |
event.reason, |
||||||
console.log(`Message from ${relayUrl}:`, event.data); |
); |
||||||
try { |
}; |
||||||
this.handleMessage(relayUrl, JSON.parse(event.data)); |
|
||||||
} catch (error) { |
|
||||||
console.error(`Failed to parse message from ${relayUrl}:`, error, event.data); |
|
||||||
} |
|
||||||
}; |
|
||||||
|
|
||||||
this.relays.set(relayUrl, ws); |
|
||||||
|
|
||||||
// Timeout after 5 seconds
|
|
||||||
setTimeout(() => { |
|
||||||
if (ws.readyState !== WebSocket.OPEN) { |
|
||||||
console.warn(`Connection timeout for ${relayUrl}`); |
|
||||||
resolve(false); |
|
||||||
} |
|
||||||
}, 5000); |
|
||||||
|
|
||||||
} catch (error) { |
|
||||||
console.error(`Failed to create WebSocket for ${relayUrl}:`, error); |
|
||||||
resolve(false); |
|
||||||
} |
|
||||||
}); |
|
||||||
}); |
|
||||||
|
|
||||||
const results = await Promise.all(connectionPromises); |
|
||||||
const successfulConnections = results.filter(Boolean).length; |
|
||||||
console.log(`Connected to ${successfulConnections}/${DEFAULT_RELAYS.length} relays`); |
|
||||||
|
|
||||||
// Wait a bit more for connections to stabilize
|
|
||||||
await new Promise(resolve => setTimeout(resolve, 1000)); |
|
||||||
} |
|
||||||
|
|
||||||
handleMessage(relayUrl, message) { |
ws.onmessage = (event) => { |
||||||
console.log(`Processing message from ${relayUrl}:`, message); |
console.log(`Message from ${relayUrl}:`, event.data); |
||||||
const [type, subscriptionId, event, ...rest] = message; |
try { |
||||||
|
this.handleMessage(relayUrl, JSON.parse(event.data)); |
||||||
console.log(`Message type: ${type}, subscriptionId: ${subscriptionId}`); |
} catch (error) { |
||||||
|
console.error( |
||||||
if (type === 'EVENT') { |
`Failed to parse message from ${relayUrl}:`, |
||||||
console.log(`Received EVENT for subscription ${subscriptionId}:`, event); |
error, |
||||||
if (this.subscriptions.has(subscriptionId)) { |
event.data, |
||||||
console.log(`Found callback for subscription ${subscriptionId}, executing...`); |
); |
||||||
const callback = this.subscriptions.get(subscriptionId); |
|
||||||
callback(event); |
|
||||||
} else { |
|
||||||
console.warn(`No callback found for subscription ${subscriptionId}`); |
|
||||||
} |
} |
||||||
} else if (type === 'EOSE') { |
}; |
||||||
console.log(`End of stored events for subscription ${subscriptionId} from ${relayUrl}`); |
|
||||||
} else if (type === 'NOTICE') { |
|
||||||
console.warn(`Notice from ${relayUrl}:`, subscriptionId); |
|
||||||
} else { |
|
||||||
console.log(`Unknown message type ${type} from ${relayUrl}:`, message); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
subscribe(filters, callback) { |
this.relays.set(relayUrl, ws); |
||||||
const subscriptionId = Math.random().toString(36).substring(7); |
|
||||||
console.log(`Creating subscription ${subscriptionId} with filters:`, filters); |
// Timeout after 5 seconds
|
||||||
|
setTimeout(() => { |
||||||
this.subscriptions.set(subscriptionId, callback); |
if (ws.readyState !== WebSocket.OPEN) { |
||||||
|
console.warn(`Connection timeout for ${relayUrl}`); |
||||||
const subscription = ['REQ', subscriptionId, filters]; |
resolve(false); |
||||||
console.log(`Subscription message:`, JSON.stringify(subscription)); |
|
||||||
|
|
||||||
let sentCount = 0; |
|
||||||
for (const [relayUrl, ws] of this.relays) { |
|
||||||
console.log(`Checking relay ${relayUrl}, readyState: ${ws.readyState} (${ws.readyState === WebSocket.OPEN ? 'OPEN' : 'NOT OPEN'})`); |
|
||||||
if (ws.readyState === WebSocket.OPEN) { |
|
||||||
try { |
|
||||||
ws.send(JSON.stringify(subscription)); |
|
||||||
console.log(`✓ Sent subscription to ${relayUrl}`); |
|
||||||
sentCount++; |
|
||||||
} catch (error) { |
|
||||||
console.error(`✗ Failed to send subscription to ${relayUrl}:`, error); |
|
||||||
} |
|
||||||
} else { |
|
||||||
console.warn(`✗ Cannot send to ${relayUrl}, connection not ready`); |
|
||||||
} |
} |
||||||
|
}, 5000); |
||||||
|
} catch (error) { |
||||||
|
console.error(`Failed to create WebSocket for ${relayUrl}:`, error); |
||||||
|
resolve(false); |
||||||
} |
} |
||||||
|
}); |
||||||
console.log(`Subscription ${subscriptionId} sent to ${sentCount}/${this.relays.size} relays`); |
}); |
||||||
return subscriptionId; |
|
||||||
|
const results = await Promise.all(connectionPromises); |
||||||
|
const successfulConnections = results.filter(Boolean).length; |
||||||
|
console.log( |
||||||
|
`Connected to ${successfulConnections}/${DEFAULT_RELAYS.length} relays`, |
||||||
|
); |
||||||
|
|
||||||
|
// Wait a bit more for connections to stabilize
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 1000)); |
||||||
|
} |
||||||
|
|
||||||
|
handleMessage(relayUrl, message) { |
||||||
|
console.log(`Processing message from ${relayUrl}:`, message); |
||||||
|
const [type, subscriptionId, event, ...rest] = message; |
||||||
|
|
||||||
|
console.log(`Message type: ${type}, subscriptionId: ${subscriptionId}`); |
||||||
|
|
||||||
|
if (type === "EVENT") { |
||||||
|
console.log(`Received EVENT for subscription ${subscriptionId}:`, event); |
||||||
|
if (this.subscriptions.has(subscriptionId)) { |
||||||
|
console.log( |
||||||
|
`Found callback for subscription ${subscriptionId}, executing...`, |
||||||
|
); |
||||||
|
const callback = this.subscriptions.get(subscriptionId); |
||||||
|
callback(event); |
||||||
|
} else { |
||||||
|
console.warn(`No callback found for subscription ${subscriptionId}`); |
||||||
|
} |
||||||
|
} else if (type === "EOSE") { |
||||||
|
console.log( |
||||||
|
`End of stored events for subscription ${subscriptionId} from ${relayUrl}`, |
||||||
|
); |
||||||
|
} else if (type === "NOTICE") { |
||||||
|
console.warn(`Notice from ${relayUrl}:`, subscriptionId); |
||||||
|
} else { |
||||||
|
console.log(`Unknown message type ${type} from ${relayUrl}:`, message); |
||||||
} |
} |
||||||
|
} |
||||||
|
|
||||||
unsubscribe(subscriptionId) { |
subscribe(filters, callback) { |
||||||
this.subscriptions.delete(subscriptionId); |
const subscriptionId = Math.random().toString(36).substring(7); |
||||||
|
console.log( |
||||||
const closeMessage = ['CLOSE', subscriptionId]; |
`Creating subscription ${subscriptionId} with filters:`, |
||||||
|
filters, |
||||||
for (const [relayUrl, ws] of this.relays) { |
); |
||||||
if (ws.readyState === WebSocket.OPEN) { |
|
||||||
ws.send(JSON.stringify(closeMessage)); |
this.subscriptions.set(subscriptionId, callback); |
||||||
} |
|
||||||
|
const subscription = ["REQ", subscriptionId, filters]; |
||||||
|
console.log(`Subscription message:`, JSON.stringify(subscription)); |
||||||
|
|
||||||
|
let sentCount = 0; |
||||||
|
for (const [relayUrl, ws] of this.relays) { |
||||||
|
console.log( |
||||||
|
`Checking relay ${relayUrl}, readyState: ${ws.readyState} (${ws.readyState === WebSocket.OPEN ? "OPEN" : "NOT OPEN"})`, |
||||||
|
); |
||||||
|
if (ws.readyState === WebSocket.OPEN) { |
||||||
|
try { |
||||||
|
ws.send(JSON.stringify(subscription)); |
||||||
|
console.log(`✓ Sent subscription to ${relayUrl}`); |
||||||
|
sentCount++; |
||||||
|
} catch (error) { |
||||||
|
console.error(`✗ Failed to send subscription to ${relayUrl}:`, error); |
||||||
} |
} |
||||||
|
} else { |
||||||
|
console.warn(`✗ Cannot send to ${relayUrl}, connection not ready`); |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
disconnect() { |
console.log( |
||||||
for (const [relayUrl, ws] of this.relays) { |
`Subscription ${subscriptionId} sent to ${sentCount}/${this.relays.size} relays`, |
||||||
ws.close(); |
); |
||||||
} |
return subscriptionId; |
||||||
this.relays.clear(); |
} |
||||||
this.subscriptions.clear(); |
|
||||||
|
unsubscribe(subscriptionId) { |
||||||
|
this.subscriptions.delete(subscriptionId); |
||||||
|
|
||||||
|
const closeMessage = ["CLOSE", subscriptionId]; |
||||||
|
|
||||||
|
for (const [relayUrl, ws] of this.relays) { |
||||||
|
if (ws.readyState === WebSocket.OPEN) { |
||||||
|
ws.send(JSON.stringify(closeMessage)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
disconnect() { |
||||||
|
for (const [relayUrl, ws] of this.relays) { |
||||||
|
ws.close(); |
||||||
} |
} |
||||||
|
this.relays.clear(); |
||||||
|
this.subscriptions.clear(); |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
// Create a global client instance
|
// Create a global client instance
|
||||||
export const nostrClient = new NostrClient(); |
export const nostrClient = new NostrClient(); |
||||||
|
|
||||||
// IndexedDB helpers for caching events (kind 0 profiles)
|
// IndexedDB helpers for caching events (kind 0 profiles)
|
||||||
const DB_NAME = 'nostrCache'; |
const DB_NAME = "nostrCache"; |
||||||
const DB_VERSION = 1; |
const DB_VERSION = 1; |
||||||
const STORE_EVENTS = 'events'; |
const STORE_EVENTS = "events"; |
||||||
|
|
||||||
function openDB() { |
function openDB() { |
||||||
return new Promise((resolve, reject) => { |
return new Promise((resolve, reject) => { |
||||||
try { |
try { |
||||||
const req = indexedDB.open(DB_NAME, DB_VERSION); |
const req = indexedDB.open(DB_NAME, DB_VERSION); |
||||||
req.onupgradeneeded = () => { |
req.onupgradeneeded = () => { |
||||||
const db = req.result; |
const db = req.result; |
||||||
if (!db.objectStoreNames.contains(STORE_EVENTS)) { |
if (!db.objectStoreNames.contains(STORE_EVENTS)) { |
||||||
const store = db.createObjectStore(STORE_EVENTS, { keyPath: 'id' }); |
const store = db.createObjectStore(STORE_EVENTS, { keyPath: "id" }); |
||||||
store.createIndex('byKindAuthor', ['kind', 'pubkey'], { unique: false }); |
store.createIndex("byKindAuthor", ["kind", "pubkey"], { |
||||||
store.createIndex('byKindAuthorCreated', ['kind', 'pubkey', 'created_at'], { unique: false }); |
unique: false, |
||||||
} |
}); |
||||||
}; |
store.createIndex( |
||||||
req.onsuccess = () => resolve(req.result); |
"byKindAuthorCreated", |
||||||
req.onerror = () => reject(req.error); |
["kind", "pubkey", "created_at"], |
||||||
} catch (e) { |
{ unique: false }, |
||||||
reject(e); |
); |
||||||
} |
} |
||||||
}); |
}; |
||||||
|
req.onsuccess = () => resolve(req.result); |
||||||
|
req.onerror = () => reject(req.error); |
||||||
|
} catch (e) { |
||||||
|
reject(e); |
||||||
|
} |
||||||
|
}); |
||||||
} |
} |
||||||
|
|
||||||
async function getLatestProfileEvent(pubkey) { |
async function getLatestProfileEvent(pubkey) { |
||||||
try { |
try { |
||||||
const db = await openDB(); |
const db = await openDB(); |
||||||
return await new Promise((resolve, reject) => { |
return await new Promise((resolve, reject) => { |
||||||
const tx = db.transaction(STORE_EVENTS, 'readonly'); |
const tx = db.transaction(STORE_EVENTS, "readonly"); |
||||||
const idx = tx.objectStore(STORE_EVENTS).index('byKindAuthorCreated'); |
const idx = tx.objectStore(STORE_EVENTS).index("byKindAuthorCreated"); |
||||||
const range = IDBKeyRange.bound([0, pubkey, -Infinity], [0, pubkey, Infinity]); |
const range = IDBKeyRange.bound( |
||||||
const req = idx.openCursor(range, 'prev'); // newest first
|
[0, pubkey, -Infinity], |
||||||
req.onsuccess = () => { |
[0, pubkey, Infinity], |
||||||
const cursor = req.result; |
); |
||||||
resolve(cursor ? cursor.value : null); |
const req = idx.openCursor(range, "prev"); // newest first
|
||||||
}; |
req.onsuccess = () => { |
||||||
req.onerror = () => reject(req.error); |
const cursor = req.result; |
||||||
}); |
resolve(cursor ? cursor.value : null); |
||||||
} catch (e) { |
}; |
||||||
console.warn('IDB getLatestProfileEvent failed', e); |
req.onerror = () => reject(req.error); |
||||||
return null; |
}); |
||||||
} |
} catch (e) { |
||||||
|
console.warn("IDB getLatestProfileEvent failed", e); |
||||||
|
return null; |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
async function putEvent(event) { |
async function putEvent(event) { |
||||||
try { |
try { |
||||||
const db = await openDB(); |
const db = await openDB(); |
||||||
await new Promise((resolve, reject) => { |
await new Promise((resolve, reject) => { |
||||||
const tx = db.transaction(STORE_EVENTS, 'readwrite'); |
const tx = db.transaction(STORE_EVENTS, "readwrite"); |
||||||
tx.oncomplete = () => resolve(); |
tx.oncomplete = () => resolve(); |
||||||
tx.onerror = () => reject(tx.error); |
tx.onerror = () => reject(tx.error); |
||||||
tx.objectStore(STORE_EVENTS).put(event); |
tx.objectStore(STORE_EVENTS).put(event); |
||||||
}); |
}); |
||||||
} catch (e) { |
} catch (e) { |
||||||
console.warn('IDB putEvent failed', e); |
console.warn("IDB putEvent failed", e); |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
function parseProfileFromEvent(event) { |
function parseProfileFromEvent(event) { |
||||||
try { |
try { |
||||||
const profile = JSON.parse(event.content || '{}'); |
const profile = JSON.parse(event.content || "{}"); |
||||||
return { |
return { |
||||||
name: profile.name || profile.display_name || '', |
name: profile.name || profile.display_name || "", |
||||||
picture: profile.picture || '', |
picture: profile.picture || "", |
||||||
banner: profile.banner || '', |
banner: profile.banner || "", |
||||||
about: profile.about || '', |
about: profile.about || "", |
||||||
nip05: profile.nip05 || '', |
nip05: profile.nip05 || "", |
||||||
lud16: profile.lud16 || profile.lud06 || '' |
lud16: profile.lud16 || profile.lud06 || "", |
||||||
}; |
}; |
||||||
} catch (e) { |
} catch (e) { |
||||||
return { name: '', picture: '', banner: '', about: '', nip05: '', lud16: '' }; |
return { |
||||||
} |
name: "", |
||||||
|
picture: "", |
||||||
|
banner: "", |
||||||
|
about: "", |
||||||
|
nip05: "", |
||||||
|
lud16: "", |
||||||
|
}; |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
// Fetch user profile metadata (kind 0)
|
// Fetch user profile metadata (kind 0)
|
||||||
export async function fetchUserProfile(pubkey) { |
export async function fetchUserProfile(pubkey) { |
||||||
return new Promise(async (resolve, reject) => { |
return new Promise(async (resolve, reject) => { |
||||||
console.log(`Starting profile fetch for pubkey: ${pubkey}`); |
console.log(`Starting profile fetch for pubkey: ${pubkey}`); |
||||||
|
|
||||||
let resolved = false; |
let resolved = false; |
||||||
let newestEvent = null; |
let newestEvent = null; |
||||||
let debounceTimer = null; |
let debounceTimer = null; |
||||||
let overallTimer = null; |
let overallTimer = null; |
||||||
let subscriptionId = null; |
let subscriptionId = null; |
||||||
|
|
||||||
function cleanup() { |
|
||||||
if (subscriptionId) { |
|
||||||
try { nostrClient.unsubscribe(subscriptionId); } catch {} |
|
||||||
} |
|
||||||
if (debounceTimer) clearTimeout(debounceTimer); |
|
||||||
if (overallTimer) clearTimeout(overallTimer); |
|
||||||
} |
|
||||||
|
|
||||||
// 1) Try cached profile first and resolve immediately if present
|
function cleanup() { |
||||||
|
if (subscriptionId) { |
||||||
try { |
try { |
||||||
const cachedEvent = await getLatestProfileEvent(pubkey); |
nostrClient.unsubscribe(subscriptionId); |
||||||
if (cachedEvent) { |
} catch {} |
||||||
console.log('Using cached profile event'); |
} |
||||||
const profile = parseProfileFromEvent(cachedEvent); |
if (debounceTimer) clearTimeout(debounceTimer); |
||||||
resolved = true; // resolve immediately with cache
|
if (overallTimer) clearTimeout(overallTimer); |
||||||
resolve(profile); |
} |
||||||
} |
|
||||||
} catch (e) { |
|
||||||
console.warn('Failed to load cached profile', e); |
|
||||||
} |
|
||||||
|
|
||||||
// 2) Set overall timeout
|
// 1) Try cached profile first and resolve immediately if present
|
||||||
overallTimer = setTimeout(() => { |
try { |
||||||
if (!newestEvent) { |
const cachedEvent = await getLatestProfileEvent(pubkey); |
||||||
console.log('Profile fetch timeout reached'); |
if (cachedEvent) { |
||||||
if (!resolved) reject(new Error('Profile fetch timeout')); |
console.log("Using cached profile event"); |
||||||
} else if (!resolved) { |
const profile = parseProfileFromEvent(cachedEvent); |
||||||
resolve(parseProfileFromEvent(newestEvent)); |
resolved = true; // resolve immediately with cache
|
||||||
} |
resolve(profile); |
||||||
cleanup(); |
} |
||||||
}, 15000); |
} catch (e) { |
||||||
|
console.warn("Failed to load cached profile", e); |
||||||
// 3) Wait a bit to ensure connections are ready and then subscribe without limit
|
} |
||||||
setTimeout(() => { |
|
||||||
console.log('Starting subscription after connection delay...'); |
// 2) Set overall timeout
|
||||||
subscriptionId = nostrClient.subscribe( |
overallTimer = setTimeout(() => { |
||||||
{ |
if (!newestEvent) { |
||||||
kinds: [0], |
console.log("Profile fetch timeout reached"); |
||||||
authors: [pubkey] |
if (!resolved) reject(new Error("Profile fetch timeout")); |
||||||
}, |
} else if (!resolved) { |
||||||
(event) => { |
resolve(parseProfileFromEvent(newestEvent)); |
||||||
// Collect all kind 0 events and pick the newest by created_at
|
} |
||||||
if (!event || event.kind !== 0) return; |
cleanup(); |
||||||
console.log('Profile event received:', event); |
}, 15000); |
||||||
|
|
||||||
if (!newestEvent || (event.created_at || 0) > (newestEvent.created_at || 0)) { |
// 3) Wait a bit to ensure connections are ready and then subscribe without limit
|
||||||
newestEvent = event; |
setTimeout(() => { |
||||||
} |
console.log("Starting subscription after connection delay..."); |
||||||
|
subscriptionId = nostrClient.subscribe( |
||||||
// Debounce to wait for more relays; then finalize selection
|
{ |
||||||
if (debounceTimer) clearTimeout(debounceTimer); |
kinds: [0], |
||||||
debounceTimer = setTimeout(async () => { |
authors: [pubkey], |
||||||
try { |
}, |
||||||
if (newestEvent) { |
(event) => { |
||||||
await putEvent(newestEvent); // cache newest only
|
// Collect all kind 0 events and pick the newest by created_at
|
||||||
const profile = parseProfileFromEvent(newestEvent); |
if (!event || event.kind !== 0) return; |
||||||
|
console.log("Profile event received:", event); |
||||||
// Notify listeners that an updated profile is available
|
|
||||||
try { |
if ( |
||||||
if (typeof window !== 'undefined' && window.dispatchEvent) { |
!newestEvent || |
||||||
window.dispatchEvent(new CustomEvent('profile-updated', { |
(event.created_at || 0) > (newestEvent.created_at || 0) |
||||||
detail: { pubkey, profile, event: newestEvent } |
) { |
||||||
})); |
newestEvent = event; |
||||||
} |
} |
||||||
} catch (e) { |
|
||||||
console.warn('Failed to dispatch profile-updated event', e); |
// Debounce to wait for more relays; then finalize selection
|
||||||
} |
if (debounceTimer) clearTimeout(debounceTimer); |
||||||
|
debounceTimer = setTimeout(async () => { |
||||||
if (!resolved) { |
try { |
||||||
resolve(profile); |
if (newestEvent) { |
||||||
resolved = true; |
await putEvent(newestEvent); // cache newest only
|
||||||
} |
const profile = parseProfileFromEvent(newestEvent); |
||||||
} |
|
||||||
} finally { |
// Notify listeners that an updated profile is available
|
||||||
cleanup(); |
try { |
||||||
} |
if (typeof window !== "undefined" && window.dispatchEvent) { |
||||||
}, 800); |
window.dispatchEvent( |
||||||
|
new CustomEvent("profile-updated", { |
||||||
|
detail: { pubkey, profile, event: newestEvent }, |
||||||
|
}), |
||||||
|
); |
||||||
|
} |
||||||
|
} catch (e) { |
||||||
|
console.warn("Failed to dispatch profile-updated event", e); |
||||||
} |
} |
||||||
); |
|
||||||
}, 2000); |
if (!resolved) { |
||||||
}); |
resolve(profile); |
||||||
|
resolved = true; |
||||||
|
} |
||||||
|
} |
||||||
|
} finally { |
||||||
|
cleanup(); |
||||||
|
} |
||||||
|
}, 800); |
||||||
|
}, |
||||||
|
); |
||||||
|
}, 2000); |
||||||
|
}); |
||||||
} |
} |
||||||
|
|
||||||
// Initialize client connection
|
// Initialize client connection
|
||||||
export async function initializeNostrClient() { |
export async function initializeNostrClient() { |
||||||
await nostrClient.connect(); |
await nostrClient.connect(); |
||||||
} |
} |
||||||
|
|||||||
Loading…
Reference in new issue