refactor and add list_snippets

This commit is contained in:
pablof7z
2025-03-31 12:26:50 +01:00
parent 6d0e5a8e79
commit f74736191a
15 changed files with 494 additions and 190 deletions

View File

@@ -1,9 +1,8 @@
import type { NDKEvent, NDKSigner } from "@nostr-dev-kit/ndk";
import { NDKPrivateKeySigner } from "@nostr-dev-kit/ndk";
import { ndk } from "../../ndk.js";
import { queryUser } from "../../users.js";
import type { CodeSnippet } from "../types/index.js";
import { getUser } from "../../config.js";
import { toPubkeys, toSnippet } from "../converters/index.js";
export const SNIPPET_KIND = 1337;
@@ -32,45 +31,5 @@ export async function getSigner(username?: string): Promise<NDKSigner> {
return new NDKPrivateKeySigner(userData.nsec);
}
/**
* Converts an identifier (pubkey, npub, or name) to pubkeys
* @param identifier The identifier to convert
* @returns Array of pubkeys
*/
export function identifierToPubkeys(identifier: string): string[] {
// If it's an npub, convert directly
if (identifier.startsWith("npub")) {
return [ndk.getUser({ npub: identifier }).pubkey];
}
// If it's a hex pubkey, return as is
if (identifier.length === 64 && /^[0-9a-f]+$/i.test(identifier)) {
return [identifier];
}
// Otherwise, search by profile name or other attributes
return queryUser(identifier);
}
/**
* Converts an NDKEvent into a CodeSnippet
* @param event NDKEvent of kind 1337
* @returns CodeSnippet object
*/
export function eventToSnippet(event: NDKEvent): CodeSnippet {
const title = event.tagValue("title") ?? event.tagValue("name");
const language = event.tagValue("l");
const tags = event.tags
.filter((tag) => tag[0] === "t" && tag[1] !== undefined)
.map((tag) => tag[1] as string);
return {
id: event.id,
title: title || "Untitled",
code: event.content,
language: language || "text",
pubkey: event.pubkey,
createdAt: event.created_at || 0,
tags,
};
}
// Re-export converter functions for backward compatibility
export { toPubkeys as identifierToPubkeys, toSnippet as eventToSnippet };