mirror of
https://github.com/fiatjaf/nak.git
synced 2026-09-14 15:26:24 +02:00
group: parse chat messages with nip27.
This commit is contained in:
34
group.go
34
group.go
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -16,7 +17,9 @@ import (
|
||||
"fiatjaf.com/nostr"
|
||||
"fiatjaf.com/nostr/nip11"
|
||||
"fiatjaf.com/nostr/nip19"
|
||||
"fiatjaf.com/nostr/nip27"
|
||||
"fiatjaf.com/nostr/nip29"
|
||||
"fiatjaf.com/nostr/nip73"
|
||||
"fiatjaf.com/nostr/nipad"
|
||||
"github.com/fatih/color"
|
||||
"github.com/urfave/cli/v3"
|
||||
@@ -276,7 +279,7 @@ var group = &cli.Command{
|
||||
select {
|
||||
case evt := <-sub.Events:
|
||||
meta := sys.FetchProfileMetadata(ctx, evt.PubKey)
|
||||
line := color.HiBlueString(meta.ShortName()) + " " + color.HiCyanString(evt.CreatedAt.Time().Format(time.DateTime)) + ": " + evt.Content
|
||||
line := color.HiBlueString(meta.ShortName()) + " " + color.HiCyanString(evt.CreatedAt.Time().Format(time.DateTime)) + ": " + colorizeNoteContent(ctx, evt.Content)
|
||||
if eosed {
|
||||
stdout(line)
|
||||
} else if base > 0 {
|
||||
@@ -963,6 +966,35 @@ func cond(b bool, ifYes string, ifNo string) string {
|
||||
return ifNo
|
||||
}
|
||||
|
||||
var hashtagRe = regexp.MustCompile(`#+\w+`)
|
||||
|
||||
func colorizeNoteContent(ctx context.Context, content string) string {
|
||||
var sb strings.Builder
|
||||
|
||||
for block := range nip27.Parse(content) {
|
||||
if block.Pointer == nil {
|
||||
sb.WriteString(hashtagRe.ReplaceAllStringFunc(block.Text, func(s string) string {
|
||||
return color.WhiteString(s)
|
||||
}))
|
||||
continue
|
||||
}
|
||||
|
||||
switch ptr := block.Pointer.(type) {
|
||||
case nostr.ProfilePointer:
|
||||
meta := sys.FetchProfileMetadata(ctx, ptr.PublicKey)
|
||||
sb.WriteString(color.GreenString(meta.ShortName()))
|
||||
case nip73.ExternalPointer:
|
||||
sb.WriteString(color.YellowString(block.Text))
|
||||
default:
|
||||
// nevent, naddr and other nostr references
|
||||
text, _ := strings.CutPrefix(block.Text, "nostr:")
|
||||
sb.WriteString(color.MagentaString(text))
|
||||
}
|
||||
}
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
func parseGroupIdentifier(ctx context.Context, c *cli.Command) (relay string, identifier string, author nostr.PubKey, err error) {
|
||||
groupArg := strings.TrimSpace(c.Args().First())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user