Skip to content

Commit

Permalink
Obtain streaming url from Mastodon instance info
Browse files Browse the repository at this point in the history
Apparently we shouldn't make assumptions about the streaming endpoint.
See: mastodon/mastodon#23383 (comment)

Also related: #176
  • Loading branch information
alexbakker authored and mattn committed Oct 24, 2024
1 parent d74af49 commit 34bd0ff
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions streaming.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
}()
Expand Down

0 comments on commit 34bd0ff

Please sign in to comment.