diff --git a/app/http/endpoints/api/whitelabelget.go b/app/http/endpoints/api/whitelabelget.go index 449ae94..81de924 100644 --- a/app/http/endpoints/api/whitelabelget.go +++ b/app/http/endpoints/api/whitelabelget.go @@ -25,9 +25,20 @@ func WhitelabelGet(ctx *gin.Context) { "error": "No bot found", }) } else { + // Get status + status, err := database.Client.WhitelabelStatuses.Get(bot.BotId) + if err != nil { + ctx.JSON(500, gin.H{ + "success": false, + "error": err.Error(), + }) + return + } + ctx.JSON(200, gin.H{ "success": true, "id": strconv.FormatUint(bot.BotId, 10), + "status": status, }) } } diff --git a/app/http/endpoints/api/whitelabelstatuspost.go b/app/http/endpoints/api/whitelabelstatuspost.go new file mode 100644 index 0000000..b19dc6c --- /dev/null +++ b/app/http/endpoints/api/whitelabelstatuspost.go @@ -0,0 +1,99 @@ +package api + +import ( + "github.com/TicketsBot/GoPanel/config" + "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/messagequeue" + "github.com/TicketsBot/GoPanel/rpc" + "github.com/TicketsBot/common/premium" + "github.com/TicketsBot/common/statusupdates" + "github.com/gin-gonic/gin" +) + +func WhitelabelStatusPost(ctx *gin.Context) { + userId := ctx.Keys["userid"].(uint64) + + premiumTier := rpc.PremiumClient.GetTierByUser(userId, false) + if premiumTier < premium.Whitelabel { + var isForced bool + for _, forced := range config.Conf.ForceWhitelabel { + if forced == userId { + isForced = true + break + } + } + + if !isForced { + ctx.JSON(402, gin.H{ + "success": false, + "error": "You must have the whitelabel premium tier", + }) + return + } + } + + // 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 status string + { + var data map[string]string + if err := ctx.BindJSON(&data); err != nil { + ctx.JSON(400, gin.H{ + "success": false, + "error": "No status provided", + }) + return + } + + var ok bool + status, ok = data["status"] + if !ok { + ctx.JSON(400, gin.H{ + "success": false, + "error": "No status provided", + }) + return + } + + if len(status) == 0 || len(status) > 255 { + ctx.JSON(400, gin.H{ + "success": false, + "error": "Status must be between 1-255 characters in length", + }) + return + } + } + + if err := database.Client.WhitelabelStatuses.Set(bot.BotId, status); err != nil { + ctx.JSON(500, gin.H{ + "success": false, + "error": err.Error(), + }) + return + } + + go statusupdates.Publish(messagequeue.Client.Client, bot.BotId) + + ctx.JSON(200, gin.H{ + "success": true, + "bot": bot, + }) +} diff --git a/app/http/server.go b/app/http/server.go index ca792ac..179593c 100644 --- a/app/http/server.go +++ b/app/http/server.go @@ -112,6 +112,7 @@ func StartServer() { userGroup.GET("/whitelabel", api.WhitelabelGet) userGroup.POST("/whitelabel", api.WhitelabelPost) + userGroup.POST("/whitelabel/status", api.WhitelabelStatusPost) } if err := router.Run(config.Conf.Server.Host); err != nil { diff --git a/go.mod b/go.mod index 132e74b..2b377f5 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.14 require ( github.com/BurntSushi/toml v0.3.1 github.com/TicketsBot/archiverclient v0.0.0-20200425115930-0ca198cc8306 - github.com/TicketsBot/common v0.0.0-20200527174950-d8ebbcbf49c9 - github.com/TicketsBot/database v0.0.0-20200527183847-2fbafde7649e + github.com/TicketsBot/common v0.0.0-20200529141045-7426ad13f1a4 + github.com/TicketsBot/database v0.0.0-20200529142046-b47b53137846 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 2c52004..fd6adbb 100644 --- a/public/templates/views/whitelabel.tmpl +++ b/public/templates/views/whitelabel.tmpl @@ -5,7 +5,7 @@
-

Whitelabel

+

Bot Token

@@ -46,6 +46,38 @@
+
+
+
+
+

Custom Status

+
+
+ +
+
+
+ + +
+
+
+ +
+
+
+ +
+
+
+ +
+
+
+
@@ -71,9 +103,8 @@
{{end}} \ No newline at end of file