move nip31, nip53, nip94 definitions to go-nostr and prepare for nip52.

This commit is contained in:
fiatjaf
2024-01-10 12:14:56 -03:00
parent 4a4c259f5b
commit 6b19f5103c
7 changed files with 54 additions and 138 deletions

80
data.go
View File

@@ -9,6 +9,9 @@ import (
"time"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/nbd-wtf/go-nostr/nip31"
"github.com/nbd-wtf/go-nostr/nip53"
"github.com/nbd-wtf/go-nostr/nip94"
sdk "github.com/nbd-wtf/nostr-sdk"
)
@@ -36,7 +39,7 @@ type Data struct {
alt string
kind1063Metadata *Kind1063Metadata
kind30311Metadata *Kind30311Metadata
kind1311Metadata *Kind1311Metadata
kind31922Or31923Metadata *Kind31922Or31923Metadata
}
func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, error) {
@@ -85,9 +88,7 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
}
}
if tag := event.Tags.GetFirst([]string{"alt", ""}); tag != nil {
data.alt = (*tag)[1]
}
data.alt = nip31.GetAlt(*event)
switch event.Kind {
case 0:
@@ -132,76 +133,17 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
}
case 1063:
data.templateId = FileMetadata
data.kind1063Metadata = &Kind1063Metadata{}
if tag := event.Tags.GetFirst([]string{"url", ""}); tag != nil {
data.kind1063Metadata.URL = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"m", ""}); tag != nil {
data.kind1063Metadata.M = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"aes-256-gcm", ""}); tag != nil {
data.kind1063Metadata.AES256GCM = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"x", ""}); tag != nil {
data.kind1063Metadata.X = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"size", ""}); tag != nil {
data.kind1063Metadata.Size = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"dim", ""}); tag != nil {
data.kind1063Metadata.Dim = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"magnet", ""}); tag != nil {
data.kind1063Metadata.Magnet = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"i", ""}); tag != nil {
data.kind1063Metadata.I = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"blurhash", ""}); tag != nil {
data.kind1063Metadata.Blurhash = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"thumb", ""}); tag != nil {
data.kind1063Metadata.Thumb = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"image", ""}); tag != nil {
data.kind1063Metadata.Image = (*tag)[1]
data.image = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"summary", ""}); tag != nil {
data.kind1063Metadata.Summary = (*tag)[1]
}
data.kind1063Metadata = &Kind1063Metadata{nip94.ParseFileMetadata(*event)}
case 30311:
data.templateId = LiveEvent
data.kind30311Metadata = &Kind30311Metadata{}
if tag := event.Tags.GetFirst([]string{"title", ""}); tag != nil {
data.kind30311Metadata.Title = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"summary", ""}); tag != nil {
data.kind30311Metadata.Summary = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"image", ""}); tag != nil {
data.kind30311Metadata.Image = (*tag)[1]
data.image = (*tag)[1]
}
if tag := event.Tags.GetFirst([]string{"status", ""}); tag != nil {
data.kind30311Metadata.Status = (*tag)[1]
}
pTags := event.Tags.GetAll([]string{"p", ""})
for _, p := range pTags {
if p[3] == "host" {
data.kind30311Metadata.Host = sdk.FetchProfileMetadata(ctx, pool, p[1], data.event.relays...)
data.kind30311Metadata.HostNpub = data.kind30311Metadata.Host.Npub()
}
}
tTags := event.Tags.GetAll([]string{"t", ""})
for _, t := range tTags {
data.kind30311Metadata.Tags = append(data.kind30311Metadata.Tags, t[1])
data.kind30311Metadata = &Kind30311Metadata{LiveEvent: nip53.ParseLiveEvent(*event)}
host := data.kind30311Metadata.GetHost()
if host != nil {
hostProfile := sdk.FetchProfileMetadata(ctx, pool, host.PubKey, data.event.relays...)
data.kind30311Metadata.Host = &hostProfile
}
case 1311:
data.templateId = LiveEventMessage
data.kind1311Metadata = &Kind1311Metadata{}
data.content = event.Content
if atag := event.Tags.GetFirst([]string{"a", ""}); atag != nil {
parts := strings.Split((*atag)[1], ":")

3
go.mod
View File

@@ -15,7 +15,7 @@ require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/microcosm-cc/bluemonday v1.0.24
github.com/nbd-wtf/emoji v0.0.3
github.com/nbd-wtf/go-nostr v0.27.3
github.com/nbd-wtf/go-nostr v0.28.0
github.com/nbd-wtf/nostr-sdk v0.0.4
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646
github.com/pelletier/go-toml v1.9.5
@@ -63,6 +63,7 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/puzpuzpuz/xsync/v2 v2.5.1 // indirect
github.com/puzpuzpuz/xsync/v3 v3.0.2 // indirect
github.com/savsgio/gotils v0.0.0-20230208104028-c358bd845dee // indirect
github.com/sebest/xff v0.0.0-20210106013422-671bd2870b3a // indirect
github.com/shopspring/decimal v1.3.1 // indirect

6
go.sum
View File

@@ -159,8 +159,8 @@ github.com/microcosm-cc/bluemonday v1.0.24 h1:NGQoPtwGVcbGkKfvyYk1yRqknzBuoMiUrO
github.com/microcosm-cc/bluemonday v1.0.24/go.mod h1:ArQySAMps0790cHSkdPEJ7bGkF2VePWH773hsJNSHf8=
github.com/nbd-wtf/emoji v0.0.3 h1:YtkT7MVPXvqU1SQjvC/CShlWexnREzqNCxmhUnL00CA=
github.com/nbd-wtf/emoji v0.0.3/go.mod h1:tS6D9iI34qwBmWc5g8X7tVDkWXulqbTJRsvsM6QsS88=
github.com/nbd-wtf/go-nostr v0.27.3 h1:u9fdP5h+Ap3LcDFD2j6F2buU/xOM9ddMY0LCDcC6ZyY=
github.com/nbd-wtf/go-nostr v0.27.3/go.mod h1:e5WOFsKEpslDOxIgK00NqemH7KuAvKIW6sBXeJYCfUs=
github.com/nbd-wtf/go-nostr v0.28.0 h1:SLYyoFeCNYb7HyWtmPUzD6rifBOMR66Spj5fzCk+5GE=
github.com/nbd-wtf/go-nostr v0.28.0/go.mod h1:OQ8sNLFJnsj17BdqZiLSmjJBIFTfDqckEYC3utS4qoY=
github.com/nbd-wtf/nostr-sdk v0.0.4 h1:vMCiYpFElKMHPXpZjFVEq4utoTLdTYbkqXVYH1/4uzs=
github.com/nbd-wtf/nostr-sdk v0.0.4/go.mod h1:KQZOtzcrXBlVhpZYG1tw83ADIONNMMPjUU3ZAH5U2RY=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
@@ -185,6 +185,8 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/puzpuzpuz/xsync/v2 v2.5.1 h1:mVGYAvzDSu52+zaGyNjC+24Xw2bQi3kTr4QJ6N9pIIU=
github.com/puzpuzpuz/xsync/v2 v2.5.1/go.mod h1:gD2H2krq/w52MfPLE+Uy64TzJDVY7lP2znR9qmR35kU=
github.com/puzpuzpuz/xsync/v3 v3.0.2 h1:3yESHrRFYr6xzkz61LLkvNiPFXxJEAABanTQpKbAaew=
github.com/puzpuzpuz/xsync/v3 v3.0.2/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rs/cors v1.10.0 h1:62NOS1h+r8p1mW6FM0FSB0exioXLhd/sh15KpjWBZ+8=

View File

@@ -38,19 +38,19 @@ templ liveEventTemplate(params LiveEventPageParams) {
}
</h1>
<div class="mb-4">
if params.LiveEvent.HostNpub != "" {
if params.LiveEvent.Host != nil {
Streaming hosted by
<a href={ templ.URL("/" + params.LiveEvent.HostNpub) }>
<a href={ templ.URL("/" + params.LiveEvent.Host.Npub()) }>
{ params.LiveEvent.Host.Name }
</a>
}
</div>
<!-- main content -->
<div class="mb-4">
for _, v := range params.LiveEvent.Tags {
<span
class="mr-2 whitespace-nowrap rounded bg-neutral-200 px-2 dark:bg-neutral-700 dark:text-white"
>{ v }</span>
for _, v := range params.LiveEvent.Hashtags {
<span class="mr-2 whitespace-nowrap rounded bg-neutral-200 px-2 dark:bg-neutral-700 dark:text-white">
{ v }
</span>
}
</div>
if params.LiveEvent.Summary != "" {

View File

@@ -221,8 +221,6 @@ type LiveEventMessagePageParams struct {
TitleizedContent string
Alt string
LiveEventMessage Kind1311Metadata
Clients []ClientReference
}

View File

@@ -436,8 +436,6 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
TitleizedContent: titleizedContent,
Alt: data.alt,
Clients: generateClientList(data.event.Kind, data.naddr),
LiveEventMessage: *data.kind1311Metadata,
})
case Other:
detailsData.HideDetails = false // always open this since we know nothing else about the event

View File

@@ -13,6 +13,9 @@ import (
"github.com/nbd-wtf/go-nostr"
"github.com/nbd-wtf/go-nostr/nip10"
"github.com/nbd-wtf/go-nostr/nip19"
"github.com/nbd-wtf/go-nostr/nip52"
"github.com/nbd-wtf/go-nostr/nip53"
"github.com/nbd-wtf/go-nostr/nip94"
sdk "github.com/nbd-wtf/nostr-sdk"
"github.com/texttheater/golang-levenshtein/levenshtein"
)
@@ -178,42 +181,14 @@ func (ee EnhancedEvent) ToJSONHTML() template.HTML {
}
type Kind1063Metadata struct {
Magnet string
Dim string
Size string
Summary string
Image string
URL string
AES256GCM string
M string
X string
I string
Blurhash string
Thumb string
nip94.FileMetadata
}
type Kind30311Metadata struct {
Title string
Summary string
Image string
Status string
Host sdk.ProfileMetadata
HostNpub string
Tags []string
nip53.LiveEvent
Host *sdk.ProfileMetadata
}
type Kind1311Metadata struct {
// ...
}
func (fm Kind1063Metadata) IsVideo() bool { return strings.Split(fm.M, "/")[0] == "video" }
func (fm Kind1063Metadata) IsImage() bool { return strings.Split(fm.M, "/")[0] == "image" }
func (fm Kind1063Metadata) DisplayImage() string {
if fm.Image != "" {
return fm.Image
} else if fm.IsImage() {
return fm.URL
} else {
return ""
}
type Kind31922Or31923Metadata struct {
nip52.CalendarEvent
}