mattn / go-mastodon

mastodon client for golang
MIT License
604 stars 88 forks source link

streaming endpoint URL not at the main domain #176

Closed mehrvarz closed 1 year ago

mehrvarz commented 1 year ago

The streaming endpoint URL may not be located at the main domain. For instance mastodon.social has it's streaming endpoint at streaming.mastodon.social.

streaming.go currently expects the endpoint to be at the main domain. The actual URL should probably be fetched in StreamingUser() and handed over to streaming() in full. Both methods would need to be touched. Do you want me to apply the change?

mehrvarz commented 1 year ago

This solves the problem for me.

diff --git a/streaming.go b/streaming.go
index 2439d67..05c7d6a 100644
--- a/streaming.go
+++ b/streaming.go
@@ -102,6 +102,22 @@ func (c *Client) streaming(ctx context.Context, p string, params url.Values) (ch
        if err != nil {
                return nil, err
        }
+
+       instance,err := c.GetInstance(ctx)
+       if err!=nil {
+               // no instance: ignore
+       } else {
+               streaming_api := instance.URLs["streaming_api"]
+               if streaming_api!="" {
+                       u2, err := url.Parse(streaming_api)
+                       if err != nil {
+                               // streaming_api parse error: ignore
+                       } else if u2.Host!="" {
+                               u.Host = u2.Host
+                       }
+               }
+       }
+
        u.Path = path.Join(u.Path, "/api/v1/streaming", p)
        u.RawQuery = params.Encode()
mehrvarz commented 1 year ago

Update: https://github.com/mastodon/mastodon/issues/23383#issuecomment-1454842966