From 1bbd4a785bb12b3fda7cae0401c6c183b8f69e9f Mon Sep 17 00:00:00 2001 From: rxdn <29165304+rxdn@users.noreply.github.com> Date: Tue, 6 Jul 2021 17:13:08 +0100 Subject: [PATCH] Autoclose settings --- .../endpoints/api/autoclose/autocloseget.go | 55 +++++++- .../endpoints/api/autoclose/autoclosepost.go | 55 ++++---- frontend/src/components/form/Duration.svelte | 117 +++++++++++++++++ .../src/components/form/EmojiInput.svelte | 1 + .../components/manage/AutoCloseCard.svelte | 118 +++++++++++++++--- frontend/src/js/timeutil.js | 11 ++ frontend/src/views/Panels.svelte | 3 +- frontend/src/views/Settings.svelte | 6 +- 8 files changed, 314 insertions(+), 52 deletions(-) create mode 100644 frontend/src/components/form/Duration.svelte create mode 100644 frontend/src/js/timeutil.js diff --git a/app/http/endpoints/api/autoclose/autocloseget.go b/app/http/endpoints/api/autoclose/autocloseget.go index 16752e4..66a97e4 100644 --- a/app/http/endpoints/api/autoclose/autocloseget.go +++ b/app/http/endpoints/api/autoclose/autocloseget.go @@ -1,20 +1,67 @@ package api import ( - "github.com/TicketsBot/GoPanel/database" + dbclient "github.com/TicketsBot/GoPanel/database" + "github.com/TicketsBot/database" "github.com/gin-gonic/gin" + "time" ) +// time.Duration marshals to nanoseconds, custom impl to marshal to seconds +type autoCloseBody struct { + Enabled bool `json:"enabled"` + SinceOpenWithNoResponse int64 `json:"since_open_with_no_response"` + SinceLastMessage int64 `json:"since_last_message"` + OnUserLeave bool `json:"on_user_leave"` +} + func GetAutoClose(ctx *gin.Context) { guildId := ctx.Keys["guildid"].(uint64) - settings, err := database.Client.AutoClose.Get(guildId); if err != nil { + settings, err := dbclient.Client.AutoClose.Get(guildId) + if err != nil { ctx.AbortWithStatusJSON(500, gin.H{ "success": false, - "error": err.Error(), + "error": err.Error(), }) return } - ctx.JSON(200, settings) + ctx.JSON(200, convertToAutoCloseBody(settings)) +} + +func convertToAutoCloseBody(settings database.AutoCloseSettings) (body autoCloseBody) { + body.Enabled = settings.Enabled + + if settings.SinceOpenWithNoResponse != nil { + body.SinceOpenWithNoResponse = int64(*settings.SinceOpenWithNoResponse / time.Second) + } + + if settings.SinceLastMessage != nil { + body.SinceLastMessage = int64(*settings.SinceLastMessage / time.Second) + } + + if settings.OnUserLeave != nil { + body.OnUserLeave = *settings.OnUserLeave + } + + return +} + +func convertFromAutoCloseBody(body autoCloseBody) (settings database.AutoCloseSettings) { + settings.Enabled = body.Enabled + + if body.SinceOpenWithNoResponse > 0 { + duration := time.Second * time.Duration(body.SinceOpenWithNoResponse) + settings.SinceOpenWithNoResponse = &duration + } + + if body.SinceLastMessage > 0 { + duration := time.Second * time.Duration(body.SinceLastMessage) + settings.SinceLastMessage = &duration + } + + settings.OnUserLeave = &body.OnUserLeave + + return } diff --git a/app/http/endpoints/api/autoclose/autoclosepost.go b/app/http/endpoints/api/autoclose/autoclosepost.go index c44c6db..ecd555b 100644 --- a/app/http/endpoints/api/autoclose/autoclosepost.go +++ b/app/http/endpoints/api/autoclose/autoclosepost.go @@ -1,36 +1,48 @@ package api import ( + "github.com/TicketsBot/GoPanel/botcontext" dbclient "github.com/TicketsBot/GoPanel/database" - "github.com/TicketsBot/database" + "github.com/TicketsBot/GoPanel/rpc" + "github.com/TicketsBot/GoPanel/utils" + "github.com/TicketsBot/common/premium" "github.com/gin-gonic/gin" + "time" ) +var maxDays = 90 +var maxLength = time.Hour * 24 * time.Duration(maxDays) + func PostAutoClose(ctx *gin.Context) { guildId := ctx.Keys["guildid"].(uint64) - var settings database.AutoCloseSettings - if err := ctx.BindJSON(&settings); err != nil { - ctx.AbortWithStatusJSON(400, gin.H{ - "success": false, - "error": err.Error(), - }) + var body autoCloseBody + if err := ctx.BindJSON(&body); err != nil { + ctx.JSON(400, utils.ErrorJson(err)) return } - if settings.Enabled && (settings.SinceLastMessage == nil || settings.SinceOpenWithNoResponse == nil || settings.OnUserLeave == nil) { - ctx.AbortWithStatusJSON(400, gin.H{ - "success": false, - "error": "No time period provided", - }) + settings := convertFromAutoCloseBody(body) + + // get premium + botContext, err := botcontext.ContextForGuild(guildId) + if err != nil { + ctx.JSON(500, utils.ErrorJson(err)) return } - if (settings.SinceOpenWithNoResponse != nil && *settings.SinceOpenWithNoResponse < 0) || (settings.SinceLastMessage != nil && *settings.SinceLastMessage < 0) { - ctx.AbortWithStatusJSON(400, gin.H{ - "success": false, - "error": "Negative time period provided", - }) + premiumTier := rpc.PremiumClient.GetTierByGuildId(guildId, true, botContext.Token, botContext.RateLimiter) + + if premiumTier < premium.Premium { + settings.SinceOpenWithNoResponse = nil + settings.SinceLastMessage = nil + } + + // Time period cannot be negative, convertFromAutoCloseBody will not allow + + if (settings.SinceOpenWithNoResponse != nil && *settings.SinceOpenWithNoResponse > maxLength) || + (settings.SinceLastMessage != nil && *settings.SinceLastMessage > maxLength) { + ctx.JSON(400, utils.ErrorStr("Time period cannot be longer than %d days", maxDays)) return } @@ -41,14 +53,9 @@ func PostAutoClose(ctx *gin.Context) { } if err := dbclient.Client.AutoClose.Set(guildId, settings); err != nil { - ctx.AbortWithStatusJSON(500, gin.H{ - "success": false, - "error": err.Error(), - }) + ctx.JSON(500, utils.ErrorJson(err)) return } - ctx.JSON(200, gin.H{ - "success": true, - }) + ctx.JSON(200, utils.SuccessResponse) } diff --git a/frontend/src/components/form/Duration.svelte b/frontend/src/components/form/Duration.svelte new file mode 100644 index 0000000..c80e41d --- /dev/null +++ b/frontend/src/components/form/Duration.svelte @@ -0,0 +1,117 @@ +