99 lines
2.2 KiB
Go
99 lines
2.2 KiB
Go
package controllers
|
|
|
|
import (
|
|
"api/globals"
|
|
"log"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// Returns all current transmissions
|
|
func GetTransmissions(c *fiber.Ctx) error {
|
|
return c.JSON(globals.Transmissions)
|
|
}
|
|
|
|
// Returns a specific transmission by channel
|
|
func GetTransmissionByChannel(c *fiber.Ctx) error {
|
|
channel := c.Params("channel")
|
|
if channel == "" {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
|
|
transmission, exists := globals.Transmissions[channel]
|
|
if !exists {
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
return c.JSON(transmission)
|
|
}
|
|
|
|
func WatchersCount(c *fiber.Ctx) error {
|
|
|
|
channel := c.Params("channel")
|
|
|
|
if channel == "" {
|
|
return fiber.ErrBadRequest
|
|
}
|
|
|
|
if transmission, exists := globals.Transmissions[channel]; exists {
|
|
return c.JSON(transmission.Assistentes)
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
// Called when a user starts to watch a transmission
|
|
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.Assistentes = transmission.Assistentes + 1
|
|
switch platform {
|
|
case "desktop":
|
|
transmission.Desktop = transmission.Desktop + 1
|
|
case "mobile":
|
|
transmission.Mobile = transmission.Mobile + 1
|
|
}
|
|
return c.JSON(transmission.Assistentes)
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
// Called when a user stops watching a transmission
|
|
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.Assistentes = transmission.Assistentes - 1
|
|
if transmission.Assistentes < 0 {
|
|
transmission.Assistentes = 0
|
|
}
|
|
switch platform {
|
|
case "desktop":
|
|
transmission.Desktop = transmission.Desktop - 1
|
|
case "mobile":
|
|
transmission.Mobile = transmission.Mobile - 1
|
|
}
|
|
return c.JSON(transmission.Assistentes)
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|