has_guilds key

This commit is contained in:
Dot-Rar 2020-05-15 17:12:38 +01:00
parent 740f058a18
commit 24a848a575
2 changed files with 39 additions and 32 deletions

View File

@ -80,39 +80,38 @@ func CallbackHandler(ctx *gin.Context) {
log.Error(err.Error())
}
var guilds []guild.Guild
err, _ = userEndpoint.CurrentUserGuilds.Request(store, nil, nil, &guilds)
if err != nil {
handleRedirect(ctx)
return
}
store.Set("has_guilds", true)
var wrappedGuilds []database.UserGuild
// endpoint's partial guild doesn't include ownerid
// we only user cached guilds on the index page, so it doesn't matter if we don't have have the real owner id
// if the user isn't the owner, as we pull from the cache on other endpoints
for _, guild := range guilds {
if guild.Owner {
guild.OwnerId = currentUser.Id
}
wrappedGuilds = append(wrappedGuilds, database.UserGuild{
GuildId: guild.Id,
Name: guild.Name,
Owner: guild.Owner,
UserPermissions: int32(guild.Permissions),
})
}
if err := dbclient.Client.UserGuilds.Set(currentUser.Id, wrappedGuilds); err != nil {
log.Error(err.Error())
}
handleRedirect(ctx)
// Cache guilds because Discord takes like 2 whole seconds to return then
go func() {
var guilds []guild.Guild
err, _ = userEndpoint.CurrentUserGuilds.Request(store, nil, nil, &guilds)
if err != nil {
log.Error(err.Error())
return
}
var wrappedGuilds []database.UserGuild
// endpoint's partial guild doesn't include ownerid
// we only user cached guilds on the index page, so it doesn't matter if we don't have have the real owner id
// if the user isn't the owner, as we pull from the cache on other endpoints
for _, guild := range guilds {
if guild.Owner {
guild.OwnerId = currentUser.Id
}
wrappedGuilds = append(wrappedGuilds, database.UserGuild{
GuildId: guild.Id,
Name: guild.Name,
Owner: guild.Owner,
UserPermissions: int32(guild.Permissions),
})
}
if err := dbclient.Client.UserGuilds.Set(currentUser.Id, wrappedGuilds); err != nil {
log.Error(err.Error())
}
}()
}
func handleRedirect(ctx *gin.Context) {

View File

@ -1,14 +1,22 @@
package root
import (
"fmt"
"github.com/TicketsBot/GoPanel/config"
"github.com/gin-gonic/contrib/sessions"
"github.com/gin-gonic/gin"
"net/url"
)
func IndexHandler(ctx *gin.Context) {
store := sessions.Default(ctx)
if _, hasGuilds := store.Get("has_guilds").(bool); !hasGuilds {
redirect := url.QueryEscape(config.Conf.Oauth.RedirectUri)
ctx.Redirect(302, fmt.Sprintf("https://discordapp.com/oauth2/authorize?response_type=code&redirect_uri=%s&scope=identify+guilds&client_id=%d&state=%s", redirect, config.Conf.Oauth.Id, ctx.Query("state")))
return
}
ctx.HTML(200, "main/index", gin.H{
"name": store.Get("name").(string),
"baseurl": config.Conf.Server.BaseUrl,