-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra.go
223 lines (193 loc) · 5.92 KB
/
extra.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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package apiextra
import (
"github.com/thewug/fsb/pkg/storage"
"github.com/thewug/gogram/data"
"regexp"
"strconv"
"strings"
"fmt"
)
const NONEXISTENT_POST = -1
const NONEXISTENT_PARENT = -2
const BLANK_PARENT = -1
const UNCHANGED_PARENT = 0
type settings interface {
GetApiEndpoint() string
GetApiFilteredEndpoint() string
GetApiStaticPrefix() string
}
func Init(s settings) error {
var err error
apiurlmatch.fields = []int{5}
apiurlmatch.regexp, err = regexp.Compile(fmt.Sprintf(`(https?://)?(www\.)?(%s|%s)/(posts|post/show)/(\d+)`,
regexp.QuoteMeta(s.GetApiEndpoint()),
regexp.QuoteMeta(s.GetApiFilteredEndpoint())))
md5hashmatch.fields = []int{3,8}
md5hashmatch.regexp, err = regexp.Compile(fmt.Sprintf(`((^|[^\w-])md5:([0-9A-Fa-f]{32})(\W|$)|(https?://)?%s(%s|%s)/data/(\w+/)+([0-9A-Za-z]{32})\.\w+)`,
regexp.QuoteMeta(s.GetApiStaticPrefix()),
regexp.QuoteMeta(s.GetApiEndpoint()),
regexp.QuoteMeta(s.GetApiFilteredEndpoint())))
return err
}
type matcher struct {
regexp *regexp.Regexp
fields []int
}
// matches one string by one matcher.
// returns the first provided matching field that is non-empty, converted to an int.
// returns NONEXISTENT_POST if no match is found.
func (this matcher) Match(text string) int {
x, err := strconv.Atoi(this.MatchString(text))
if err == nil {
return x
}
return NONEXISTENT_POST
}
// matches one string by one matcher.
// returns the first provided matching field that is non-empty
// returns the empty string if no match is found.
func (this matcher) MatchString(text string) string {
matching := this.regexp.FindStringSubmatch(text)
for _, field := range this.fields {
if len(matching) >= field + 1 && matching[field] != "" {
return matching[field]
}
}
return ""
}
var apiurlmatch matcher
var numericmatch = matcher{
regexp.MustCompile(`(^|[^\w-])(\d+)(\W|$)`),
[]int{2},
}
var md5hashmatch matcher
// attempts to recover a post id from the specified text string.
// first, searches for a matching post url and returns its post number if present.
// second, searches for and returns a non-negative number not part of another word.
// returns NONEXISTENT_POST if no matches were found.
func GetPostIDFromText(text string) int {
found := apiurlmatch.Match(text)
if found == NONEXISTENT_POST {
found = numericmatch.Match(text)
}
if found == NONEXISTENT_POST {
md5 := md5hashmatch.MatchString(text)
if md5 != "" {
post, err := storage.PostByMD5(storage.DefaultNoTx(), md5)
if post != nil && err == nil {
found = post.Id
}
}
}
return found
}
// attempts to recover a post id from a telegram message.
// first, tries to match any URL in a url text entity.
// second, tries GetPostIDFromText on the full message plaintext.
// returns NONEXISTENT_POST if no matches were found.
func GetPostIDFromMessage(msg *data.TMessage) (int) {
for _, entity := range msg.GetEntities() {
if entity.Url != nil {
found := apiurlmatch.Match(*entity.Url)
if found != NONEXISTENT_POST { return found }
}
}
for _, entity := range msg.GetEntities() {
if entity.Url != nil {
md5 := md5hashmatch.MatchString(*entity.Url)
if md5 != "" {
post, err := storage.PostByMD5(storage.DefaultNoTx(), md5)
if post != nil && err == nil {
return post.Id
} else {
break
}
}
}
}
return GetPostIDFromText(msg.PlainText())
}
// attempts to recover a post id from a string, for use as a parent post to another post.
// relies on GetPostIDFromText for this.
// also accepts the special value "none" to reset the parent's post to nothing, or "original" to leave the parent unchanged.
// returns a positive integer post id, BLANK_PARENT to indicate "none", or UNCHANGED_PARENT to indicate "original".
// if no post id could be discovered, returns NONEXISTENT_PARENT.
func GetParentPostFromText(text string) int {
if text == "none" {
return BLANK_PARENT
} else if text == "original" {
return UNCHANGED_PARENT
} else {
found := GetPostIDFromText(text)
if found > 0 {
return found
}
}
return NONEXISTENT_PARENT
}
type Ratings struct {
Safe, Questionable, Explicit bool
}
func (this Ratings) And(other Ratings) Ratings {
this.Safe = this.Safe && other.Safe
this.Questionable = this.Questionable && other.Questionable
this.Explicit = this.Explicit && other.Explicit
return this
}
func (this Ratings) RatingTag() string {
if this.Safe {
if this.Questionable {
if this.Explicit {
return "" // SQE
} else {
return "-rating:e" // SQ
}
} else {
if this.Explicit {
return "-rating:q" // SE
} else {
return "rating:s" // S
}
}
} else {
if this.Questionable {
if this.Explicit {
return "-rating:s" // QE
} else {
return "rating:q" // Q
}
} else {
if this.Explicit {
return "rating:e" // E
} else {
return "id:<0" // none
}
}
}
}
var ws *regexp.Regexp = regexp.MustCompile(`\s+`)
func RatingsFromString(tags string) Ratings {
var r Ratings = Ratings{true, true, true}
for _, tag := range ws.Split(tags, -1) {
if strings.HasPrefix(tag, "rating:") {
switch {
case strings.HasPrefix(tag[7:], "s"):
r.Safe, r.Questionable, r.Explicit = true, false, false
case strings.HasPrefix(tag[7:], "q"):
r.Safe, r.Questionable, r.Explicit = false, true, false
case strings.HasPrefix(tag[7:], "e"):
r.Safe, r.Questionable, r.Explicit = false, false, true
}
} else if strings.HasPrefix(tag, "-rating:") {
switch {
case strings.HasPrefix(tag[8:], "s"):
r.Safe, r.Questionable, r.Explicit = false, true, true
case strings.HasPrefix(tag[8:], "q"):
r.Safe, r.Questionable, r.Explicit = true, false, true
case strings.HasPrefix(tag[8:], "e"):
r.Safe, r.Questionable, r.Explicit = true, true, false
}
}
}
return r
}