Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change tweet format to include an nevent link #36

Merged
merged 1 commit into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions service/domain/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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 {
Expand Down
34 changes: 34 additions & 0 deletions service/domain/event_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 4 additions & 4 deletions service/domain/tweet_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
}
9 changes: 5 additions & 4 deletions service/domain/tweet_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(),
),
),
},
Expand Down