Skip to content

Commit

Permalink
feat(live): create chat conversion endpoint for faster testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Zibbp committed Oct 25, 2022
1 parent d3ad6b4 commit 6548f5a
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
31 changes: 31 additions & 0 deletions internal/live/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/zibbp/ganymede/internal/archive"
"github.com/zibbp/ganymede/internal/database"
"github.com/zibbp/ganymede/internal/twitch"
"github.com/zibbp/ganymede/internal/utils"
"strconv"
"time"
)

Expand All @@ -29,6 +31,15 @@ type Live struct {
LastLive time.Time `json:"last_live"`
}

type ConvertChat struct {
FileName string `json:"file_name"`
ChannelName string `json:"channel_name"`
VodID string `json:"vod_id"`
ChannelID int `json:"channel_id"`
VodExternalID string `json:"vod_external_id"`
ChatStart string `json:"chat_start"`
}

func NewService(store *database.Database, twitchService *twitch.Service, archiveService *archive.Service) *Service {
return &Service{Store: store, TwitchService: twitchService, ArchiveService: archiveService}
}
Expand Down Expand Up @@ -146,6 +157,26 @@ func (s *Service) Check() error {
return nil
}

func (s *Service) ConvertChat(c echo.Context, convertChatDto ConvertChat) error {
i, err := strconv.ParseInt(convertChatDto.ChatStart, 10, 64)
if err != nil {
return fmt.Errorf("error parsing chat start: %v", err)
}
tm := time.Unix(i, 0)
err = utils.ConvertTwitchLiveChatToVodChat(
fmt.Sprintf("/tmp/%s", convertChatDto.FileName),
convertChatDto.ChannelName,
convertChatDto.VodID,
convertChatDto.VodExternalID,
convertChatDto.ChannelID,
tm,
)
if err != nil {
return fmt.Errorf("error converting chat: %v", err)
}
return nil
}

func stringInSlice(a string, list []twitch.Live) twitch.Live {
for _, b := range list {
if b.UserLogin == a {
Expand Down
1 change: 1 addition & 0 deletions internal/transport/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ func groupV1Routes(e *echo.Group, h *Handler) {
liveGroup.PUT("/:id", h.UpdateLiveWatchedChannel, authMiddleware, auth.GetUserMiddleware, auth.UserRoleMiddleware(utils.EditorRole))
liveGroup.DELETE("/:id", h.DeleteLiveWatchedChannel, authMiddleware, auth.GetUserMiddleware, auth.UserRoleMiddleware(utils.EditorRole))
liveGroup.GET("/check", h.Check, authMiddleware, auth.GetUserMiddleware, auth.UserRoleMiddleware(utils.EditorRole))
liveGroup.POST("/chat-convert", h.ConvertChat, authMiddleware, auth.GetUserMiddleware, auth.UserRoleMiddleware(utils.EditorRole))

// Playback
playbackGroup := e.Group("/playback")
Expand Down
35 changes: 35 additions & 0 deletions internal/transport/http/live.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type LiveService interface {
DeleteLiveWatchedChannel(c echo.Context, lID uuid.UUID) error
UpdateLiveWatchedChannel(c echo.Context, liveDto live.Live) (*ent.Live, error)
Check() error
ConvertChat(c echo.Context, convertDto live.ConvertChat) error
}

type AddWatchedChannelRequest struct {
Expand All @@ -27,6 +28,15 @@ type UpdateWatchedChannelRequest struct {
ArchiveChat bool `json:"archive_chat"`
}

type ConvertChatRequest struct {
FileName string `json:"file_name" validate:"required"`
ChannelName string `json:"channel_name" validate:"required"`
VodID string `json:"vod_id" validate:"required"`
ChannelID int `json:"channel_id" validate:"required"`
VodExternalID string `json:"vod_external_id" validate:"required"`
ChatStart string `json:"chat_start" validate:"required"`
}

func (h *Handler) GetLiveWatchedChannels(c echo.Context) error {
channels, err := h.Service.LiveService.GetLiveWatchedChannels(c)
if err != nil {
Expand Down Expand Up @@ -110,3 +120,28 @@ func (h *Handler) Check(c echo.Context) error {
}
return c.JSON(http.StatusOK, "ok")
}

// ConvertChat Adhoc convert live chat to TD chat format
func (h *Handler) ConvertChat(c echo.Context) error {
ccr := new(ConvertChatRequest)
if err := c.Bind(ccr); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
if err := c.Validate(ccr); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
}
convertDto := live.ConvertChat{
FileName: ccr.FileName,
ChannelName: ccr.ChannelName,
VodID: ccr.VodID,
ChannelID: ccr.ChannelID,
VodExternalID: ccr.VodExternalID,
ChatStart: ccr.ChatStart,
}
err := h.Service.LiveService.ConvertChat(c, convertDto)
if err != nil {
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

return c.JSON(http.StatusOK, "ok - converted chat found in /tmp/")
}

0 comments on commit 6548f5a

Please sign in to comment.