mattn / go-mastodon

mastodon client for golang
MIT License
600 stars 85 forks source link

Enable anonymous streaming #142

Closed marians closed 2 years ago

marians commented 2 years ago

The purpose of this PR is to enable anonymous (unauthenticated) streaming.

This curl command demonstrates that streaming works (at least on some instances) without a need for authentication.

curl https://mastodon.social/api/v1/streaming/public

With v0.0.4, we assume that an AccessToken is set and use it to set an Authorization header in the request. With an empty AccessToken, this leads to an authorization error.

Omitting the entire header in that case fixes the problem. The following programm illustrates it.

package main

import (
    "context"
    "log"

    mastodon "github.com/mattn/go-mastodon"
)

const serverURL = "https://mastodon.social"

func main() {
    ctx := context.Background()

    client := mastodon.NewClient(&mastodon.Config{
        Server: serverURL,
    })

    q, err := client.StreamingPublic(ctx, false)
    if err != nil {
        log.Fatalf("StreamingPublic failed: %v", err)
    }

    for e := range q {
        switch event := e.(type) {
        case *mastodon.UpdateEvent:
            log.Printf("UpdateEvent: account %s", event.Status.Account.URL)
        case *mastodon.NotificationEvent:
            log.Printf("NotificationEvent: type %#v", event.Notification.Type)
        case *mastodon.ErrorEvent:
            log.Printf("ErrorEvent: %#v", event)
        }
    }
}
marians commented 2 years ago

@mattn Any opinion on this one?

mattn commented 2 years ago

Sorry my delay. LGTM

mattn commented 2 years ago

Thank you