-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't create tweets from replies (#28)
- Loading branch information
Showing
4 changed files
with
102 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |