forked from Cliffbreak/tsviewer
100 lines
1.8 KiB
Go
100 lines
1.8 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"git.cliffbreak.de/Cliffbreak/tsviewer/features/api/channel"
|
|
"github.com/multiplay/go-ts3"
|
|
)
|
|
|
|
func (s Service) Channel(id int) (*channel.Channel, error) {
|
|
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
|
|
}
|
|
|
|
func (s Service) Channels() ([]*channel.Channel, error) {
|
|
channels, err := s.TSClient.Server.ChannelList()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var cc []*channel.Channel
|
|
|
|
for i, channel := range channels {
|
|
if channel == nil {
|
|
continue
|
|
}
|
|
|
|
if channel.ParentID == 0 {
|
|
channels[i] = nil
|
|
cc = append(cc, convertChannel(channel))
|
|
}
|
|
}
|
|
|
|
for _, c := range cc {
|
|
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))
|
|
}
|
|
|
|
return cc, nil
|
|
}
|
|
|
|
func convertChannel(c *ts3.Channel) *channel.Channel {
|
|
return &channel.Channel{
|
|
ID: c.ID,
|
|
Name: c.ChannelName,
|
|
Subchannels: []channel.Channel{},
|
|
TotalClients: c.TotalClients,
|
|
NeededSubscribePower: c.NeededSubscribePower,
|
|
}
|
|
}
|
|
|
|
func addSubChannels(c *channel.Channel, channels []*ts3.Channel) {
|
|
for i, channel := range channels {
|
|
if channel == nil {
|
|
continue
|
|
}
|
|
|
|
if c.ID == channel.ParentID {
|
|
channels[i] = nil
|
|
subChannel := convertChannel(channel)
|
|
addSubChannels(subChannel, channels)
|
|
c.Subchannels = append(c.Subchannels, *subChannel)
|
|
}
|
|
}
|
|
}
|