replaceableイベント用のパスを追加

This commit is contained in:
Haruki
2023-08-25 18:26:09 +09:00
parent 0986561440
commit eeacfb2ecc

View File

@@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/studiokaiji/nostr-webhost/nostrh/cmd/consts"
"github.com/studiokaiji/nostr-webhost/nostrh/cmd/relays"
)
@@ -77,5 +78,53 @@ func Start(port string) {
return
})
// Replaceable Event (NIP-33)
r.GET("/p/:pubKey/d/:dTag", func(ctx *gin.Context) {
// pubKeyを取得しFilterに追加
pubKey := ctx.Param("pubKey")
// npubから始まる場合はデコードする
if pubKey[0:4] == "npub" {
_, v, err := nip19.Decode(pubKey)
if err != nil {
ctx.String(http.StatusBadRequest, "Invalid npub")
return
}
pubKey = v.(string)
}
authors := []string{pubKey}
// dTagを取得しFilterに追加
dTag := ctx.Param("dTag")
tags := nostr.TagMap{}
tags["d"] = []string{dTag}
// Poolからデータを取得する
ev := pool.QuerySingle(ctx, allRelays, nostr.Filter{
Kinds: []int{
consts.KindWebhostReplaceableHTML,
consts.KindWebhostReplaceableCSS,
consts.KindWebhostReplaceableJS,
},
Authors: authors,
Tags: tags,
})
if ev != nil {
switch ev.Kind {
case consts.KindWebhostReplaceableHTML:
ctx.Data(http.StatusOK, "text/html", []byte(ev.Content))
case consts.KindWebhostReplaceableCSS:
ctx.Data(http.StatusOK, "text/css", []byte(ev.Content))
case consts.KindWebhostReplaceableJS:
ctx.Data(http.StatusOK, "text/javascript", []byte(ev.Content))
default:
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
} else {
ctx.String(http.StatusNotFound, http.StatusText(http.StatusNotFound))
}
return
})
r.Run(":" + port)
}