111 lines
2.4 KiB
Go
Raw Normal View History

2022-09-12 22:36:56 +02:00
package handler
2020-09-27 00:10:34 +02:00
import (
2021-10-05 22:38:24 +02:00
"m1k1o/neko/internal/types"
"m1k1o/neko/internal/types/event"
"m1k1o/neko/internal/types/message"
2020-09-27 00:10:34 +02:00
)
func (h *MessageHandler) boradcastCreate(session types.Session, payload *message.BroadcastCreate) error {
2022-09-17 12:43:17 +02:00
broadcast := h.capture.Broadcast()
2020-09-27 00:10:34 +02:00
if !session.Admin() {
h.logger.Debug().Msg("user not admin")
return nil
}
2022-09-17 12:43:17 +02:00
if payload.URL == "" {
return session.Send(
message.SystemMessage{
Event: event.SYSTEM_ERROR,
Title: "Error while starting broadcast",
Message: "missing broadcast URL",
})
}
if broadcast.Started() {
return session.Send(
message.SystemMessage{
Event: event.SYSTEM_ERROR,
Title: "Error while starting broadcast",
Message: "server is already broadcasting",
})
}
if err := broadcast.Start(payload.URL); err != nil {
2021-08-15 16:05:26 +02:00
if err := session.Send(
message.SystemMessage{
Event: event.SYSTEM_ERROR,
Title: "Error while starting broadcast",
2022-09-17 12:43:17 +02:00
Message: err.Error(),
2021-08-15 16:05:26 +02:00
}); err != nil {
h.logger.Warn().Err(err).Msgf("sending event %s has failed", event.SYSTEM_ERROR)
return err
}
2021-08-15 15:37:27 +02:00
}
2020-09-27 00:10:34 +02:00
2021-12-11 13:52:31 +01:00
if err := h.boradcastStatus(nil); err != nil {
2020-09-27 00:10:34 +02:00
return err
}
return nil
}
func (h *MessageHandler) boradcastDestroy(session types.Session) error {
2022-09-17 12:43:17 +02:00
broadcast := h.capture.Broadcast()
2020-09-27 00:10:34 +02:00
if !session.Admin() {
h.logger.Debug().Msg("user not admin")
return nil
}
2022-09-17 12:43:17 +02:00
if !broadcast.Started() {
return session.Send(
message.SystemMessage{
Event: event.SYSTEM_ERROR,
Title: "Error while stopping broadcast",
Message: "server is not broadcasting",
})
}
broadcast.Stop()
2020-09-27 00:10:34 +02:00
2021-12-11 13:52:31 +01:00
if err := h.boradcastStatus(nil); err != nil {
2020-09-27 00:10:34 +02:00
return err
}
return nil
}
func (h *MessageHandler) boradcastStatus(session types.Session) error {
2022-09-17 12:43:17 +02:00
broadcast := h.capture.Broadcast()
msg := message.BroadcastStatus{
Event: event.BORADCAST_STATUS,
IsActive: broadcast.Started(),
URL: broadcast.Url(),
}
2021-12-11 13:52:31 +01:00
// if no session, broadcast change
if session == nil {
2022-09-17 12:43:17 +02:00
if err := h.sessions.AdminBroadcast(msg, nil); err != nil {
2021-12-11 13:52:31 +01:00
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.BORADCAST_STATUS)
return err
}
return nil
}
2020-09-27 00:10:34 +02:00
if !session.Admin() {
h.logger.Debug().Msg("user not admin")
return nil
}
2022-09-17 12:43:17 +02:00
if err := session.Send(msg); err != nil {
2020-09-27 00:10:34 +02:00
h.logger.Warn().Err(err).Msgf("sending event %s has failed", event.BORADCAST_STATUS)
return err
}
return nil
}