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 { type Channel struct {
ID int `json:"id"` ID int `json:"id"`
Subchannels []Channel `json:"subchannels,omitempty"`
Name string `json:"name"` Name string `json:"name"`
Subchannels []Channel `json:"subchannels,omitempty"`
TotalClients int `json:"totalClients"` TotalClients int `json:"totalClients"`
NeededSubscribePower int `json:"neededSubscribePower"` NeededSubscribePower int `json:"neededSubscribePower"`
} }

View file

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

View file

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

View file

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