-
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtwitter.go
More file actions
75 lines (61 loc) · 1.56 KB
/
Copy pathtwitter.go
File metadata and controls
75 lines (61 loc) · 1.56 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package link
import (
"net/url"
"strings"
"github.com/pkg/errors"
)
type twitter struct{}
// Twitter service.
var Twitter = &twitter{}
func (s *twitter) ID() string {
return "twitter"
}
func (s *twitter) NormalizeURLString(name string, urs string) (string, error) {
return basicURLString(strings.ToLower(urs))
}
func (s *twitter) ValidateURLString(name string, urs string) (string, error) {
u, err := url.Parse(urs)
if err != nil {
return "", err
}
if u.Scheme != "https" {
return "", errors.Errorf("invalid scheme for url %s", u)
}
switch u.Host {
case "twitter.com", "mobile.twitter.com":
// OK
default:
return "", errors.Errorf("invalid host for url %s", u)
}
path := u.Path
path = strings.TrimPrefix(path, "/")
paths := strings.Split(path, "/")
if len(paths) != 3 {
return "", errors.Errorf("path invalid %s for url %s", paths, u)
}
if paths[0] != name {
return "", errors.Errorf("path invalid (name mismatch) for url %s", u)
}
// Use mobile twitter url.
return "https://mobile.twitter.com/" + path, nil
}
func (s *twitter) NormalizeName(name string) string {
name = strings.ToLower(name)
if len(name) > 0 && name[0] == '@' {
name = name[1:]
}
return name
}
func (s *twitter) ValidateName(name string) error {
ok := isAlphaNumericWithUnderscore(name)
if !ok {
return errors.Errorf("name has an invalid character")
}
if len(name) > 15 {
return errors.Errorf("twitter name is too long, it must be less than 16 characters")
}
return nil
}
func (s *twitter) CheckContent(name string, b []byte) ([]byte, error) {
return b, nil
}