2020-03-01 12:33:55 +00:00

53 lines
1.3 KiB
Go

package root
import (
"github.com/TicketsBot/GoPanel/config"
"github.com/TicketsBot/GoPanel/database/table"
"github.com/TicketsBot/GoPanel/utils"
"github.com/TicketsBot/GoPanel/utils/discord/objects"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
"strconv"
)
func IndexHandler(ctx *gin.Context) {
store := sessions.Default(ctx)
if store == nil {
return
}
defer store.Save()
if utils.IsLoggedIn(store) {
userIdStr := store.Get("userid").(string)
userId, err := utils.GetUserId(store)
if err != nil {
ctx.String(500, err.Error())
return
}
adminGuilds := make([]objects.Guild, 0)
adminGuildIds := table.GetAdminGuilds(userId)
for _, guild := range table.GetGuilds(userIdStr) {
guildId, err := strconv.ParseInt(guild.Id, 10, 64)
if err != nil { // I think this happens when a server was deleted? We should just skip though.
continue
}
if guild.Owner || utils.Contains(adminGuildIds, guildId) {
adminGuilds = append(adminGuilds, guild)
}
}
ctx.HTML(200, "main/index", gin.H{
"name": store.Get("name").(string),
"baseurl": config.Conf.Server.BaseUrl,
"servers": adminGuilds,
"empty": len(adminGuilds) == 0,
"isIndex": true,
"avatar": store.Get("avatar").(string),
})
} else {
ctx.Redirect(302, "/login")
}
}