2016-01-26 21:10:44 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2016-03-01 00:51:26 +00:00
|
|
|
"github.com/spf13/viper"
|
2016-01-26 21:10:44 +00:00
|
|
|
|
|
|
|
"github.com/khlieng/dispatch/storage"
|
|
|
|
)
|
|
|
|
|
|
|
|
type connectDefaults struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Address string `json:"address"`
|
|
|
|
Channels []string `json:"channels"`
|
2016-01-27 17:08:20 +00:00
|
|
|
Password bool `json:"password"`
|
2016-01-26 21:10:44 +00:00
|
|
|
SSL bool `json:"ssl"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type indexData struct {
|
|
|
|
Defaults connectDefaults `json:"defaults"`
|
|
|
|
Servers []storage.Server `json:"servers,omitempty"`
|
|
|
|
Channels []storage.Channel `json:"channels,omitempty"`
|
|
|
|
|
|
|
|
// Users in the selected channel
|
|
|
|
Users *Userlist `json:"users,omitempty"`
|
|
|
|
|
|
|
|
// Last messages in the selected channel
|
2017-04-19 23:51:55 +00:00
|
|
|
Messages *Messages `json:"messages,omitempty"`
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getIndexData(r *http.Request, session *Session) *indexData {
|
|
|
|
servers := session.user.GetServers()
|
|
|
|
connections := session.getConnectionStates()
|
|
|
|
for i, server := range servers {
|
|
|
|
servers[i].Connected = connections[server.Host]
|
|
|
|
servers[i].Port = ""
|
|
|
|
servers[i].TLS = false
|
|
|
|
servers[i].Password = ""
|
|
|
|
servers[i].Username = ""
|
|
|
|
servers[i].Realname = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
channels := session.user.GetChannels()
|
|
|
|
for i, channel := range channels {
|
|
|
|
channels[i].Topic = channelStore.GetTopic(channel.Server, channel.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
data := indexData{
|
|
|
|
Defaults: connectDefaults{
|
|
|
|
Name: viper.GetString("defaults.name"),
|
|
|
|
Address: viper.GetString("defaults.address"),
|
|
|
|
Channels: viper.GetStringSlice("defaults.channels"),
|
2016-01-27 17:08:20 +00:00
|
|
|
Password: viper.GetString("defaults.password") != "",
|
2016-01-26 21:10:44 +00:00
|
|
|
SSL: viper.GetBool("defaults.ssl"),
|
|
|
|
},
|
|
|
|
Servers: servers,
|
|
|
|
Channels: channels,
|
|
|
|
}
|
|
|
|
|
|
|
|
params := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
|
|
|
|
if len(params) == 2 && isChannel(params[1]) {
|
|
|
|
users := channelStore.GetUsers(params[0], params[1])
|
|
|
|
if len(users) > 0 {
|
|
|
|
data.Users = &Userlist{
|
|
|
|
Server: params[0],
|
|
|
|
Channel: params[1],
|
|
|
|
Users: users,
|
|
|
|
}
|
|
|
|
}
|
2017-04-19 23:51:55 +00:00
|
|
|
|
2017-05-02 21:21:25 +00:00
|
|
|
messages, hasMore, err := session.user.GetLastMessages(params[0], params[1], 50)
|
2017-04-19 23:51:55 +00:00
|
|
|
if err == nil && len(messages) > 0 {
|
2017-05-02 21:21:25 +00:00
|
|
|
m := Messages{
|
2017-04-19 23:51:55 +00:00
|
|
|
Server: params[0],
|
|
|
|
To: params[1],
|
|
|
|
Messages: messages,
|
|
|
|
}
|
2017-05-02 21:21:25 +00:00
|
|
|
|
|
|
|
if hasMore {
|
|
|
|
m.Next = messages[0].ID
|
|
|
|
}
|
|
|
|
|
|
|
|
data.Messages = &m
|
2017-04-19 23:51:55 +00:00
|
|
|
}
|
2016-01-26 21:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &data
|
|
|
|
}
|