-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
0 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
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,45 @@ | ||
package domain | ||
|
||
import ( | ||
"github.com/boreq/errors" | ||
"github.com/planetary-social/nos-event-service/internal" | ||
"github.com/planetary-social/nos-event-service/internal/logging" | ||
) | ||
|
||
// ContactsExtractor does its best to get relay addresses out of events. It | ||
// should only error out on obvious programming errors but not on malformed user | ||
// input. | ||
type ContactsExtractor struct { | ||
logger logging.Logger | ||
} | ||
|
||
func NewContactsExtractor(logger logging.Logger) *ContactsExtractor { | ||
return &ContactsExtractor{logger: logger.New("contactsExtractor")} | ||
} | ||
|
||
func (e *ContactsExtractor) Extract(event Event) ([]PublicKey, error) { | ||
switch event.Kind() { | ||
case EventKindContacts: | ||
return e.extractFromContacts(event) | ||
default: | ||
return nil, nil | ||
} | ||
} | ||
|
||
func (e *ContactsExtractor) extractFromContacts(event Event) ([]PublicKey, error) { | ||
if event.Kind() != EventKindContacts { | ||
return nil, errors.New("invalid event kind") | ||
} | ||
|
||
results := internal.NewEmptySet[PublicKey]() | ||
for _, tag := range event.Tags() { | ||
if tag.IsProfile() { | ||
publicKey, err := tag.Profile() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "error grabbing the profile tag") | ||
} | ||
results.Put(publicKey) | ||
} | ||
} | ||
return results.List(), 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package domain_test | ||
|
||
import ( | ||
"sort" | ||
"testing" | ||
|
||
"github.com/planetary-social/nos-event-service/internal/fixtures" | ||
"github.com/planetary-social/nos-event-service/service/domain" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestContactsExtractor(t *testing.T) { | ||
publicKey1 := fixtures.SomePublicKey() | ||
publicKey2 := fixtures.SomePublicKey() | ||
|
||
testCases := []struct { | ||
Name string | ||
|
||
Kind domain.EventKind | ||
Tags []domain.EventTag | ||
|
||
Result []domain.PublicKey | ||
}{ | ||
{ | ||
Name: "contacts", | ||
|
||
Kind: domain.EventKindContacts, | ||
Tags: []domain.EventTag{ | ||
domain.MustNewEventTag([]string{ | ||
"p", publicKey1.Hex(), | ||
}), | ||
domain.MustNewEventTag([]string{ | ||
"p", publicKey2.Hex(), | ||
}), | ||
}, | ||
|
||
Result: []domain.PublicKey{ | ||
publicKey1, | ||
publicKey2, | ||
}, | ||
}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.Name, func(t *testing.T) { | ||
event := fixtures.Event(testCase.Kind, testCase.Tags, fixtures.SomeString()) | ||
|
||
logger := fixtures.TestLogger(t) | ||
extractor := domain.NewContactsExtractor(logger) | ||
|
||
result, err := extractor.Extract(event) | ||
require.NoError(t, err) | ||
|
||
sort.Slice(result, func(i, j int) bool { | ||
return result[i].Hex() < result[j].Hex() | ||
}) | ||
|
||
sort.Slice(testCase.Result, func(i, j int) bool { | ||
return testCase.Result[i].Hex() < testCase.Result[j].Hex() | ||
}) | ||
|
||
require.Equal(t, testCase.Result, result) | ||
}) | ||
} | ||
} |
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