~netlandish/links

e67ee5d323216eb0230e0be5d04e2f2dd43928dc — Peter Sanchez 9 months ago 9b44f4e
Moving Slack helper to slack module
3 files changed, 46 insertions(+), 36 deletions(-)

M core/routes.go
M helpers.go
A slack/helpers.go
M core/routes.go => core/routes.go +2 -1
@@ 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
	}

M helpers.go => helpers.go +0 -35
@@ 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)

A slack/helpers.go => slack/helpers.go +44 -0
@@ 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
}