Add support for live chat message, kind:1311

This commit is contained in:
Daniele Tonon
2023-11-07 15:05:53 +01:00
parent 9f0b3fa4af
commit 12c1bebf99
6 changed files with 172 additions and 0 deletions

View File

@@ -57,6 +57,12 @@ func generateClientList(style Style, code string, event *nostr.Event) []ClientRe
{ID: "zap.stream", Name: "zap.stream", URL: template.URL("https://zap.stream/" + 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)}, {ID: "nostrudel", Name: "Nostrudel", URL: template.URL("https://nostrudel.ninja/#/streams/" + code)},
} }
} else if event.Kind == 1311 {
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 return nil
} }

16
data.go
View File

@@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"html" "html"
"html/template" "html/template"
"strconv"
"strings" "strings"
"time" "time"
@@ -88,6 +89,7 @@ type Data struct {
alt string alt string
kind1063Metadata *Kind1063Metadata kind1063Metadata *Kind1063Metadata
kind30311Metadata *Kind30311Metadata kind30311Metadata *Kind30311Metadata
kind1311Metadata *Kind1311Metadata
} }
type Kind1063Metadata struct { type Kind1063Metadata struct {
@@ -115,6 +117,10 @@ type Kind30311Metadata struct {
Tags []string Tags []string
} }
type Kind1311Metadata struct {
// ...
}
func (fm Kind1063Metadata) IsVideo() bool { return strings.Split(fm.M, "/")[0] == "video" } 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) IsImage() bool { return strings.Split(fm.M, "/")[0] == "image" }
func (fm Kind1063Metadata) DisplayImage() string { func (fm Kind1063Metadata) DisplayImage() string {
@@ -281,6 +287,16 @@ func grabData(ctx context.Context, code string, isProfileSitemap bool) (*Data, e
for _, t := range tTags { for _, t := range tTags {
data.kind30311Metadata.Tags = append(data.kind30311Metadata.Tags, t[1]) data.kind30311Metadata.Tags = append(data.kind30311Metadata.Tags, t[1])
} }
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], ":")
kind, _ := strconv.Atoi(parts[0])
parentNevent, _ := nip19.EncodeEntity(parts[1], kind, parts[2], data.relays)
data.parentLink = template.HTML(replaceNostrURLsWithTags(nostrEveryMatcher, "nostr:"+parentNevent))
}
default: default:
data.templateId = Other data.templateId = Other
} }

View File

@@ -19,6 +19,7 @@ const (
TelegramInstantView TelegramInstantView
FileMetadata FileMetadata
LiveEvent LiveEvent
LiveEventMessage
Other Other
) )
@@ -328,6 +329,37 @@ type LiveEventPage struct {
func (*LiveEventPage) TemplateText() string { return tmplLiveEvent } func (*LiveEventPage) TemplateText() string { return tmplLiveEvent }
var (
//go:embed templates/live_event_message.html
tmplLiveEventMessage string
LiveEventMessageTemplate = tmpl.MustCompile(&LiveEventMessagePage{})
)
type LiveEventMessagePage struct {
OpenGraphPartial `tmpl:"opengraph"`
HeadCommonPartial `tmpl:"head_common"`
TopPartial `tmpl:"top"`
DetailsPartial `tmpl:"details"`
ClientsPartial `tmpl:"clients"`
FooterPartial `tmpl:"footer"`
Content template.HTML
CreatedAt string
Metadata *sdk.ProfileMetadata
Npub string
NpubShort string
ParentLink template.HTML
SeenOn []string
Style Style
Subject string
TitleizedContent string
Alt string
LiveEventMessage Kind1311Metadata
}
func (*LiveEventMessagePage) TemplateText() string { return tmplLiveEventMessage }
var ( var (
//go:embed templates/relay.html //go:embed templates/relay.html
tmplRelay string tmplRelay string

View File

@@ -362,6 +362,35 @@ func renderEvent(w http.ResponseWriter, r *http.Request) {
LiveEvent: *data.kind30311Metadata, LiveEvent: *data.kind30311Metadata,
}) })
case LiveEventMessage:
// opengraph.Image = data.kind1311Metadata.Image
err = LiveEventMessageTemplate.Render(w, &LiveEventMessagePage{
OpenGraphPartial: opengraph,
HeadCommonPartial: HeadCommonPartial{
IsProfile: false,
TailwindDebugStuff: tailwindDebugStuff,
NaddrNaked: data.naddrNaked,
NeventNaked: data.neventNaked,
},
DetailsPartial: detailsData,
ClientsPartial: ClientsPartial{
Clients: generateClientList(style, data.naddr, data.event),
},
Content: template.HTML(data.content),
CreatedAt: data.createdAt,
Metadata: data.metadata,
Npub: data.npub,
NpubShort: data.npubShort,
ParentLink: data.parentLink,
Style: style,
Subject: subject,
TitleizedContent: titleizedContent,
Alt: data.alt,
LiveEventMessage: *data.kind1311Metadata,
})
case Other: case Other:
detailsData.HideDetails = false // always open this since we know nothing else about the event detailsData.HideDetails = false // always open this since we know nothing else about the event

