56 lines
1.7 KiB
Go

// internal/nostr/events/encoder.go
package events
import (
"github.com/nbd-wtf/go-nostr"
"git.sovbit.dev/Enki/nostr-poster/internal/models"
"git.sovbit.dev/Enki/nostr-poster/internal/utils"
)
// EncodeEvent converts a nostr.Event to models.EventResponse with NIP-19 encoded IDs
func EncodeEvent(event *nostr.Event, publishedRelays []string) (*models.EventResponse, error) {
// Create the basic response
response := &models.EventResponse{
ID: event.ID,
Pubkey: event.PubKey,
CreatedAt: int64(event.CreatedAt),
Kind: event.Kind,
Content: event.Content,
Tags: convertTags(event.Tags), // Use a helper function to convert tags
Relays: publishedRelays,
}
// Encode the event ID as nevent (with relay info)
nevent, err := utils.EncodeEventAsNevent(event.ID, publishedRelays, event.PubKey)
if err == nil {
response.Nevent = nevent
}
// Encode the event ID as note (without relay info)
note, err := utils.EncodeEventAsNote(event.ID)
if err == nil {
response.Note = note
}
// Encode the pubkey as npub
npub, err := utils.EncodePubkey(event.PubKey)
if err == nil {
response.Npub = npub
}
return response, nil
}
// Helper function to convert nostr.Tags to [][]string
func convertTags(tags nostr.Tags) [][]string {
result := make([][]string, len(tags))
for i, tag := range tags {
// Create a new string slice for each tag
tagStrings := make([]string, len(tag))
for j, v := range tag {
tagStrings[j] = v
}
result[i] = tagStrings
}
return result
}