Skip to content

Commit

Permalink
Don't create tweets from replies (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
boreq authored Oct 31, 2023
1 parent cc829c3 commit 09a7ba7
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 27 deletions.
19 changes: 19 additions & 0 deletions service/domain/event_note.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package domain

import (
"github.com/boreq/errors"
)

func NoteIsReplyingToOtherEvent(event Event) (bool, error) {
if event.Kind() != EventKindNote {
return false, errors.New("incorrect event kind")
}

for _, tag := range event.Tags() {
if tag.IsEvent() {
return true, nil
}
}

return false, nil
}
5 changes: 5 additions & 0 deletions service/domain/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
var (
tagProfile = MustNewEventTagName("p")
tagRelay = MustNewEventTagName("r")
tagEvent = MustNewEventTagName("e")
)

type EventTag struct {
Expand Down Expand Up @@ -49,6 +50,10 @@ func (e EventTag) IsRelay() bool {
return e.name == tagRelay
}

func (e EventTag) IsEvent() bool {
return e.name == tagEvent
}

func (e EventTag) Profile() (PublicKey, error) {
if !e.IsProfile() {
return PublicKey{}, errors.New("not a profile tag")
Expand Down
11 changes: 11 additions & 0 deletions service/domain/tweet_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"fmt"
"strings"
"unicode/utf8"

"github.com/boreq/errors"
)

const noteContentMaxLengthInRunes = 200
Expand All @@ -20,6 +22,15 @@ func (g *TweetGenerator) Generate(event Event) ([]Tweet, error) {
return nil, nil
}

isReply, err := NoteIsReplyingToOtherEvent(event)
if err != nil {
return nil, errors.Wrap(err, "error checking if note is a reply")
}

if isReply {
return nil, nil
}

tweetText := g.createText(event)

return []Tweet{
Expand Down
94 changes: 67 additions & 27 deletions service/domain/tweet_generator_test.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,78 @@
package domain_test

import (
"fmt"
"testing"

"github.com/nbd-wtf/go-nostr"
"github.com/planetary-social/nos-crossposting-service/internal/fixtures"
"github.com/planetary-social/nos-crossposting-service/service/domain"
"github.com/stretchr/testify/require"
)

func TestTweetGenerator_GeneratesTweetsFromNotes(t *testing.T) {
eventJSON := `{
"id": "4376c65d2f232afbe9b882a35baa4f6fe8667c4e684749af565f981833ed6a65",
"pubkey": "6e468422dfb74a5738702a8823b9b28168abab8655faacb6853cd0ee15deee93",
"created_at": 1673347337,
"kind": 1,
"content": "Walled gardens became prisons, and nostr is the first step towards tearing down the prison walls.",
"tags": [
["e", "3da979448d9ba263864c4d6f14984c423a3838364ec255f03c7904b1ae77f206"],
["p", "bf2376e17ba4ec269d10fcc996a4746b451152be9031fa48e74553dde5526bce"]
],
"sig": "908a15e46fb4d8675bab026fc230a0e3542bfade63da02d542fb78b2a8513fcd0092619a2c8c1221e581946e0191f2af505dfdf8657a414dbca329186f009262"
}`

event, err := domain.NewEventFromJSON([]byte(eventJSON))
require.NoError(t, err)

g := domain.NewTweetGenerator()
tweets, err := g.Generate(event)
require.NoError(t, err)
require.Equal(t,
[]domain.Tweet{
domain.NewTweet(
"Nostr note made by https://njump.me/npub1dergggklka99wwrs92yz8wdjs952h2ux2ha2ed598ngwu9w7a6fsh9xzpc:\n\nWalled gardens became prisons, and nostr is the first step towards tearing down the prison walls.",
),
func TestTweetGenerator(t *testing.T) {
testCases := []struct {
Name string
Event nostr.Event
GeneratesTweet bool
}{
{
Name: "not_a_reply",
Event: nostr.Event{
Kind: domain.EventKindNote.Int(),
Tags: []nostr.Tag{
[]string{"p", fixtures.SomePublicKey().Hex()},
},
Content: "Some text.",
},
GeneratesTweet: true,
},
tweets,
)
{
Name: "reply",
Event: nostr.Event{
Kind: domain.EventKindNote.Int(),
Tags: []nostr.Tag{
[]string{"p", fixtures.SomePublicKey().Hex()},
[]string{"e", fixtures.SomeEventID().Hex()},
},
Content: "Some text.",
},
GeneratesTweet: false,
},
}

for _, testCase := range testCases {
t.Run(testCase.Name, func(t *testing.T) {
libevent := testCase.Event

authorPublicKey, authorPrivateKey := fixtures.SomeKeyPair()

err := libevent.Sign(authorPrivateKey)
require.NoError(t, err)

event, err := domain.NewEvent(libevent)
require.NoError(t, err)

g := domain.NewTweetGenerator()
tweets, err := g.Generate(event)
require.NoError(t, err)

if testCase.GeneratesTweet {
require.Equal(t,
[]domain.Tweet{
domain.NewTweet(
fmt.Sprintf(
"Nostr note made by https://njump.me/%s:\n\n%s",
authorPublicKey.Npub(),
event.Content(),
),
),
},
tweets,
)
} else {
require.Empty(t, tweets)
}
})
}
}

0 comments on commit 09a7ba7

Please sign in to comment.