Browse Source

remove redundancy

Nostr-Signature: 11ac91151bebd4dd49b91bcdef7b0b7157f0afd8ce710f7231be4860fb073d08 573634b648634cbad10f2451776089ea21090d9407f715e83c577b4611ae6edc a7efcafa5ea83a0c37eae4562a84a7581c3d5c5dd1416f8f3e2bd2633d8523ae0eb7cc56dc4292c127ea16fb2dd5bc639483cb096263a850956b47312ed7ff6f
main
Silberengel 2 months ago
parent
commit
68ac941e6b
  1. 1
      nostr/commit-signatures.jsonl
  2. 202
      src/routes/repos/[npub]/[repo]/utils/nostr-links.ts

1
nostr/commit-signatures.jsonl

@ -105,3 +105,4 @@ @@ -105,3 +105,4 @@
{"kind":1640,"pubkey":"573634b648634cbad10f2451776089ea21090d9407f715e83c577b4611ae6edc","created_at":1772131858,"tags":[["author","Silberengel","silberengel7@protonmail.com"],["message","refactor 9"]],"content":"Signed commit: refactor 9","id":"0d92496bc69fe5a2005be0eba26653a729d358f9d9f227e1af01c330eb0c4387","sig":"9203dcc9cfb44804957d37d3b47079ac324bffb42e445a3a79130622e5e20fd10513d987f48edf195514ebf6cb136ba6c5992b39de21b8b47a086194e22cbaeb"}
{"kind":1640,"pubkey":"573634b648634cbad10f2451776089ea21090d9407f715e83c577b4611ae6edc","created_at":1772136696,"tags":[["author","Silberengel","silberengel7@protonmail.com"],["message","refactor 10"]],"content":"Signed commit: refactor 10","id":"7fb8d54e26ab59486f3b56d97e225ed02f893140025c03ccb95a991e523e6182","sig":"f4bb5a037c48d06854d9346ebf96aa9f65f11d3f96e23d08b7d38d0ebea9bab242ffa917239aa432d83a55f369586d66603f439f40eac8156aeaaf80737b81a1"}
{"kind":1640,"pubkey":"573634b648634cbad10f2451776089ea21090d9407f715e83c577b4611ae6edc","created_at":1772141183,"tags":[["author","Silberengel","silberengel7@protonmail.com"],["message","bug-fixes"]],"content":"Signed commit: bug-fixes","id":"b92b203686c0629409fef055e7f3189cf9f26be5cca0253ab00cf7e8498e1115","sig":"06a13aac9d2f794e52b0416044db6ebf9dd248d254d2166d7e7f3fefd2b7d37d1a85072c3e92316898c31068e25cf37bc5afd2fcd8ae2050d0a30b1bc1973678"}
{"kind":1640,"pubkey":"573634b648634cbad10f2451776089ea21090d9407f715e83c577b4611ae6edc","created_at":1772142448,"tags":[["author","Silberengel","silberengel7@protonmail.com"],["message","refactor 11"]],"content":"Signed commit: refactor 11","id":"bb9d5c56a291e48221df96868fb925e309cb560aa350c2cf5f9c4ddd5e5c4a6b","sig":"75662c916bf4d8bb3d70cdae4e4882382692c6f1ca67598a69abe3dc96069ef6f2bda5a1b8f91b724aa43b3cb3c6b8ad6cbce286b5d165377a34a881e7275d2a"}

202
src/routes/repos/[npub]/[repo]/utils/nostr-links.ts

