From 66add26714bb5c75ce96f7b8d6bff6bbd4e42eef Mon Sep 17 00:00:00 2001 From: Dot-Rar Date: Tue, 14 Apr 2020 17:01:50 +0100 Subject: [PATCH] some optimizations --- app/http/endpoints/manage/blacklist.go | 13 +- app/http/endpoints/manage/blacklistremove.go | 11 +- app/http/endpoints/manage/logs.go | 11 +- app/http/endpoints/manage/panelcreate.go | 36 ++-- app/http/endpoints/manage/paneldelete.go | 11 +- app/http/endpoints/manage/panels.go | 48 +----- app/http/endpoints/manage/sendmessage.go | 22 +-- app/http/endpoints/manage/settings.go | 78 ++++----- app/http/endpoints/manage/ticketclose.go | 15 +- app/http/endpoints/manage/ticketlist.go | 11 +- app/http/endpoints/manage/ticketview.go | 52 +++--- app/http/endpoints/manage/updatesettings.go | 59 +++---- app/http/endpoints/manage/viewlog.go | 11 +- app/http/endpoints/manage/webchatws.go | 24 +-- app/http/endpoints/root/callback.go | 43 +---- app/http/endpoints/root/index.go | 19 ++- cache/guildobjectcache.go | 49 ------ cache/uriparser.go | 21 --- cmd/panel/main.go | 12 +- config.toml.example | 3 + config/config.go | 161 +++++++++--------- database/table/channels.go | 56 ------ database/table/guilds.go | 77 ++++----- database/table/rolescache.go | 47 ----- go.mod | 24 +++ {cache => messagequeue}/panelcreate.go | 2 +- {cache => messagequeue}/redis.go | 50 +++--- {cache => messagequeue}/ticketclose.go | 2 +- {cache => messagequeue}/webchat.go | 2 +- public/templates/views/panels.tmpl | 20 ++- public/templates/views/settings.tmpl | 4 +- rpc/cache/cache.go | 33 ++++ rpc/ratelimit/ratelimit.go | 8 + .../endpoints/channel/CreateMessage.go | 18 -- .../endpoints/channel/GetChannelMessages.go | 14 -- utils/discord/endpoints/guild/getGuild.go | 15 -- .../endpoints/guild/getGuildChannels.go | 14 -- .../discord/endpoints/guild/getGuildMember.go | 14 -- .../endpoints/webhooks/executewebhook.go | 22 --- utils/discord/objects/activity.go | 16 -- utils/discord/objects/allowedmention.go | 7 - utils/discord/objects/allowedmentiontype.go | 9 - utils/discord/objects/asset.go | 8 - utils/discord/objects/attachment.go | 11 -- utils/discord/objects/channel.go | 22 --- utils/discord/objects/clientstatus.go | 7 - utils/discord/objects/embed.go | 17 -- utils/discord/objects/embedauthor.go | 8 - utils/discord/objects/embedfield.go | 7 - utils/discord/objects/embedfooter.go | 7 - utils/discord/objects/embedimage.go | 8 - utils/discord/objects/embedprovider.go | 6 - utils/discord/objects/embedthumbnail.go | 8 - utils/discord/objects/embedvideo.go | 7 - utils/discord/objects/emoji.go | 11 -- utils/discord/objects/guild.go | 44 +---- utils/discord/objects/member.go | 12 -- utils/discord/objects/message.go | 25 --- utils/discord/objects/messageactivity.go | 6 - utils/discord/objects/messageapplication.go | 9 - utils/discord/objects/overwrite.go | 8 - utils/discord/objects/party.go | 6 - utils/discord/objects/presence.go | 11 -- utils/discord/objects/reaction.go | 7 - utils/discord/objects/role.go | 12 -- utils/discord/objects/secret.go | 7 - utils/discord/objects/timestamp.go | 6 - utils/discord/objects/voicestate.go | 14 -- utils/permissionutils.go | 55 +++--- utils/premiumutils.go | 48 ++---- utils/sliceutils.go | 85 ++++----- 71 files changed, 464 insertions(+), 1182 deletions(-) delete mode 100644 cache/guildobjectcache.go delete mode 100644 cache/uriparser.go delete mode 100644 database/table/channels.go delete mode 100644 database/table/rolescache.go create mode 100644 go.mod rename {cache => messagequeue}/panelcreate.go (93%) rename {cache => messagequeue}/redis.go (91%) rename {cache => messagequeue}/ticketclose.go (95%) rename {cache => messagequeue}/webchat.go (96%) create mode 100644 rpc/cache/cache.go create mode 100644 rpc/ratelimit/ratelimit.go delete mode 100644 utils/discord/endpoints/channel/CreateMessage.go delete mode 100644 utils/discord/endpoints/channel/GetChannelMessages.go delete mode 100644 utils/discord/endpoints/guild/getGuild.go delete mode 100644 utils/discord/endpoints/guild/getGuildChannels.go delete mode 100644 utils/discord/endpoints/guild/getGuildMember.go delete mode 100644 utils/discord/endpoints/webhooks/executewebhook.go delete mode 100644 utils/discord/objects/activity.go delete mode 100644 utils/discord/objects/allowedmention.go delete mode 100644 utils/discord/objects/allowedmentiontype.go delete mode 100644 utils/discord/objects/asset.go delete mode 100644 utils/discord/objects/attachment.go delete mode 100644 utils/discord/objects/channel.go delete mode 100644 utils/discord/objects/clientstatus.go delete mode 100644 utils/discord/objects/embed.go delete mode 100644 utils/discord/objects/embedauthor.go delete mode 100644 utils/discord/objects/embedfield.go delete mode 100644 utils/discord/objects/embedfooter.go delete mode 100644 utils/discord/objects/embedimage.go delete mode 100644 utils/discord/objects/embedprovider.go delete mode 100644 utils/discord/objects/embedthumbnail.go delete mode 100644 utils/discord/objects/embedvideo.go delete mode 100644 utils/discord/objects/emoji.go delete mode 100644 utils/discord/objects/member.go delete mode 100644 utils/discord/objects/message.go delete mode 100644 utils/discord/objects/messageactivity.go delete mode 100644 utils/discord/objects/messageapplication.go delete mode 100644 utils/discord/objects/overwrite.go delete mode 100644 utils/discord/objects/party.go delete mode 100644 utils/discord/objects/presence.go delete mode 100644 utils/discord/objects/reaction.go delete mode 100644 utils/discord/objects/role.go delete mode 100644 utils/discord/objects/secret.go delete mode 100644 utils/discord/objects/timestamp.go delete mode 100644 utils/discord/objects/voicestate.go diff --git a/app/http/endpoints/manage/blacklist.go b/app/http/endpoints/manage/blacklist.go index c6c36de..1516863 100644 --- a/app/http/endpoints/manage/blacklist.go +++ b/app/http/endpoints/manage/blacklist.go @@ -3,8 +3,8 @@ package manage import ( "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "strconv" @@ -18,7 +18,6 @@ func BlacklistHandler(ctx *gin.Context) { 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()) @@ -34,13 +33,7 @@ func BlacklistHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) @@ -79,7 +72,7 @@ func BlacklistHandler(ctx *gin.Context) { exists := targetId != 0 if exists { - if guild.OwnerId == strconv.Itoa(int(targetId)) || table.IsStaff(guildId, targetId) { // Prevent users from blacklisting staff + if guild.OwnerId == targetId || table.IsStaff(guildId, targetId) { // Prevent users from blacklisting staff isStaff = true } else { if !utils.Contains(blacklistedIds, targetId) { // Prevent duplicates diff --git a/app/http/endpoints/manage/blacklistremove.go b/app/http/endpoints/manage/blacklistremove.go index 12aa4df..90cd2e6 100644 --- a/app/http/endpoints/manage/blacklistremove.go +++ b/app/http/endpoints/manage/blacklistremove.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "strconv" @@ -19,7 +19,6 @@ func BlacklistRemoveHandler(ctx *gin.Context) { 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()) @@ -35,13 +34,7 @@ func BlacklistRemoveHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) diff --git a/app/http/endpoints/manage/logs.go b/app/http/endpoints/manage/logs.go index 89b4588..2ca9ebc 100644 --- a/app/http/endpoints/manage/logs.go +++ b/app/http/endpoints/manage/logs.go @@ -3,8 +3,8 @@ package manage import ( "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "strconv" @@ -18,7 +18,6 @@ func LogsHandler(ctx *gin.Context) { 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()) @@ -43,13 +42,7 @@ func LogsHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) diff --git a/app/http/endpoints/manage/panelcreate.go b/app/http/endpoints/manage/panelcreate.go index e686921..48411ba 100644 --- a/app/http/endpoints/manage/panelcreate.go +++ b/app/http/endpoints/manage/panelcreate.go @@ -2,13 +2,14 @@ package manage import ( "fmt" - "github.com/TicketsBot/GoPanel/cache" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/messagequeue" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" + "github.com/rxdn/gdl/objects/channel" "strconv" "strings" ) @@ -21,7 +22,6 @@ func PanelCreateHandler(ctx *gin.Context) { 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()) @@ -37,13 +37,7 @@ func PanelCreateHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) @@ -150,7 +144,7 @@ func PanelCreateHandler(ctx *gin.Context) { ReactionEmote: emoji, } - go cache.Client.PublishPanelCreate(settings) + go messagequeue.Client.PublishPanelCreate(settings) ctx.Redirect(302, fmt.Sprintf("/manage/%d/panels?created=true&validColour=%t", guildId, validColour)) } else { @@ -159,15 +153,10 @@ func PanelCreateHandler(ctx *gin.Context) { } func validateChannel(guildId, channelId uint64, res chan bool) { - // Get channels from DB - channelsChan := make(chan []table.Channel) - go table.GetCachedChannelsByGuild(guildId, channelsChan) - channels := <-channelsChan - // Compare channel IDs validChannel := false - for _, guildChannel := range channels { - if guildChannel.ChannelId == channelId { + for _, guildChannel := range cache.Instance.GetGuildChannels(guildId) { + if guildChannel.Id == channelId { validChannel = true break } @@ -177,15 +166,10 @@ func validateChannel(guildId, channelId uint64, res chan bool) { } func validateCategory(guildId, categoryId uint64, res chan bool) { - // Get channels from DB - categoriesChan := make(chan []table.Channel) - go table.GetCategories(guildId, categoriesChan) - categories := <-categoriesChan - - // Compare channel IDs + // Compare ch IDs validCategory := false - for _, category := range categories { - if category.ChannelId == categoryId { + for _, ch := range cache.Instance.GetGuildChannels(guildId) { + if ch.Type == channel.ChannelTypeGuildCategory && ch.Id == categoryId { validCategory = true break } diff --git a/app/http/endpoints/manage/paneldelete.go b/app/http/endpoints/manage/paneldelete.go index 3bbc0c9..403d752 100644 --- a/app/http/endpoints/manage/paneldelete.go +++ b/app/http/endpoints/manage/paneldelete.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "strconv" @@ -19,7 +19,6 @@ func PanelDeleteHandler(ctx *gin.Context) { 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()) @@ -41,13 +40,7 @@ func PanelDeleteHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) diff --git a/app/http/endpoints/manage/panels.go b/app/http/endpoints/manage/panels.go index 44b4ec0..e3d5f69 100644 --- a/app/http/endpoints/manage/panels.go +++ b/app/http/endpoints/manage/panels.go @@ -3,8 +3,8 @@ package manage import ( "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "strconv" @@ -26,7 +26,6 @@ func PanelHandler(ctx *gin.Context) { 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()) @@ -42,13 +41,7 @@ func PanelHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) @@ -64,12 +57,7 @@ func PanelHandler(ctx *gin.Context) { panels := <-panelChan // Get channels - channelsChan := make(chan []table.Channel) - go table.GetCachedChannelsByGuild(guildId, channelsChan) - channels := <-channelsChan - - // Get default panel settings - settings := table.GetPanelSettings(guildId) + channels := cache.Instance.GetGuildChannels(guildId) // Convert to wrapped panels wrappedPanels := make([]wrappedPanel, 0) @@ -81,18 +69,11 @@ func PanelHandler(ctx *gin.Context) { CategoryName: "", } - if panel.Title == "" { - wrapper.Title = settings.Title - } - if panel.Content == "" { - wrapper.Content = settings.Content - } - // Get channel name & category name for _, guildChannel := range channels { - if guildChannel.ChannelId == panel.ChannelId { + if guildChannel.Id == panel.ChannelId { wrapper.ChannelName = guildChannel.Name - } else if guildChannel.ChannelId == panel.TargetCategory { + } else if guildChannel.Id == panel.TargetCategory { wrapper.CategoryName = guildChannel.Name } } @@ -100,22 +81,6 @@ func PanelHandler(ctx *gin.Context) { wrappedPanels = append(wrappedPanels, wrapper) } - // Format channels to be text channels only - channelMap := make(map[uint64]string) - for _, channel := range channels { - if channel.Type == 0 { - channelMap[channel.ChannelId] = channel.Name - } - } - - // Get categories & format - categories := make(map[uint64]string) - for _, channel := range channels { - if channel.Type == 4 { - categories[channel.ChannelId] = channel.Name - } - } - // Get is premium isPremiumChan := make(chan bool) go utils.IsPremiumGuild(store, guildId, isPremiumChan) @@ -130,8 +95,7 @@ func PanelHandler(ctx *gin.Context) { "panelcount": len(panels), "premium": isPremium, "panels": wrappedPanels, - "channels": channelMap, - "categories": categories, + "channels": channels, "validTitle": ctx.Query("validTitle") != "true", "validContent": ctx.Query("validContent") != "false", diff --git a/app/http/endpoints/manage/sendmessage.go b/app/http/endpoints/manage/sendmessage.go index 1bd774d..ad24c41 100644 --- a/app/http/endpoints/manage/sendmessage.go +++ b/app/http/endpoints/manage/sendmessage.go @@ -4,12 +4,12 @@ import ( "fmt" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" + "github.com/TicketsBot/GoPanel/rpc/ratelimit" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord" - "github.com/TicketsBot/GoPanel/utils/discord/endpoints/channel" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" + "github.com/rxdn/gdl/rest" "strconv" ) @@ -21,7 +21,6 @@ func SendMessage(ctx *gin.Context) { 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()) @@ -37,13 +36,7 @@ func SendMessage(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) @@ -65,18 +58,13 @@ func SendMessage(ctx *gin.Context) { return } - contentType := discord.ApplicationJson - if exists { content := fmt.Sprintf("**%s**: %s", store.Get("name").(string), ctx.PostForm("message")) if len(content) > 2000 { content = content[0:1999] } - endpoint := channel.CreateMessage(int(ticket.Channel)) - err, _ = endpoint.Request(store, &contentType, channel.CreateMessageBody{ - Content: content, - }, nil) + _, _ = rest.CreateMessage(config.Conf.Bot.Token, ratelimit.Ratelimiter, ticket.Channel, rest.CreateMessageData{Content: content}) } } else { ctx.Redirect(302, "/login") diff --git a/app/http/endpoints/manage/settings.go b/app/http/endpoints/manage/settings.go index fe83f34..25a0766 100644 --- a/app/http/endpoints/manage/settings.go +++ b/app/http/endpoints/manage/settings.go @@ -2,13 +2,13 @@ package manage import ( "fmt" - "github.com/TicketsBot/GoPanel/cache" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" + "github.com/rxdn/gdl/objects/channel" "strconv" ) @@ -20,7 +20,6 @@ func SettingsHandler(ctx *gin.Context) { 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()) @@ -36,21 +35,14 @@ func SettingsHandler(ctx *gin.Context) { } // Check the bot is in the guild - isInGuild := make(chan bool) - go cache.Client.GuildExists(guildIdStr, isInGuild) - if !<-isInGuild { + _, isInGuild := cache.Instance.GetGuild(guildId, false) + if !isInGuild { ctx.Redirect(302, fmt.Sprintf("https://invite.ticketsbot.net/?guild_id=%s&disable_guild_select=true&response_type=code&scope=bot%%20identify&redirect_uri=%s", guildIdStr, config.Conf.Server.BaseUrl)) return } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) @@ -72,21 +64,15 @@ func SettingsHandler(ctx *gin.Context) { go table.GetTicketNamingScheme(guildId, namingSchemeChan) namingScheme := <-namingSchemeChan - // /users/@me/guilds doesn't return channels, so we have to get them for the specific guild - channelsChan := make(chan []table.Channel) - go table.GetCachedChannelsByGuild(guildId, channelsChan) - channels := <-channelsChan + // get guild channels from cache + channels := cache.Instance.GetGuildChannels(guildId) - // Get a list of actual category IDs - categoriesChan := make(chan []table.Channel) - go table.GetCategories(guildId, categoriesChan) - categories := <-categoriesChan - - // Archive channel - // Create a list of IDs - var channelIds []string - for _, c := range channels { - channelIds = append(channelIds, strconv.Itoa(int(c.ChannelId))) + // separate out categories + categories := make([]channel.Channel, 0) + for _, ch := range channels { + if ch.Type == channel.ChannelTypeGuildCategory { + categories = append(categories, ch) + } } panelSettings := table.GetPanelSettings(guildId) @@ -101,26 +87,26 @@ func SettingsHandler(ctx *gin.Context) { invalidTicketLimit := ctx.Query("validTicketLimit") == "false" ctx.HTML(200, "manage/settings", gin.H{ - "name": store.Get("name").(string), - "guildId": guildIdStr, - "avatar": store.Get("avatar").(string), - "prefix": prefix, - "welcomeMessage": welcomeMessage, - "ticketLimit": limit, - "categories": categories, - "activecategory": categoryId, - "channels": channels, - "archivechannel": archiveChannel, - "invalidPrefix": invalidPrefix, + "name": store.Get("name").(string), + "guildId": guildIdStr, + "avatar": store.Get("avatar").(string), + "prefix": prefix, + "welcomeMessage": welcomeMessage, + "ticketLimit": limit, + "categories": categories, + "activecategory": categoryId, + "channels": channels, + "archivechannel": archiveChannel, + "invalidPrefix": invalidPrefix, "invalidWelcomeMessage": invalidWelcomeMessage, - "invalidTicketLimit": invalidTicketLimit, - "csrf": store.Get("csrf").(string), - "pingEveryone": pingEveryone, - "paneltitle": panelSettings.Title, - "panelcontent": panelSettings.Content, - "panelcolour": strconv.FormatInt(int64(panelSettings.Colour), 16), - "usersCanClose": usersCanClose, - "namingScheme": string(namingScheme), + "invalidTicketLimit": invalidTicketLimit, + "csrf": store.Get("csrf").(string), + "pingEveryone": pingEveryone, + "paneltitle": panelSettings.Title, + "panelcontent": panelSettings.Content, + "panelcolour": strconv.FormatInt(int64(panelSettings.Colour), 16), + "usersCanClose": usersCanClose, + "namingScheme": string(namingScheme), }) } else { ctx.Redirect(302, "/login") diff --git a/app/http/endpoints/manage/ticketclose.go b/app/http/endpoints/manage/ticketclose.go index 60df6f6..3aede35 100644 --- a/app/http/endpoints/manage/ticketclose.go +++ b/app/http/endpoints/manage/ticketclose.go @@ -2,11 +2,11 @@ package manage import ( "fmt" - "github.com/TicketsBot/GoPanel/cache" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/messagequeue" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "strconv" @@ -20,7 +20,6 @@ func TicketCloseHandler(ctx *gin.Context) { 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()) @@ -36,13 +35,7 @@ func TicketCloseHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) @@ -78,7 +71,7 @@ func TicketCloseHandler(ctx *gin.Context) { reason = reason[:255] } - go cache.Client.PublishTicketClose(ticket.Uuid, userId, reason) + go messagequeue.Client.PublishTicketClose(ticket.Uuid, userId, reason) ctx.Redirect(302, fmt.Sprintf("/manage/%d/tickets", guildId)) } else { diff --git a/app/http/endpoints/manage/ticketlist.go b/app/http/endpoints/manage/ticketlist.go index 86d831a..06b033b 100644 --- a/app/http/endpoints/manage/ticketlist.go +++ b/app/http/endpoints/manage/ticketlist.go @@ -3,8 +3,8 @@ package manage import ( "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "strconv" @@ -19,7 +19,6 @@ func TicketListHandler(ctx *gin.Context) { 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()) @@ -35,13 +34,7 @@ func TicketListHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) diff --git a/app/http/endpoints/manage/ticketview.go b/app/http/endpoints/manage/ticketview.go index adf7f95..efa7f23 100644 --- a/app/http/endpoints/manage/ticketview.go +++ b/app/http/endpoints/manage/ticketview.go @@ -4,11 +4,12 @@ import ( "fmt" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" + "github.com/TicketsBot/GoPanel/rpc/ratelimit" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/endpoints/channel" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" + "github.com/rxdn/gdl/rest" "regexp" "strconv" "strings" @@ -24,7 +25,6 @@ func TicketViewHandler(ctx *gin.Context) { 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()) @@ -40,13 +40,7 @@ func TicketViewHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) @@ -76,16 +70,7 @@ func TicketViewHandler(ctx *gin.Context) { } // Get messages - var messages []objects.Message - // We want to show users error messages so they can report them - isError := false - var errorMessage string - - endpoint := channel.GetChannelMessages(int(ticket.Channel)) - if err, _ = endpoint.Request(store, nil, nil, &messages); err != nil { - isError = true - errorMessage = err.Error() - } + messages, err := rest.GetChannelMessages(config.Conf.Bot.Token, ratelimit.Ratelimiter, ticket.Channel, rest.GetChannelMessagesData{Limit: 100}) // Format messages, exclude unneeded data var messagesFormatted []map[string]interface{} @@ -96,7 +81,8 @@ func TicketViewHandler(ctx *gin.Context) { match := MentionRegex.FindAllStringSubmatch(content, -1) for _, mention := range match { if len(mention) >= 2 { - mentionedId, err := strconv.ParseUint(mention[1], 10, 64); if err != nil { + mentionedId, err := strconv.ParseUint(mention[1], 10, 64) + if err != nil { continue } @@ -108,7 +94,7 @@ func TicketViewHandler(ctx *gin.Context) { messagesFormatted = append(messagesFormatted, map[string]interface{}{ "username": message.Author.Username, - "content": content, + "content": content, }) } @@ -116,18 +102,18 @@ func TicketViewHandler(ctx *gin.Context) { go utils.IsPremiumGuild(store, guildId, premium) ctx.HTML(200, "manage/ticketview", gin.H{ - "name": store.Get("name").(string), - "guildId": guildIdStr, - "csrf": store.Get("csrf").(string), - "avatar": store.Get("avatar").(string), - "baseUrl": config.Conf.Server.BaseUrl, - "isError": isError, - "error": errorMessage, - "messages": messagesFormatted, - "ticketId": ticket.TicketId, - "uuid": ticket.Uuid, + "name": store.Get("name").(string), + "guildId": guildIdStr, + "csrf": store.Get("csrf").(string), + "avatar": store.Get("avatar").(string), + "baseUrl": config.Conf.Server.BaseUrl, + "isError": err != nil, + "error": err.Error(), + "messages": messagesFormatted, + "ticketId": ticket.TicketId, + "uuid": ticket.Uuid, "include_mock": true, - "premium": <-premium, + "premium": <-premium, }) } else { ctx.Redirect(302, "/login") diff --git a/app/http/endpoints/manage/updatesettings.go b/app/http/endpoints/manage/updatesettings.go index 6c8883d..88c39ce 100644 --- a/app/http/endpoints/manage/updatesettings.go +++ b/app/http/endpoints/manage/updatesettings.go @@ -4,10 +4,11 @@ import ( "fmt" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" + "github.com/rxdn/gdl/objects/channel" "strconv" ) @@ -19,7 +20,6 @@ func UpdateSettingsHandler(ctx *gin.Context) { 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()) @@ -35,13 +35,7 @@ func UpdateSettingsHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) @@ -95,39 +89,30 @@ func UpdateSettingsHandler(ctx *gin.Context) { table.UpdatePingEveryone(guildId, pingEveryone) // Get a list of actual category IDs - categories := make(chan []table.Channel) - go table.GetCategories(guildId, categories) - - // Convert to category IDs - var categoryIds []string - for _, category := range <-categories { - categoryIds = append(categoryIds, strconv.Itoa(int(category.ChannelId))) - } + channels := cache.Instance.GetGuildChannels(guildId) // Update category - categoryStr := ctx.PostForm("category") - if utils.Contains(categoryIds, categoryStr) { - // Error is impossible, as we check it's a valid channel already - category, _ := strconv.ParseUint(categoryStr, 10, 64) - table.UpdateChannelCategory(guildId, category) + if categoryId, err := strconv.ParseUint(ctx.PostForm("category"), 10, 64); err == nil { + for _, ch := range channels { + if ch.Id == categoryId { // compare ID + if ch.Type == channel.ChannelTypeGuildCategory { // verify we're dealing with a category + table.UpdateChannelCategory(guildId, categoryId) + } + break + } + } } // Archive channel - // Create a list of IDs - channelsChan := make(chan []table.Channel) - go table.GetCachedChannelsByGuild(guildId, channelsChan) - channels := <-channelsChan - - var channelIds []uint64 - for _, channel := range channels { - channelIds = append(channelIds, channel.ChannelId) - } - - // Update or archive channel - archiveChannelStr := ctx.PostForm("archivechannel") - archiveChannelId, err := strconv.ParseUint(archiveChannelStr, 10, 64) - if err == nil && utils.Contains(channelIds, archiveChannelId) { - table.UpdateArchiveChannel(guildId, archiveChannelId) + if archiveChannelId, err := strconv.ParseUint(ctx.PostForm("archivechannel"), 10, 64); err == nil { + for _, ch := range channels { + if ch.Id == archiveChannelId { // compare ID + if ch.Type == channel.ChannelTypeGuildText { // verify channel type + table.UpdateArchiveChannel(guildId, archiveChannelId) + } + break + } + } } // Users can close diff --git a/app/http/endpoints/manage/viewlog.go b/app/http/endpoints/manage/viewlog.go index 6187060..ecdb8be 100644 --- a/app/http/endpoints/manage/viewlog.go +++ b/app/http/endpoints/manage/viewlog.go @@ -4,8 +4,8 @@ import ( "fmt" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "io/ioutil" @@ -22,7 +22,6 @@ func LogViewHandler(ctx *gin.Context) { 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()) @@ -38,13 +37,7 @@ func LogViewHandler(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildIdStr { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildId, false) // Verify the user has permissions to be here isAdmin := make(chan bool) diff --git a/app/http/endpoints/manage/webchatws.go b/app/http/endpoints/manage/webchatws.go index 0a4f1ed..b66bd73 100644 --- a/app/http/endpoints/manage/webchatws.go +++ b/app/http/endpoints/manage/webchatws.go @@ -4,14 +4,15 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/rpc/cache" + "github.com/TicketsBot/GoPanel/rpc/ratelimit" "github.com/TicketsBot/GoPanel/utils" - "github.com/TicketsBot/GoPanel/utils/discord" - "github.com/TicketsBot/GoPanel/utils/discord/endpoints/channel" - "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" + "github.com/rxdn/gdl/rest" "net/http" "strconv" "sync" @@ -84,8 +85,6 @@ func WebChatWs(ctx *gin.Context) { SocketsLock.Lock() Sockets = append(Sockets, socket) SocketsLock.Unlock() - - userIdStr := store.Get("userid").(string) userId, err := utils.GetUserId(store) if err != nil { conn.Close() @@ -127,13 +126,7 @@ func WebChatWs(ctx *gin.Context) { } // Get object for selected guild - var guild objects.Guild - for _, g := range table.GetGuilds(userIdStr) { - if g.Id == guildId { - guild = g - break - } - } + guild, _ := cache.Instance.GetGuild(guildIdParsed, false) // Verify the user has permissions to be here isAdmin := make(chan bool) @@ -164,8 +157,6 @@ func WebChatWs(ctx *gin.Context) { ticket := <-ticketChan exists := ticket != table.Ticket{} - contentType := discord.ApplicationJson - if exists { content := data if len(content) > 2000 { @@ -188,10 +179,7 @@ func WebChatWs(ctx *gin.Context) { content = content[0:1999] } - endpoint := channel.CreateMessage(int(ticket.Channel)) - err, _ = endpoint.Request(store, &contentType, channel.CreateMessageBody{ - Content: content, - }, nil) + _, _ = rest.CreateMessage(config.Conf.Bot.Token, ratelimit.Ratelimiter, ticket.Channel, rest.CreateMessageData{Content: content}) } } } diff --git a/app/http/endpoints/root/callback.go b/app/http/endpoints/root/callback.go index 5bb3598..d546b0d 100644 --- a/app/http/endpoints/root/callback.go +++ b/app/http/endpoints/root/callback.go @@ -4,17 +4,16 @@ import ( "encoding/base64" "encoding/json" "fmt" - "github.com/TicketsBot/GoPanel/cache" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" "github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/GoPanel/utils/discord" - "github.com/TicketsBot/GoPanel/utils/discord/endpoints/user" + userEndpoint "github.com/TicketsBot/GoPanel/utils/discord/endpoints/user" "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/apex/log" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" - "strconv" + "github.com/rxdn/gdl/objects/user" "time" ) @@ -65,8 +64,8 @@ func CallbackHandler(ctx *gin.Context) { store.Set("expiry", (time.Now().UnixNano()/int64(time.Second))+int64(res.ExpiresIn)) // Get ID + name - var currentUser objects.User - err, _ = user.CurrentUser.Request(store, nil, nil, ¤tUser) + var currentUser user.User + err, _ = userEndpoint.CurrentUser.Request(store, nil, nil, ¤tUser) if err != nil { ctx.String(500, err.Error()) return @@ -83,28 +82,14 @@ func CallbackHandler(ctx *gin.Context) { ctx.Redirect(302, config.Conf.Server.BaseUrl) - userId, err := strconv.ParseUint(currentUser.Id, 10, 64); if err != nil { // ??? - return - } - // Cache guilds because Discord takes like 2 whole seconds to return then go func() { var guilds []objects.Guild - err, _ = user.CurrentUserGuilds.Request(store, nil, nil, &guilds) + err, _ = userEndpoint.CurrentUserGuilds.Request(store, nil, nil, &guilds) if err != nil { log.Error(err.Error()) return } - - for _, guild := range guilds { - go cache.Client.StoreGuild(guild) - - // cache roles - guildId, err := strconv.ParseUint(guild.Id, 10, 64); if err != nil { - continue - } - go cacheRoles(store, guildId, userId) - } marshalled, err := json.Marshal(guilds) if err != nil { @@ -112,21 +97,7 @@ func CallbackHandler(ctx *gin.Context) { return } + // TODO: unfuck this table.UpdateGuilds(currentUser.Id, base64.StdEncoding.EncodeToString(marshalled)) }() -} - -func cacheRoles(store sessions.Session, guildId, userId uint64) { - roles := utils.GetRolesRest(store, guildId, userId) - - if roles == nil { - return - } - - // Delete old roles - table.DeleteRoles(guildId, userId) - - for _, role := range *roles { - table.CacheRole(guildId, userId, role) - } -} +} \ No newline at end of file diff --git a/app/http/endpoints/root/index.go b/app/http/endpoints/root/index.go index 0817cdd..1153e45 100644 --- a/app/http/endpoints/root/index.go +++ b/app/http/endpoints/root/index.go @@ -7,6 +7,7 @@ import ( "github.com/TicketsBot/GoPanel/utils/discord/objects" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" + "github.com/rxdn/gdl/objects/guild" "strconv" ) @@ -18,25 +19,29 @@ func IndexHandler(ctx *gin.Context) { 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 } - userGuilds := table.GetGuilds(userIdStr) + userGuilds := table.GetGuilds(userId) adminGuilds := make([]objects.Guild, 0) - for _, guild := range userGuilds { - guildId, err := strconv.ParseUint(guild.Id, 10, 64) + for _, g := range userGuilds { + guildId, err := strconv.ParseUint(g.Id, 10, 64) if err != nil { // I think this happens when a server was deleted? We should just skip though. continue } + fakeGuild := guild.Guild{ + Owner: g.Owner, + Permissions: g.Permissions, + } + isAdmin := make(chan bool) - go utils.IsAdmin(guild, guildId, userId, isAdmin) + go utils.IsAdmin(fakeGuild, guildId, userId, isAdmin) if <-isAdmin { - adminGuilds = append(adminGuilds, guild) + adminGuilds = append(adminGuilds, g) } } @@ -46,7 +51,7 @@ func IndexHandler(ctx *gin.Context) { "servers": adminGuilds, "empty": len(adminGuilds) == 0, "isIndex": true, - "avatar": store.Get("avatar").(string), + "avatar": store.Get("avatar").(string), }) } else { ctx.Redirect(302, "/login") diff --git a/cache/guildobjectcache.go b/cache/guildobjectcache.go deleted file mode 100644 index 680c127..0000000 --- a/cache/guildobjectcache.go +++ /dev/null @@ -1,49 +0,0 @@ -package cache - -import ( - "fmt" - "github.com/TicketsBot/GoPanel/utils/discord/objects" - "github.com/apex/log" - "github.com/vmihailenco/msgpack" - "time" -) - -func (c *RedisClient) StoreGuild(guild objects.Guild) { - packed, err := msgpack.Marshal(guild) - if err != nil { - log.Error(err.Error()) - return - } - - key := fmt.Sprintf("ticketspanel:guilds:%s", string(packed)) - c.Set(key, string(packed), time.Hour*48) -} - -func (c *RedisClient) GetGuildByID(guildId string, res chan *objects.Guild) { - key := fmt.Sprintf("ticketspanel:guilds:%s", guildId) - packed, err := c.Get(key).Result() - - if err != nil { - res <- nil - } else { - var unpacked objects.Guild - if err = msgpack.Unmarshal([]byte(packed), &unpacked); err != nil { - log.Error(err.Error()) - res <- nil - } else { - res <- &unpacked - } - } -} - -func (c *RedisClient) GuildExists(guildId string, res chan bool) { - key := fmt.Sprintf("tickets:guilds:%s", guildId) - - intResult, err := c.Exists(key).Result() - if err != nil { - res <- false - return - } - - res <- intResult == 1 -} diff --git a/cache/uriparser.go b/cache/uriparser.go deleted file mode 100644 index 679d08b..0000000 --- a/cache/uriparser.go +++ /dev/null @@ -1,21 +0,0 @@ -package cache - -import "net/url" - -type RedisURI struct { - Addr string - Password string -} - -func ParseURI(raw string) RedisURI { - parsed, err := url.Parse(raw); if err != nil { - panic(err) - } - - passwd, _ := parsed.User.Password() - - return RedisURI{ - Addr: parsed.Host, - Password: passwd, - } -} diff --git a/cmd/panel/main.go b/cmd/panel/main.go index 7f99e21..08155c0 100644 --- a/cmd/panel/main.go +++ b/cmd/panel/main.go @@ -6,9 +6,10 @@ import ( "fmt" "github.com/TicketsBot/GoPanel/app/http" "github.com/TicketsBot/GoPanel/app/http/endpoints/manage" - "github.com/TicketsBot/GoPanel/cache" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/messagequeue" + "github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/utils" "github.com/apex/log" "math/rand" @@ -27,17 +28,18 @@ func main() { config.LoadConfig() database.ConnectToDatabase() + cache.Instance = cache.NewCache() utils.LoadEmoji() - cache.Client = cache.NewRedisClient() - go Listen(cache.Client) + messagequeue.Client = messagequeue.NewRedisClient() + go Listen(messagequeue.Client) http.StartServer() } -func Listen(client cache.RedisClient) { - ch := make(chan cache.TicketMessage) +func Listen(client messagequeue.RedisClient) { + ch := make(chan messagequeue.TicketMessage) go client.ListenForMessages(ch) for decoded := range ch { diff --git a/config.toml.example b/config.toml.example index 997e378..1e273bc 100644 --- a/config.toml.example +++ b/config.toml.example @@ -33,3 +33,6 @@ host="127.0.0.1" port=6379 password="" threads=5 + +[cache] +uri="postgres://pwd:user@localhost:5432/db" diff --git a/config/config.go b/config/config.go index 3aecec0..8972071 100644 --- a/config/config.go +++ b/config/config.go @@ -1,78 +1,83 @@ -package config - -import ( - "github.com/BurntSushi/toml" - "io/ioutil" -) - -type ( - Config struct { - Admins []string - Server Server - Oauth Oauth - MariaDB MariaDB - Bot Bot - Redis Redis - } - - Server struct { - Host string - BaseUrl string - MainSite string - Ratelimit Ratelimit - Session Session - } - - Ratelimit struct { - Window int - Max int - } - - Session struct { - Threads int - Secret string - } - - Oauth struct { - Id int64 - Secret string - RedirectUri string - } - - MariaDB struct { - Host string - Username string - Password string - Database string - Threads int - } - - Bot struct { - Token string - PremiumLookupProxyUrl string `toml:"premium-lookup-proxy-url"` - PremiumLookupProxyKey string `toml:"premium-lookup-proxy-key"` - } - - Redis struct { - Host string - Port int - Password string - Threads int - } -) - -var ( - Conf Config -) - -func LoadConfig() { - raw, err := ioutil.ReadFile("config.toml") - if err != nil { - panic(err) - } - - _, err = toml.Decode(string(raw), &Conf) - if err != nil { - panic(err) - } -} +package config + +import ( + "github.com/BurntSushi/toml" + "io/ioutil" +) + +type ( + Config struct { + Admins []string + Server Server + Oauth Oauth + MariaDB MariaDB + Bot Bot + Redis Redis + Cache Cache + } + + Server struct { + Host string + BaseUrl string + MainSite string + Ratelimit Ratelimit + Session Session + } + + Ratelimit struct { + Window int + Max int + } + + Session struct { + Threads int + Secret string + } + + Oauth struct { + Id int64 + Secret string + RedirectUri string + } + + MariaDB struct { + Host string + Username string + Password string + Database string + Threads int + } + + Bot struct { + Token string + PremiumLookupProxyUrl string `toml:"premium-lookup-proxy-url"` + PremiumLookupProxyKey string `toml:"premium-lookup-proxy-key"` + } + + Redis struct { + Host string + Port int + Password string + Threads int + } + + Cache struct { + Uri string + } +) + +var ( + Conf Config +) + +func LoadConfig() { + raw, err := ioutil.ReadFile("config.toml") + if err != nil { + panic(err) + } + + _, err = toml.Decode(string(raw), &Conf) + if err != nil { + panic(err) + } +} diff --git a/database/table/channels.go b/database/table/channels.go deleted file mode 100644 index b4db35d..0000000 --- a/database/table/channels.go +++ /dev/null @@ -1,56 +0,0 @@ -package table - -import ( - "github.com/TicketsBot/GoPanel/database" -) - -type Channel struct { - ChannelId uint64 `gorm:"column:CHANNELID"` - GuildId uint64 `gorm:"column:GUILDID"` - Name string `gorm:"column:NAME;type:VARCHAR(32)"` - Type int `gorm:"column:CHANNELTYPE;type:TINYINT(1)"` -} - -func (Channel) TableName() string { - return "Channel" -} - -func StoreChannel(channelId, guildId uint64, name string, channelType int) { - channel := Channel{ - ChannelId: channelId, - GuildId: guildId, - Name: name, - Type: channelType, - } - - database.Database.Where(&Channel{ChannelId: channelId}).Assign(&channel).FirstOrCreate(&Channel{}) -} - -func DeleteChannel(channelId uint64) { - var node Channel - database.Database.Where(Channel{ChannelId: channelId}).Take(&node) - database.Database.Delete(&node) -} - -func GetCachedChannelsByGuild(guildId uint64, res chan []Channel) { - var nodes []Channel - database.Database.Where(Channel{GuildId: guildId}).Find(&nodes) - res <- nodes -} - -// Util function ig -func GetCategories(guildId uint64, res chan []Channel) { - channelsChan := make(chan []Channel) - go GetCachedChannelsByGuild(guildId, channelsChan) - channels := <-channelsChan - - var categories []Channel - - for _, channel := range channels { - if channel.Type == 4 { - categories = append(categories, channel) - } - } - - res <- categories -} diff --git a/database/table/guilds.go b/database/table/guilds.go index eb7409b..8156e6b 100644 --- a/database/table/guilds.go +++ b/database/table/guilds.go @@ -1,38 +1,39 @@ -package table - -import ( - "encoding/base64" - "encoding/json" - "github.com/TicketsBot/GoPanel/database" - "github.com/TicketsBot/GoPanel/utils/discord/objects" -) - -type GuildCache struct { - UserId string `gorm:"column:USERID;type:varchar(20)"` // Apparently I made this a VARCHAR in the JS version - Guilds string `gorm:"column:guilds;type:mediumtext"` -} - -func (GuildCache) TableName() string { - return "guildscache" -} - -func UpdateGuilds(userId string, guilds string) { - var cache GuildCache - database.Database.Where(&GuildCache{UserId: userId}).Assign(&GuildCache{Guilds: guilds}).FirstOrCreate(&cache) -} - -func GetGuilds(userId string) []objects.Guild { - var cache GuildCache - database.Database.Where(&GuildCache{UserId: userId}).First(&cache) - decoded, err := base64.StdEncoding.DecodeString(cache.Guilds) - if err != nil { - return make([]objects.Guild, 0) - } - - var guilds []objects.Guild - if err := json.Unmarshal(decoded, &guilds); err != nil { - return make([]objects.Guild, 0) - } - - return guilds -} +package table + +import ( + "encoding/base64" + "encoding/json" + "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/utils/discord/objects" +) + +type GuildCache struct { + UserId uint64 `gorm:"column:USERID"` + Guilds string `gorm:"column:guilds;type:mediumtext"` +} + +func (GuildCache) TableName() string { + return "guildscache" +} + +// this is horrible +func UpdateGuilds(userId uint64, guilds string) { + var cache GuildCache + database.Database.Where(&GuildCache{UserId: userId}).Assign(&GuildCache{Guilds: guilds}).FirstOrCreate(&cache) +} + +func GetGuilds(userId uint64) []objects.Guild { + var cache GuildCache + database.Database.Where(&GuildCache{UserId: userId}).First(&cache) + decoded, err := base64.StdEncoding.DecodeString(cache.Guilds) + if err != nil { + return make([]objects.Guild, 0) + } + + var guilds []objects.Guild + if err := json.Unmarshal(decoded, &guilds); err != nil { + return make([]objects.Guild, 0) + } + + return guilds +} diff --git a/database/table/rolescache.go b/database/table/rolescache.go deleted file mode 100644 index abad226..0000000 --- a/database/table/rolescache.go +++ /dev/null @@ -1,47 +0,0 @@ -package table - -import ( - "github.com/TicketsBot/GoPanel/database" -) - -type CachedRole struct { - AssociationId int `gorm:"column:ASSOCIATIONID;primary_key;auto_increment"` - GuildId uint64 `gorm:"column:GUILDID"` - UserId uint64 `gorm:"column:USERID"` - RoleId uint64 `gorm:"column:ROLEID"` -} - -func (CachedRole) TableName() string { - return "cache_roles" -} - -func DeleteRoles(guildId, userId uint64) { - database.Database.Where(CachedRole{ - GuildId: guildId, - UserId: userId, - }).Delete(CachedRole{}) -} - -// TODO: Cache invalidation -func CacheRole(guildId, userId, roleId uint64) { - database.Database.Create(&CachedRole{ - GuildId: guildId, - UserId: userId, - RoleId: roleId, - }) -} - -func GetCachedRoles(guildId, userId uint64, res chan []uint64) { - var rows []CachedRole - database.Database.Where(&CachedRole{ - GuildId: guildId, - UserId: userId, - }).Find(&rows) - - roles := make([]uint64, 0) - for _, row := range rows { - roles = append(roles, row.RoleId) - } - - res <- roles -} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b9f0abc --- /dev/null +++ b/go.mod @@ -0,0 +1,24 @@ +module github.com/TicketsBot/GoPanel + +go 1.14 + +require ( + github.com/BurntSushi/toml v0.3.1 + github.com/apex/log v1.1.2 + github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect + github.com/gin-contrib/multitemplate v0.0.0-20200226145339-3e397ee01bc6 + github.com/gin-contrib/static v0.0.0-20191128031702-f81c604d8ac2 + github.com/gin-gonic/contrib v0.0.0-20191209060500-d6e26eeaa607 + github.com/gin-gonic/gin v1.6.2 + github.com/go-redis/redis v6.15.7+incompatible + github.com/go-sql-driver/mysql v1.5.0 + github.com/gorilla/sessions v1.2.0 // indirect + github.com/gorilla/websocket v1.4.2 + github.com/jackc/pgx/v4 v4.6.0 + github.com/jinzhu/gorm v1.9.12 + github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c + github.com/pkg/errors v0.9.1 + github.com/robfig/go-cache v0.0.0-20130306151617-9fc39e0dbf62 + github.com/rxdn/gdl v0.0.0-20200410134146-c1b0871088c5 + github.com/vmihailenco/msgpack v4.0.4+incompatible +) diff --git a/cache/panelcreate.go b/messagequeue/panelcreate.go similarity index 93% rename from cache/panelcreate.go rename to messagequeue/panelcreate.go index dc08137..b079c52 100644 --- a/cache/panelcreate.go +++ b/messagequeue/panelcreate.go @@ -1,4 +1,4 @@ -package cache +package messagequeue import ( "encoding/json" diff --git a/cache/redis.go b/messagequeue/redis.go similarity index 91% rename from cache/redis.go rename to messagequeue/redis.go index a16d623..f33b329 100644 --- a/cache/redis.go +++ b/messagequeue/redis.go @@ -1,25 +1,25 @@ -package cache - -import ( - "fmt" - "github.com/TicketsBot/GoPanel/config" - "github.com/go-redis/redis" -) - -type RedisClient struct { - *redis.Client -} - -var Client RedisClient - -func NewRedisClient() RedisClient { - client := redis.NewClient(&redis.Options{ - Addr: fmt.Sprintf("%s:%d", config.Conf.Redis.Host, config.Conf.Redis.Port), - Password: config.Conf.Redis.Password, - PoolSize: config.Conf.Redis.Threads, - }) - - return RedisClient{ - client, - } -} +package messagequeue + +import ( + "fmt" + "github.com/TicketsBot/GoPanel/config" + "github.com/go-redis/redis" +) + +type RedisClient struct { + *redis.Client +} + +var Client RedisClient + +func NewRedisClient() RedisClient { + client := redis.NewClient(&redis.Options{ + Addr: fmt.Sprintf("%s:%d", config.Conf.Redis.Host, config.Conf.Redis.Port), + Password: config.Conf.Redis.Password, + PoolSize: config.Conf.Redis.Threads, + }) + + return RedisClient{ + client, + } +} diff --git a/cache/ticketclose.go b/messagequeue/ticketclose.go similarity index 95% rename from cache/ticketclose.go rename to messagequeue/ticketclose.go index d04180e..3e2f162 100644 --- a/cache/ticketclose.go +++ b/messagequeue/ticketclose.go @@ -1,4 +1,4 @@ -package cache +package messagequeue import ( "encoding/json" diff --git a/cache/webchat.go b/messagequeue/webchat.go similarity index 96% rename from cache/webchat.go rename to messagequeue/webchat.go index 601eb39..db89c84 100644 --- a/cache/webchat.go +++ b/messagequeue/webchat.go @@ -1,4 +1,4 @@ -package cache +package messagequeue import ( "encoding/json" diff --git a/public/templates/views/panels.tmpl b/public/templates/views/panels.tmpl index 61c6735..b48a50d 100644 --- a/public/templates/views/panels.tmpl +++ b/public/templates/views/panels.tmpl @@ -74,8 +74,10 @@
#
@@ -88,8 +90,10 @@
#
@@ -155,8 +159,9 @@
- Invalid panel colour. You must use the hex value of the colour, which you can find here. -
Colour has defaulted to green. + Invalid panel colour. You must use the hex value of the colour, which you can find here. +
Colour has defaulted to green.
{{end}} @@ -221,7 +226,8 @@
- You've hit your panel quota. Premium users can create unlimited panels. Click here to learn more about premium. + You've hit your panel quota. Premium users can create unlimited panels. Click here to learn more about premium.
{{end}} diff --git a/public/templates/views/settings.tmpl b/public/templates/views/settings.tmpl index a671446..2c56c8b 100644 --- a/public/templates/views/settings.tmpl +++ b/public/templates/views/settings.tmpl @@ -51,7 +51,7 @@ @@ -62,7 +62,7 @@ diff --git a/rpc/cache/cache.go b/rpc/cache/cache.go new file mode 100644 index 0000000..cbc6808 --- /dev/null +++ b/rpc/cache/cache.go @@ -0,0 +1,33 @@ +package cache + +import ( + "context" + "github.com/TicketsBot/GoPanel/config" + "github.com/jackc/pgx/v4/pgxpool" + gdlcache "github.com/rxdn/gdl/cache" +) + +type Cache struct { + *gdlcache.PgCache +} + +var Instance *Cache + +func NewCache() *Cache { + pool, err := pgxpool.Connect(context.Background(), config.Conf.Cache.Uri) + if err != nil { + panic(err) + } + + cache := gdlcache.NewPgCache(pool, gdlcache.CacheOptions{ + Guilds: true, + Users: true, + Members: true, + Channels: true, + Roles: true, + }) + + return &Cache{ + PgCache: &cache, + } +} diff --git a/rpc/ratelimit/ratelimit.go b/rpc/ratelimit/ratelimit.go new file mode 100644 index 0000000..72a00c6 --- /dev/null +++ b/rpc/ratelimit/ratelimit.go @@ -0,0 +1,8 @@ +package ratelimit + +import ( + "github.com/TicketsBot/GoPanel/messagequeue" + "github.com/rxdn/gdl/rest/ratelimit" +) + +var Ratelimiter = ratelimit.NewRateLimiter(ratelimit.NewRedisStore(messagequeue.Client.Client, "ratelimit")) // TODO: Use values from config diff --git a/utils/discord/endpoints/channel/CreateMessage.go b/utils/discord/endpoints/channel/CreateMessage.go deleted file mode 100644 index 37c1346..0000000 --- a/utils/discord/endpoints/channel/CreateMessage.go +++ /dev/null @@ -1,18 +0,0 @@ -package channel - -import ( - "fmt" - "github.com/TicketsBot/GoPanel/utils/discord" -) - -type CreateMessageBody struct { - Content string `json:"content"` -} - -func CreateMessage(id int) discord.Endpoint { - return discord.Endpoint{ - RequestType: discord.POST, - AuthorizationType: discord.BOT, - Endpoint: fmt.Sprintf("/channels/%d/messages", id), - } -} diff --git a/utils/discord/endpoints/channel/GetChannelMessages.go b/utils/discord/endpoints/channel/GetChannelMessages.go deleted file mode 100644 index adcb115..0000000 --- a/utils/discord/endpoints/channel/GetChannelMessages.go +++ /dev/null @@ -1,14 +0,0 @@ -package channel - -import ( - "fmt" - "github.com/TicketsBot/GoPanel/utils/discord" -) - -func GetChannelMessages(id int) discord.Endpoint { - return discord.Endpoint{ - RequestType: discord.GET, - AuthorizationType: discord.BOT, - Endpoint: fmt.Sprintf("/channels/%d/messages", id), - } -} diff --git a/utils/discord/endpoints/guild/getGuild.go b/utils/discord/endpoints/guild/getGuild.go deleted file mode 100644 index 195b192..0000000 --- a/utils/discord/endpoints/guild/getGuild.go +++ /dev/null @@ -1,15 +0,0 @@ -package guild - -import ( - "fmt" - "github.com/TicketsBot/GoPanel/utils/discord" - "strconv" -) - -func GetGuild(id int) discord.Endpoint { - return discord.Endpoint{ - RequestType: discord.GET, - AuthorizationType: discord.BOT, - Endpoint: fmt.Sprintf("/guilds/%s", strconv.Itoa(id)), - } -} diff --git a/utils/discord/endpoints/guild/getGuildChannels.go b/utils/discord/endpoints/guild/getGuildChannels.go deleted file mode 100644 index d7116a6..0000000 --- a/utils/discord/endpoints/guild/getGuildChannels.go +++ /dev/null @@ -1,14 +0,0 @@ -package guild - -import ( - "fmt" - "github.com/TicketsBot/GoPanel/utils/discord" -) - -func GetGuildChannels(id int) discord.Endpoint { - return discord.Endpoint{ - RequestType: discord.GET, - AuthorizationType: discord.BOT, - Endpoint: fmt.Sprintf("/guilds/%d/channels", id), - } -} diff --git a/utils/discord/endpoints/guild/getGuildMember.go b/utils/discord/endpoints/guild/getGuildMember.go deleted file mode 100644 index 310ff5f..0000000 --- a/utils/discord/endpoints/guild/getGuildMember.go +++ /dev/null @@ -1,14 +0,0 @@ -package guild - -import ( - "fmt" - "github.com/TicketsBot/GoPanel/utils/discord" -) - -func GetGuildMember(guildId, userId uint64) discord.Endpoint { - return discord.Endpoint{ - RequestType: discord.GET, - AuthorizationType: discord.BOT, - Endpoint: fmt.Sprintf("/guilds/%d/members/%d", guildId, userId), - } -} diff --git a/utils/discord/endpoints/webhooks/executewebhook.go b/utils/discord/endpoints/webhooks/executewebhook.go deleted file mode 100644 index a917921..0000000 --- a/utils/discord/endpoints/webhooks/executewebhook.go +++ /dev/null @@ -1,22 +0,0 @@ -package webhooks - -import ( - "fmt" - "github.com/TicketsBot/GoPanel/utils/discord" - "github.com/TicketsBot/GoPanel/utils/discord/objects" -) - -type ExecuteWebhookBody struct { - Content string `json:"content"` - Username string `json:"username"` - AvatarUrl string `json:"avatar_url"` - AllowedMentions objects.AllowedMention `json:"allowed_mentions"` -} - -func ExecuteWebhook(webhook string) discord.Endpoint { - return discord.Endpoint{ - RequestType: discord.POST, - AuthorizationType: discord.NONE, - Endpoint: fmt.Sprintf("/webhooks/%s?wait=true", webhook), - } -} diff --git a/utils/discord/objects/activity.go b/utils/discord/objects/activity.go deleted file mode 100644 index 611a0cb..0000000 --- a/utils/discord/objects/activity.go +++ /dev/null @@ -1,16 +0,0 @@ -package objects - -type Activity struct { - Name string - Type int - Url string - Timestamps Timestamp - ApplicationId string - Details string - State string - Party Party - Assets Asset - Secrets Secret - Instance bool - Flags int -} diff --git a/utils/discord/objects/allowedmention.go b/utils/discord/objects/allowedmention.go deleted file mode 100644 index 4f1eb5c..0000000 --- a/utils/discord/objects/allowedmention.go +++ /dev/null @@ -1,7 +0,0 @@ -package objects - -type AllowedMention struct { - Parse []AllowedMentionType - Roles []string - Users []string -} diff --git a/utils/discord/objects/allowedmentiontype.go b/utils/discord/objects/allowedmentiontype.go deleted file mode 100644 index f50f029..0000000 --- a/utils/discord/objects/allowedmentiontype.go +++ /dev/null @@ -1,9 +0,0 @@ -package objects - -type AllowedMentionType string - -const( - EVERYONE AllowedMentionType = "everyone" - USERS AllowedMentionType = "users" - ROLES AllowedMentionType = "roles" -) diff --git a/utils/discord/objects/asset.go b/utils/discord/objects/asset.go deleted file mode 100644 index a6ab4cf..0000000 --- a/utils/discord/objects/asset.go +++ /dev/null @@ -1,8 +0,0 @@ -package objects - -type Asset struct { - LargeImage string - LargeText string - SmallImage string - SmallText string -} diff --git a/utils/discord/objects/attachment.go b/utils/discord/objects/attachment.go deleted file mode 100644 index cfec781..0000000 --- a/utils/discord/objects/attachment.go +++ /dev/null @@ -1,11 +0,0 @@ -package objects - -type Attachment struct { - Id string - Filename string - Size int - url string - ProxyUrl string - height int - Width int -} diff --git a/utils/discord/objects/channel.go b/utils/discord/objects/channel.go deleted file mode 100644 index 45fdcd1..0000000 --- a/utils/discord/objects/channel.go +++ /dev/null @@ -1,22 +0,0 @@ -package objects - -type Channel struct { - Id string - Type int - GuildId string - Position int - PermissionsOverwrites []Overwrite - Name string - Topic string - Nsfw bool - LastMessageId string - Bitrate int - userLimit int - RateLimitPerUser int - Recipients []User - Icon string - Ownerid string - ApplicationId string - ParentId string - LastPinTimestamp string -} diff --git a/utils/discord/objects/clientstatus.go b/utils/discord/objects/clientstatus.go deleted file mode 100644 index a687b3d..0000000 --- a/utils/discord/objects/clientstatus.go +++ /dev/null @@ -1,7 +0,0 @@ -package objects - -type ClientStatus struct { - Desktop string - Mobile string - Web string -} diff --git a/utils/discord/objects/embed.go b/utils/discord/objects/embed.go deleted file mode 100644 index 1657843..0000000 --- a/utils/discord/objects/embed.go +++ /dev/null @@ -1,17 +0,0 @@ -package objects - -type Embed struct { - Title string - Type string - Description string - Url string - Timestamp string - Color int - Footer EmbedField - Image EmbedImage - Thumbnail EmbedThumbnail - Video EmbedVideo - Provider EmbedProvider - Author EmbedAuthor - Fields []EmbedField -} diff --git a/utils/discord/objects/embedauthor.go b/utils/discord/objects/embedauthor.go deleted file mode 100644 index 2bbf254..0000000 --- a/utils/discord/objects/embedauthor.go +++ /dev/null @@ -1,8 +0,0 @@ -package objects - -type EmbedAuthor struct { - Name string - Url string - IconUrl string - ProxyIconUrl string -} diff --git a/utils/discord/objects/embedfield.go b/utils/discord/objects/embedfield.go deleted file mode 100644 index 1bfb9d5..0000000 --- a/utils/discord/objects/embedfield.go +++ /dev/null @@ -1,7 +0,0 @@ -package objects - -type EmbedField struct { - Name string - Value string - Inline bool -} diff --git a/utils/discord/objects/embedfooter.go b/utils/discord/objects/embedfooter.go deleted file mode 100644 index b11368d..0000000 --- a/utils/discord/objects/embedfooter.go +++ /dev/null @@ -1,7 +0,0 @@ -package objects - -type EmbedFooter struct { - Text string - IconUrl string - ProxyIconUrl string -} diff --git a/utils/discord/objects/embedimage.go b/utils/discord/objects/embedimage.go deleted file mode 100644 index 6a4a941..0000000 --- a/utils/discord/objects/embedimage.go +++ /dev/null @@ -1,8 +0,0 @@ -package objects - -type EmbedImage struct { - Url string - ProxyUrl string - Height int - Width int -} diff --git a/utils/discord/objects/embedprovider.go b/utils/discord/objects/embedprovider.go deleted file mode 100644 index 9860e9d..0000000 --- a/utils/discord/objects/embedprovider.go +++ /dev/null @@ -1,6 +0,0 @@ -package objects - -type EmbedProvider struct { - Name string - Url string -} diff --git a/utils/discord/objects/embedthumbnail.go b/utils/discord/objects/embedthumbnail.go deleted file mode 100644 index dfefad1..0000000 --- a/utils/discord/objects/embedthumbnail.go +++ /dev/null @@ -1,8 +0,0 @@ -package objects - -type EmbedThumbnail struct { - Url string - ProxyUrl string - Height int - Width int -} diff --git a/utils/discord/objects/embedvideo.go b/utils/discord/objects/embedvideo.go deleted file mode 100644 index 36213be..0000000 --- a/utils/discord/objects/embedvideo.go +++ /dev/null @@ -1,7 +0,0 @@ -package objects - -type EmbedVideo struct { - Url string - Height int - Width int -} diff --git a/utils/discord/objects/emoji.go b/utils/discord/objects/emoji.go deleted file mode 100644 index e4b3ef0..0000000 --- a/utils/discord/objects/emoji.go +++ /dev/null @@ -1,11 +0,0 @@ -package objects - -type Emoji struct { - Id string - Name string - Roles []string - User User - RequireColons bool - Managed bool - Animated bool -} diff --git a/utils/discord/objects/guild.go b/utils/discord/objects/guild.go index 5d1e755..8d6b7cc 100644 --- a/utils/discord/objects/guild.go +++ b/utils/discord/objects/guild.go @@ -1,43 +1,9 @@ package objects type Guild struct { - Id string - Name string - Icon string - Splash string - Owner bool - OwnerId string `json:"owner_id"` - Permissions int - Region string - AfkChannelid string `json:"afk_channel_id"` - AfkTimeout int - EmbedEnabled bool `json:"embed_enabled"` - EmbedChannelId string `json:"embed_channel_id"` - VerificationLevel int `json:"verification_level"` - DefaultMessageNotifications int `json:"default_message_notifications"` - ExplicitContentFilter int `json:"explicit_content_filter"` - Roles []Role - Emojis []Emoji - Features []string - MfaLevel int `json:"mfa_level"` - ApplicationId string `json:"application_id"` - WidgetEnabled bool `json:"widget_enabled"` - WidgetChannelId string `json:"widget_channel_id"` - SystemChannelId string `json:"system_channel_id"` - JoinedAt string `json:"joined_at"` - Large bool - Unavailable bool - MemberCount int `json:"member_count"` - VoiceStates []VoiceState - Members []Member - Channels []Channel - Presences []Presence - MaxPresences int `json:"max_presences"` - MaxMembers int `json:"max_members"` - VanityUrlCode string `json:"vanity_url_code"` - Description string - Banner string - PremiumTier int `json:"premium_tier"` - PremiumSubscriptionCount int `json:"premium_subscription_count"` - PreferredLocale string `json:"preferred_locale"` + Id string + Name string + Icon string + Owner bool + Permissions int } diff --git a/utils/discord/objects/member.go b/utils/discord/objects/member.go deleted file mode 100644 index 465e574..0000000 --- a/utils/discord/objects/member.go +++ /dev/null @@ -1,12 +0,0 @@ -package objects - -import "github.com/TicketsBot/GoPanel/utils/types" - -type Member struct { - User User - Nick string - Roles types.UInt64StringSlice `json:"roles,string"` - JoinedAt string - Deaf bool - Mute bool -} diff --git a/utils/discord/objects/message.go b/utils/discord/objects/message.go deleted file mode 100644 index c12304a..0000000 --- a/utils/discord/objects/message.go +++ /dev/null @@ -1,25 +0,0 @@ -package objects - -type Message struct { - Id string - ChannelId string - GuildId string - Author User - Member Member - Content string - Timestamp string - EditedTimestamp string - Tts bool - MentionEveryone bool - Mentions []interface{} - MentionsRoles []int64 - Attachments []Attachment - Embeds []Embed - Reactions []Reaction - Nonce string - Pinned bool - WebhookId string - Type int - Activity MessageActivity - Application MessageApplication -} diff --git a/utils/discord/objects/messageactivity.go b/utils/discord/objects/messageactivity.go deleted file mode 100644 index fa1afe0..0000000 --- a/utils/discord/objects/messageactivity.go +++ /dev/null @@ -1,6 +0,0 @@ -package objects - -type MessageActivity struct { - Type int - PartyId string -} diff --git a/utils/discord/objects/messageapplication.go b/utils/discord/objects/messageapplication.go deleted file mode 100644 index c3a11a4..0000000 --- a/utils/discord/objects/messageapplication.go +++ /dev/null @@ -1,9 +0,0 @@ -package objects - -type MessageApplication struct { - Id string - CoverImage string - Description string - Icon string - Name string -} diff --git a/utils/discord/objects/overwrite.go b/utils/discord/objects/overwrite.go deleted file mode 100644 index ac007fc..0000000 --- a/utils/discord/objects/overwrite.go +++ /dev/null @@ -1,8 +0,0 @@ -package objects - -type Overwrite struct { - Id string - Type string - Allow int - Deny int -} diff --git a/utils/discord/objects/party.go b/utils/discord/objects/party.go deleted file mode 100644 index 8ee055c..0000000 --- a/utils/discord/objects/party.go +++ /dev/null @@ -1,6 +0,0 @@ -package objects - -type Party struct { - Id string - Size []int -} diff --git a/utils/discord/objects/presence.go b/utils/discord/objects/presence.go deleted file mode 100644 index 43a27fc..0000000 --- a/utils/discord/objects/presence.go +++ /dev/null @@ -1,11 +0,0 @@ -package objects - -type Presence struct { - User User - Roles []string - Game Activity - GuildId string - Status string - Activities []Activity - ClientStatus ClientStatus -} diff --git a/utils/discord/objects/reaction.go b/utils/discord/objects/reaction.go deleted file mode 100644 index 6b588d5..0000000 --- a/utils/discord/objects/reaction.go +++ /dev/null @@ -1,7 +0,0 @@ -package objects - -type Reaction struct { - Count int - Me bool - Emoji Emoji -} diff --git a/utils/discord/objects/role.go b/utils/discord/objects/role.go deleted file mode 100644 index 7f396b0..0000000 --- a/utils/discord/objects/role.go +++ /dev/null @@ -1,12 +0,0 @@ -package objects - -type Role struct { - Id string - Name string - Color int - Hoist bool - Position int - Permissions int - Managed bool - Mentionable bool -} diff --git a/utils/discord/objects/secret.go b/utils/discord/objects/secret.go deleted file mode 100644 index f1c32ee..0000000 --- a/utils/discord/objects/secret.go +++ /dev/null @@ -1,7 +0,0 @@ -package objects - -type Secret struct { - Join string - Spectate string - Match string -} diff --git a/utils/discord/objects/timestamp.go b/utils/discord/objects/timestamp.go deleted file mode 100644 index 20145e1..0000000 --- a/utils/discord/objects/timestamp.go +++ /dev/null @@ -1,6 +0,0 @@ -package objects - -type Timestamp struct { - Start int - End int -} diff --git a/utils/discord/objects/voicestate.go b/utils/discord/objects/voicestate.go deleted file mode 100644 index a6ab409..0000000 --- a/utils/discord/objects/voicestate.go +++ /dev/null @@ -1,14 +0,0 @@ -package objects - -type VoiceState struct { - GuildId string - ChannelId string - UserId string - Member Member - SessionId string - Deaf bool - Mute bool - SelfDeaf bool - SelfMute bool - Suppress bool -} diff --git a/utils/permissionutils.go b/utils/permissionutils.go index 10e06f2..befd612 100644 --- a/utils/permissionutils.go +++ b/utils/permissionutils.go @@ -3,23 +3,19 @@ package utils import ( "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" - "github.com/TicketsBot/GoPanel/utils/discord/endpoints/guild" - "github.com/TicketsBot/GoPanel/utils/discord/objects" - "github.com/apex/log" - "github.com/gin-gonic/contrib/sessions" - "github.com/robfig/go-cache" + "github.com/TicketsBot/GoPanel/rpc/cache" + "github.com/TicketsBot/GoPanel/rpc/ratelimit" + "github.com/rxdn/gdl/objects/guild" + "github.com/rxdn/gdl/rest" "strconv" - "time" ) -var roleCache = cache.New(time.Minute, time.Minute) - -func IsAdmin(guild objects.Guild, guildId, userId uint64, res chan bool) { +func IsAdmin(g guild.Guild, guildId, userId uint64, res chan bool) { if Contains(config.Conf.Admins, strconv.Itoa(int(userId))) { res <- true } - if guild.Owner { + if g.Owner { res <- true } @@ -27,24 +23,24 @@ func IsAdmin(guild objects.Guild, guildId, userId uint64, res chan bool) { res <- true } - if guild.Permissions & 0x8 != 0 { + if g.Permissions & 0x8 != 0 { res <- true } - userRolesChan := make(chan []uint64) - go table.GetCachedRoles(guildId, userId, userRolesChan) - userRoles := <-userRolesChan - adminRolesChan := make(chan []uint64) go table.GetAdminRoles(guildId, adminRolesChan) adminRoles := <- adminRolesChan + userRoles, found := getRoles(guildId, userId) + hasAdminRole := false - for _, userRole := range userRoles { - for _, adminRole := range adminRoles { - if userRole == adminRole { - hasAdminRole = true - break + if found { + for _, userRole := range userRoles { + for _, adminRole := range adminRoles { + if userRole == adminRole { + hasAdminRole = true + break + } } } } @@ -56,15 +52,18 @@ func IsAdmin(guild objects.Guild, guildId, userId uint64, res chan bool) { res <- false } -func GetRolesRest(store sessions.Session, guildId, userId uint64) *[]uint64 { - var member objects.Member - endpoint := guild.GetGuildMember(guildId, userId) +func getRoles(guildId, userId uint64) ([]uint64, bool) { + member, found := cache.Instance.GetMember(guildId, userId) + if !found { // get from rest + var err error + member, err = rest.GetGuildMember(config.Conf.Bot.Token, ratelimit.Ratelimiter, guildId, userId) + if err != nil { + return nil, false + } - if err, _ := endpoint.Request(store, nil, nil, &member); err != nil { - log.Error(err.Error()) - return nil + // cache + cache.Instance.StoreMember(member, guildId) } - roles := []uint64(member.Roles) - return &roles + return member.Roles, true } diff --git a/utils/premiumutils.go b/utils/premiumutils.go index 9860431..0b2eb5a 100644 --- a/utils/premiumutils.go +++ b/utils/premiumutils.go @@ -3,13 +3,13 @@ package utils import ( "encoding/json" "fmt" - redis "github.com/TicketsBot/GoPanel/cache" "github.com/TicketsBot/GoPanel/config" "github.com/TicketsBot/GoPanel/database/table" - "github.com/TicketsBot/GoPanel/utils/discord/endpoints/guild" - "github.com/TicketsBot/GoPanel/utils/discord/objects" + "github.com/TicketsBot/GoPanel/rpc/cache" + "github.com/TicketsBot/GoPanel/rpc/ratelimit" "github.com/gin-gonic/contrib/sessions" - "github.com/robfig/go-cache" + gocache "github.com/robfig/go-cache" + "github.com/rxdn/gdl/rest" "io/ioutil" "net/http" "strconv" @@ -21,7 +21,7 @@ type ProxyResponse struct { Tier int } -var premiumCache = cache.New(10 * time.Minute, 10 * time.Minute) +var premiumCache = gocache.New(10 * time.Minute, 10 * time.Minute) func IsPremiumGuild(store sessions.Session, guildId uint64, ch chan bool) { guildIdRaw := strconv.FormatUint(guildId, 10) @@ -43,47 +43,25 @@ func IsPremiumGuild(store sessions.Session, guildId uint64, ch chan bool) { ch<-true } else { // Get guild object - guildChan := make(chan *objects.Guild) - go redis.Client.GetGuildByID(guildIdRaw, guildChan) - g := <-guildChan + guild, found := cache.Instance.GetGuild(guildId, false) - ownerIdRaw := "" - if g == nil { - var g objects.Guild - endpoint := guild.GetGuild(int(guildId)) + if !found { + var err error + guild, err = rest.GetGuild(config.Conf.Bot.Token, ratelimit.Ratelimiter, guildId) - endpoint.Request(store, nil, nil, &g) - - ownerIdRaw = g.OwnerId - go redis.Client.StoreGuild(g) + if err == nil { // cache + go cache.Instance.StoreGuild(guild) + } } // TODO: Find a way to stop people using votes to exploit panels - // Lookup votes - /*ownerId, err := strconv.ParseInt(ownerIdRaw, 10, 64); if err != nil { - fmt.Println(err.Error()) - ch <- false - return - } - - hasVoted := make(chan bool) - go table.HasVoted(ownerId, hasVoted) - if <-hasVoted { - ch <- true - - if err := premiumCache.Add(guildIdRaw, true, 10 * time.Minute); err != nil { - fmt.Println(err.Error()) - } - - return - }*/ // Lookup Patreon client := &http.Client{ Timeout: time.Second * 3, } - url := fmt.Sprintf("%s/ispremium?key=%s&id=%s", config.Conf.Bot.PremiumLookupProxyUrl, config.Conf.Bot.PremiumLookupProxyKey, ownerIdRaw) + url := fmt.Sprintf("%s/ispremium?key=%s&id=%d", config.Conf.Bot.PremiumLookupProxyUrl, config.Conf.Bot.PremiumLookupProxyKey, guild.OwnerId) req, err := http.NewRequest("GET", url, nil) res, err := client.Do(req); if err != nil { diff --git a/utils/sliceutils.go b/utils/sliceutils.go index 2fc564a..8dcab91 100644 --- a/utils/sliceutils.go +++ b/utils/sliceutils.go @@ -1,42 +1,43 @@ -package utils - -import ( - "github.com/TicketsBot/GoPanel/utils/discord/objects" - "reflect" -) - -func Contains(s interface{}, elem interface{}) bool { - arrV := reflect.ValueOf(s) - - if arrV.Kind() == reflect.Slice { - for i := 0; i < arrV.Len(); i++ { - - // XXX - panics if slice element points to an unexported struct field - // see https://golang.org/pkg/reflect/#Value.Interface - if arrV.Index(i).Interface() == elem { - return true - } - } - } - - return false -} - -func Insert(slice []objects.Guild, index int, value objects.Guild) []objects.Guild { - // Grow the slice by one element. - slice = slice[0 : len(slice)+1] - // Use copy to move the upper part of the slice out of the way and open a hole. - copy(slice[index+1:], slice[index:]) - // Store the new value. - slice[index] = value - // Return the result. - return slice -} - -func Reverse(slice []objects.Message) []objects.Message { - for i := len(slice)/2-1; i >= 0; i-- { - opp := len(slice)-1-i - slice[i], slice[opp] = slice[opp], slice[i] - } - return slice -} +package utils + +import ( + "github.com/TicketsBot/GoPanel/utils/discord/objects" + "github.com/rxdn/gdl/objects/channel/message" + "reflect" +) + +func Contains(s interface{}, elem interface{}) bool { + arrV := reflect.ValueOf(s) + + if arrV.Kind() == reflect.Slice { + for i := 0; i < arrV.Len(); i++ { + + // XXX - panics if slice element points to an unexported struct field + // see https://golang.org/pkg/reflect/#Value.Interface + if arrV.Index(i).Interface() == elem { + return true + } + } + } + + return false +} + +func Insert(slice []objects.Guild, index int, value objects.Guild) []objects.Guild { + // Grow the slice by one element. + slice = slice[0 : len(slice)+1] + // Use copy to move the upper part of the slice out of the way and open a hole. + copy(slice[index+1:], slice[index:]) + // Store the new value. + slice[index] = value + // Return the result. + return slice +} + +func Reverse(slice []message.Message) []message.Message { + for i := len(slice)/2-1; i >= 0; i-- { + opp := len(slice)-1-i + slice[i], slice[opp] = slice[opp], slice[i] + } + return slice +}