-
Notifications
You must be signed in to change notification settings - Fork 37
/
logrequest.go
190 lines (160 loc) · 5 KB
/
logrequest.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
package api
import (
"errors"
"fmt"
"net/http"
"regexp"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
)
type logRequest struct {
channel string
user string
channelid string
userid string
time logTime
reverse bool
responseType string
redirectPath string
isUserRequest bool
isChannelRequest bool
}
// userRandomMessageRequest /channel/pajlada/user/gempir/random
type logTime struct {
from string
to string
year string
month string
day string
random bool
}
var (
pathRegex = regexp.MustCompile(`\/(channel|channelid)\/(\w+)(?:\/(user|userid)\/(\w+))?(?:(?:\/(\d{4})\/(\d{1,2})(?:\/(\d{1,2}))?)|(?:\/(random)))?`)
)
func (s *Server) newLogRequestFromURL(r *http.Request) (logRequest, error) {
path := r.URL.EscapedPath()
if path != strings.ToLower(path) {
return logRequest{redirectPath: fmt.Sprintf("%s?%s", strings.ToLower(path), r.URL.Query().Encode())}, nil
}
if !strings.HasPrefix(path, "/channel") && !strings.HasPrefix(path, "/channelid") {
return logRequest{}, errors.New("route not found")
}
url := strings.TrimRight(path, "/")
matches := pathRegex.FindAllStringSubmatch(url, -1)
if len(matches) == 0 || len(matches[0]) < 5 {
return logRequest{}, errors.New("route not found")
}
request := logRequest{
time: logTime{},
}
params := []string{}
for _, match := range matches[0] {
if match != "" {
params = append(params, match)
}
}
request.isUserRequest = len(params) > 4 && (params[3] == "user" || params[3] == "userid")
request.isChannelRequest = len(params) < 4 || (len(params) >= 4 && params[3] != "user" && params[3] != "userid")
if params[1] == "channel" {
request.channel = params[2]
}
if params[1] == "channelid" {
request.channelid = params[2]
}
if request.isUserRequest && params[3] == "user" {
request.user = params[4]
}
if request.isUserRequest && params[3] == "userid" {
request.userid = params[4]
}
var err error
request, err = s.fillIds(request)
if err != nil {
log.Error(err)
return logRequest{}, nil
}
if request.isUserRequest && len(params) == 7 {
request.time.year = params[5]
request.time.month = params[6]
} else if request.isUserRequest && len(params) == 8 {
return logRequest{}, errors.New("route not found")
} else if request.isChannelRequest && len(params) == 6 {
request.time.year = params[3]
request.time.month = params[4]
request.time.day = params[5]
} else if request.isUserRequest && len(params) == 6 && params[5] == "random" {
request.time.random = true
} else if request.isChannelRequest && len(params) == 4 && params[3] == "random" {
request.time.random = true
} else {
if request.isChannelRequest {
request.time.year = fmt.Sprintf("%d", time.Now().Year())
request.time.month = fmt.Sprintf("%d", time.Now().Month())
} else {
year, month, err := s.fileLogger.GetLastLogYearAndMonthForUser(request.channelid, request.userid)
if err == nil {
request.time.year = fmt.Sprintf("%d", year)
request.time.month = fmt.Sprintf("%d", month)
} else {
request.time.year = fmt.Sprintf("%d", time.Now().Year())
request.time.month = fmt.Sprintf("%d", time.Now().Month())
}
}
timePath := request.time.year + "/" + request.time.month
if request.isChannelRequest {
request.time.day = fmt.Sprintf("%d", time.Now().Day())
timePath += "/" + request.time.day
}
query := r.URL.Query()
encodedQuery := ""
if query.Encode() != "" {
encodedQuery = "?" + query.Encode()
}
return logRequest{redirectPath: fmt.Sprintf("%s/%s%s", params[0], timePath, encodedQuery)}, nil
}
if r.URL.Query().Get("from") != "" || r.URL.Query().Get("to") != "" {
request.time.from = r.URL.Query().Get("from")
if request.time.from == "" {
request.time.from = strconv.FormatInt(time.Now().Unix(), 10)
}
request.time.to = r.URL.Query().Get("to")
if request.time.to == "" {
request.time.to = strconv.FormatInt(time.Now().Unix(), 10)
}
}
if _, ok := r.URL.Query()["reverse"]; ok {
request.reverse = true
} else {
request.reverse = false
}
if _, ok := r.URL.Query()["json"]; ok || r.URL.Query().Get("type") == "json" || r.Header.Get("Content-Type") == "application/json" {
request.responseType = responseTypeJSON
} else if _, ok := r.URL.Query()["raw"]; ok || r.URL.Query().Get("type") == "raw" {
request.responseType = responseTypeRaw
} else {
request.responseType = responseTypeText
}
return request, nil
}
func (s *Server) fillIds(request logRequest) (logRequest, error) {
usernames := []string{}
if request.channelid == "" && request.channel != "" {
usernames = append(usernames, request.channel)
}
if request.userid == "" && request.user != "" {
usernames = append(usernames, request.user)
}
ids, err := s.helixClient.GetUsersByUsernames(usernames)
if err != nil {
return request, err
}
if request.channelid == "" {
request.channelid = ids[strings.ToLower(request.channel)].ID
}
if request.userid == "" {
request.userid = ids[strings.ToLower(request.user)].ID
}
return request, nil
}