tsviewer/service/channel.go

65 lines
1.2 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
"git.cliffbreak.de/haveachin/go-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
for _, channel := range channels {
if channel.ParentID == 0 {
cc = append(cc, convertChannel(channel))
continue
}
for _, c := range cc {
if c.ID == channel.ParentID {
c.Subchannels = append(c.Subchannels, *convertChannel(channel))
}
}
2019-01-14 20:07:11 +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,
Subchannels: []channel.Channel{},
Name: c.ChannelName,
TotalClients: c.TotalClients,
NeededSubscribePower: c.NeededSubscribePower,
}
2019-01-14 20:07:11 +00:00
}