From 93c91f3b3611c84049f4575545b308695a2733e8 Mon Sep 17 00:00:00 2001 From: boreq Date: Fri, 3 Nov 2023 10:18:22 +0900 Subject: [PATCH] Change tweet format to include an nevent link --- service/domain/event.go | 9 +++++++ service/domain/event_test.go | 34 ++++++++++++++++++++++++++ service/domain/tweet_generator.go | 8 +++--- service/domain/tweet_generator_test.go | 9 ++++--- 4 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 service/domain/event_test.go diff --git a/service/domain/event.go b/service/domain/event.go index c943233..ba27160 100644 --- a/service/domain/event.go +++ b/service/domain/event.go @@ -6,6 +6,7 @@ import ( "github.com/boreq/errors" "github.com/nbd-wtf/go-nostr" + "github.com/nbd-wtf/go-nostr/nip19" "github.com/planetary-social/nos-crossposting-service/internal" ) @@ -128,6 +129,14 @@ func (e Event) MarshalJSON() ([]byte, error) { return e.libevent.MarshalJSON() } +func (e Event) Nevent() string { + nevent, err := nip19.EncodeEvent(e.id.Hex(), nil, e.pubKey.Hex()) + if err != nil { + panic(err) + } + return nevent +} + func (e Event) Raw() []byte { j, err := e.libevent.MarshalJSON() if err != nil { diff --git a/service/domain/event_test.go b/service/domain/event_test.go new file mode 100644 index 0000000..8646294 --- /dev/null +++ b/service/domain/event_test.go @@ -0,0 +1,34 @@ +package domain_test + +import ( + "strings" + "testing" + + "github.com/nbd-wtf/go-nostr" + "github.com/nbd-wtf/go-nostr/nip19" + "github.com/planetary-social/nos-crossposting-service/internal/fixtures" + "github.com/planetary-social/nos-crossposting-service/service/domain" + "github.com/stretchr/testify/require" +) + +func TestEvent_Nevent(t *testing.T) { + _, sk := fixtures.SomeKeyPair() + + libevent := nostr.Event{ + Kind: domain.EventKindNote.Int(), + Content: "Note text.", + } + err := libevent.Sign(sk) + require.NoError(t, err) + + event, err := domain.NewEvent(libevent) + require.NoError(t, err) + require.Greater(t, len(event.Nevent()), 0) + require.True(t, strings.HasPrefix(event.Nevent(), "nevent")) + + _, v, err := nip19.Decode(event.Nevent()) + require.NoError(t, err) + readNevent := v.(nostr.EventPointer) + require.Equal(t, event.Id().Hex(), readNevent.ID) + require.Equal(t, event.PublicKey().Hex(), readNevent.Author) +} diff --git a/service/domain/tweet_generator.go b/service/domain/tweet_generator.go index b83a59a..89bb501 100644 --- a/service/domain/tweet_generator.go +++ b/service/domain/tweet_generator.go @@ -40,8 +40,6 @@ func (g *TweetGenerator) Generate(event Event) ([]Tweet, error) { func (g *TweetGenerator) createText(event Event) string { var builder strings.Builder - builder.WriteString(fmt.Sprintf("Nostr note made by %s:\n", g.njumpLinkPublicKey(event.PublicKey()))) - builder.WriteString("\n") if utf8.RuneCountInString(event.Content()) <= noteContentMaxLengthInRunes { builder.WriteString(event.Content()) @@ -57,9 +55,11 @@ func (g *TweetGenerator) createText(event Event) string { builder.WriteString("...") } + builder.WriteString(fmt.Sprintf("\n\n%s", g.njumpLinkEvent(event))) + return builder.String() } -func (g *TweetGenerator) njumpLinkPublicKey(publicKey PublicKey) string { - return fmt.Sprintf("https://njump.me/%s", publicKey.Npub()) +func (g *TweetGenerator) njumpLinkEvent(event Event) string { + return fmt.Sprintf("https://njump.me/%s", event.Nevent()) } diff --git a/service/domain/tweet_generator_test.go b/service/domain/tweet_generator_test.go index bcf0c93..fee7e9e 100644 --- a/service/domain/tweet_generator_test.go +++ b/service/domain/tweet_generator_test.go @@ -45,7 +45,7 @@ func TestTweetGenerator(t *testing.T) { t.Run(testCase.Name, func(t *testing.T) { libevent := testCase.Event - authorPublicKey, authorPrivateKey := fixtures.SomeKeyPair() + _, authorPrivateKey := fixtures.SomeKeyPair() err := libevent.Sign(authorPrivateKey) require.NoError(t, err) @@ -62,9 +62,10 @@ func TestTweetGenerator(t *testing.T) { []domain.Tweet{ domain.NewTweet( fmt.Sprintf( - "Nostr note made by https://njump.me/%s:\n\n%s", - authorPublicKey.Npub(), - event.Content(), + `Some text. + +https://njump.me/%s`, + event.Nevent(), ), ), },