diff --git a/app/http/endpoints/root/callback.go b/app/http/endpoints/root/callback.go index 4aaaa9a..a2c375b 100644 --- a/app/http/endpoints/root/callback.go +++ b/app/http/endpoints/root/callback.go @@ -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) { diff --git a/app/http/endpoints/root/index.go b/app/http/endpoints/root/index.go index 2ec8fcb..4214cd7 100644 --- a/app/http/endpoints/root/index.go +++ b/app/http/endpoints/root/index.go @@ -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,