From 49ce52283c64f814e400cef4232530845434e28f Mon Sep 17 00:00:00 2001 From: rxdn <29165304+rxdn@users.noreply.github.com> Date: Tue, 21 Jun 2022 19:26:54 +0100 Subject: [PATCH] Different activity types --- app/http/endpoints/api/blacklist/blacklist.go | 6 +- app/http/endpoints/api/guilds.go | 5 +- app/http/endpoints/api/panel/paneldelete.go | 2 - app/http/endpoints/api/panel/panellist.go | 6 +- app/http/endpoints/api/user.go | 11 +- .../endpoints/api/whitelabel/whitelabelget.go | 50 ++--- .../api/whitelabel/whitelabelgeterrors.go | 8 +- .../api/whitelabel/whitelabelgetguilds.go | 15 +- .../api/whitelabel/whitelabelgetpublickey.go | 11 +- .../api/whitelabel/whitelabelpost.go | 15 +- .../api/whitelabel/whitelabelpostpublickey.go | 11 +- .../api/whitelabel/whitelabelstatuspost.go | 85 ++++--- app/http/endpoints/root/webchatws.go | 3 - chatreplica/proxy.go | 2 - frontend/src/views/Whitelabel.svelte | 208 ++++++++++-------- go.mod | 8 +- go.sum | 20 +- 17 files changed, 220 insertions(+), 246 deletions(-) diff --git a/app/http/endpoints/api/blacklist/blacklist.go b/app/http/endpoints/api/blacklist/blacklist.go index ea9388e..483afef 100644 --- a/app/http/endpoints/api/blacklist/blacklist.go +++ b/app/http/endpoints/api/blacklist/blacklist.go @@ -4,6 +4,7 @@ import ( "context" "github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/rpc/cache" + "github.com/TicketsBot/GoPanel/utils" "github.com/gin-gonic/gin" "github.com/rxdn/gdl/objects/user" "golang.org/x/sync/errgroup" @@ -21,10 +22,7 @@ func GetBlacklistHandler(ctx *gin.Context) { blacklistedUsers, err := database.Client.Blacklist.GetBlacklistedUsers(guildId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } diff --git a/app/http/endpoints/api/guilds.go b/app/http/endpoints/api/guilds.go index d598042..c089568 100644 --- a/app/http/endpoints/api/guilds.go +++ b/app/http/endpoints/api/guilds.go @@ -26,10 +26,7 @@ func GetGuilds(ctx *gin.Context) { guilds, err := database.Client.UserGuilds.Get(userId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } diff --git a/app/http/endpoints/api/panel/paneldelete.go b/app/http/endpoints/api/panel/paneldelete.go index 1e37e10..04801d7 100644 --- a/app/http/endpoints/api/panel/paneldelete.go +++ b/app/http/endpoints/api/panel/paneldelete.go @@ -1,7 +1,6 @@ package api import ( - "fmt" "github.com/TicketsBot/GoPanel/botcontext" "github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/rpc" @@ -42,7 +41,6 @@ func DeletePanel(ctx *gin.Context) { // Get any multi panels this panel is part of to use later multiPanels, err := database.Client.MultiPanelTargets.GetMultiPanels(panelId) if err != nil { - fmt.Println(err.Error()) ctx.JSON(500, utils.ErrorJson(err)) return } diff --git a/app/http/endpoints/api/panel/panellist.go b/app/http/endpoints/api/panel/panellist.go index f5504a8..a5a5c32 100644 --- a/app/http/endpoints/api/panel/panellist.go +++ b/app/http/endpoints/api/panel/panellist.go @@ -3,6 +3,7 @@ package api import ( "context" dbclient "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/GoPanel/utils/types" "github.com/TicketsBot/database" "github.com/gin-gonic/gin" @@ -88,10 +89,7 @@ func ListPanels(ctx *gin.Context) { } if err := group.Wait(); err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } diff --git a/app/http/endpoints/api/user.go b/app/http/endpoints/api/user.go index 0d9e0d8..dfb24f5 100644 --- a/app/http/endpoints/api/user.go +++ b/app/http/endpoints/api/user.go @@ -3,6 +3,7 @@ package api import ( "context" "github.com/TicketsBot/GoPanel/rpc/cache" + "github.com/TicketsBot/GoPanel/utils" "github.com/gin-gonic/gin" "strconv" ) @@ -12,19 +13,13 @@ func UserHandler(ctx *gin.Context) { userId, err := strconv.ParseUint(ctx.Param("user"), 10, 64) if err != nil { - ctx.AbortWithStatusJSON(400, gin.H{ - "success": false, - "error": "Invalid user ID", - }) + ctx.JSON(400, utils.ErrorStr("Invalid user ID")) return } var username string if err := cache.Instance.QueryRow(context.Background(), `SELECT "data"->>'Username' FROM users WHERE users.user_id=$1 AND EXISTS(SELECT FROM members WHERE members.guild_id=$2);`, userId, guildId).Scan(&username); err != nil { - ctx.JSON(404, gin.H{ - "success": false, - "error": "Not found", - }) + ctx.JSON(404, utils.ErrorStr("Not found")) return } diff --git a/app/http/endpoints/api/whitelabel/whitelabelget.go b/app/http/endpoints/api/whitelabel/whitelabelget.go index a8adca7..8e2a645 100644 --- a/app/http/endpoints/api/whitelabel/whitelabelget.go +++ b/app/http/endpoints/api/whitelabel/whitelabelget.go @@ -2,43 +2,43 @@ package api import ( "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/utils" "github.com/gin-gonic/gin" - "strconv" + "github.com/rxdn/gdl/objects/user" ) +type whitelabelResponse struct { + Id uint64 `json:"id,string"` + statusUpdateBody +} + func WhitelabelGet(ctx *gin.Context) { userId := ctx.Keys["userid"].(uint64) // Check if this is a different token bot, err := database.Client.Whitelabel.GetByUserId(userId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } if bot.BotId == 0 { - ctx.JSON(404, gin.H{ - "success": false, - "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, - }) + ctx.JSON(404, utils.ErrorStr("No bot found")) + return } + + // Get status + status, statusType, _, err := database.Client.WhitelabelStatuses.Get(bot.BotId) + if err != nil { + ctx.JSON(500, utils.ErrorJson(err)) + return + } + + ctx.JSON(200, whitelabelResponse{ + Id: bot.BotId, + statusUpdateBody: statusUpdateBody{ // Zero values if no status is fine + Status: status, + StatusType: user.ActivityType(statusType), + }, + }) } diff --git a/app/http/endpoints/api/whitelabel/whitelabelgeterrors.go b/app/http/endpoints/api/whitelabel/whitelabelgeterrors.go index 29dc71c..605a8d6 100644 --- a/app/http/endpoints/api/whitelabel/whitelabelgeterrors.go +++ b/app/http/endpoints/api/whitelabel/whitelabelgeterrors.go @@ -2,6 +2,7 @@ package api import ( "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/utils" "github.com/gin-gonic/gin" ) @@ -10,15 +11,12 @@ func WhitelabelGetErrors(ctx *gin.Context) { errors, err := database.Client.WhitelabelErrors.GetRecent(userId, 10) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } ctx.JSON(200, gin.H{ "success": true, - "errors": errors, + "errors": errors, }) } diff --git a/app/http/endpoints/api/whitelabel/whitelabelgetguilds.go b/app/http/endpoints/api/whitelabel/whitelabelgetguilds.go index dd98144..96a09e0 100644 --- a/app/http/endpoints/api/whitelabel/whitelabelgetguilds.go +++ b/app/http/endpoints/api/whitelabel/whitelabelgetguilds.go @@ -3,6 +3,7 @@ package api import ( "github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/rpc/cache" + "github.com/TicketsBot/GoPanel/utils" "github.com/gin-gonic/gin" "strconv" ) @@ -12,10 +13,7 @@ func WhitelabelGetGuilds(ctx *gin.Context) { bot, err := database.Client.Whitelabel.GetByUserId(userId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } @@ -24,17 +22,14 @@ func WhitelabelGetGuilds(ctx *gin.Context) { if bot.BotId == 0 { ctx.JSON(404, gin.H{ "success": false, - "guilds": guilds, + "guilds": guilds, }) return } ids, err := database.Client.WhitelabelGuilds.GetGuilds(bot.BotId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } @@ -47,6 +42,6 @@ func WhitelabelGetGuilds(ctx *gin.Context) { ctx.JSON(200, gin.H{ "success": true, - "guilds": guilds, + "guilds": guilds, }) } diff --git a/app/http/endpoints/api/whitelabel/whitelabelgetpublickey.go b/app/http/endpoints/api/whitelabel/whitelabelgetpublickey.go index c95ef61..d37891b 100644 --- a/app/http/endpoints/api/whitelabel/whitelabelgetpublickey.go +++ b/app/http/endpoints/api/whitelabel/whitelabelgetpublickey.go @@ -2,6 +2,7 @@ package api import ( "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/utils" "github.com/gin-gonic/gin" ) @@ -15,10 +16,7 @@ func WhitelabelGetPublicKey(ctx *gin.Context) { // Get bot bot, err := database.Client.Whitelabel.GetByUserId(userId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } @@ -33,10 +31,7 @@ func WhitelabelGetPublicKey(ctx *gin.Context) { key, err := database.Client.WhitelabelKeys.Get(bot.BotId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } diff --git a/app/http/endpoints/api/whitelabel/whitelabelpost.go b/app/http/endpoints/api/whitelabel/whitelabelpost.go index 9596936..38116e6 100644 --- a/app/http/endpoints/api/whitelabel/whitelabelpost.go +++ b/app/http/endpoints/api/whitelabel/whitelabelpost.go @@ -52,10 +52,7 @@ func WhitelabelPost(ctx *gin.Context) { // Check if this is a different token existing, err := dbclient.Client.Whitelabel.GetByUserId(userId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } @@ -64,10 +61,7 @@ func WhitelabelPost(ctx *gin.Context) { BotId: bot.Id, Token: token, }); err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } @@ -78,10 +72,7 @@ func WhitelabelPost(ctx *gin.Context) { } if err := tokenchange.PublishTokenChange(redis.Client.Client, tokenChangeData); err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } diff --git a/app/http/endpoints/api/whitelabel/whitelabelpostpublickey.go b/app/http/endpoints/api/whitelabel/whitelabelpostpublickey.go index 773d5ff..efbeeb5 100644 --- a/app/http/endpoints/api/whitelabel/whitelabelpostpublickey.go +++ b/app/http/endpoints/api/whitelabel/whitelabelpostpublickey.go @@ -3,6 +3,7 @@ package api import ( "encoding/hex" "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/GoPanel/utils" "github.com/gin-gonic/gin" ) @@ -16,10 +17,7 @@ func WhitelabelPostPublicKey(ctx *gin.Context) { // Get bot bot, err := database.Client.Whitelabel.GetByUserId(userId) if err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } @@ -52,10 +50,7 @@ func WhitelabelPostPublicKey(ctx *gin.Context) { } if err := database.Client.WhitelabelKeys.Set(bot.BotId, body.PublicKey); err != nil { - ctx.JSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } diff --git a/app/http/endpoints/api/whitelabel/whitelabelstatuspost.go b/app/http/endpoints/api/whitelabel/whitelabelstatuspost.go index 58d9596..2927873 100644 --- a/app/http/endpoints/api/whitelabel/whitelabelstatuspost.go +++ b/app/http/endpoints/api/whitelabel/whitelabelstatuspost.go @@ -3,75 +3,66 @@ package api import ( "github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/redis" + "github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/common/statusupdates" "github.com/gin-gonic/gin" + "github.com/rxdn/gdl/objects/user" ) +type statusUpdateBody struct { + Status string `json:"status"` + StatusType user.ActivityType `json:"status_type,string"` +} + func WhitelabelStatusPost(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(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } // Ensure bot exists if bot.BotId == 0 { - ctx.JSON(404, gin.H{ - "success": false, - "error": "No bot found", - }) + ctx.JSON(404, utils.ErrorStr("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(), - }) + var data statusUpdateBody + if err := ctx.BindJSON(&data); err != nil { + ctx.JSON(400, utils.ErrorStr("Invalid request body")) return } + // Validate status length + if len(data.Status) == 0 || len(data.Status) > 255 { + ctx.JSON(400, utils.ErrorStr("Status must be between 1-255 characters in length")) + return + } + + // Validate status type + validActivities := []user.ActivityType{ + user.ActivityTypePlaying, + user.ActivityTypeListening, + user.ActivityTypeWatching, + } + + if !utils.Contains(validActivities, data.StatusType) { + ctx.JSON(400, utils.ErrorStr("Invalid status type")) + return + } + + // Update in database + if err := database.Client.WhitelabelStatuses.Set(bot.BotId, data.Status, int16(data.StatusType)); err != nil { + ctx.JSON(500, utils.ErrorJson(err)) + return + } + + // Send status update to sharder go statusupdates.Publish(redis.Client.Client, bot.BotId) - ctx.JSON(200, gin.H{ - "success": true, - "bot": bot, - }) + ctx.JSON(200, utils.SuccessResponse) } diff --git a/app/http/endpoints/root/webchatws.go b/app/http/endpoints/root/webchatws.go index 0126861..4a64e71 100644 --- a/app/http/endpoints/root/webchatws.go +++ b/app/http/endpoints/root/webchatws.go @@ -53,7 +53,6 @@ var timeout = time.Second * 60 func WebChatWs(ctx *gin.Context) { conn, err := upgrader.Upgrade(ctx.Writer, ctx.Request, nil) if err != nil { - fmt.Println(err.Error()) return } @@ -95,7 +94,6 @@ func WebChatWs(ctx *gin.Context) { for { err := conn.WriteMessage(websocket.PingMessage, []byte("keepalive")) if err != nil { - fmt.Println(err.Error()) conn.Close() conn.CloseHandler()(1000, "") return @@ -156,7 +154,6 @@ func WebChatWs(ctx *gin.Context) { // Verify the user has permissions to be here permLevel, err := utils.GetPermissionLevel(authData.GuildId, userId) if err != nil { - fmt.Println(err.Error()) conn.Close() return } diff --git a/chatreplica/proxy.go b/chatreplica/proxy.go index 40cb83f..378abfd 100644 --- a/chatreplica/proxy.go +++ b/chatreplica/proxy.go @@ -30,8 +30,6 @@ func Render(payload Payload) ([]byte, error) { } if res.StatusCode != 200 { - fmt.Println(string(encoded)) - sentry.CaptureEvent(&sentry.Event{ Extra: map[string]interface{}{ "request_body": string(encoded), diff --git a/frontend/src/views/Whitelabel.svelte b/frontend/src/views/Whitelabel.svelte index e0ec06b..5cf2e68 100644 --- a/frontend/src/views/Whitelabel.svelte +++ b/frontend/src/views/Whitelabel.svelte @@ -1,99 +1,111 @@
-
-
- -

Bot Token

-
-
- - -

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

+
+
+ +

Bot Token

+
+ + + +

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

-
-
- -
-
- -
-
- +
+
+ +
+
+ +
+
+ +
+
- -
-
- -

Slash Commands

-
-
- - +
+ +

Slash Commands

+
+ + + - - + + -
-
- -
-
- -
-
- +
+
+ +
+
+ +
+
+ +
+
-
-
-
-
- -

Custom Status

-
-
- - +
+
+ +

Custom Status

+
+ +
+ + + + + -
- -
- +
+ +
+
+ +
+ +
+ +
+
- -
-
- -

Error Log

-
- - - - - - - - - {#each errors as error} - - - - - {/each} - -
ErrorTime
{error.message}{error.time.toLocaleString()}
+
+ +

Error Log

+
+ + + + + + + + + {#each errors as error} + + + + + {/each} + +
ErrorTime
{error.message}{error.time.toLocaleString()}
+
+
-
-