Skip to content

Commit

Permalink
fix: use string or number type for boostagram field flexibility (#713)
Browse files Browse the repository at this point in the history
## Description
Currently if someone sends a boostagram with wrong datatype for Episode,
FeedId, ItemId or SenderId (i.e. number instead of string) we throw an
error, this fix makes it flexible to accept both string/number in
boostagram json
  • Loading branch information
im-adithya authored Oct 8, 2024
2 parents 7e9dd81 + 0a72daa commit f2670bd
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 22 deletions.
8 changes: 4 additions & 4 deletions api/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func toApiBoostagram(boostagram *transactions.Boostagram) *Boostagram {
Name: boostagram.Name,
Podcast: boostagram.Podcast,
URL: boostagram.URL,
Episode: boostagram.Episode,
FeedId: boostagram.FeedId,
ItemId: boostagram.ItemId,
Episode: boostagram.Episode.String(),
FeedId: boostagram.FeedId.String(),
ItemId: boostagram.ItemId.String(),
Timestamp: boostagram.Timestamp,
Message: boostagram.Message,
SenderId: boostagram.SenderId,
SenderId: boostagram.SenderId.String(),
SenderName: boostagram.SenderName,
Time: boostagram.Time,
Action: boostagram.Action,
Expand Down
4 changes: 2 additions & 2 deletions transactions/keysend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ func TestSendKeysend_TLVs(t *testing.T) {
assert.Equal(t, "⚡ WebLN Demo", boostagram.AppName)
assert.Equal(t, "⚡ WebLN Demo", boostagram.Name)
assert.Equal(t, "Podcasting 2.0", boostagram.Podcast)
assert.Equal(t, "Episode 104: A New Dump", boostagram.Episode)
assert.Equal(t, "https://feeds.podcastindex.org/pc20.xml", boostagram.FeedId)
assert.Equal(t, "Episode 104: A New Dump", boostagram.Episode.String())
assert.Equal(t, "https://feeds.podcastindex.org/pc20.xml", boostagram.FeedId.String())
assert.Equal(t, int64(21), boostagram.Timestamp)
assert.Equal(t, "Go podcasting!", boostagram.Message)
assert.Equal(t, "Satoshi Nakamoto", boostagram.SenderName)
Expand Down
4 changes: 2 additions & 2 deletions transactions/notifications_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ func TestNotifications_ReceivedKeysend(t *testing.T) {
assert.Equal(t, "⚡ WebLN Demo", boostagram.AppName)
assert.Equal(t, "⚡ WebLN Demo", boostagram.Name)
assert.Equal(t, "Podcasting 2.0", boostagram.Podcast)
assert.Equal(t, "Episode 104: A New Dump", boostagram.Episode)
assert.Equal(t, "https://feeds.podcastindex.org/pc20.xml", boostagram.FeedId)
assert.Equal(t, "Episode 104: A New Dump", boostagram.Episode.String())
assert.Equal(t, "https://feeds.podcastindex.org/pc20.xml", boostagram.FeedId.String())
assert.Equal(t, int64(21), boostagram.Timestamp)
assert.Equal(t, "Go podcasting!", boostagram.Message)
assert.Equal(t, "Satoshi Nakamoto", boostagram.SenderName)
Expand Down
52 changes: 38 additions & 14 deletions transactions/transactions_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,44 @@ const (
type Transaction = db.Transaction

type Boostagram struct {
AppName string `json:"app_name"`
Name string `json:"name"`
Podcast string `json:"podcast"`
URL string `json:"url"`
Episode string `json:"episode,omitempty"`
FeedId string `json:"feedID,omitempty"`
ItemId string `json:"itemID,omitempty"`
Timestamp int64 `json:"ts,omitempty"`
Message string `json:"message,omitempty"`
SenderId string `json:"sender_id"`
SenderName string `json:"sender_name"`
Time string `json:"time"`
Action string `json:"action"`
ValueMsatTotal int64 `json:"value_msat_total"`
AppName string `json:"app_name"`
Name string `json:"name"`
Podcast string `json:"podcast"`
URL string `json:"url"`
Episode StringOrNumber `json:"episode,omitempty"`
FeedId StringOrNumber `json:"feedID,omitempty"`
ItemId StringOrNumber `json:"itemID,omitempty"`
Timestamp int64 `json:"ts,omitempty"`
Message string `json:"message,omitempty"`
SenderId StringOrNumber `json:"sender_id"`
SenderName string `json:"sender_name"`
Time string `json:"time"`
Action string `json:"action"`
ValueMsatTotal int64 `json:"value_msat_total"`
}

type StringOrNumber struct {
StringData string
NumberData int64
}

func (sn *StringOrNumber) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &sn.StringData); err == nil {
return nil
}

if err := json.Unmarshal(data, &sn.NumberData); err == nil {
return nil
}

return fmt.Errorf("cannot unmarshal %s into StringOrNumber type", data)
}

func (sn StringOrNumber) String() string {
if sn.StringData != "" {
return sn.StringData
}
return fmt.Sprintf("%d", sn.NumberData)
}

type notFoundError struct {
Expand Down

0 comments on commit f2670bd

Please sign in to comment.