From 92af8a1078cff988e396ae8f7c7c676ca955574a Mon Sep 17 00:00:00 2001 From: Alexander Bakker Date: Sat, 11 Mar 2023 12:18:00 +0100 Subject: [PATCH] Obtain streaming url from Mastodon instance info Apparently we shouldn't make assumptions about the streaming endpoint. See: https://github.com/mastodon/mastodon/issues/23383#issuecomment-1455054265 Also related: #176 --- streaming.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/streaming.go b/streaming.go index b109f6c..f403feb 100644 --- a/streaming.go +++ b/streaming.go @@ -118,11 +118,11 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch u.Path = path.Join(u.Path, "/api/v1/streaming", p) u.RawQuery = params.Encode() - req, err := http.NewRequest(http.MethodGet, u.String(), nil) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) if err != nil { return nil, err } - req = req.WithContext(ctx) + req.URL = u if c.Config.AccessToken != "" { req.Header.Set("Authorization", "Bearer "+c.Config.AccessToken) @@ -138,6 +138,15 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch default: } + if i, err := c.GetInstance(ctx); err == nil { + if su, ok := i.URLs["streaming_api"]; ok { + if u2, err := url.Parse(su); err == nil && u2.Host != "" { + u.Host = u2.Host + req.Host = u.Host + } + } + } + c.doStreaming(req, q) } }()