diff --git a/controllers/transmissionController.go b/controllers/transmissionController.go index 93a2240..16400af 100644 --- a/controllers/transmissionController.go +++ b/controllers/transmissionController.go @@ -2,6 +2,7 @@ package controllers import ( "api/globals" + "log" "github.com/gofiber/fiber/v2" ) @@ -27,7 +28,9 @@ func GetTransmissionByChannel(c *fiber.Ctx) error { } func WatchersCount(c *fiber.Ctx) error { + channel := c.Params("channel") + if channel == "" { return fiber.ErrBadRequest } @@ -43,13 +46,22 @@ func WatchersCount(c *fiber.Ctx) error { func Watch(c *fiber.Ctx) error { channel := c.Params("channel") + platform := c.Params("platform") if channel == "" { return fiber.ErrBadRequest } + log.Printf("User started watching channel on %s: %s", platform, channel) + if transmission, exists := globals.Transmissions[channel]; exists { transmission.Watchers = transmission.Watchers + 1 + switch platform { + case "desktop": + transmission.Desktop = transmission.Desktop + 1 + case "mobile": + transmission.Mobile = transmission.Mobile + 1 + } return c.JSON(transmission.Watchers) } @@ -60,16 +72,25 @@ func Watch(c *fiber.Ctx) error { func Leave(c *fiber.Ctx) error { channel := c.Params("channel") + platform := c.Params("platform") if channel == "" { return fiber.ErrBadRequest } + log.Printf("User stopped watching channel: %s", channel) + if transmission, exists := globals.Transmissions[channel]; exists { transmission.Watchers = transmission.Watchers - 1 if transmission.Watchers < 0 { transmission.Watchers = 0 } + switch platform { + case "desktop": + transmission.Desktop = transmission.Desktop - 1 + case "mobile": + transmission.Mobile = transmission.Mobile - 1 + } return c.JSON(transmission.Watchers) } diff --git a/controllers/webhookController.go b/controllers/webhookController.go index 400d8d4..99b2874 100644 --- a/controllers/webhookController.go +++ b/controllers/webhookController.go @@ -121,7 +121,7 @@ func OnPubStop(c *fiber.Ctx) error { return c.SendString("On_Pub_Stop: " + string(c.Body())) } - log.Printf("======================== Stop StreamName %s - Duration %d\n", p.StreamName, duration) + log.Printf("======================== Transmissão encerrada do canal %s - Duration %d\n", p.StreamName, duration) // TODO: Updates the transmission on database for future calculation of remaining daily time @@ -129,9 +129,13 @@ func OnPubStop(c *fiber.Ctx) error { minutes = 1 } - msg := fmt.Sprintf("Transmissão encerrada. Duração %d minutos.\n", int(minutes)) + // msg := fmt.Sprintf("Transmissão encerrada. Duração %d minutos.\n", int(minutes)) - services.AddTransmissionlog(p.StreamName, msg) + // services.AddTransmissionlog(p.StreamName, msg) + + // Saves the transmission on the database + transm.Duration = minutes + globals.DB.Save(&transm) delete(globals.Transmissions, p.StreamName) return c.SendString("On_Pub_Stop: " + string(c.Body())) diff --git a/models/models.go b/models/models.go index 33d4a39..74c6f94 100644 --- a/models/models.go +++ b/models/models.go @@ -229,13 +229,23 @@ func (TransmissionLog) TableName() string { return "transmissionlog" } -// Available only on RAM +// Available on RAM during the execution +// CurrentTransmission represents the transmissions table in the database type CurrentTransmission struct { - Channel string - StartTime time.Time - Limit time.Time - SessionID string - PlayerKey string - PlanDailyLimit int - Watchers int + ID uint `gorm:"primaryKey;column:id;autoIncrement"` + Channel string `gorm:"column:channel;type:text;not null"` + StartTime time.Time `gorm:"column:starttime;type:timestamp;not null"` + Duration int `gorm:"column:duration;not null"` + Limit time.Time `gorm:"-"` + SessionID string `gorm:"-"` + PlayerKey string `gorm:"-"` + PlanDailyLimit int `gorm:"-"` + Watchers int `gorm:"column:watchers;not null"` + Desktop int `gorm:"column:desktop;not null"` + Mobile int `gorm:"column:mobile;not null"` +} + +// TableName overrides the table name +func (CurrentTransmission) TableName() string { + return "transmissions" } diff --git a/routes/routes.go b/routes/routes.go index 8e4ecf5..b2a280a 100644 --- a/routes/routes.go +++ b/routes/routes.go @@ -34,6 +34,6 @@ func Setup(app *fiber.App) { // Interfaces to player app.Get("/c/:channel", controllers.WatchersCount) - app.Get("/w/:channel", controllers.Watch) - app.Get("/l/:channel", controllers.Leave) + app.Get("/w/:channel/:platform", controllers.Watch) + app.Get("/l/:channel/:platform", controllers.Leave) }