From eeacfb2eccd12cf0951b637c48ee91f7d345680c Mon Sep 17 00:00:00 2001 From: Haruki Date: Fri, 25 Aug 2023 18:26:09 +0900 Subject: [PATCH] =?UTF-8?q?replaceable=E3=82=A4=E3=83=99=E3=83=B3=E3=83=88?= =?UTF-8?q?=E7=94=A8=E3=81=AE=E3=83=91=E3=82=B9=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- nostrh/cmd/server/server.go | 49 +++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/nostrh/cmd/server/server.go b/nostrh/cmd/server/server.go index a0c206c..1145192 100644 --- a/nostrh/cmd/server/server.go +++ b/nostrh/cmd/server/server.go @@ -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) }