tsviewer/service/channel.go

101 lines
1.8 KiB
Go
Raw Normal View History

2019-01-14 20:07:11 +00:00
package service
import (
2019-01-15 00:46:39 +00:00
"errors"
2019-01-14 20:07:11 +00:00
2019-01-21 17:53:33 +00:00
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/channel"
2019-01-15 00:46:39 +00:00
"github.com/multiplay/go-ts3"
2019-01-14 20:07:11 +00:00
)
func (s Service) Channel(id int) (*channel.Channel, error) {
2019-01-15 00:46:39 +00:00
channels, err := s.TSClient.Server.ChannelList()
if err != nil {
return nil, err
}
var c *channel.Channel
for _, channel := range channels {
if channel.ID == id {
c = convertChannel(channel)
break
}
}
if c == nil {
return nil, errors.New("channel does not exist")
}
return c, nil
2019-01-14 20:07:11 +00:00
}
func (s Service) Channels() ([]*channel.Channel, error) {
2019-01-15 00:46:39 +00:00
channels, err := s.TSClient.Server.ChannelList()
2019-01-14 20:07:11 +00:00
if err != nil {
return nil, err
}
2019-01-15 00:46:39 +00:00
var cc []*channel.Channel
2019-01-17 00:35:51 +00:00
for i, channel := range channels {
if channel == nil {
2019-01-15 00:46:39 +00:00
continue
}
2019-01-17 00:35:51 +00:00
if channel.ParentID == 0 {
2019-02-03 23:40:43 +00:00
channels[i] = nil
2019-01-17 00:35:51 +00:00
cc = append(cc, convertChannel(channel))
2019-01-15 00:46:39 +00:00
}
2019-01-14 20:07:11 +00:00
}
2019-01-17 00:35:51 +00:00
for _, c := range cc {
2019-02-03 23:40:43 +00:00
addSubChannels(c, channels)
}
return cc, nil
}
func (s Service) ChannelsRaw() ([]*channel.Channel, error) {
channels, err := s.TSClient.Server.ChannelList()
if err != nil {
return nil, err
}
var cc []*channel.Channel
for _, channel := range channels {
if channel == nil {
continue
}
cc = append(cc, convertChannel(channel))
2019-01-17 00:35:51 +00:00
}
2019-01-15 00:46:39 +00:00
return cc, nil
}
func convertChannel(c *ts3.Channel) *channel.Channel {
return &channel.Channel{
ID: c.ID,
Name: c.ChannelName,
2019-02-03 23:40:43 +00:00
Subchannels: []channel.Channel{},
2019-01-15 00:46:39 +00:00
TotalClients: c.TotalClients,
NeededSubscribePower: c.NeededSubscribePower,
}
2019-01-14 20:07:11 +00:00
}
2019-01-17 00:35:51 +00:00
2019-02-03 23:40:43 +00:00
func addSubChannels(c *channel.Channel, channels []*ts3.Channel) {
for i, channel := range channels {
if channel == nil {
continue
}
2019-01-17 00:35:51 +00:00
if c.ID == channel.ParentID {
2019-02-03 23:40:43 +00:00
channels[i] = nil
2019-01-17 00:35:51 +00:00
subChannel := convertChannel(channel)
addSubChannels(subChannel, channels)
c.Subchannels = append(c.Subchannels, *subChannel)
}
}
}