From e67ee5d323216eb0230e0be5d04e2f2dd43928dc Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Wed, 14 Feb 2024 17:45:49 -0600 Subject: [PATCH] Moving Slack helper to slack module --- core/routes.go | 3 ++- helpers.go | 35 ----------------------------------- slack/helpers.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 36 deletions(-) create mode 100644 slack/helpers.go diff --git a/core/routes.go b/core/routes.go index 72dec00..62eeee4 100644 --- a/core/routes.go +++ b/core/routes.go @@ -9,6 +9,7 @@ import ( "links/domain" "links/internal/localizer" "links/models" + "links/slack" "net/http" "net/url" "strconv" @@ -2088,7 +2089,7 @@ func (s *Service) Integrations(c echo.Context) error { mmConn = mmConns[0] } - slackURL, err := links.GetSlackURL(c, org.Slug) + slackURL, err := slack.GetSlackURL(c, org.Slug) if err != nil { return err } diff --git a/helpers.go b/helpers.go index 5977c6b..6d5b2f1 100644 --- a/helpers.go +++ b/helpers.go @@ -244,41 +244,6 @@ func GetUserTZ(user *models.User) string { return DefaultTZ } -// GetSlackURL ... -func GetSlackURL(c echo.Context, slug string) (string, error) { - gctx := c.(*server.Context) - redirectURL, err := url.JoinPath( - fmt.Sprintf("%s://%s", gctx.Server.Config.Scheme, gctx.Server.Config.Domain), - c.Echo().Reverse("slack:slack_connect")) - if err != nil { - return "", err - } - base, ok := gctx.Server.Config.File.Get("slack", "oauth-install-url") - if !ok { - return "", fmt.Errorf("Slack is not configured correctly") - } - clientID, ok := gctx.Server.Config.File.Get("slack", "client-id") - if !ok { - return "", fmt.Errorf("Slack is not configured correctly") - } - scope, ok := gctx.Server.Config.File.Get("slack", "scopes") - if !ok { - return "", fmt.Errorf("Slack is not configured correctly") - } - - parts, err := url.Parse(base) - if err != nil { - return "", err - } - qs := parts.Query() - qs.Set("client_id", clientID) - qs.Set("redirect_uri", redirectURL) - qs.Set("scope", scope) - qs.Set("state", slug) - parts.RawQuery = qs.Encode() - return parts.String(), nil -} - // https://gist.github.com/inotnako/c4a82f6723f6ccea5d83c5d3689373dd func extract(resp io.Reader) *models.HTMLMeta { z := html.NewTokenizer(resp) diff --git a/slack/helpers.go b/slack/helpers.go new file mode 100644 index 0000000..0ad19e6 --- /dev/null +++ b/slack/helpers.go @@ -0,0 +1,44 @@ +package slack + +import ( + "fmt" + "net/url" + + "github.com/labstack/echo/v4" + "netlandish.com/x/gobwebs/server" +) + +// GetSlackURL ... +func GetSlackURL(c echo.Context, slug string) (string, error) { + gctx := c.(*server.Context) + redirectURL, err := url.JoinPath( + fmt.Sprintf("%s://%s", gctx.Server.Config.Scheme, gctx.Server.Config.Domain), + c.Echo().Reverse("slack:slack_connect")) + if err != nil { + return "", err + } + base, ok := gctx.Server.Config.File.Get("slack", "oauth-install-url") + if !ok { + return "", fmt.Errorf("Slack is not configured correctly") + } + clientID, ok := gctx.Server.Config.File.Get("slack", "client-id") + if !ok { + return "", fmt.Errorf("Slack is not configured correctly") + } + scope, ok := gctx.Server.Config.File.Get("slack", "scopes") + if !ok { + return "", fmt.Errorf("Slack is not configured correctly") + } + + parts, err := url.Parse(base) + if err != nil { + return "", err + } + qs := parts.Query() + qs.Set("client_id", clientID) + qs.Set("redirect_uri", redirectURL) + qs.Set("scope", scope) + qs.Set("state", slug) + parts.RawQuery = qs.Encode() + return parts.String(), nil +} -- 2.45.2