-
Notifications
You must be signed in to change notification settings - Fork 19
/
check.go
48 lines (38 loc) · 1.3 KB
/
check.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package twitch
import (
"net/url"
"regexp"
"github.com/Chatterino/api/pkg/resolver"
)
var (
clipSlugRegex = regexp.MustCompile(`^\/(\w{2,25}\/clip\/)?(clip\/)?([a-zA-Z0-9]+(?:-[-\w]{16})?)$`)
)
func check(url *url.URL) bool {
// Regardless of domain path needs to match anyway, so we do it here to avoid duplication
matches := clipSlugRegex.FindStringSubmatch(url.Path)
match, domain := resolver.MatchesHosts(url, domains)
if !match {
return false
}
if len(matches) != 4 {
return false
}
if domain == "m.twitch.tv" {
// Find clips that look like https://m.twitch.tv/clip/SlugHere
// matches[2] contains "clip/" - both this and matches[1] cannot be non-empty at the same time
if matches[2] == "clip/" {
return matches[1] == ""
}
// Find clips that look like https://m.twitch.tv/StreamerName/clip/SlugHere
// matches[1] contains "StreamerName/clip/" - we need it in this check
return matches[1] != ""
}
// Find clips that look like https://clips.twitch.tv/SlugHere
if domain == "clips.twitch.tv" {
// matches[1] contains "StreamerName/clip/" - we don't want it in this check though
return matches[1] == ""
}
// Find clips that look like https://twitch.tv/StreamerName/clip/SlugHere
// matches[1] contains "StreamerName/clip/" - we need it in this check
return matches[1] != ""
}