From 9295c972c49409e92c2a6a083012b908e26dddbf Mon Sep 17 00:00:00 2001 From: Peter Sanchez Date: Wed, 14 Feb 2024 18:28:45 -0600 Subject: [PATCH] Moving to helper functions to get links-service-domain when needed --- api/graph/schema.resolvers.go | 22 +++++++-------- billing/routes.go | 31 +++++----------------- core/import.go | 6 +---- core/routes.go | 6 ++--- helpers.go | 20 ++++++++++++++ list/routes.go | 9 +------ mattermost/routes.go | 50 ++++++++++++++++++++--------------- slack/helpers.go | 12 ++++----- slack/routes.go | 24 ++++++++++------- 9 files changed, 88 insertions(+), 92 deletions(-) diff --git a/api/graph/schema.resolvers.go b/api/graph/schema.resolvers.go index b9bf43a..a5f4036 100644 --- a/api/graph/schema.resolvers.go +++ b/api/graph/schema.resolvers.go @@ -293,10 +293,8 @@ func (r *mutationResolver) AddNote(ctx context.Context, input *model.NoteInput) } // Create the note url - mainDomain, ok := srv.Config.File.Get("links", "links-service-domain") - if !ok { - return nil, fmt.Errorf(lt.Translate("links-service-domain not found")) - } + c := server.EchoForContext(ctx) + mainDomain := links.GetLinksDomain(c) noteURL, noteHash := links.CreateNoteURL(mainDomain) // If the note is public we create a based link @@ -3471,16 +3469,14 @@ func (r *mutationResolver) AddQRCode(ctx context.Context, input model.AddQRCodeI // Create QR image pointing to the QR redirect page srv := server.ForContext(ctx) - mainDomain, ok := srv.Config.File.Get("links", "links-service-domain") - if !ok { - return nil, fmt.Errorf(lt.Translate("links-service-domain not found")) - } + c := server.EchoForContext(ctx) + + mainDomain := links.GetLinksDomain(c) mainDomain = strings.ToLower(mainDomain) - qrRedirect := &url.URL{ - Scheme: "https", - Host: mainDomain, - Path: fmt.Sprintf("q/%s", qr.HashID), - } + + qrRedirect := links.GetLinksDomainURL(c) + qrRedirect.Path = fmt.Sprintf("q/%s", qr.HashID) + qrPath := fmt.Sprintf("media/qrcodes/%s/%s.jpg", ksuid.New().String(), ksuid.New().String()) options := []standard.ImageOption{ diff --git a/billing/routes.go b/billing/routes.go index bc0ca41..6b13e38 100644 --- a/billing/routes.go +++ b/billing/routes.go @@ -7,7 +7,6 @@ import ( "links/internal/localizer" "links/models" "net/http" - "net/url" "strconv" "git.sr.ht/~emersion/gqlclient" @@ -102,15 +101,9 @@ func (s *Service) SubscriptionPortal(c echo.Context) error { if err != nil { return err } - mainDomain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") - if !ok { - return fmt.Errorf("main domain not found") - } - returnURL := &url.URL{ - Scheme: gctx.Server.Config.Scheme, - Host: mainDomain, - Path: c.Echo().Reverse("core:org_list"), - } + + returnURL := links.GetLinksDomainURL(c) + returnURL.Path = c.Echo().Reverse("core:org_list") params := &stripe.BillingPortalSessionParams{ Customer: stripe.String(resultSub.Customer.ID), @@ -518,20 +511,10 @@ func (s *Service) CreateSubscription(c echo.Context) error { return gctx.Render(http.StatusOK, "billing_create_subscription.html", gmap) } - mainDomain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") - if !ok { - return fmt.Errorf("main domain not found") - } - successURL := &url.URL{ - Scheme: gctx.Server.Config.Scheme, - Host: mainDomain, - Path: c.Echo().Reverse(s.RouteName("success")), - } - cancelURL := &url.URL{ - Scheme: gctx.Server.Config.Scheme, - Host: mainDomain, - Path: c.Echo().Reverse(s.RouteName("cancel")), - } + successURL := links.GetLinksDomainURL(c) + successURL.Path = c.Echo().Reverse(s.RouteName("success")) + cancelURL := links.GetLinksDomainURL(c) + cancelURL.Path = c.Echo().Reverse(s.RouteName("cancel")) params := &stripe.CheckoutSessionParams{ CustomerEmail: stripe.String(user.Email), ClientReferenceID: stripe.String(strconv.Itoa(org.ID)), diff --git a/core/import.go b/core/import.go index 41adf1a..5d611f5 100644 --- a/core/import.go +++ b/core/import.go @@ -4,7 +4,6 @@ import ( "context" "database/sql" "encoding/json" - "fmt" "links" "links/models" "mime/multipart" @@ -328,10 +327,7 @@ func ImportFromPinBoard(c echo.Context, src multipart.File, gctx := c.(*server.Context) billEnabled := links.BillingEnabled(gctx.Server.Config) - domain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") - if !ok { - return fmt.Errorf("No links-service-domain found") - } + domain := links.GetLinksDomain(c) for start < len(pinBoardList) { if end+step > len(pinBoardList) { diff --git a/core/routes.go b/core/routes.go index 62eeee4..cd9bbe4 100644 --- a/core/routes.go +++ b/core/routes.go @@ -2073,8 +2073,8 @@ func (s *Service) Integrations(c echo.Context) error { isRestricted = true } - baseURL := fmt.Sprintf("%s://%s", gctx.Server.Config.Scheme, gctx.Server.Config.Domain) - mmURL, _ := url.JoinPath(baseURL, c.Echo().Reverse("mattermost:manifest", slug)) + baseURL := links.GetLinksDomainURL(c) + baseURL.Path = c.Echo().Reverse("mattermost:manifest", slug) opts = &database.FilterOptions{ Filter: sq.Eq{"o.owner_id": user.ID}, Limit: 1, @@ -2098,7 +2098,7 @@ func (s *Service) Integrations(c echo.Context) error { "org": org, "pd": pd, "isRestricted": isRestricted, - "mmURL": mmURL, + "mmURL": baseURL.String(), "slackURL": slackURL, "mmConn": mmConn, } diff --git a/helpers.go b/helpers.go index 6d5b2f1..ee129cc 100644 --- a/helpers.go +++ b/helpers.go @@ -767,3 +767,23 @@ func AddQueryElement(q url.Values, param, val string) template.URL { q.Set(param, curVal) return template.URL(q.Encode()) } + +// GetLinksDomain will simply return the config value for `links:links-service-domain` +func GetLinksDomain(c echo.Context) string { + gctx := c.(*server.Context) + mainDomain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") + if !ok { + panic("links:links-service-domain domain not found") + } + return mainDomain +} + +// GetLinksDomainURL will return a url.URL instance with the Scheme and Host populated. +func GetLinksDomainURL(c echo.Context) *url.URL { + mainDomain := GetLinksDomain(c) + gctx := c.(*server.Context) + return &url.URL{ + Scheme: gctx.Server.Config.Scheme, + Host: mainDomain, + } +} diff --git a/list/routes.go b/list/routes.go index e1ec119..1f68806 100644 --- a/list/routes.go +++ b/list/routes.go @@ -1491,14 +1491,7 @@ func (r *DetailService) ListDetail(c echo.Context) error { domain := domain.ForContext(c.Request().Context()) if slug == "" && domain.Level == models.DomainLevelSystem { - linksDomain, ok := gctx.Server.Config.File.Get("links", "links-service-domain") - if !ok { - return fmt.Errorf("links:links-service-domain not found") - } - nextURL := &url.URL{ - Scheme: gctx.Server.Config.Scheme, - Host: linksDomain, - } + nextURL := links.GetLinksDomainURL(c) return c.Redirect(http.StatusMovedPermanently, nextURL.String()) } type GraphQLResponse struct { diff --git a/mattermost/routes.go b/mattermost/routes.go index 86f6b4c..5a7f17b 100644 --- a/mattermost/routes.go +++ b/mattermost/routes.go @@ -7,7 +7,6 @@ import ( "links/internal/localizer" "links/models" "net/http" - "net/url" "strconv" "git.sr.ht/~emersion/gqlclient" @@ -264,11 +263,13 @@ func (s *Service) SearchCommand(c echo.Context) error { } if len(mmUsers) == 0 { // Send private msg with instrucctions - baseURL := fmt.Sprintf("%s://%s", gctx.Server.Config.Scheme, gctx.Server.Config.Domain) - url, _ := url.JoinPath( - baseURL, - c.Echo().Reverse(s.RouteName("connect_user"), creq.Context.Team.Id, creq.Context.ActingUser.Id)) - err = sendInstructionLink(creq, url) + baseURL := links.GetLinksDomainURL(c) + baseURL.Path = c.Echo().Reverse( + s.RouteName("connect_user"), + creq.Context.Team.Id, + creq.Context.ActingUser.Id, + ) + err = sendInstructionLink(creq, baseURL.String()) if err != nil { return err } @@ -422,11 +423,13 @@ func (s *Service) ShortCommand(c echo.Context) error { } if len(mmUsers) == 0 { // Send private msg with instrucctions - baseURL := fmt.Sprintf("%s://%s", gctx.Server.Config.Scheme, gctx.Server.Config.Domain) - url, _ := url.JoinPath( - baseURL, - c.Echo().Reverse(s.RouteName("connect_user"), creq.Context.Team.Id, creq.Context.ActingUser.Id)) - err = sendInstructionLink(creq, url) + baseURL := links.GetLinksDomainURL(c) + baseURL.Path = c.Echo().Reverse( + s.RouteName("connect_user"), + creq.Context.Team.Id, + creq.Context.ActingUser.Id, + ) + err = sendInstructionLink(creq, baseURL.String()) if err != nil { return err } @@ -563,11 +566,13 @@ func (s *Service) AddCommand(c echo.Context) error { } if len(mmUsers) == 0 { // Send private msg with instrucctions - baseURL := fmt.Sprintf("%s://%s", gctx.Server.Config.Scheme, gctx.Server.Config.Domain) - url, _ := url.JoinPath( - baseURL, - c.Echo().Reverse(s.RouteName("connect_user"), creq.Context.Team.Id, creq.Context.ActingUser.Id)) - err = sendInstructionLink(creq, url) + baseURL := links.GetLinksDomainURL(c) + baseURL.Path = c.Echo().Reverse( + s.RouteName("connect_user"), + creq.Context.Team.Id, + creq.Context.ActingUser.Id, + ) + err = sendInstructionLink(creq, baseURL.String()) if err != nil { return err } @@ -631,12 +636,13 @@ func (s *Service) ConnectCommand(c echo.Context) error { if err != nil { return err } - gctx := c.(*server.Context) - baseURL := fmt.Sprintf("%s://%s", gctx.Server.Config.Scheme, gctx.Server.Config.Domain) - url, _ := url.JoinPath( - baseURL, - c.Echo().Reverse(s.RouteName("connect_org"), slug, creq.Context.Team.Id)) - msg := fmt.Sprintf("Please click in the following link to tie a org %s", url) + baseURL := links.GetLinksDomainURL(c) + baseURL.Path = c.Echo().Reverse( + s.RouteName("connect_org"), + slug, + creq.Context.Team.Id, + ) + msg := fmt.Sprintf("Please click in the following link to tie a org %s", baseURL.String()) post := &model.Post{ChannelId: channel.Id, Message: msg} _, err = botClient.CreatePost(post) if err != nil { diff --git a/slack/helpers.go b/slack/helpers.go index 0ad19e6..de60cae 100644 --- a/slack/helpers.go +++ b/slack/helpers.go @@ -2,6 +2,7 @@ package slack import ( "fmt" + "links" "net/url" "github.com/labstack/echo/v4" @@ -10,13 +11,10 @@ import ( // GetSlackURL ... func GetSlackURL(c echo.Context, slug string) (string, error) { + redirectURL := links.GetLinksDomainURL(c) + redirectURL.Path = c.Echo().Reverse("slack:slack_connect") + 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") @@ -36,7 +34,7 @@ func GetSlackURL(c echo.Context, slug string) (string, error) { } qs := parts.Query() qs.Set("client_id", clientID) - qs.Set("redirect_uri", redirectURL) + qs.Set("redirect_uri", redirectURL.String()) qs.Set("scope", scope) qs.Set("state", slug) parts.RawQuery = qs.Encode() diff --git a/slack/routes.go b/slack/routes.go index 9ce3428..98a1ac6 100644 --- a/slack/routes.go +++ b/slack/routes.go @@ -228,13 +228,15 @@ func (s *Service) SlashCommand(c echo.Context) error { return err } case "/link-add": - baseURL := fmt.Sprintf("%s://%s", gctx.Server.Config.Scheme, gctx.Server.Config.Domain) - url, _ := url.JoinPath( - baseURL, - c.Echo().Reverse(s.RouteName("slack_user_connect"), reqValues.Get("team_id"), reqValues.Get("user_id"))) + baseURL := links.GetLinksDomainURL(c) + baseURL.Path = c.Echo().Reverse( + s.RouteName("slack_user_connect"), + reqValues.Get("team_id"), + reqValues.Get("user_id"), + ) commandResp, err = linkAdd( c.Request().Context(), - url, + baseURL.String(), reqValues.Get("team_id"), reqValues.Get("user_id"), reqValues.Get("text"), @@ -246,13 +248,15 @@ func (s *Service) SlashCommand(c echo.Context) error { return err } case "/link-add-short": - baseURL := fmt.Sprintf("%s://%s", gctx.Server.Config.Scheme, gctx.Server.Config.Domain) - url, _ := url.JoinPath( - baseURL, - c.Echo().Reverse(s.RouteName("slack_user_connect"), reqValues.Get("team_id"), reqValues.Get("user_id"))) + baseURL := links.GetLinksDomainURL(c) + baseURL.Path = c.Echo().Reverse( + s.RouteName("slack_user_connect"), + reqValues.Get("team_id"), + reqValues.Get("user_id"), + ) commandResp, err = linkAddShort( c.Request().Context(), - url, + baseURL.String(), reqValues.Get("team_id"), reqValues.Get("user_id"), reqValues.Get("text"), -- 2.45.2