View File

@@ -0,0 +1,85 @@
<!doctype html>
<html class="theme--default text-lg font-light print:text-base sm:text-xl">
<meta charset="UTF-8" />
<head>
<title>{{.TitleizedContent}}</title>
{{template "opengraph" .OpenGraphPartial}}
<!---->
{{template "head_common" .HeadCommonPartial}}
</head>
<body
class="mb-16 bg-white text-gray-600 dark:bg-neutral-900 dark:text-neutral-50 print:text-black"
>
{{template "top" .}}
<div class="mx-auto px-4 sm:flex sm:items-center sm:justify-center sm:px-0">
<div
class="w-full max-w-screen-2xl justify-between gap-10 overflow-visible print:w-full sm:flex sm:w-11/12 sm:px-4 md:w-10/12 lg:w-9/12 lg:gap-48vw"
>
<div class="w-full break-words print:w-full">
<header class="mb-4 max-w-full">
<a class="flex flex-wrap items-center" href="/{{.Npub}}">
<div
class="print:basis-1-12 imgclip mr-2 max-w-full basis-1/6 overflow-hidden sm:mr-4"
>
<img class="block h-auto w-full" src="{{.Metadata.Picture}}" />
</div>
<div class="block print:text-base sm:grow">
<div class="text-sm leading-4 sm:text-2xl">
{{.Metadata.Name}}
<!---->
{{if not (eq .Metadata.Name .Metadata.DisplayName)}}
<span class="text-sm text-stone-400 sm:text-xl"
>{{.Metadata.DisplayName}}</span
>
{{end}}
</div>
<div class="text-sm leading-4 text-stone-400 sm:text-base">
{{.NpubShort}}
</div>
</div>
</a>
</header>
<div class="w-full text-right text-sm text-stone-400">
{{.CreatedAt}}
</div>
<div class="w-full text-right text-sm text-stone-400">
{{ if not (eq "" .ParentLink) }} messagin during the live event
<span class="text-strongpink">{{ .ParentLink }}</span> {{ end }}
</div>
<div
class="-ml-4 mb-6 h-1.5 w-1/3 bg-zinc-100 dark:bg-zinc-700 sm:-ml-2.5"
></div>
<article
class="prose-cite:text-sm prose mb-6 leading-5 dark:prose-invert prose-headings:font-light prose-p:m-0 prose-p:mb-2 prose-blockquote:mx-0 prose-blockquote:my-8 prose-blockquote:border-l-05rem prose-blockquote:border-solid prose-blockquote:border-l-gray-100 prose-blockquote:py-2 prose-blockquote:pl-4 prose-blockquote:pr-0 prose-ol:m-0 prose-ol:p-0 prose-ol:pl-4 prose-ul:m-0 prose-ul:p-0 prose-ul:pl-4 prose-li:mb-2 dark:prose-blockquote:border-l-zinc-800 sm:prose-a:text-justify"
>
{{ if (not (eq "" .Subject))}}
<h1 class="text-2xl">{{.Subject}}</h1>
{{ else }}
<h1 class="hidden">
{{.Metadata.Name}} on Nostr: {{.TitleizedContent}}
</h1>
{{ end }}
<!-- main content -->
{{ .Content }}
</article>
{{template "details" .DetailsPartial}}
<div
class="-ml-4 mb-6 h-1.5 w-1/3 bg-zinc-100 dark:bg-zinc-700 sm:-ml-2.5"
></div>
</div>
{{template "clients" .ClientsPartial}}
</div>
</div>
{{template "footer" .}}
</body>
</html>

View File

@@ -61,6 +61,7 @@ var kindNames = map[int]string{
43: "Channel Hide Message", 43: "Channel Hide Message",
44: "Channel Mute User", 44: "Channel Mute User",
1063: "File Metadata", 1063: "File Metadata",
1311: "Live Chat Message",
1984: "Reporting", 1984: "Reporting",
9734: "Zap Request", 9734: "Zap Request",
9735: "Zap", 9735: "Zap",
@@ -80,6 +81,7 @@ var kindNames = map[int]string{
30018: "Create or update a product", 30018: "Create or update a product",
30023: "Long-form Content", 30023: "Long-form Content",
30078: "Application-specific Data", 30078: "Application-specific Data",
30311: "Live Event",
} }
var kindNIPs = map[int]string{ var kindNIPs = map[int]string{
@@ -98,6 +100,7 @@ var kindNIPs = map[int]string{
43: "28", 43: "28",
44: "28", 44: "28",
1063: "94", 1063: "94",
1311: "53",
1984: "56", 1984: "56",
9734: "57", 9734: "57",
9735: "57", 9735: "57",
@@ -117,6 +120,7 @@ var kindNIPs = map[int]string{
30018: "15", 30018: "15",
30023: "23", 30023: "23",
30078: "78", 30078: "78",
30311: "53",
} }
type Style string type Style string