feat: sync host-based public app links
This commit is contained in:
parent
10de462191
commit
8cfb6f74c1
19 changed files with 375 additions and 26 deletions
|
|
@ -24,6 +24,7 @@ type Config struct {
|
|||
Addr string
|
||||
PublicBaseURL string
|
||||
AppScheme string
|
||||
AppLinkBase string
|
||||
WebBaseURL string
|
||||
AppName string
|
||||
StickerSets StickerSetResolver
|
||||
|
|
@ -102,6 +103,7 @@ func Start(ctx context.Context, cfg Config, logger *zap.Logger) (*http.Server, e
|
|||
zap.String("addr", addr),
|
||||
zap.String("public_base_url", cfg.PublicBaseURL),
|
||||
zap.String("app_scheme", cfg.AppScheme),
|
||||
zap.String("app_link_base", cfg.AppLinkBase),
|
||||
zap.String("web_base_url", cfg.WebBaseURL))
|
||||
if err := srv.Serve(ln); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
logger.Warn("Public link Web endpoint exited", zap.Error(err))
|
||||
|
|
@ -134,8 +136,9 @@ func newHandler(cfg Config, logger *zap.Logger) (http.Handler, error) {
|
|||
if cfg.PublicBaseURL, err = links.ValidateBaseURL(cfg.PublicBaseURL); err != nil {
|
||||
return nil, fmt.Errorf("public base URL: %w", err)
|
||||
}
|
||||
if cfg.AppScheme, err = links.ValidateAppScheme(cfg.AppScheme); err != nil {
|
||||
return nil, fmt.Errorf("app scheme: %w", err)
|
||||
appLinks, err := links.NewAppLinkBuilder(cfg.AppScheme, cfg.AppLinkBase)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("app links: %w", err)
|
||||
}
|
||||
if cfg.WebBaseURL, err = links.ValidateBaseURL(cfg.WebBaseURL); err != nil {
|
||||
return nil, fmt.Errorf("Web base URL: %w", err)
|
||||
|
|
@ -155,7 +158,7 @@ func newHandler(cfg Config, logger *zap.Logger) (http.Handler, error) {
|
|||
uniqueGifts: cfg.UniqueGifts,
|
||||
giftWithdrawals: cfg.GiftWithdrawals,
|
||||
publicBaseURL: cfg.PublicBaseURL,
|
||||
appScheme: cfg.AppScheme,
|
||||
appLinks: appLinks,
|
||||
webBaseURL: cfg.WebBaseURL,
|
||||
appName: cfg.AppName,
|
||||
logger: logger,
|
||||
|
|
@ -195,7 +198,7 @@ type handler struct {
|
|||
uniqueGifts UniqueStarGiftResolver
|
||||
giftWithdrawals StarGiftWithdrawalResolver
|
||||
publicBaseURL string
|
||||
appScheme string
|
||||
appLinks links.AppLinkBuilder
|
||||
webBaseURL string
|
||||
appName string
|
||||
logger *zap.Logger
|
||||
|
|
@ -376,8 +379,8 @@ func (h *handler) usernameLink(w http.ResponseWriter, r *http.Request) {
|
|||
h.serveUsernameNotFound(w, username)
|
||||
return
|
||||
}
|
||||
app := h.appLinks.BuildUsername(peer.username, params)
|
||||
params.Set("domain", peer.username)
|
||||
app := schemeURLValues(h.appScheme, "resolve", params)
|
||||
legacy := schemeURLValues("tg", "resolve", params)
|
||||
description := peer.about
|
||||
if description == "" {
|
||||
|
|
@ -988,7 +991,7 @@ func itemNoun(set domain.StickerSet, count int) string {
|
|||
}
|
||||
|
||||
func (h *handler) appURL(kind, key, value string) string {
|
||||
return schemeURL(h.appScheme, kind, key, value)
|
||||
return h.appLinks.Build(kind, url.Values{key: []string{value}})
|
||||
}
|
||||
|
||||
func legacyTgURL(kind, key, value string) string {
|
||||
|
|
|
|||
|
|
@ -344,6 +344,7 @@ func TestHandlerUsesConfiguredClientLinksAndBrand(t *testing.T) {
|
|||
}},
|
||||
PublicBaseURL: "https://links.example.test",
|
||||
AppScheme: "example-chat",
|
||||
AppLinkBase: "owpg://tenant.example.test",
|
||||
WebBaseURL: "https://web.example.test/client/",
|
||||
AppName: "Example Chat",
|
||||
})
|
||||
|
|
@ -357,7 +358,7 @@ func TestHandlerUsesConfiguredClientLinksAndBrand(t *testing.T) {
|
|||
}
|
||||
body := rr.Body.String()
|
||||
for _, want := range []string{
|
||||
"example-chat://resolve?domain=Alice&start=hello",
|
||||
"owpg://tenant.example.test/Alice?start=hello",
|
||||
"https://web.example.test/client/#?tgaddr=",
|
||||
"Example Chat",
|
||||
"Open Example Chat to send a message to @Alice.",
|
||||
|
|
@ -373,10 +374,10 @@ func TestHandlerUsesConfiguredClientLinksAndBrand(t *testing.T) {
|
|||
path string
|
||||
want string
|
||||
}{
|
||||
{path: "/addstickers/stickers_pack", want: "example-chat://addstickers?set=stickers_pack"},
|
||||
{path: "/addemoji/emoji_pack", want: "example-chat://addemoji?set=emoji_pack"},
|
||||
{path: "/addlist/shared-folder", want: "example-chat://addlist?slug=shared-folder"},
|
||||
{path: "/nft/gift-1", want: "example-chat://nft?slug=gift-1"},
|
||||
{path: "/addstickers/stickers_pack", want: "owpg://tenant.example.test/addstickers?set=stickers_pack"},
|
||||
{path: "/addemoji/emoji_pack", want: "owpg://tenant.example.test/addemoji?set=emoji_pack"},
|
||||
{path: "/addlist/shared-folder", want: "owpg://tenant.example.test/addlist?slug=shared-folder"},
|
||||
{path: "/nft/gift-1", want: "owpg://tenant.example.test/nft?slug=gift-1"},
|
||||
} {
|
||||
rr := httptest.NewRecorder()
|
||||
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, tc.path, nil))
|
||||
|
|
@ -393,6 +394,8 @@ func TestNewHandlerRejectsInvalidClientLinkConfig(t *testing.T) {
|
|||
}{
|
||||
{name: "missing sticker resolver", cfg: Config{}},
|
||||
{name: "official scheme", cfg: Config{StickerSets: fakeResolver{}, AppScheme: "tg"}},
|
||||
{name: "official app link base", cfg: Config{StickerSets: fakeResolver{}, AppLinkBase: "tg://links.example.test"}},
|
||||
{name: "app link base path", cfg: Config{StickerSets: fakeResolver{}, AppLinkBase: "owpg://links.example.test/root"}},
|
||||
{name: "invalid Web base URL", cfg: Config{StickerSets: fakeResolver{}, WebBaseURL: "file:///tmp/web"}},
|
||||
{name: "invalid app name", cfg: Config{StickerSets: fakeResolver{}, AppName: "bad\nname"}},
|
||||
} {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue