diff --git a/.env.example b/.env.example index afd0c0d..ca7796b 100644 --- a/.env.example +++ b/.env.example @@ -15,5 +15,7 @@ MATRIX_WEBHOOK_API_KEY="" MATRIX_WEBHOOK_CHANNEL="" MASTODON_INSTANCE_URL="" -MASTODON_USERNAME="" -MASTODON_PASSWORD="" \ No newline at end of file +MASTODON_CLIENT_ID="" +MASTODON_CLIENT_SECRET="" +MASTODON_CLIENT_USERNAME="" +MASTODON_CLIENT_PASSWORD="" \ No newline at end of file diff --git a/README.md b/README.md index 56334bb..d4fff43 100644 --- a/README.md +++ b/README.md @@ -34,4 +34,4 @@ CREATE UNIQUE INDEX service_lookup ON statsdb USING btree (service,metric_name, GRANT ALL ON ALL TABLES IN SCHEMA public TO gomastodonstats; GRANT USAGE, SELECT ON ALL SEQUENCES IN SCHEMA public TO gomastodonstats; -``` \ No newline at end of file +``` diff --git a/cmd/gomastodonstats/main.go b/cmd/gomastodonstats/main.go index 0d751a4..a169cc5 100644 --- a/cmd/gomastodonstats/main.go +++ b/cmd/gomastodonstats/main.go @@ -51,12 +51,16 @@ func main() { // Load mastodon data if exists gms.MASTODON_INSTANCE_URL = os.Getenv("MASTODON_INSTANCE_URL") - gms.MASTODON_USERNAME = os.Getenv("MASTODON_USERNAME") - gms.MASTODON_PASSWORD = os.Getenv("MASTODON_PASSWORD") + gms.MASTODON_CLIENT_ID = os.Getenv("MASTODON_CLIENT_ID") + gms.MASTODON_CLIENT_SECRET = os.Getenv("MASTODON_CLIENT_SECRET") + gms.MASTODON_CLIENT_USERNAME = os.Getenv("MASTODON_CLIENT_USERNAME") + gms.MASTODON_CLIENT_PASSWORD = os.Getenv("MASTODON_CLIENT_PASSWORD") if gms.MASTODON_INSTANCE_URL == "" || - gms.MASTODON_USERNAME == "" || - gms.MASTODON_PASSWORD == "" { - log.Println("MASTODON_INSTANCE_URL info incompelete. Skipping") + gms.MASTODON_CLIENT_ID == "" || + gms.MASTODON_CLIENT_SECRET == "" || + gms.MASTODON_CLIENT_USERNAME == "" || + gms.MASTODON_CLIENT_PASSWORD == "" { + log.Println("MASTODON_INSTANCE info incompelete. Skipping") // Set URL empty so we can check this later on gms.MASTODON_INSTANCE_URL = "" diff --git a/internal/gomastodonstats/consts.go b/internal/gomastodonstats/consts.go index d0b4764..bea83ca 100644 --- a/internal/gomastodonstats/consts.go +++ b/internal/gomastodonstats/consts.go @@ -14,10 +14,13 @@ var ( MATRIX_WEBHOOK_API_KEY string MATRIX_WEBHOOK_CHANNEL string - MASTODON_INSTANCE_URL string - MASTODON_USERNAME string - MASTODON_PASSWORD string - MASTODON_CLIENT_NAME = "go-mastodon-stats" + MASTODON_CLIENT_NAME = "go-mastodon-stats" + MASTODON_INSTANCE_URL string + MASTODON_CLIENT_ID string + MASTODON_CLIENT_SECRET string + MASTODON_CLIENT_USERNAME string + MASTODON_CLIENT_PASSWORD string + // UserCount metric name METRICNAME_USERCOUNT = "userCount" diff --git a/internal/gomastodonstats/mastodon.go b/internal/gomastodonstats/mastodon.go index 0f193aa..15f2ff0 100644 --- a/internal/gomastodonstats/mastodon.go +++ b/internal/gomastodonstats/mastodon.go @@ -4,10 +4,29 @@ import ( "context" "fmt" "log" + "os" "github.com/mattn/go-mastodon" ) +func doTheRegisterThing() { + srv, err := mastodon.RegisterApp(context.Background(), &mastodon.AppConfig{ + Server: fmt.Sprintf("https://%s", MASTODON_INSTANCE_URL), + ClientName: MASTODON_CLIENT_NAME, + Scopes: "read write follow", + RedirectURIs: "urn:ietf:wg:oauth:2.0:oob", + Website: "https://github.com/mattn/go-mastodon", + }) + if err != nil { + log.Fatal("Couldn't register the app. Error: %v\n\nExiting...\n", err) + } + + log.Println( + "Pls open this link: %s\n\n" + srv.AuthURI + + "When completeAdd this to your config under MASTODON_TOKEN and rerun the app") + os.Exit(0) +} + func postToMastodon(metrics []metric) { if MASTODON_INSTANCE_URL == "" { log.Printf("Skipping posting to mastodon. Missing configuration") @@ -16,24 +35,22 @@ func postToMastodon(metrics []metric) { c := mastodon.NewClient(&mastodon.Config{ Server: fmt.Sprintf("https://%s", MASTODON_INSTANCE_URL), - ClientID: MASTODON_CLIENT_NAME, - ClientSecret: "random-secret-that-doesnt-really-matter-lol", + ClientID: MASTODON_CLIENT_ID, + ClientSecret: MASTODON_CLIENT_SECRET, }) - err := c.Authenticate(context.Background(), MASTODON_USERNAME, MASTODON_PASSWORD) + err := c.Authenticate(context.Background(), MASTODON_CLIENT_USERNAME, MASTODON_CLIENT_PASSWORD) if err != nil { - log.Println("Invalid mastodon credentials?") - log.Print(err) - return + log.Fatal(err) } // Build status startOfDay := getStartofDay() - msg := fmt.Sprintf("Stats for %s\n\n", startOfDay.String()) + msg := fmt.Sprintf("User counts for %s :laserkiwi:\n\n", startOfDay.String()) for _, m := range metrics { msg = msg + getPrintableString(m) + "\n" } - msg = msg + "\n\n" + "#Stats" + msg = msg + "\n\n" + "#DailyStats" toot := &mastodon.Toot{ Status: msg,