justlog/api/user.go

351 lines
9.6 KiB
Go
Raw Normal View History

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
)
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
}
func (s *Server) getLastUserLogs(c echo.Context) error {
2018-12-02 14:53:01 +01:00
channelID := c.Param("channelid")
userID := c.Param("userid")
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)
if len(c.QueryString()) > 0 {
redirectURL += "?" + c.QueryString()
}
2017-03-08 21:38:01 +01:00
return c.Redirect(303, redirectURL)
}
// 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
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)
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)
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)
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)
}
// 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"
// @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)
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")
userID := c.Param("userid")
2017-03-08 21:38:01 +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
}
2019-03-11 21:41:12 +01:00
user, parsedMessage := twitch.ParseMessage(rawMessage)
2019-03-11 21:41:12 +01:00
switch parsedMessage.(type) {
case *twitch.PrivateMessage:
message := *parsedMessage.(*twitch.PrivateMessage)
if shouldRespondWithJson(c) {
randomQ := RandomQuoteJSON{
Channel: message.Channel,
Username: user.Name,
DisplayName: user.DisplayName,
Message: message.Message,
Timestamp: timestamp{message.Time},
}
2019-03-11 21:41:12 +01:00
return c.JSON(http.StatusOK, randomQ)
}
2019-03-11 21:41:12 +01:00
return c.String(http.StatusOK, message.Message)
case *twitch.ClearChatMessage:
message := *parsedMessage.(*twitch.ClearChatMessage)
if shouldRespondWithJson(c) {
randomQ := RandomQuoteJSON{
Channel: message.Channel,
Username: message.TargetUsername,
DisplayName: message.TargetUsername,
Message: message.Message,
Timestamp: timestamp{message.Time},
}
return c.JSON(http.StatusOK, randomQ)
}
return c.String(http.StatusOK, message.Message)
}
2019-03-11 21:41:12 +01:00
return c.String(http.StatusNotFound, "No quote found")
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)
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)
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)
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 {
2019-03-11 21:41:12 +01:00
user, parsedMessage := twitch.ParseMessage(rawMessage)
var chatMsg chatMessage
switch parsedMessage.(type) {
case *twitch.PrivateMessage:
message := *parsedMessage.(*twitch.PrivateMessage)
chatMsg = chatMessage{
Timestamp: timestamp{message.Time},
Username: user.Name,
DisplayName: user.DisplayName,
Text: message.Message,
Type: message.Type,
Channel: message.Channel,
}
case *twitch.ClearChatMessage:
message := *parsedMessage.(*twitch.ClearChatMessage)
chatMsg = chatMessage{
Timestamp: timestamp{message.Time},
Username: message.TargetUsername,
DisplayName: message.TargetUsername,
Text: message.Message,
Type: message.Type,
Channel: message.Channel,
}
2018-12-02 14:53:01 +01:00
}
2019-03-11 21:41:12 +01:00
logResult.Messages = append(logResult.Messages, chatMsg)
2018-12-02 14:53:01 +01:00
}
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 {
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 {
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 {
2019-03-11 21:41:12 +01:00
user, parsedMessage := twitch.ParseMessage(rawMessage)
var chatMsg chatMessage
switch parsedMessage.(type) {
case *twitch.PrivateMessage:
message := *parsedMessage.(*twitch.PrivateMessage)
if message.Time.Unix() < fromTime.Unix() || message.Time.Unix() > toTime.Unix() {
continue
}
chatMsg = chatMessage{
Timestamp: timestamp{message.Time},
Username: user.Name,
DisplayName: user.DisplayName,
Text: message.Message,
Type: message.Type,
Channel: message.Channel,
}
case *twitch.ClearChatMessage:
message := *parsedMessage.(*twitch.ClearChatMessage)
if message.Time.Unix() < fromTime.Unix() || message.Time.Unix() > toTime.Unix() {
continue
}
chatMsg = chatMessage{
Timestamp: timestamp{message.Time},
Username: message.TargetUsername,
DisplayName: message.TargetUsername,
Text: message.Message,
Type: message.Type,
Channel: message.Channel,
}
2018-12-02 14:53:01 +01:00
}
2019-03-11 21:41:12 +01:00
logResult.Messages = append(logResult.Messages, chatMsg)
2018-12-02 14:53:01 +01:00
}
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)
}