Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion internal/resolvers/default/link_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand Down
6 changes: 5 additions & 1 deletion internal/resolvers/twitter/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down