fixed channels route

This commit is contained in:
Hendrik Schlehlein 2019-02-04 00:40:43 +01:00
parent 036d6f604c
commit c3fb6ce53b
4 changed files with 31 additions and 22 deletions

View file

@ -7,8 +7,8 @@ type Service interface {
type Channel struct {
ID int `json:"id"`
Subchannels []Channel `json:"subchannels,omitempty"`
Name string `json:"name"`
Subchannels []Channel `json:"subchannels,omitempty"`
TotalClients int `json:"totalClients"`
NeededSubscribePower int `json:"neededSubscribePower"`
}

View file

@ -15,7 +15,7 @@ func IndexGUIHandler(s Service, t template.Template) http.HandlerFunc {
return
}
channels, err := s.Channels()
channels, err := s.ChannelsRaw()
if err != nil {
weberror.NewPage(err, http.StatusNotFound).Send(w, t)
return

View file

@ -13,6 +13,7 @@ type Service interface {
ServerInfo() (*server.Server, error)
Clients() ([]*client.Client, error)
Channels() ([]*channel.Channel, error)
ChannelsRaw() ([]*channel.Channel, error)
}
type IndexPage struct {

View file

@ -43,13 +43,32 @@ func (s Service) Channels() ([]*channel.Channel, error) {
}
if channel.ParentID == 0 {
channels[i] = nil
cc = append(cc, convertChannel(channel))
removeItem(&channels, i)
}
}
for _, c := range cc {
addSubChannels(c, &channels)
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
@ -58,32 +77,21 @@ func (s Service) Channels() ([]*channel.Channel, error) {
func convertChannel(c *ts3.Channel) *channel.Channel {
return &channel.Channel{
ID: c.ID,
Subchannels: []channel.Channel{},
Name: c.ChannelName,
Subchannels: []channel.Channel{},
TotalClients: c.TotalClients,
NeededSubscribePower: c.NeededSubscribePower,
}
}
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]
} else if length > id+1 {
ss = append(ss[:id], ss[id+1:]...)
func addSubChannels(c *channel.Channel, channels []*ts3.Channel) {
for i, channel := range channels {
if channel == nil {
continue
}
*slice = ss
}
func addSubChannels(c *channel.Channel, channels *[]*ts3.Channel) {
for i, channel := range *channels {
if c.ID == channel.ParentID {
removeItem(channels, i)
channels[i] = nil
subChannel := convertChannel(channel)
addSubChannels(subChannel, channels)
c.Subchannels = append(c.Subchannels, *subChannel)