From 495a6c5ac2873015fcb1f7d586b9e603d3e922c4 Mon Sep 17 00:00:00 2001 From: Dot-Rar Date: Sat, 13 Jun 2020 17:59:34 +0100 Subject: [PATCH] force modmail server --- app/http/endpoints/api/whitelabelget.go | 14 +-- app/http/endpoints/api/whitelabelgetguilds.go | 52 +++++++++ .../endpoints/api/whitelabelpostmodmail.go | 110 ++++++++++++++++++ app/http/server.go | 2 + go.mod | 2 +- public/templates/views/whitelabel.tmpl | 98 +++++++++++++++- 6 files changed, 264 insertions(+), 14 deletions(-) create mode 100644 app/http/endpoints/api/whitelabelgetguilds.go create mode 100644 app/http/endpoints/api/whitelabelpostmodmail.go diff --git a/app/http/endpoints/api/whitelabelget.go b/app/http/endpoints/api/whitelabelget.go index 76c2f38..adba1e8 100644 --- a/app/http/endpoints/api/whitelabelget.go +++ b/app/http/endpoints/api/whitelabelget.go @@ -22,7 +22,7 @@ func WhitelabelGet(ctx *gin.Context) { if bot.BotId == 0 { ctx.JSON(404, gin.H{ "success": false, - "error": "No bot found", + "error": "No bot found", }) } else { // Get status @@ -35,8 +35,8 @@ func WhitelabelGet(ctx *gin.Context) { return } - // Get errors - errors, err := database.Client.WhitelabelErrors.GetRecent(bot.UserId, 10) + // Get forced modmail guild + forcedGuild, err := database.Client.ModmailForcedGuilds.Get(bot.BotId) if err != nil { ctx.JSON(500, gin.H{ "success": false, @@ -46,10 +46,10 @@ func WhitelabelGet(ctx *gin.Context) { } ctx.JSON(200, gin.H{ - "success": true, - "id": strconv.FormatUint(bot.BotId, 10), - "status": status, - "errors": errors, + "success": true, + "id": strconv.FormatUint(bot.BotId, 10), + "status": status, + "modmail_forced_guild": strconv.FormatUint(forcedGuild, 10), }) } } diff --git a/app/http/endpoints/api/whitelabelgetguilds.go b/app/http/endpoints/api/whitelabelgetguilds.go new file mode 100644 index 0000000..dd98144 --- /dev/null +++ b/app/http/endpoints/api/whitelabelgetguilds.go @@ -0,0 +1,52 @@ +package api + +import ( + "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/rpc/cache" + "github.com/gin-gonic/gin" + "strconv" +) + +func WhitelabelGetGuilds(ctx *gin.Context) { + userId := ctx.Keys["userid"].(uint64) + + bot, err := database.Client.Whitelabel.GetByUserId(userId) + if err != nil { + ctx.JSON(500, gin.H{ + "success": false, + "error": err.Error(), + }) + return + } + + // id -> name + guilds := make(map[string]string, 0) + if bot.BotId == 0 { + ctx.JSON(404, gin.H{ + "success": false, + "guilds": guilds, + }) + return + } + + ids, err := database.Client.WhitelabelGuilds.GetGuilds(bot.BotId) + if err != nil { + ctx.JSON(500, gin.H{ + "success": false, + "error": err.Error(), + }) + return + } + + for _, id := range ids { + // get guild name + if guild, found := cache.Instance.GetGuild(id, false); found { + guilds[strconv.FormatUint(id, 10)] = guild.Name + } + } + + ctx.JSON(200, gin.H{ + "success": true, + "guilds": guilds, + }) +} diff --git a/app/http/endpoints/api/whitelabelpostmodmail.go b/app/http/endpoints/api/whitelabelpostmodmail.go new file mode 100644 index 0000000..1b267f1 --- /dev/null +++ b/app/http/endpoints/api/whitelabelpostmodmail.go @@ -0,0 +1,110 @@ +package api + +import ( + "github.com/TicketsBot/GoPanel/database" + "github.com/gin-gonic/gin" + "strconv" +) + +func WhitelabelModmailPost(ctx *gin.Context) { + userId := ctx.Keys["userid"].(uint64) + + // Get bot + bot, err := database.Client.Whitelabel.GetByUserId(userId) + if err != nil { + ctx.JSON(500, gin.H{ + "success": false, + "error": err.Error(), + }) + return + } + + // Ensure bot exists + if bot.BotId == 0 { + ctx.JSON(404, gin.H{ + "success": false, + "error": "No bot found", + }) + return + } + + // Parse status + var guildId uint64 + { + var data map[string]string + if err := ctx.BindJSON(&data); err != nil { + ctx.JSON(400, gin.H{ + "success": false, + "error": "No guild ID provided", + }) + return + } + + guildIdStr, ok := data["guild"] + if !ok { + ctx.JSON(400, gin.H{ + "success": false, + "error": "No guild ID provided", + }) + return + } + + guildId, err = strconv.ParseUint(guildIdStr, 10, 64) + if err != nil { + ctx.JSON(400, gin.H{ + "success": false, + "error": "Invalid guild ID provided", + }) + return + } + } + + if guildId == 0 { + if err := database.Client.ModmailForcedGuilds.Delete(bot.BotId); err != nil { + ctx.JSON(500, gin.H{ + "success": false, + "error": err.Error(), + }) + return + } + } + + // verify that the bot is in the specified guild + guilds, err := database.Client.WhitelabelGuilds.GetGuilds(bot.BotId) + if err != nil { + ctx.JSON(500, gin.H{ + "success": false, + "error": err.Error(), + }) + return + } + + var found bool + for _, botGuild := range guilds { + if botGuild == guildId { + found = true + break + } + } + + if !found { + ctx.JSON(400, gin.H{ + "success": false, + "error": "The bot isn't in the guild your provided", + }) + return + } + + if err := database.Client.ModmailForcedGuilds.Set(bot.BotId, guildId); err != nil { + ctx.JSON(500, gin.H{ + "success": false, + "error": err.Error(), + }) + return + } + + ctx.JSON(200, gin.H{ + "success": true, + "bot": bot, + }) +} diff --git a/app/http/server.go b/app/http/server.go index d982f26..2b0d7bd 100644 --- a/app/http/server.go +++ b/app/http/server.go @@ -119,6 +119,8 @@ func StartServer() { whitelabelGroup.GET("/", api.WhitelabelGet) whitelabelApiGroup.GET("/errors", api.WhitelabelGetErrors) + whitelabelApiGroup.GET("/guilds", api.WhitelabelGetGuilds) + whitelabelApiGroup.POST("/modmail", api.WhitelabelModmailPost) whitelabelApiGroup.Group("/").Use(createLimiter(10, time.Minute)).POST("/", api.WhitelabelPost) whitelabelApiGroup.Group("/").Use(createLimiter(1, time.Second * 5)).POST("/status", api.WhitelabelStatusPost) diff --git a/go.mod b/go.mod index 1f677fa..d3051d5 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/BurntSushi/toml v0.3.1 github.com/TicketsBot/archiverclient v0.0.0-20200425115930-0ca198cc8306 github.com/TicketsBot/common v0.0.0-20200529141045-7426ad13f1a4 - github.com/TicketsBot/database v0.0.0-20200612180221-a26ff96874ea + github.com/TicketsBot/database v0.0.0-20200613162408-5b3847cebd07 github.com/apex/log v1.1.2 github.com/boj/redistore v0.0.0-20180917114910-cd5dcc76aeff // indirect github.com/dgrijalva/jwt-go v3.2.0+incompatible diff --git a/public/templates/views/whitelabel.tmpl b/public/templates/views/whitelabel.tmpl index 0c49ee0..2b64be4 100644 --- a/public/templates/views/whitelabel.tmpl +++ b/public/templates/views/whitelabel.tmpl @@ -2,7 +2,7 @@
-
+

Bot Token

@@ -20,13 +20,13 @@
-
+

Note: You will not be able to view the token after submitting it

-
+
-
+
+ +
+
+
+

Modmail

+
+
+
+
+
+
+ + +
+
+
+ +
+
+
+ +
+
+
+
+
+
+
@@ -66,7 +99,7 @@
-
+