Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(twitter): respect routes to non-user pages #220

Merged
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 @@ -2,6 +2,7 @@

## Unreleased

- Twitter: Blacklist special pages from being resolved as user pages. (#220)
- Updated Facebook & Instagram endpoints to oembed v10. (#201)

## 1.2.1
Expand Down
15 changes: 14 additions & 1 deletion internal/resolvers/twitter/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ func check(url *url.URL) bool {
return true
}

isTwitterUser := twitterUserRegexp.MatchString(url.String())
/* Simply matching the regex isn't enough for user pages. Pages like
twitter.com/explore and twitter.com/settings match the expression but do not refer
to a valid user page. We therefore need to check the captured name against a list
of known non-user pages.
*/
m := twitterUserRegexp.FindAllStringSubmatch(url.String(), -1)
if len(m) == 0 || len(m[0]) == 0 {
return false
}
userName := m[0][1]

_, notAUser := nonUserPages[userName]
isTwitterUser := !notAUser

return isTwitterUser
}
45 changes: 45 additions & 0 deletions internal/resolvers/twitter/check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package twitter

import (
"net/url"
"testing"
)

func TestShouldMatch(t *testing.T) {
tests := []string{
"https://twitter.com/pajlada",
"http://www.twitter.com/forsen",
}

for _, test := range tests {
u, err := url.Parse(test)
if err != nil {
t.Fatalf("invalid url %s", test)
}

if !check(u) {
t.Fatalf("url %s didn't match twitter check while it should have", test)
}
}
}

func TestShouldNotMatch(t *testing.T) {
tests := []string{
"https://google.com",
"https://twitter.com/compose",
"https://twitter.com/logout",
"https://twitter.com/logout",
"https://nontwitter.com/forsen",
}

for _, test := range tests {
u, err := url.Parse(test)
if err != nil {
t.Fatalf("invalid url %s", test)
}

if check(u) {
t.Fatalf("url %s matched twitter check while it shouldn't have", test)
}
}
}
20 changes: 20 additions & 0 deletions internal/resolvers/twitter/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/Chatterino/api/pkg/cache"
"github.com/Chatterino/api/pkg/config"
"github.com/Chatterino/api/pkg/resolver"
"github.com/Chatterino/api/pkg/utils"
)

const (
Expand All @@ -35,6 +36,25 @@ var (
tweetRegexp = regexp.MustCompile(`(?i)\/.*\/status(?:es)?\/([^\/\?]+)`)
twitterUserRegexp = regexp.MustCompile(`(?i)twitter\.com\/([^\/\?\s]+)(\/?$|(\?).*)`)

/* These routes refer to non-user pages. If the capture group in twitterUserRegexp
matches any of these names, we must not resolve it as a Twitter user link.

The pages are listed alphabetically. They were sourced by simply looking around the
Twitter web page. AFAIK, there is no resource describing these "special" routes.
*/
nonUserPages = utils.SetFromSlice([]interface{}{
"compose",
"explore",
"home",
"logout",
"messages",
"notifications",
"search",
"settings",
"tos",
"privacy",
})

tweetTooltipTemplate = template.Must(template.New("tweetTooltip").Parse(tweetTooltip))

twitterUserTooltipTemplate = template.Must(template.New("twitterUserTooltip").Parse(twitterUserTooltip))
Expand Down
11 changes: 11 additions & 0 deletions pkg/utils/set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

// SetFromSlice takes a slice and returns a set (represented as a map of struct{}s)
func SetFromSlice(slice []interface{}) map[interface{}]struct{} {
set := make(map[interface{}]struct{})
for _, v := range slice {
set[v] = struct{}{}
}

return set
}