~netlandish/links

9295c972c49409e92c2a6a083012b908e26dddbf — Peter Sanchez 9 months ago e67ee5d
Moving to helper functions to get links-service-domain when needed
M api/graph/schema.resolvers.go => api/graph/schema.resolvers.go +9 -13
@@ 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{

M billing/routes.go => billing/routes.go +7 -24
@@ 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)),

M core/import.go => core/import.go +1 -5
@@ 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) {

M core/routes.go => core/routes.go +3 -3
@@ 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,
	}

M helpers.go => helpers.go +20 -0
@@ 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,
	}
}

M list/routes.go => list/routes.go +1 -8
@@ 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 {

M mattermost/routes.go => mattermost/routes.go +28 -22
@@ 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 {

M slack/helpers.go => slack/helpers.go +5 -7
@@ 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()

M slack/routes.go => slack/routes.go +14 -10
@@ 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"),