You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.2 KiB
71 lines
2.2 KiB
/** |
|
* API endpoint for Issues (NIP-34 kind 1621) |
|
*/ |
|
|
|
import { json, error } from '@sveltejs/kit'; |
|
import type { RequestHandler } from './$types'; |
|
import { IssuesService } from '$lib/services/nostr/issues-service.js'; |
|
import { DEFAULT_NOSTR_RELAYS } from '$lib/config.js'; |
|
import { nip19 } from 'nostr-tools'; |
|
|
|
export const GET: RequestHandler = async ({ params, url }) => { |
|
const { npub, repo } = params; |
|
|
|
if (!npub || !repo) { |
|
return error(400, 'Missing npub or repo parameter'); |
|
} |
|
|
|
try { |
|
// Convert npub to pubkey |
|
const decoded = nip19.decode(npub); |
|
if (decoded.type !== 'npub') { |
|
return error(400, 'Invalid npub format'); |
|
} |
|
const repoOwnerPubkey = decoded.data as string; |
|
|
|
const issuesService = new IssuesService(DEFAULT_NOSTR_RELAYS); |
|
const issues = await issuesService.getIssues(repoOwnerPubkey, repo); |
|
|
|
return json(issues); |
|
} catch (err) { |
|
console.error('Error fetching issues:', err); |
|
return error(500, err instanceof Error ? err.message : 'Failed to fetch issues'); |
|
} |
|
}; |
|
|
|
export const POST: RequestHandler = async ({ params, request }) => { |
|
// For creating issues, we accept a pre-signed event from the client |
|
// since NIP-07 signing must happen client-side |
|
const { npub, repo } = params; |
|
|
|
if (!npub || !repo) { |
|
return error(400, 'Missing npub or repo parameter'); |
|
} |
|
|
|
try { |
|
const body = await request.json(); |
|
const { event } = body; |
|
|
|
if (!event) { |
|
return error(400, 'Missing event in request body'); |
|
} |
|
|
|
// Verify the event is properly signed (basic check) |
|
if (!event.sig || !event.id) { |
|
return error(400, 'Invalid event: missing signature or ID'); |
|
} |
|
|
|
// Publish the event to relays |
|
const issuesService = new IssuesService(DEFAULT_NOSTR_RELAYS); |
|
const result = await issuesService['nostrClient'].publishEvent(event, DEFAULT_NOSTR_RELAYS); |
|
|
|
if (result.failed.length > 0 && result.success.length === 0) { |
|
return error(500, 'Failed to publish issue to all relays'); |
|
} |
|
|
|
return json({ success: true, event, published: result }); |
|
} catch (err) { |
|
console.error('Error creating issue:', err); |
|
return error(500, err instanceof Error ? err.message : 'Failed to create issue'); |
|
} |
|
};
|
|
|