From f647d203a553f30f2092f6124938a654ce21bf1c Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 31 Jan 2020 14:50:39 +0000 Subject: [PATCH] replicate switches to pluralkit and switchcounter --- go.mod | 2 +- go.sum | 2 ++ switchcounter/pluralkit.go | 36 ++++++++++++++++++++++++++++++++++ switchcounter/storage.go | 2 ++ switchcounter/switch.go | 12 ++++++++++++ switchcounter/switchcounter.go | 27 +++++++++++++++++++++++++ 6 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 switchcounter/pluralkit.go create mode 100644 switchcounter/switchcounter.go diff --git a/go.mod b/go.mod index 263d887..3141f23 100644 --- a/go.mod +++ b/go.mod @@ -26,6 +26,6 @@ require ( golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d gopkg.in/mxpv/patreon-go.v1 v1.0.0-20171031001022-1d2f253ac700 gopkg.in/rethinkdb/rethinkdb-go.v6 v6.0.0 - within.website/ln v0.7.0 + within.website/ln v0.8.0 within.website/x v1.2.2 ) diff --git a/go.sum b/go.sum index ddcb131..93b0630 100644 --- a/go.sum +++ b/go.sum @@ -294,5 +294,7 @@ within.website/confyg v0.4.0/go.mod h1:KD5rDgkE3B+vbDiH/usiuK+CfiOkCiuD87NEF3/bu within.website/johaus v1.1.0/go.mod h1:0RBs2TucQ3CZQiVoqNj9Wch7mtZY05uvNO14v0lKcM4= within.website/ln v0.7.0 h1:cZUc53cZF/+hWuEAv1VbqlYJ5czuPFHKfH0hLKmlIUA= within.website/ln v0.7.0/go.mod h1:ifURKqsCJekcsdUE+hyCdcuhQqQ+9v9DfA++ZqYxZFE= +within.website/ln v0.8.0 h1:NX6Eo3LkM9RU8lLRbWpmR5/jQRYKtZ8zuiYi7mmKa6w= +within.website/ln v0.8.0/go.mod h1:I+Apk8qxMStNXTZdyDMqDqe6CB8Hn6+W/Gyf5QbY+2E= within.website/x v1.2.2 h1:679eknZs5Inji31vCrvUxsA68VHHYPY4ro55grv4BqQ= within.website/x v1.2.2/go.mod h1:f6yg4hO/pprWxgRNHxKZU0Z/aKCsZq+WLg3iR8n1V6U= diff --git a/switchcounter/pluralkit.go b/switchcounter/pluralkit.go new file mode 100644 index 0000000..4b9f45a --- /dev/null +++ b/switchcounter/pluralkit.go @@ -0,0 +1,36 @@ +package switchcounter + +import ( + "encoding/json" + "flag" + "fmt" + "net/http" + + "within.website/mi/pluralkit" +) + +var ( + pkToken = flag.String("pluralkit-token", "", "PluralKit API token") + pkSystem = flag.String("pluralkit-system-id", "", "PluralKit system ID") + pkMembers = flag.String("pluralkit-mappings", "", "JSON-encoded map of member -> pluralkit IDs") + + mappings = map[string]string{} +) + +func initMappings() { + err := json.Unmarshal([]byte(*pkMembers), &mappings) + if err != nil { + panic(err) + } +} + +func replicateToPluralKit(member string) error { + c := pluralkit.New(*pkToken, http.DefaultClient) + + id, ok := mappings[member] + if !ok { + return fmt.Errorf("unexpected member %s", member) + } + + return c.Switch([]string{id}) +} diff --git a/switchcounter/storage.go b/switchcounter/storage.go index 1a108ed..b624975 100644 --- a/switchcounter/storage.go +++ b/switchcounter/storage.go @@ -15,6 +15,8 @@ func New(session *r.Session, mux *http.ServeMux) *Switches { session: session, } + initMappings() + mux.HandleFunc("/switches/id/", result.GetID) mux.HandleFunc("/switches/", result.GetSwitches) mux.HandleFunc("/switches/current", result.Current) diff --git a/switchcounter/switch.go b/switchcounter/switch.go index cde313e..df6647e 100644 --- a/switchcounter/switch.go +++ b/switchcounter/switch.go @@ -97,6 +97,18 @@ func (s *Switches) Switch(ctx context.Context, who string) (Switch, Switch, erro return lastSw, currentSw, err } + go func(who string) { + err = replicateToPluralKit(who) + if err != nil { + ln.Error(ctx, err, ln.Action("while switching"), ln.F{"to": who}) + } + + err = replicateToSwitchCounter(who) + if err != nil { + ln.Error(ctx, err, ln.Action("while switching"), ln.F{"to": who}) + } + }(who) + ln.Log(ctx, f, ln.Info("switched")) return lastSw, currentSw, nil diff --git a/switchcounter/switchcounter.go b/switchcounter/switchcounter.go new file mode 100644 index 0000000..00031da --- /dev/null +++ b/switchcounter/switchcounter.go @@ -0,0 +1,27 @@ +package switchcounter + +import ( + "flag" + "net/http" + + "within.website/x/web/switchcounter" +) + +var ( + scWebhook = flag.String("switchcounter-webhook", "", "Webhook for switchcounter.science") +) + +func replicateToSwitchCounter(who string) error { + a := switchcounter.NewHTTPClient(*scWebhook) + req := a.Switch(who) + resp, err := http.DefaultClient.Do(req) + if err != nil { + return nil + } + + if err := switchcounter.Validate(resp); err != nil { + return err + } + + return nil +}