apifiber/services/channelservices.go

81 lines
2.6 KiB
Go

package services
import (
"api/globals"
"api/models"
"api/utils"
"time"
)
func VerifyTransmissionsLimits() {
// log.Println("Verificando")
for channelname, currentTransmission := range globals.Transmissions {
// If the channel has no daily transmission limit, skip the verification (Channels with 24 hours daily limit)
if time.Duration(currentTransmission.PlanDailyLimit) == 1440 {
continue
}
AddTransmissionlog(channelname, "Verificando expiração de transmissão")
// TODO: Implementar verificação de limite de transmissão diária (ou seja, quanto já foi transmitido HOJE)
if time.Now().After(currentTransmission.Limit) {
AddTransmissionlog(channelname, "Limite de transmissão diária atingido")
utils.KickSession(channelname, currentTransmission.SessionID)
}
}
}
func VerifyTransmissionAuthorization(channelname, sessionid, transmissionkey, playerKey string) (bool, models.CurrentTransmission) {
var (
channel models.Channel
plan models.Plan
currentTransmission models.CurrentTransmission
)
globals.DB.Where("name = ?", channelname).First(&channel)
globals.DB.First(&plan, channel.PlanID)
currentTransmission.Channel = channel.Name
currentTransmission.SessionID = sessionid
currentTransmission.PlayerKey = playerKey
currentTransmission.PlanDailyLimit = plan.DailyLimitTransmission
currentTransmission.StartTime = time.Now()
currentTransmission.Limit = currentTransmission.StartTime.Add(time.Duration(plan.DailyLimitTransmission) * time.Minute)
// If the channel is not found, kick the session
if channel.ID == 0 {
AddTransmissionlog(channelname, "Canal inexistente")
utils.KickSession(channelname, sessionid)
return false, currentTransmission
}
// If the channel expiration date is reached, kick the session
if time.Now().After(channel.DateLimit) {
AddTransmissionlog(channelname, "Canal com data de expiração vencida")
utils.KickSession(channelname, sessionid)
return false, currentTransmission
}
// If the channel is not active, kick the session
if channel.Status != "A" {
AddTransmissionlog(channelname, "Canal não está ativo")
utils.KickSession(channelname, sessionid)
return false, currentTransmission
}
// If the transmission key does not match, kick the session
if transmissionkey != channel.TransmissionKey {
AddTransmissionlog(channelname, "Transmissão com chave de transmissão errada")
utils.KickSession(channelname, sessionid)
return false, currentTransmission
}
// TODO: Save the transmission on database for future calculation of remaining daily time
globals.Transmissions[channelname] = &currentTransmission
return true, currentTransmission
}