mirror of
https://github.com/studiokaiji/nostr-webhost.git
synced 2025-12-17 23:04:23 +01:00
通常イベントをneventでアクセスできるようにした
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user