@ -1,202 +0,0 @@ @@ -1,202 +0,0 @@
/**
* Nostr link processing utilities
* Handles parsing and loading of nostr: links
*/
import type { NostrEvent } from '$lib/types/nostr.js';
import { nip19 } from 'nostr-tools';
import { NostrClient } from '$lib/services/nostr/nostr-client.js';
import { DEFAULT_NOSTR_RELAYS } from '$lib/config.js';
export interface ParsedNostrLink {
type: 'nevent' | 'naddr' | 'note1' | 'npub' | 'profile';
value: string;
start: number;
end: number;
}
/**
* Parse nostr: links from content
*/
export function parseNostrLinks(content: string): ParsedNostrLink[] {
const links: ParsedNostrLink[] = [];
const nostrLinkRegex = /nostr:(nevent1|naddr1|note1|npub1|nprofile1)[a-zA-Z0-9]+/g;
let match;
while ((match = nostrLinkRegex.exec(content)) !== null) {
const fullMatch = match[0];
const prefix = match[1];
let type: 'nevent' | 'naddr' | 'note1' | 'npub' | 'profile';
if (prefix === 'nevent1') type = 'nevent';
else if (prefix === 'naddr1') type = 'naddr';
else if (prefix === 'note1') type = 'note1';
else if (prefix === 'npub1') type = 'npub';
else if (prefix === 'nprofile1') type = 'profile';
else continue;
links.push({
type,
value: fullMatch,
start: match.index,
end: match.index + fullMatch.length
});
}
return links;
}
/**
* Get event from nostr link
*/
export function getEventFromNostrLink(link: string): NostrEvent | undefined {
try {
const decoded = nip19.decode(link.replace('nostr:', ''));
if (decoded.type === 'nevent' || decoded.type === 'note') {
return decoded.data as NostrEvent;
}
} catch {
// Invalid link
}
return undefined;
}
/**
* Get pubkey from nostr link
*/
export function getPubkeyFromNostrLink(link: string): string | undefined {
try {
const decoded = nip19.decode(link.replace('nostr:', ''));
if (decoded.type === 'npub' || decoded.type === 'nprofile') {
return decoded.data as string;
}
} catch {
// Invalid link
}
return undefined;
}
/**
* Process content with nostr links, replacing them with event/profile data
*/
export function processContentWithNostrLinks(
content: string,
events: Map<string, NostrEvent>,
profiles: Map<string, any>
): Array<{ type: 'text' | 'event' | 'profile' | 'placeholder'; value: string; event?: NostrEvent; pubkey?: string }> {
const links = parseNostrLinks(content);
const parts: Array<{ type: 'text' | 'event' | 'profile' | 'placeholder'; value: string; event?: NostrEvent; pubkey?: string }> = [];
let lastIndex = 0;
for (const link of links) {
// Add text before link
if (link.start > lastIndex) {
const textPart = content.slice(lastIndex, link.start);
if (textPart) {
parts.push({ type: 'text', value: textPart });
}
}
// Add link
const event = getEventFromNostrLink(link.value);
const pubkey = getPubkeyFromNostrLink(link.value);
if (event) {
parts.push({ type: 'event', value: link.value, event });
} else if (pubkey) {
parts.push({ type: 'profile', value: link.value, pubkey });
} else {
parts.push({ type: 'placeholder', value: link.value });
}
lastIndex = link.end;
}
// Add remaining text
if (lastIndex < content.length) {
const textPart = content.slice(lastIndex);
if (textPart) {
parts.push({ type: 'text', value: textPart });
}
}
return parts.length > 0 ? parts : [{ type: 'text', value: content }];
}
/**
* Load events and profiles from nostr links
*/
export async function loadNostrLinks(
content: string,
setEvents: (events: Map<string, NostrEvent>) => void,
setProfiles: (profiles: Map<string, any>) => void
): Promise<void> {
const links = parseNostrLinks(content);
if (links.length === 0) return;
const eventIds: string[] = [];
const aTags: string[] = [];
const npubs: string[] = [];
for (const link of links) {
try {
const decoded = nip19.decode(link.value.replace('nostr:', ''));
if (decoded.type === 'nevent') {
const data = decoded.data as { id: string; relays?: string[] };
if (data.id) eventIds.push(data.id);
} else if (decoded.type === 'naddr') {
const data = decoded.data as { identifier: string; pubkey: string; relays?: string[] };
if (data.identifier && data.pubkey) {
aTags.push(`${data.pubkey}:${data.identifier}`);
}
} else if (decoded.type === 'note') {
eventIds.push(decoded.data as string);
} else if (decoded.type === 'npub' || decoded.type === 'nprofile') {
npubs.push(decoded.data as string);
}
} catch {
// Invalid link, skip
}
}
if (eventIds.length === 0 && aTags.length === 0 && npubs.length === 0) return;
const client = new NostrClient(DEFAULT_NOSTR_RELAYS);
const eventsMap = new Map<string, NostrEvent>();
const profilesMap = new Map<string, any>();
// Load events
if (eventIds.length > 0) {
try {
const events = await client.fetchEvents([
{ ids: eventIds, limit: eventIds.length }
]);
for (const event of events) {
eventsMap.set(event.id, event);
}
} catch (err) {
console.warn('Failed to load events from nostr links:', err);
}
}
// Load profiles
if (npubs.length > 0) {
try {
const profiles = await client.fetchEvents([
{ kinds: [0], authors: npubs, limit: npubs.length }
]);
for (const profile of profiles) {
try {
const data = JSON.parse(profile.content);
profilesMap.set(profile.pubkey, data);
} catch {
// Invalid JSON
}
}
} catch (err) {
console.warn('Failed to load profiles from nostr links:', err);
}
}
setEvents(eventsMap);
setProfiles(profilesMap);
}
Loading…
Cancel
Save