archy/demo/aiui/assets/useNostr-zyhtrXba.js

2 lines
6.2 KiB
JavaScript
Raw Normal View History

import{o as D,r as R,s as P}from"./index-BzKy-nNf.js";const W=[{url:"wss://relay.damus.io",read:!0,write:!0},{url:"wss://nos.lol",read:!0,write:!0},{url:"wss://relay.snort.social",read:!0,write:!0}],_="aiui-nostr-relays";function x(){try{const e=localStorage.getItem(_);if(e)return JSON.parse(e)}catch{}return W}function N(e){localStorage.setItem(_,JSON.stringify(e))}const w=P([]),h=R(!1),J=R([]),v=new Map,T=new Set;let i=[],p=null,k=!1;function S(){return"aiui-"+Math.random().toString(36).slice(2,10)}function O(e){return e.length<=12?e:e.slice(0,8)+"..."+e.slice(-4)}function Q(e){if(!Array.isArray(e)||e[0]!=="EVENT"||!e[2])return null;const t=e[2];return!t.id||!t.pubkey||typeof t.kind!="number"||typeof t.content!="string"?null:t}function V(e){if(e.kind===0)try{const t=JSON.parse(e.content),r={name:(t.display_name??t.name??"")||void 0,picture:(t.picture??"")||void 0,nip05:(t.nip05??"")||void 0};v.set(e.pubkey,r),T.delete(e.pubkey);const n=w.value.map(s=>s.pubkey!==e.pubkey?s:{...s,authorName:r.name||s.authorName,authorPicture:r.picture,nip05:r.nip05||s.nip05});w.value=n}catch{}}function U(e){const t=v.get(e.pubkey);return t?{...e,authorName:t.name||e.authorName,authorPicture:t.picture,nip05:t.nip05||e.nip05}:e}function Y(e){const t=e.filter(n=>!v.has(n)&&!T.has(n));if(t.length===0)return;for(const n of t)T.add(n);const r="prof-"+Math.random().toString(36).slice(2,8);for(const n of i)n.connected&&n.ws&&n.read&&n.ws.send(JSON.stringify(["REQ",r,{kinds:[0],authors:t,limit:t.length}]))}function L(e){if(e.kind===0){V(e);return}if(w.value.find(s=>s.id===e.id))return;const r=U({id:e.id,pubkey:e.pubkey,authorName:O(e.pubkey),kind:e.kind,content:e.content,created_at:e.created_at,tags:e.tags??[]}),n=[...w.value,r].sort((s,o)=>o.created_at-s.created_at).slice(0,200);w.value=n,Y([e.pubkey])}function M(e){if(!e.ws)try{e.connectTime=Date.now();const t=new WebSocket(e.url);e.ws=t,t.onopen=()=>{e.connected=!0,e.latencyMs=e.connectTime?Date.now()-e.connectTime:null,f(),h.value=i.some(r=>r.connected),e.read&&(p||(p=S()),t.send(JSON.stringify(["REQ",p,{kinds:[1],limit:50}])))},t.onmessage=r=>{try{const n=JSON.parse(r.data),s=Q(n);s&&L(s)}catch{}},t.onclose=()=>{e.connected=!1,e.ws=null,e.latencyMs=null,f(),h.value=i.some(r=>r.connected)},t.onerror=()=>{}}catch{e.connected=!1,f()}}function f(){J.value=i.map(e=>({url:e.url,connected:e.connected,read:e.read,write:e.write,latencyMs:e.latencyMs}))}function K(){if(k)return;k=!0,i=x().map(t=>({url:t.url,ws:null,connected:!1,read:t.read,write:t.write,latencyMs:null,connectTime:null})),f(),i.forEach(t=>M(t))}function A(){p&&(i.forEach(e=>{if(e.ws&&e.connected)try{e.ws.send(JSON.stringify(["CLOSE",p]))}catch{}}),p=null),i.forEach(e=>{e.ws&&(e.ws.close(),e.ws=null,e.connected=!1)}),f(),h.value=!1,k=!1}function I(e,t=!0,r=!0){if(i.some(s=>s.url===e))return;const n={url:e,ws:null,connected:!1,read:t,write:r,latencyMs:null,connectTime:null};i.push(n),N(i.map(s=>({url:s.url,read:s.read,write:s.write}))),f(),k&&M(n)}function j(e){const t=i.findIndex(n=>n.url===e);if(t===-1)return;const r=i[t];r.ws&&(r.ws.close(),r.ws=null),i.splice(t,1),N(i.map(n=>({url:n.url,read:n.read,write:n.write}))),f(),h.value=i.some(n=>n.connected)}function z(e){const t=i.find(r=>r.url===e);t&&(t.read=!t.read,N(i.map(r=>({url:r.url,read:r.read,write:r.write}))),f())}function F(e){const t=i.find(r=>r.url===e);t&&(t.write=!t.write,N(i.map(r=>({url:r.url,read:r.read,write:r.write}))),f())}function G(e){return new Promise(t=>{const r=Date.now(),n=setTimeout(()=>t(null),5e3);try{const s=new WebSocket(e);s.onopen=()=>{const o=Date.now()-r;clearTimeout(n),s.close(),t(o)},s.onerror=()=>{clearTimeout(n),t(null)}}catch{clearTimeout(n),t(null)}})}function q(e){if(e.kind===10002){for(const t of e.tags)if(t[0]==="r"&&t[1]){const r=t[1],n=t[2];I(r,!n||n==="read",!n||n==="write")}}}const b=["wss://relay.nostr.band","wss://nostr.wine"],g=P([]),E=R(!1);function B(e,t){if(!e.trim()){g.value=[];return}E.value=!0,g.value=[];const r=S(),n={search:e,limit:50};t&&t.length>0&&(n.kinds=t);const s=[];let o=0;for(const d of b)try{const u=new WebSocket(d),l=s