-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add payment, new claim notifications
- Loading branch information
Showing
9 changed files
with
164 additions
and
3 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
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
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,29 @@ | ||
package notifications | ||
|
||
import ( | ||
"net/url" | ||
|
||
"github.com/spf13/cast" | ||
) | ||
|
||
const payment = "payment" | ||
const newClaim = "new_claim" | ||
|
||
func PaymentEvent(lbc float64, address, txid string, vout uint) { | ||
values := url.Values{} | ||
values.Add("lbc", cast.ToString(lbc)) | ||
values.Add("tx_id", txid) | ||
values.Add("vout", cast.ToString(vout)) | ||
values.Add("address", address) | ||
go Notify(payment, values) | ||
} | ||
|
||
func ClaimEvent(claimID, name, title, txID, channeClaimID string) { | ||
values := url.Values{} | ||
values.Add("claim_id", claimID) | ||
values.Add("name", name) | ||
values.Add("title", title) | ||
values.Add("tx_id", txID) | ||
values.Add("channel_claim_id", channeClaimID) | ||
go Notify(newClaim, values) | ||
} |
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,26 @@ | ||
package notifications | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
|
||
"github.com/spf13/viper" | ||
) | ||
|
||
func TestAddSubscriber(t *testing.T) { | ||
viper.SetConfigType("toml") | ||
err := viper.ReadConfig(strings.NewReader(` | ||
[[subscription.payment]] | ||
url= "http://localhost:8080/event/payment" | ||
auth_token="mytoken" | ||
[[subscription.payment]] | ||
url= "http://localhost:8080/event/payment" | ||
auth_token="mytoken" | ||
`)) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
subs := viper.GetStringMap("subscription") | ||
applySubscribers(subs) | ||
|
||
} |
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,55 @@ | ||
package notifications | ||
|
||
import ( | ||
"net/http" | ||
"net/url" | ||
|
||
"github.com/lbryio/lbry.go/v2/extras/errors" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
type subscriber struct { | ||
URL string | ||
Type string | ||
Params url.Values | ||
} | ||
|
||
var subscriptions map[string][]subscriber | ||
|
||
func AddSubscriber(address, subType string, params map[string]interface{}) { | ||
if subscriptions == nil { | ||
subscriptions = make(map[string][]subscriber) | ||
} | ||
urlParams := url.Values{} | ||
for param, v := range params { | ||
value, ok := v.(string) | ||
if ok { | ||
urlParams.Set(param, value) | ||
} | ||
} | ||
subscriptions[subType] = append(subscriptions[subType], subscriber{URL: address, Type: subType, Params: urlParams}) | ||
} | ||
|
||
func ClearSubscribers() { | ||
subscriptions = make(map[string][]subscriber) | ||
} | ||
|
||
func Notify(t string, values url.Values) { | ||
subs, ok := subscriptions[t] | ||
if ok { | ||
for _, s := range subs { | ||
for param, value := range s.Params { | ||
values.Set(param, value[0]) | ||
} | ||
s.notify(values) | ||
} | ||
} | ||
} | ||
|
||
func (s subscriber) notify(values url.Values) { | ||
_, err := http.PostForm(s.URL, values) | ||
if err != nil { | ||
logrus.Error(errors.Prefix("Notify:", errors.Err(err))) | ||
} | ||
} |