diff --git a/hostr/cmd/deploy/deploy.go b/hostr/cmd/deploy/deploy.go index 762070f..5576bd1 100644 --- a/hostr/cmd/deploy/deploy.go +++ b/hostr/cmd/deploy/deploy.go @@ -12,6 +12,7 @@ import ( "sync" "github.com/nbd-wtf/go-nostr" + "github.com/nbd-wtf/go-nostr/nip19" "github.com/studiokaiji/nostr-webhost/hostr/cmd/consts" "github.com/studiokaiji/nostr-webhost/hostr/cmd/keystore" "github.com/studiokaiji/nostr-webhost/hostr/cmd/relays" @@ -61,17 +62,13 @@ func addNostrEventQueue(event *nostr.Event) { nostrEventsQueue = append(nostrEventsQueue, event) } -func publishEventsFromQueue() (string, error) { +var allRelays []string + +func publishEventsFromQueue(replaceable bool) (string, string) { ctx := context.Background() fmt.Println("Publishing...") - // リレーを取得 - allRelays, err := relays.GetAllRelays() - if err != nil { - return "", err - } - // 各リレーに接続 var relays []*nostr.Relay @@ -122,9 +119,18 @@ func publishEventsFromQueue() (string, error) { fmt.Println("Failed to deploy", allEventsCount-uploadedFilesCount, "files.") } - indexEventId := nostrEventsQueue[len(nostrEventsQueue)-1].ID + indexEvent := nostrEventsQueue[len(nostrEventsQueue)-1] - return indexEventId, err + encoded := "" + if !replaceable { + if enc, err := nip19.EncodeEvent(indexEvent.ID, allRelays, indexEvent.PubKey); err == nil { + encoded = enc + } else { + fmt.Println("❌ Failed to covert nevent:", err) + } + } + + return indexEvent.ID, encoded } func isExternalURL(urlStr string) bool { @@ -136,7 +142,7 @@ func isValidFileType(str string) bool { return strings.HasSuffix(str, ".html") || strings.HasSuffix(str, ".css") || strings.HasSuffix(str, ".js") } -func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, error) { +func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, string, error) { // 引数からデプロイしたいサイトのパスを受け取る。 filePath := filepath.Join(basePath, "index.html") @@ -144,26 +150,26 @@ func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, e content, err := os.ReadFile(filePath) if err != nil { fmt.Println("❌ Failed to read index.html:", err) - return "", err + return "", "", err } // HTMLの解析 doc, err := html.Parse(bytes.NewReader(content)) if err != nil { fmt.Println("❌ Failed to parse index.html:", err) - return "", nil + return "", "", err } // Eventの取得に必要になるキーペアを取得 priKey, err := keystore.GetSecret() if err != nil { fmt.Println("❌ Failed to get private key:", err) - return "", err + return "", "", err } pubKey, err := nostr.GetPublicKey(priKey) if err != nil { fmt.Println("❌ Failed to get public key:", err) - return "", err + return "", "", err } // htmlIdentifierの存在チェック @@ -179,6 +185,12 @@ func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, e fmt.Printf("Identifier: %s\n", htmlIdentifier) } + // リレーを取得 + allRelays, err = relays.GetAllRelays() + if err != nil { + return "", "", err + } + // リンクの解析と変換 convertLinks(priKey, pubKey, basePath, replaceable, htmlIdentifier, doc) @@ -204,12 +216,14 @@ func Deploy(basePath string, replaceable bool, htmlIdentifier string) (string, e event, err := getEvent(priKey, pubKey, strHtml, indexHtmlKind, tags) if err != nil { fmt.Println("❌ Failed to get public key:", err) - return "", err + return "", "", err } addNostrEventQueue(event) fmt.Println("Added", filePath, "event to publish queue") - return publishEventsFromQueue() + eventId, encoded := publishEventsFromQueue(replaceable) + + return eventId, encoded, err } func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlIdentifier string, n *html.Node) { @@ -253,9 +267,15 @@ func convertLinks(priKey, pubKey, basePath string, replaceable bool, indexHtmlId addNostrEventQueue(event) fmt.Println("Added", filePath, "event to publish queue") + // 置き換え可能なイベントでない場合 if !replaceable { - // 元のパスをEvent[.]IDに変更 - n.Attr[i].Val = event.ID + // neventを指定 + nevent, err := nip19.EncodeEvent(event.ID, allRelays, pubKey) + if err != nil { + fmt.Println("❌ Failed to encode event", filePath, ":", err) + break + } + n.Attr[i].Val = nevent } } } diff --git a/hostr/cmd/server/server.go b/hostr/cmd/server/server.go index 5930087..ace2625 100644 --- a/hostr/cmd/server/server.go +++ b/hostr/cmd/server/server.go @@ -24,14 +24,33 @@ func Start(port string) { r := gin.Default() - r.GET("/e/:idHex", func(ctx *gin.Context) { - // IDを取得 - id := ctx.Param("idHex") + r.GET("/e/:hex_or_nevent", func(ctx *gin.Context) { + hexOrNevent := ctx.Param("hex_or_nevent") + + ids := []string{} + + // neventからIDを取得 + if hexOrNevent[0:6] == "nevent" { + _, res, err := nip19.Decode(hexOrNevent) + if err != nil { + ctx.String(http.StatusBadRequest, "Invalid nevent") + return + } + + data, ok := res.(nostr.EventPointer) + if !ok { + ctx.String(http.StatusBadRequest, "Failed to decode nevent") + return + } + + ids = append(ids, data.ID) + allRelays = append(allRelays, data.Relays...) + } // Poolからデータを取得する ev := pool.QuerySingle(ctx, allRelays, nostr.Filter{ Kinds: []int{consts.KindWebhostHTML, consts.KindWebhostCSS, consts.KindWebhostJS, consts.KindWebhostPicture}, - IDs: []string{id}, + IDs: ids, }) if ev != nil { switch ev.Kind { diff --git a/hostr/main.go b/hostr/main.go index dfc445c..8eaa10f 100644 --- a/hostr/main.go +++ b/hostr/main.go @@ -52,10 +52,19 @@ func main() { replaceable := ctx.Bool("replaceable") dTag := ctx.String("identifier") - indexEventId, err := deploy.Deploy(path, replaceable, dTag) + id, encoded, err := deploy.Deploy(path, replaceable, dTag) if err == nil { fmt.Println("🌐 Deploy Complete!") - fmt.Println("🌐 index.html Event ID:", indexEventId) + fmt.Println("index.html:") + fmt.Println(" - event.id:", id) + + label := " - " + if replaceable { + label += "naddr" + } else { + label += "nevent" + } + fmt.Printf("%s: %s\n", label, encoded) } return err }, diff --git a/nostr-rs-relay/config.toml b/nostr-rs-relay/config.toml index fd547c3..9f992a5 100644 --- a/nostr-rs-relay/config.toml +++ b/nostr-rs-relay/config.toml @@ -146,16 +146,7 @@ max_ws_frame_bytes = 1024000 #] # Event kind allowlist. Events other than these kinds will be discarded. -event_kind_allowlist = [ - 1964, - 1965, - 5392, - 5393, - 5394, - 35382, - 35393, - 35394, -] +event_kind_allowlist = [1964, 1965, 5392, 5393, 5394, 35392, 35393, 35394] [authorization] # Pubkey addresses in this array are whitelisted for event publishing.