2018-12-03 22:58:45 +01:00
|
|
|
package api
|
|
|
|
|
2020-02-25 21:14:05 +01:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/gempir/go-twitch-irc/v2"
|
|
|
|
)
|
|
|
|
|
2020-02-25 21:44:25 +01:00
|
|
|
func (s *Server) getChannelLogs(request logRequest) (*chatLog, error) {
|
2020-02-25 21:14:05 +01:00
|
|
|
yearStr := request.time.year
|
|
|
|
monthStr := request.time.month
|
|
|
|
dayStr := request.time.day
|
|
|
|
|
|
|
|
year, err := strconv.Atoi(yearStr)
|
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, errors.New("invalid year")
|
|
|
|
}
|
|
|
|
month, err := strconv.Atoi(monthStr)
|
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, errors.New("invalid month")
|
|
|
|
}
|
|
|
|
day, err := strconv.Atoi(dayStr)
|
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, errors.New("invalid day")
|
|
|
|
}
|
|
|
|
|
|
|
|
logMessages, err := s.fileLogger.ReadLogForChannel(request.channelid, year, month, day)
|
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.reverse {
|
|
|
|
reverseSlice(logMessages)
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:22:26 +01:00
|
|
|
logResult := createLogResult()
|
2020-02-25 21:14:05 +01:00
|
|
|
|
|
|
|
for _, rawMessage := range logMessages {
|
2020-11-19 21:22:26 +01:00
|
|
|
logResult.Messages = append(logResult.Messages, createChatMessage(twitch.ParseMessage(rawMessage)))
|
2020-02-25 21:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &logResult, nil
|
|
|
|
}
|
|
|
|
|
2020-02-25 21:44:25 +01:00
|
|
|
func (s *Server) getChannelLogsRange(request logRequest) (*chatLog, error) {
|
2020-02-25 21:14:05 +01:00
|
|
|
fromTime, toTime, err := parseFromTo(request.time.from, request.time.to, userHourLimit)
|
|
|
|
if err != nil {
|
|
|
|
return &chatLog{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var logMessages []string
|
|
|
|
|
|
|
|
logMessages, _ = s.fileLogger.ReadLogForChannel(request.channelid, fromTime.Year(), int(fromTime.Month()), fromTime.Day())
|
|
|
|
|
|
|
|
if fromTime.Month() != toTime.Month() {
|
|
|
|
additionalMessages, _ := s.fileLogger.ReadLogForChannel(request.channelid, toTime.Year(), int(toTime.Month()), toTime.Day())
|
|
|
|
|
|
|
|
logMessages = append(logMessages, additionalMessages...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if request.reverse {
|
|
|
|
reverseSlice(logMessages)
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:22:26 +01:00
|
|
|
logResult := createLogResult()
|
2020-02-25 21:14:05 +01:00
|
|
|
|
|
|
|
for _, rawMessage := range logMessages {
|
|
|
|
parsedMessage := twitch.ParseMessage(rawMessage)
|
|
|
|
|
2020-11-19 21:22:26 +01:00
|
|
|
switch message := parsedMessage.(type) {
|
2020-02-25 21:14:05 +01:00
|
|
|
case *twitch.PrivateMessage:
|
|
|
|
if message.Time.Unix() < fromTime.Unix() || message.Time.Unix() > toTime.Unix() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case *twitch.ClearChatMessage:
|
|
|
|
if message.Time.Unix() < fromTime.Unix() || message.Time.Unix() > toTime.Unix() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
case *twitch.UserNoticeMessage:
|
2020-11-19 21:22:26 +01:00
|
|
|
if message.Time.Unix() < fromTime.Unix() || message.Time.Unix() > toTime.Unix() {
|
|
|
|
continue
|
2020-02-25 21:14:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-19 21:22:26 +01:00
|
|
|
logResult.Messages = append(logResult.Messages, createChatMessage(parsedMessage))
|
2020-02-25 21:14:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return &logResult, nil
|
|
|
|
}
|