2017-03-08 21:38:01 +01:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-09-13 21:12:11 +02:00
|
|
|
|
2018-12-02 14:53:01 +01:00
|
|
|
"github.com/gempir/go-twitch-irc"
|
2017-09-13 21:12:11 +02:00
|
|
|
"github.com/labstack/echo"
|
2018-12-02 19:23:54 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2017-03-08 21:38:01 +01:00
|
|
|
)
|
|
|
|
|
2017-04-26 19:08:01 +02:00
|
|
|
type RandomQuoteJSON struct {
|
2018-12-03 21:52:15 +01:00
|
|
|
Channel string `json:"channel"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
DisplayName string `json:"displayName"`
|
|
|
|
Message string `json:"message"`
|
|
|
|
Timestamp timestamp `json:"timestamp"`
|
2017-03-08 21:38:01 +01:00
|
|
|
}
|
|
|
|
|
2019-01-02 21:36:03 +01:00
|
|
|
func (s *Server) getLastUserLogs(c echo.Context) error {
|
2018-12-02 14:53:01 +01:00
|
|
|
channelID := c.Param("channelid")
|
|
|
|
userID := c.Param("userid")
|
|
|
|
|
2019-01-02 21:36:03 +01:00
|
|
|
year, month, err := s.fileLogger.GetLastLogYearAndMonthForUser(channelID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusNotFound, errorResponse{"No logs found"})
|
|
|
|
}
|
2017-03-08 21:38:01 +01:00
|
|
|
|
2018-12-02 14:53:01 +01:00
|
|
|
redirectURL := fmt.Sprintf("/channelid/%s/userid/%s/%d/%d", channelID, userID, year, month)
|
2018-12-08 16:47:32 +01:00
|
|
|
if len(c.QueryString()) > 0 {
|
|
|
|
redirectURL += "?" + c.QueryString()
|
|
|
|
}
|
2017-03-08 21:38:01 +01:00
|
|
|
return c.Redirect(303, redirectURL)
|
|
|
|
}
|
|
|
|
|
2019-01-02 21:36:03 +01:00
|
|
|
// getLastUserLogsByName godoc
|
|
|
|
// @Summary Redirect to last logs of user
|
|
|
|
// @tags user
|
|
|
|
// @Produce json
|
|
|
|
// @Produce plain
|
|
|
|
// @Param channel path string true "channelname"
|
|
|
|
// @Param username path string true "username"
|
|
|
|
// @Param json query any false "response as json"
|
|
|
|
// @Param type query string false "define response type only json supported currently, rest defaults to plain"
|
|
|
|
// @Success 303
|
|
|
|
// @Router /channel/{channel}/user/{username} [get]
|
|
|
|
func (s *Server) getLastUserLogsByName(c echo.Context) error {
|
2019-01-08 19:37:01 +01:00
|
|
|
channel := strings.ToLower(c.Param("channel"))
|
|
|
|
username := strings.ToLower(c.Param("username"))
|
2018-12-03 21:24:35 +01:00
|
|
|
|
2019-01-02 21:36:03 +01:00
|
|
|
userMap, err := s.helixClient.GetUsersByUsernames([]string{channel, username})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return c.JSON(http.StatusInternalServerError, errorResponse{"Failure fetching userIDs"})
|
|
|
|
}
|
|
|
|
var year int
|
|
|
|
var month int
|
|
|
|
year, month, err = s.fileLogger.GetLastLogYearAndMonthForUser(userMap[channel].ID, userMap[username].ID)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusNotFound, errorResponse{"No logs found"})
|
|
|
|
}
|
2018-12-03 21:24:35 +01:00
|
|
|
|
|
|
|
redirectURL := fmt.Sprintf("/channel/%s/user/%s/%d/%d", channel, username, year, month)
|
2018-12-08 16:47:32 +01:00
|
|
|
if len(c.QueryString()) > 0 {
|
|
|
|
redirectURL += "?" + c.QueryString()
|
|
|
|
}
|
2018-12-03 21:24:35 +01:00
|
|
|
return c.Redirect(303, redirectURL)
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:58:45 +01:00
|
|
|
func (s *Server) getUserLogsRangeByName(c echo.Context) error {
|
2018-12-03 21:59:48 +01:00
|
|
|
channel := strings.ToLower(c.Param("channel"))
|
2018-12-03 22:58:45 +01:00
|
|
|
username := strings.ToLower(c.Param("username"))
|
2018-12-03 21:59:48 +01:00
|
|
|
|
2018-12-03 22:58:45 +01:00
|
|
|
userMap, err := s.helixClient.GetUsersByUsernames([]string{channel, username})
|
2018-12-03 21:59:48 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2018-12-22 09:20:34 +01:00
|
|
|
return c.JSON(http.StatusInternalServerError, errorResponse{"Failure fetching userIDs"})
|
2018-12-03 21:59:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
names := c.ParamNames()
|
|
|
|
names = append(names, "channelid")
|
2018-12-03 22:58:45 +01:00
|
|
|
names = append(names, "userid")
|
2018-12-03 21:59:48 +01:00
|
|
|
|
|
|
|
values := c.ParamValues()
|
|
|
|
values = append(values, userMap[channel].ID)
|
2018-12-03 22:58:45 +01:00
|
|
|
values = append(values, userMap[username].ID)
|
2018-12-03 21:59:48 +01:00
|
|
|
|
|
|
|
c.SetParamNames(names...)
|
|
|
|
c.SetParamValues(values...)
|
|
|
|
|
2018-12-03 22:58:45 +01:00
|
|
|
return s.getUserLogsRange(c)
|
2018-12-03 21:59:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Server) getUserLogsByName(c echo.Context) error {
|
|
|
|
channel := strings.ToLower(c.Param("channel"))
|
|
|
|
username := strings.ToLower(c.Param("username"))
|
|
|
|
|
|
|
|
userMap, err := s.helixClient.GetUsersByUsernames([]string{channel, username})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2018-12-22 09:20:34 +01:00
|
|
|
return c.JSON(http.StatusInternalServerError, errorResponse{"Failure fetching userIDs"})
|
2018-12-03 21:59:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
names := c.ParamNames()
|
|
|
|
names = append(names, "channelid")
|
|
|
|
names = append(names, "userid")
|
|
|
|
|
|
|
|
values := c.ParamValues()
|
|
|
|
values = append(values, userMap[channel].ID)
|
|
|
|
values = append(values, userMap[username].ID)
|
|
|
|
|
|
|
|
c.SetParamNames(names...)
|
|
|
|
c.SetParamValues(values...)
|
|
|
|
|
|
|
|
return s.getUserLogs(c)
|
|
|
|
}
|
|
|
|
|
2018-12-22 09:20:34 +01:00
|
|
|
// getRandomQuoteByName godoc
|
|
|
|
// @Summary Get a random chat message from a user
|
|
|
|
// @tags user
|
|
|
|
// @Produce json
|
|
|
|
// @Produce plain
|
|
|
|
// @Param channel path string true "channelname"
|
|
|
|
// @Param username path string true "username"
|
2018-12-23 22:26:24 +01:00
|
|
|
// @Param json query any false "response as json"
|
|
|
|
// @Param type query string false "define response type only json supported currently, rest defaults to plain"
|
2018-12-22 09:20:34 +01:00
|
|
|
// @Success 200 {object} api.RandomQuoteJSON json
|
|
|
|
// @Router /channel/{channel}/user/{username}/random [get]
|
2018-12-03 21:59:48 +01:00
|
|
|
func (s *Server) getRandomQuoteByName(c echo.Context) error {
|
|
|
|
channel := strings.ToLower(c.Param("channel"))
|
|
|
|
username := strings.ToLower(c.Param("username"))
|
|
|
|
|
|
|
|
userMap, err := s.helixClient.GetUsersByUsernames([]string{channel, username})
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2018-12-22 09:20:34 +01:00
|
|
|
return c.JSON(http.StatusInternalServerError, errorResponse{"Failure fetching userIDs"})
|
2018-12-03 21:59:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
names := c.ParamNames()
|
|
|
|
names = append(names, "channelid")
|
|
|
|
names = append(names, "userid")
|
|
|
|
|
|
|
|
values := c.ParamValues()
|
|
|
|
values = append(values, userMap[channel].ID)
|
|
|
|
values = append(values, userMap[username].ID)
|
|
|
|
|
|
|
|
c.SetParamNames(names...)
|
|
|
|
c.SetParamValues(values...)
|
|
|
|
|
|
|
|
return s.getRandomQuote(c)
|
|
|
|
}
|
|
|
|
|
2017-03-08 21:38:01 +01:00
|
|
|
func (s *Server) getRandomQuote(c echo.Context) error {
|
2018-12-03 21:52:15 +01:00
|
|
|
channelID := c.Param("channelid")
|
2018-12-08 16:47:32 +01:00
|
|
|
userID := c.Param("userid")
|
2017-03-08 21:38:01 +01:00
|
|
|
|
2018-12-08 16:47:32 +01:00
|
|
|
rawMessage, err := s.fileLogger.ReadRandomMessageForUser(channelID, userID)
|
|
|
|
if err != nil {
|
|
|
|
return c.JSON(http.StatusInternalServerError, err.Error())
|
2017-03-08 21:38:01 +01:00
|
|
|
}
|
2018-12-08 16:47:32 +01:00
|
|
|
channel, user, message := twitch.ParseMessage(rawMessage)
|
2017-04-26 19:08:01 +02:00
|
|
|
|
2018-12-03 21:52:15 +01:00
|
|
|
if shouldRespondWithJson(c) {
|
2017-04-26 19:08:01 +02:00
|
|
|
|
|
|
|
randomQ := RandomQuoteJSON{
|
2018-12-03 21:52:15 +01:00
|
|
|
Channel: channel,
|
|
|
|
Username: user.Username,
|
|
|
|
DisplayName: user.DisplayName,
|
|
|
|
Message: message.Text,
|
|
|
|
Timestamp: timestamp{message.Time},
|
2017-04-26 19:08:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, randomQ)
|
|
|
|
}
|
|
|
|
|
2018-12-08 16:47:32 +01:00
|
|
|
return c.String(http.StatusOK, message.Text)
|
2017-03-11 21:51:08 +01:00
|
|
|
}
|
2018-12-02 14:53:01 +01:00
|
|
|
|
|
|
|
func (s *Server) getUserLogs(c echo.Context) error {
|
|
|
|
channelID := c.Param("channelid")
|
|
|
|
userID := c.Param("userid")
|
|
|
|
|
|
|
|
yearStr := c.Param("year")
|
|
|
|
monthStr := c.Param("month")
|
|
|
|
|
|
|
|
year, err := strconv.Atoi(yearStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2018-12-22 09:20:34 +01:00
|
|
|
return c.JSON(http.StatusInternalServerError, errorResponse{"Invalid year"})
|
2018-12-02 14:53:01 +01:00
|
|
|
}
|
|
|
|
month, err := strconv.Atoi(monthStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2018-12-22 09:20:34 +01:00
|
|
|
return c.JSON(http.StatusInternalServerError, errorResponse{"Invalid month"})
|
2018-12-02 14:53:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logMessages, err := s.fileLogger.ReadLogForUser(channelID, userID, year, month)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
2018-12-22 09:20:34 +01:00
|
|
|
return c.JSON(http.StatusInternalServerError, errorResponse{"Failure reading log"})
|
2018-12-02 14:53:01 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 20:53:03 +01:00
|
|
|
if shouldReverse(c) {
|
|
|
|
reverse(logMessages)
|
|
|
|
}
|
|
|
|
|
2018-12-02 14:53:01 +01:00
|
|
|
var logResult chatLog
|
|
|
|
|
|
|
|
for _, rawMessage := range logMessages {
|
|
|
|
channel, user, parsedMessage := twitch.ParseMessage(rawMessage)
|
|
|
|
|
|
|
|
message := chatMessage{
|
2018-12-03 21:52:15 +01:00
|
|
|
Timestamp: timestamp{parsedMessage.Time},
|
|
|
|
Username: user.Username,
|
|
|
|
DisplayName: user.DisplayName,
|
|
|
|
Text: parsedMessage.Text,
|
|
|
|
Type: parsedMessage.Type,
|
|
|
|
Channel: channel,
|
2018-12-02 14:53:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logResult.Messages = append(logResult.Messages, message)
|
|
|
|
}
|
|
|
|
|
2018-12-03 21:52:15 +01:00
|
|
|
if shouldRespondWithJson(c) {
|
2018-12-02 14:53:01 +01:00
|
|
|
return writeJSONResponse(c, &logResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeTextResponse(c, &logResult)
|
|
|
|
}
|
|
|
|
|
2018-12-03 22:58:45 +01:00
|
|
|
func (s *Server) getUserLogsRange(c echo.Context) error {
|
2018-12-02 14:53:01 +01:00
|
|
|
channelID := c.Param("channelid")
|
2018-12-03 22:58:45 +01:00
|
|
|
userID := c.Param("userid")
|
2018-12-02 14:53:01 +01:00
|
|
|
|
2018-12-03 22:58:45 +01:00
|
|
|
fromTime, toTime, err := parseFromTo(c.QueryParam("from"), c.QueryParam("to"), userHourLimit)
|
2018-12-02 14:53:01 +01:00
|
|
|
if err != nil {
|
2018-12-22 09:20:34 +01:00
|
|
|
return c.JSON(http.StatusInternalServerError, errorResponse{err.Error()})
|
2018-12-02 14:53:01 +01:00
|
|
|
}
|
2018-12-03 22:58:45 +01:00
|
|
|
|
|
|
|
var logMessages []string
|
|
|
|
|
|
|
|
logMessages, _ = s.fileLogger.ReadLogForUser(channelID, userID, fromTime.Year(), int(fromTime.Month()))
|
|
|
|
|
|
|
|
if fromTime.Month() != toTime.Month() {
|
|
|
|
additionalMessages, _ := s.fileLogger.ReadLogForUser(channelID, userID, toTime.Year(), int(toTime.Month()))
|
|
|
|
|
|
|
|
logMessages = append(logMessages, additionalMessages...)
|
2018-12-02 14:53:01 +01:00
|
|
|
}
|
|
|
|
|
2018-12-03 22:58:45 +01:00
|
|
|
if len(logMessages) == 0 {
|
2018-12-22 09:20:34 +01:00
|
|
|
return c.JSON(http.StatusNotFound, errorResponse{"No logs found"})
|
2018-12-02 14:53:01 +01:00
|
|
|
}
|
|
|
|
|
2018-12-04 20:53:03 +01:00
|
|
|
if shouldReverse(c) {
|
|
|
|
reverse(logMessages)
|
|
|
|
}
|
|
|
|
|
2018-12-02 14:53:01 +01:00
|
|
|
var logResult chatLog
|
|
|
|
|
|
|
|
for _, rawMessage := range logMessages {
|
|
|
|
channel, user, parsedMessage := twitch.ParseMessage(rawMessage)
|
|
|
|
|
2018-12-03 22:58:45 +01:00
|
|
|
if parsedMessage.Time.Unix() < fromTime.Unix() || parsedMessage.Time.Unix() > toTime.Unix() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-12-02 14:53:01 +01:00
|
|
|
message := chatMessage{
|
2018-12-03 21:52:15 +01:00
|
|
|
Timestamp: timestamp{parsedMessage.Time},
|
|
|
|
Username: user.Username,
|
|
|
|
DisplayName: user.DisplayName,
|
|
|
|
Text: parsedMessage.Text,
|
|
|
|
Type: parsedMessage.Type,
|
|
|
|
Channel: channel,
|
2018-12-02 14:53:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logResult.Messages = append(logResult.Messages, message)
|
|
|
|
}
|
|
|
|
|
2018-12-03 21:52:15 +01:00
|
|
|
if shouldRespondWithJson(c) {
|
2018-12-02 14:53:01 +01:00
|
|
|
return writeJSONResponse(c, &logResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
return writeTextResponse(c, &logResult)
|
|
|
|
}
|