mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 14:24:27 +01:00
use naddr as code in client urls when param-replaceable and fix relay filtering for nip19.
fixes https://github.com/fiatjaf/njump/issues/14
This commit is contained in:
68
clients.go
Normal file
68
clients.go
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"html/template"
|
||||||
|
|
||||||
|
"github.com/nbd-wtf/go-nostr"
|
||||||
|
)
|
||||||
|
|
||||||
|
type ClientReference struct {
|
||||||
|
ID string
|
||||||
|
Name string
|
||||||
|
URL template.URL
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateClientList(style Style, code string, event *nostr.Event) []ClientReference {
|
||||||
|
if event.Kind == 1 || event.Kind == 6 {
|
||||||
|
return []ClientReference{
|
||||||
|
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
||||||
|
{ID: "snort", Name: "Snort", URL: template.URL("https://Snort.social/e/" + code)},
|
||||||
|
{ID: "nostrudel", Name: "Nostrudel", URL: template.URL("https://nostrudel.ninja/#/n/" + code)},
|
||||||
|
{ID: "satellite", Name: "Satellite", URL: template.URL("https://satellite.earth/thread/" + event.ID)},
|
||||||
|
{ID: "coracle", Name: "Coracle", URL: template.URL("https://coracle.social/" + code)},
|
||||||
|
{ID: "primal", Name: "Primal", URL: template.URL("https://primal.net/thread/" + event.ID)},
|
||||||
|
{ID: "nostter", Name: "Nostter", URL: template.URL("https://nostter.vercel.app/" + code)},
|
||||||
|
{ID: "highlighter", Name: "Highlighter", URL: template.URL("https://highlighter.com/a/" + code)},
|
||||||
|
{ID: "iris", Name: "Iris", URL: template.URL("https://iris.to/" + code)},
|
||||||
|
}
|
||||||
|
} else if event.Kind == 0 {
|
||||||
|
return []ClientReference{
|
||||||
|
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
||||||
|
{ID: "nosta", Name: "Nosta", URL: template.URL("https://nosta.me/" + code)},
|
||||||
|
{ID: "snort", Name: "Snort", URL: template.URL("https://snort.social/p/" + code)},
|
||||||
|
{ID: "satellite", Name: "Satellite", URL: template.URL("https://satellite.earth/@" + code)},
|
||||||
|
{ID: "coracle", Name: "Coracle", URL: template.URL("https://coracle.social/" + code)},
|
||||||
|
{ID: "primal", Name: "Primal", URL: template.URL("https://primal.net/profile/" + event.PubKey)},
|
||||||
|
{ID: "nostrudel", Name: "Nostrudel", URL: template.URL("https://nostrudel.ninja/#/u/" + code)},
|
||||||
|
{ID: "nostter", Name: "Nostter", URL: template.URL("https://nostter.vercel.app/" + code)},
|
||||||
|
{ID: "iris", Name: "Iris", URL: template.URL("https://iris.to/" + code)},
|
||||||
|
}
|
||||||
|
} else if event.Kind == 30023 || event.Kind == 30024 {
|
||||||
|
return []ClientReference{
|
||||||
|
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
||||||
|
{ID: "yakihonne", Name: "YakiHonne", URL: template.URL("https://yakihonne.com/article/" + code)},
|
||||||
|
{ID: "habla", Name: "Habla", URL: template.URL("https://habla.news/a/" + code)},
|
||||||
|
{ID: "highlighter", Name: "Highlighter", URL: template.URL("https://highlighter.com/a/" + code)},
|
||||||
|
{ID: "blogstack", Name: "Blogstack", URL: template.URL("https://blogstack.io/" + code)},
|
||||||
|
}
|
||||||
|
} else if event.Kind == 1063 {
|
||||||
|
return []ClientReference{
|
||||||
|
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
||||||
|
{ID: "snort", Name: "Snort", URL: template.URL("https://snort.social/p/" + code)},
|
||||||
|
{ID: "coracle", Name: "Coracle", URL: template.URL("https://coracle.social/" + code)},
|
||||||
|
}
|
||||||
|
} else if event.Kind == 30311 {
|
||||||
|
return []ClientReference{
|
||||||
|
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
||||||
|
{ID: "zap.stream", Name: "zap.stream", URL: template.URL("https://zap.stream/" + code)},
|
||||||
|
{ID: "nostrudel", Name: "Nostrudel", URL: template.URL("https://nostrudel.ninja/#/streams/" + code)},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateRelayBrowserClientList(host string) []ClientReference {
|
||||||
|
return []ClientReference{
|
||||||
|
{ID: "coracle", Name: "Coracle", URL: template.URL("https://coracle.social/relays/" + host)},
|
||||||
|
}
|
||||||
|
}
|
||||||
33
data.go
33
data.go
@@ -136,10 +136,13 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
relaysForNip19 := make([]string, 0, 3)
|
relaysForNip19 := make([]string, 0, 3)
|
||||||
for i, relay := range relays {
|
c := 0
|
||||||
relaysForNip19 = append(relaysForNip19, relay)
|
for _, relayUrl := range relays {
|
||||||
if i == 2 {
|
if shouldUseRelayForNip19(relayUrl) {
|
||||||
break
|
relaysForNip19 = append(relaysForNip19, relayUrl)
|
||||||
|
if c == 2 {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,17 +163,11 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
|
|||||||
data.modifiedAt = time.Unix(int64(event.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00")
|
data.modifiedAt = time.Unix(int64(event.CreatedAt), 0).Format("2006-01-02T15:04:05Z07:00")
|
||||||
data.authorRelays = []string{}
|
data.authorRelays = []string{}
|
||||||
|
|
||||||
eventRelays := []string{}
|
if event.Kind >= 30000 && event.Kind < 40000 {
|
||||||
for _, relay := range relays {
|
if d := event.Tags.GetFirst([]string{"d", ""}); d != nil {
|
||||||
for _, excluded := range excludedRelays {
|
data.naddr, _ = nip19.EncodeEntity(event.PubKey, event.Kind, d.Value(), relaysForNip19)
|
||||||
if strings.Contains(relay, excluded) {
|
data.naddrNaked, _ = nip19.EncodeEntity(event.PubKey, event.Kind, d.Value(), nil)
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if strings.Contains(relay, "/npub1") {
|
|
||||||
continue // skip relays with personalyzed query like filter.nostr.wine
|
|
||||||
}
|
|
||||||
eventRelays = append(eventRelays, trimProtocol(relay))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if tag := event.Tags.GetFirst([]string{"alt", ""}); tag != nil {
|
if tag := event.Tags.GetFirst([]string{"alt", ""}); tag != nil {
|
||||||
@@ -259,8 +256,6 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
|
|||||||
}
|
}
|
||||||
case 30311:
|
case 30311:
|
||||||
data.templateId = LiveEvent
|
data.templateId = LiveEvent
|
||||||
d := event.Tags.GetFirst([]string{"d", ""})
|
|
||||||
data.naddr, _ = nip19.EncodeEntity(event.PubKey, event.Kind, d.Value(), data.relays)
|
|
||||||
data.kind30311Metadata = &Kind30311Metadata{}
|
data.kind30311Metadata = &Kind30311Metadata{}
|
||||||
|
|
||||||
if tag := event.Tags.GetFirst([]string{"title", ""}); tag != nil {
|
if tag := event.Tags.GetFirst([]string{"title", ""}); tag != nil {
|
||||||
@@ -288,12 +283,6 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
|
|||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
data.templateId = Other
|
data.templateId = Other
|
||||||
if event.Kind >= 30000 && event.Kind < 40000 {
|
|
||||||
if d := event.Tags.GetFirst([]string{"d", ""}); d != nil {
|
|
||||||
data.naddr, _ = nip19.EncodeEntity(event.PubKey, event.Kind, d.Value(), relaysForNip19)
|
|
||||||
data.naddrNaked, _ = nip19.EncodeEntity(event.PubKey, event.Kind, d.Value(), nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if event.Kind == 0 {
|
if event.Kind == 0 {
|
||||||
|
|||||||
@@ -279,6 +279,10 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
opengraph.SingleTitle = "by " + data.authorShort + " at " + humanDate(data.event.CreatedAt)
|
opengraph.SingleTitle = "by " + data.authorShort + " at " + humanDate(data.event.CreatedAt)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if data.naddr != "" {
|
||||||
|
code = data.naddr
|
||||||
|
}
|
||||||
|
|
||||||
err = NoteTemplate.Render(w, &NotePage{
|
err = NoteTemplate.Render(w, &NotePage{
|
||||||
OpenGraphPartial: opengraph,
|
OpenGraphPartial: opengraph,
|
||||||
HeadCommonPartial: HeadCommonPartial{
|
HeadCommonPartial: HeadCommonPartial{
|
||||||
|
|||||||
81
utils.go
81
utils.go
@@ -8,6 +8,7 @@ import (
|
|||||||
"html/template"
|
"html/template"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -118,67 +119,6 @@ var kindNIPs = map[int]string{
|
|||||||
30078: "78",
|
30078: "78",
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientReference struct {
|
|
||||||
ID string
|
|
||||||
Name string
|
|
||||||
URL template.URL
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateClientList(style Style, code string, event *nostr.Event) []ClientReference {
|
|
||||||
if event.Kind == 1 || event.Kind == 6 {
|
|
||||||
return []ClientReference{
|
|
||||||
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
|
||||||
{ID: "snort", Name: "Snort", URL: template.URL("https://Snort.social/e/" + code)},
|
|
||||||
{ID: "nostrudel", Name: "Nostrudel", URL: template.URL("https://nostrudel.ninja/#/n/" + code)},
|
|
||||||
{ID: "satellite", Name: "Satellite", URL: template.URL("https://satellite.earth/thread/" + event.ID)},
|
|
||||||
{ID: "coracle", Name: "Coracle", URL: template.URL("https://coracle.social/" + code)},
|
|
||||||
{ID: "primal", Name: "Primal", URL: template.URL("https://primal.net/thread/" + event.ID)},
|
|
||||||
{ID: "nostter", Name: "Nostter", URL: template.URL("https://nostter.vercel.app/" + code)},
|
|
||||||
{ID: "highlighter", Name: "Highlighter", URL: template.URL("https://highlighter.com/a/" + code)},
|
|
||||||
{ID: "iris", Name: "Iris", URL: template.URL("https://iris.to/" + code)},
|
|
||||||
}
|
|
||||||
} else if event.Kind == 0 {
|
|
||||||
return []ClientReference{
|
|
||||||
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
|
||||||
{ID: "nosta", Name: "Nosta", URL: template.URL("https://nosta.me/" + code)},
|
|
||||||
{ID: "snort", Name: "Snort", URL: template.URL("https://snort.social/p/" + code)},
|
|
||||||
{ID: "satellite", Name: "Satellite", URL: template.URL("https://satellite.earth/@" + code)},
|
|
||||||
{ID: "coracle", Name: "Coracle", URL: template.URL("https://coracle.social/" + code)},
|
|
||||||
{ID: "primal", Name: "Primal", URL: template.URL("https://primal.net/profile/" + event.PubKey)},
|
|
||||||
{ID: "nostrudel", Name: "Nostrudel", URL: template.URL("https://nostrudel.ninja/#/u/" + code)},
|
|
||||||
{ID: "nostter", Name: "Nostter", URL: template.URL("https://nostter.vercel.app/" + code)},
|
|
||||||
{ID: "iris", Name: "Iris", URL: template.URL("https://iris.to/" + code)},
|
|
||||||
}
|
|
||||||
} else if event.Kind == 30023 || event.Kind == 30024 {
|
|
||||||
return []ClientReference{
|
|
||||||
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
|
||||||
{ID: "yakihonne", Name: "YakiHonne", URL: template.URL("https://yakihonne.com/article/" + code)},
|
|
||||||
{ID: "habla", Name: "Habla", URL: template.URL("https://habla.news/a/" + code)},
|
|
||||||
{ID: "highlighter", Name: "Highlighter", URL: template.URL("https://highlighter.com/a/" + code)},
|
|
||||||
{ID: "blogstack", Name: "Blogstack", URL: template.URL("https://blogstack.io/" + code)},
|
|
||||||
}
|
|
||||||
} else if event.Kind == 1063 {
|
|
||||||
return []ClientReference{
|
|
||||||
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
|
||||||
{ID: "snort", Name: "Snort", URL: template.URL("https://snort.social/p/" + code)},
|
|
||||||
{ID: "coracle", Name: "Coracle", URL: template.URL("https://coracle.social/" + code)},
|
|
||||||
}
|
|
||||||
} else if event.Kind == 30311 {
|
|
||||||
return []ClientReference{
|
|
||||||
{ID: "native", Name: "your native client", URL: template.URL("nostr:" + code)},
|
|
||||||
{ID: "zap.stream", Name: "zap.stream", URL: template.URL("https://zap.stream/" + code)},
|
|
||||||
{ID: "nostrudel", Name: "Nostrudel", URL: template.URL("https://nostrudel.ninja/#/streams/" + code)},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func generateRelayBrowserClientList(host string) []ClientReference {
|
|
||||||
return []ClientReference{
|
|
||||||
{ID: "coracle", Name: "Coracle", URL: template.URL("https://coracle.social/relays/" + host)},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type Style string
|
type Style string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -567,3 +507,22 @@ func humanDate(createdAt nostr.Timestamp) string {
|
|||||||
return ts.UTC().Format("Mon, Jan _2 15:04 UTC")
|
return ts.UTC().Format("Mon, Jan _2 15:04 UTC")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func shouldUseRelayForNip19(relayUrl string) bool {
|
||||||
|
for _, excluded := range excludedRelays {
|
||||||
|
if strings.Contains(relayUrl, excluded) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
urlp, err := url.Parse(relayUrl)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if urlp.Scheme != "wss" && urlp.Scheme != "ws" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if urlp.Path != "" && urlp.Path != "/" {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user