From c3fb6ce53be244a8d00c6371155c91266227edaa Mon Sep 17 00:00:00 2001 From: Hendrik Schlehlein Date: Mon, 4 Feb 2019 00:40:43 +0100 Subject: [PATCH] fixed channels route --- features/api/channel/channel.go | 2 +- features/web/index/handler.go | 2 +- features/web/index/page.go | 1 + service/channel.go | 48 +++++++++++++++++++-------------- 4 files changed, 31 insertions(+), 22 deletions(-) diff --git a/features/api/channel/channel.go b/features/api/channel/channel.go index cad648b..67e0f30 100644 --- a/features/api/channel/channel.go +++ b/features/api/channel/channel.go @@ -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"` } diff --git a/features/web/index/handler.go b/features/web/index/handler.go index 23913b4..ca77456 100644 --- a/features/web/index/handler.go +++ b/features/web/index/handler.go @@ -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 diff --git a/features/web/index/page.go b/features/web/index/page.go index 421decc..c706649 100644 --- a/features/web/index/page.go +++ b/features/web/index/page.go @@ -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 { diff --git a/service/channel.go b/service/channel.go index 41f9201..34712f9 100644 --- a/service/channel.go +++ b/service/channel.go @@ -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) +func addSubChannels(c *channel.Channel, channels []*ts3.Channel) { + for i, channel := range channels { + if channel == nil { + continue + } - 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:]...) - } - - *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)