diff --git a/CHANGELOG.md b/CHANGELOG.md index 3fdc7e22..6d1e4e2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased - Breaking: Remove the `/twitchemotes/` endpoints. See [issue 332](https://github.com/Chatterino/api/issues/332) for more information. (#465) +- Minor: Use Twitter OG tags if no Twitter credentials are configured. (#522) - Fix: We do some more YouTube video ID parsing to ensure broken links (such as `youtube.com/watch?v=foobar?feature=share`) still return the actual video ID, since this is how the browser operates. (#488) - Dev: Document the `log-development` setting. (#491) - Dev: Document the `log-level` setting. (#490) diff --git a/internal/resolvers/default/link_loader.go b/internal/resolvers/default/link_loader.go index 54ea5f3e..54148438 100644 --- a/internal/resolvers/default/link_loader.go +++ b/internal/resolvers/default/link_loader.go @@ -13,7 +13,9 @@ import ( "time" "github.com/Chatterino/api/internal/logger" + "github.com/Chatterino/api/internal/resolvers/twitter" "github.com/Chatterino/api/internal/staticresponse" + "github.com/Chatterino/api/internal/version" "github.com/Chatterino/api/pkg/cache" "github.com/Chatterino/api/pkg/resolver" "github.com/Chatterino/api/pkg/thumbnail" @@ -48,7 +50,13 @@ func (l *LinkLoader) Load(ctx context.Context, urlString string, r *http.Request return resolver.ReturnInvalidURL() } - resp, err := resolver.RequestGET(ctx, requestUrl.String()) + extraHeaders := make(map[string]string) + ctx, isTwitterRequest := twitter.Check(ctx, requestUrl) + if isTwitterRequest { + extraHeaders["User-Agent"] = fmt.Sprintf("chatterino-api-cache/%s link-resolver (bot)", version.Version) + } + + resp, err := resolver.RequestGETWithHeaders(requestUrl.String(), extraHeaders) if err != nil { if strings.HasSuffix(err.Error(), "no such host") { return staticresponse.SNoLinkInfoFound. diff --git a/internal/resolvers/twitter/resolver.go b/internal/resolvers/twitter/resolver.go index 56465a39..050bf97c 100644 --- a/internal/resolvers/twitter/resolver.go +++ b/internal/resolvers/twitter/resolver.go @@ -18,7 +18,7 @@ type TwitterResolver struct { userCache cache.Cache } -func (r *TwitterResolver) Check(ctx context.Context, url *url.URL) (context.Context, bool) { +func Check(ctx context.Context, url *url.URL) (context.Context, bool) { if !utils.IsSubdomainOf(url, "twitter.com") { return ctx, false } @@ -45,6 +45,10 @@ func (r *TwitterResolver) Check(ctx context.Context, url *url.URL) (context.Cont return ctx, isTwitterUser } +func (r *TwitterResolver) Check(ctx context.Context, url *url.URL) (context.Context, bool) { + return Check(ctx, url) +} + func (r *TwitterResolver) Run(ctx context.Context, url *url.URL, req *http.Request) (*cache.Response, error) { tweetMatch := tweetRegexp.FindStringSubmatch(url.Path) if len(tweetMatch) == 2 && len(tweetMatch[1]) > 0 {