replicate switches to pluralkit and switchcounter
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Cadey Ratio 2020-01-31 14:50:39 +00:00
parent cc5d17a9af
commit f647d203a5
6 changed files with 80 additions and 1 deletions

2
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

View File

@ -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})
}

View File

@ -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)

View File

@ -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

View File

@ -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
}