Skip to content

Commit 84f7235

Browse files
feat(Nostr): allow to load nostr files
1 parent 70ace70 commit 84f7235

2 files changed

Lines changed: 79 additions & 11 deletions

File tree

src/views/Export/Nostr.vue

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,14 @@ export default {
664664
tags.push(["image", this.imageUrl]);
665665
}
666666
667-
const event = {
667+
const event: {
668+
kind: number;
669+
pubkey: string;
670+
created_at: number;
671+
tags: any[];
672+
content: string;
673+
id?: string;
674+
} = {
668675
kind: 30023,
669676
pubkey: pubkey,
670677
created_at: Math.floor(Date.now() / 1000),

src/views/File.vue

Lines changed: 71 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,56 @@ function replaceMacroURLs(macro: string, base: string, code: string): string {
4949
});
5050
}
5151
52+
const defaultRelays = ["wss://relay.damus.io", "wss://relay.nostr.band", "wss://nos.lol"];
53+
54+
async function fetchNostrContent(url: string): Promise<string> {
55+
const nostrTools = await import("nostr-tools");
56+
const pool = new nostrTools.SimplePool();
57+
58+
try {
59+
// Parse the URI to get the Nostr identifier
60+
const identifier = url.substring(6); // Remove 'nostr:'
61+
const decoded = nostrTools.nip19.decode(identifier);
62+
63+
let fetchedEvent;
64+
switch (decoded.type) {
65+
case "note":
66+
fetchedEvent = await pool.get(defaultRelays, {
67+
ids: [decoded.data],
68+
});
69+
break;
70+
71+
case "nevent":
72+
const relays = decoded.data.relays || defaultRelays;
73+
fetchedEvent = await pool.get(relays, {
74+
ids: [decoded.data.id],
75+
});
76+
break;
77+
78+
case "naddr":
79+
fetchedEvent = await pool.get(decoded.data.relays, {
80+
kinds: [decoded.data.kind],
81+
authors: [decoded.data.pubkey],
82+
"#d": [decoded.data.identifier],
83+
});
84+
break;
85+
86+
default:
87+
throw new Error("Unknown Nostr identifier type: " + decoded.type);
88+
}
89+
90+
if (!fetchedEvent) {
91+
throw new Error("Content not found on specified relays");
92+
}
93+
94+
return fetchedEvent.content;
95+
} catch (error) {
96+
throw new Error(`Error fetching Nostr content: ${error.message}`);
97+
} finally {
98+
pool.close(defaultRelays);
99+
}
100+
}
101+
52102
export default {
53103
name: "LiaScript-FileView",
54104
props: ["fileUrl", "embed", "mode"],
@@ -63,17 +113,28 @@ export default {
63113
};
64114
},
65115
116+
// Modify the created hook
66117
async created() {
67-
const response = await fetch(this.fileUrl);
68-
69-
if (response.ok) {
70-
this.data = await response.text();
71-
const baseURL = this.fileUrl.replace(/\/[^\/]*$/, "/");
72-
this.data = replaceURLs(baseURL, this.data);
73-
this.data = replaceMacroURLs("script", baseURL, this.data);
74-
this.data = replaceMacroURLs("link", baseURL, this.data);
75-
} else {
76-
this.data = errorMsg(this.fileUrl, response.status + ": " + response.statusText);
118+
try {
119+
if (this.fileUrl.startsWith("nostr:")) {
120+
this.data = await fetchNostrContent(this.fileUrl);
121+
} else {
122+
const response = await fetch(this.fileUrl);
123+
if (response.ok) {
124+
this.data = await response.text();
125+
const baseURL = this.fileUrl.replace(/\/[^\/]*$/, "/");
126+
this.data = replaceURLs(baseURL, this.data);
127+
this.data = replaceMacroURLs("script", baseURL, this.data);
128+
this.data = replaceMacroURLs("link", baseURL, this.data);
129+
} else {
130+
this.data = errorMsg(
131+
this.fileUrl,
132+
response.status + ": " + response.statusText
133+
);
134+
}
135+
}
136+
} catch (error) {
137+
this.data = errorMsg(this.fileUrl, error.message);
77138
}
78139
},
79140
components: { LiaScript, Toast },

0 commit comments

Comments
 (0)