tsviewer/service/channel.go

93 lines
1.7 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-17 00:35:51 +00:00
"git.cliffbreak.de/Cliffbreak/tsviewer/features/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 {
cc = append(cc, convertChannel(channel))
removeItem(&channels, i)
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 {
addSubChannels(c, &channels)
}
2019-01-15 00:46:39 +00:00
return cc, nil
}
func convertChannel(c *ts3.Channel) *channel.Channel {
return &channel.Channel{
ID: c.ID,
Subchannels: []channel.Channel{},
Name: c.ChannelName,
TotalClients: c.TotalClients,
NeededSubscribePower: c.NeededSubscribePower,
}
2019-01-14 20:07:11 +00:00
}
2019-01-17 00:35:51 +00:00
func removeItem(slice *[]*ts3.Channel, id int) {
ss := *slice
length := len(ss)
if length < id+1 {
ss = nil
} else if length == id+1 {
ss = ss[id-1:]
} else if length > id+1 {
ss = append(ss[:id], ss[id+1:]...)
}
*slice = ss
}
func addSubChannels(c *channel.Channel, channels *[]*ts3.Channel) {
for i, channel := range *channels {
if c.ID == channel.ParentID {
removeItem(channels, i)
subChannel := convertChannel(channel)
addSubChannels(subChannel, channels)
c.Subchannels = append(c.Subchannels, *subChannel)
}
}
}