Better logging

This commit is contained in:
rxdn 2024-11-17 15:26:58 +00:00
parent fd1dbf7798
commit a1eae9b4bb
4 changed files with 57 additions and 82 deletions

View File

@ -1,29 +1,30 @@
package api package api
import ( import (
"context" "github.com/TicketsBot/GoPanel/app"
"github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/GoPanel/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http"
"strconv" "strconv"
) )
func GetPermissionLevel(ctx *gin.Context) { func GetPermissionLevel(c *gin.Context) {
userId := ctx.Keys["userid"].(uint64) userId := c.Keys["userid"].(uint64)
guildId, err := strconv.ParseUint(ctx.Query("guild"), 10, 64) guildId, err := strconv.ParseUint(c.Query("guild"), 10, 64)
if err != nil { if err != nil {
ctx.JSON(400, utils.ErrorStr("Invalid guild ID")) c.JSON(400, utils.ErrorStr("Invalid guild ID"))
return return
} }
// TODO: Use proper context // TODO: Use proper context
permissionLevel, err := utils.GetPermissionLevel(context.Background(), guildId, userId) permissionLevel, err := utils.GetPermissionLevel(c, guildId, userId)
if err != nil { if err != nil {
ctx.JSON(500, utils.ErrorJson(err)) _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err))
return return
} }
ctx.JSON(200, gin.H{ c.JSON(200, gin.H{
"success": true, "success": true,
"permission_level": permissionLevel, "permission_level": permissionLevel,
}) })

View File

@ -2,11 +2,13 @@ package api
import ( import (
"context" "context"
"github.com/TicketsBot/GoPanel/app"
"github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/redis" "github.com/TicketsBot/GoPanel/redis"
"github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/GoPanel/utils"
"github.com/TicketsBot/common/closerelay" "github.com/TicketsBot/common/closerelay"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http"
"strconv" "strconv"
) )
@ -14,55 +16,44 @@ type closeBody struct {
Reason string `json:"reason"` Reason string `json:"reason"`
} }
func CloseTicket(ctx *gin.Context) { func CloseTicket(c *gin.Context) {
userId := ctx.Keys["userid"].(uint64) userId := c.Keys["userid"].(uint64)
guildId := ctx.Keys["guildid"].(uint64) guildId := c.Keys["guildid"].(uint64)
ticketId, err := strconv.Atoi(ctx.Param("ticketId")) ticketId, err := strconv.Atoi(c.Param("ticketId"))
if err != nil { if err != nil {
ctx.JSON(400, gin.H{ c.JSON(http.StatusBadRequest, utils.ErrorStr("Invalid ticket ID"))
"success": true,
"error": "Invalid ticket ID",
})
return return
} }
var body closeBody var body closeBody
if err := ctx.BindJSON(&body); err != nil { if err := c.BindJSON(&body); err != nil {
ctx.JSON(400, gin.H{ c.JSON(400, utils.ErrorStr("Invalid request body"))
"success": false,
"error": "Missing reason",
})
return return
} }
// Get the ticket struct // Get the ticket struct
ticket, err := database.Client.Tickets.Get(ctx, ticketId, guildId) ticket, err := database.Client.Tickets.Get(c, ticketId, guildId)
if err != nil { if err != nil {
ctx.JSON(500, gin.H{ _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err))
"success": true,
"error": err.Error(),
})
return return
} }
// Verify the ticket exists // Verify the ticket exists
if ticket.UserId == 0 { if ticket.UserId == 0 {
ctx.JSON(404, gin.H{ c.JSON(http.StatusNotFound, utils.ErrorStr("Ticket not found"))
"success": false,
"error": "Ticket does not exist",
})
return return
} }
hasPermission, requestErr := utils.HasPermissionToViewTicket(context.Background(), guildId, userId, ticket) hasPermission, requestErr := utils.HasPermissionToViewTicket(context.Background(), guildId, userId, ticket)
if requestErr != nil { if requestErr != nil {
ctx.JSON(requestErr.StatusCode, utils.ErrorJson(requestErr)) // TODO
c.JSON(requestErr.StatusCode, utils.ErrorJson(requestErr))
return return
} }
if !hasPermission { if !hasPermission {
ctx.JSON(403, utils.ErrorStr("You do not have permission to close this ticket")) c.JSON(http.StatusForbidden, utils.ErrorStr("You do not have permission to close this ticket"))
return return
} }
@ -74,9 +65,9 @@ func CloseTicket(ctx *gin.Context) {
} }
if err := closerelay.Publish(redis.Client.Client, data); err != nil { if err := closerelay.Publish(redis.Client.Client, data); err != nil {
ctx.JSON(500, utils.ErrorJson(err)) _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err))
return return
} }
ctx.JSON(200, utils.SuccessResponse) c.JSON(200, utils.SuccessResponse)
} }

View File

@ -2,6 +2,7 @@ package api
import ( import (
"context" "context"
"github.com/TicketsBot/GoPanel/app"
"github.com/TicketsBot/GoPanel/botcontext" "github.com/TicketsBot/GoPanel/botcontext"
dbclient "github.com/TicketsBot/GoPanel/database" dbclient "github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/GoPanel/utils"
@ -11,6 +12,7 @@ import (
"github.com/rxdn/gdl/objects/channel/embed" "github.com/rxdn/gdl/objects/channel/embed"
"github.com/rxdn/gdl/objects/user" "github.com/rxdn/gdl/objects/user"
"github.com/rxdn/gdl/rest" "github.com/rxdn/gdl/rest"
"net/http"
"regexp" "regexp"
"strconv" "strconv"
"time" "time"
@ -18,83 +20,63 @@ import (
var MentionRegex, _ = regexp.Compile("<@(\\d+)>") var MentionRegex, _ = regexp.Compile("<@(\\d+)>")
func GetTicket(ctx *gin.Context) { func GetTicket(c *gin.Context) {
guildId := ctx.Keys["guildid"].(uint64) guildId := c.Keys["guildid"].(uint64)
userId := ctx.Keys["userid"].(uint64) userId := c.Keys["userid"].(uint64)
botContext, err := botcontext.ContextForGuild(guildId) botContext, err := botcontext.ContextForGuild(guildId)
if err != nil { if err != nil {
ctx.JSON(500, gin.H{ _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err))
"success": false,
"error": err.Error(),
})
return return
} }
ticketId, err := strconv.Atoi(ctx.Param("ticketId")) ticketId, err := strconv.Atoi(c.Param("ticketId"))
if err != nil { if err != nil {
ctx.JSON(400, gin.H{ c.JSON(http.StatusBadRequest, utils.ErrorStr("Invalid ticket ID"))
"success": true,
"error": "Invalid ticket ID",
})
return return
} }
// Get the ticket struct // Get the ticket struct
ticket, err := dbclient.Client.Tickets.Get(ctx, ticketId, guildId) ticket, err := dbclient.Client.Tickets.Get(c, ticketId, guildId)
if err != nil { if err != nil {
ctx.JSON(500, gin.H{ _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err))
"success": true,
"error": err.Error(),
})
return return
} }
if ticket.GuildId != guildId { if ticket.GuildId != guildId {
ctx.JSON(403, gin.H{ c.JSON(http.StatusForbidden, utils.ErrorStr("Ticket does not belong to guild"))
"success": false,
"error": "Guild ID doesn't match",
})
return return
} }
if !ticket.Open { if !ticket.Open {
ctx.JSON(404, gin.H{ c.JSON(http.StatusNotFound, utils.ErrorStr("Ticket is closed"))
"success": false,
"error": "Ticket does not exist",
})
return return
} }
hasPermission, requestErr := utils.HasPermissionToViewTicket(context.Background(), guildId, userId, ticket) hasPermission, requestErr := utils.HasPermissionToViewTicket(c, guildId, userId, ticket)
if requestErr != nil { if requestErr != nil {
ctx.JSON(requestErr.StatusCode, utils.ErrorJson(requestErr)) // TODO
c.JSON(requestErr.StatusCode, utils.ErrorJson(requestErr))
return return
} }
if !hasPermission { if !hasPermission {
ctx.JSON(403, utils.ErrorStr("You do not have permission to view this ticket")) c.JSON(http.StatusForbidden, utils.ErrorStr("You do not have permission to view this ticket"))
return return
} }
if ticket.ChannelId == nil { if ticket.ChannelId == nil {
ctx.JSON(404, gin.H{ c.JSON(http.StatusNotFound, utils.ErrorStr("Ticket channel not found"))
"success": false,
"error": "Channel ID is nil",
})
return return
} }
messages, err := fetchMessages(botContext, ticket) messages, err := fetchMessages(botContext, ticket)
if err != nil { if err != nil {
ctx.JSON(500, gin.H{ _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err))
"success": false,
"error": err.Error(),
})
return return
} }
ctx.JSON(200, gin.H{ c.JSON(200, gin.H{
"success": true, "success": true,
"ticket": ticket, "ticket": ticket,
"messages": messages, "messages": messages,

View File

@ -1,11 +1,12 @@
package api package api
import ( import (
"github.com/TicketsBot/GoPanel/app"
"github.com/TicketsBot/GoPanel/database" "github.com/TicketsBot/GoPanel/database"
"github.com/TicketsBot/GoPanel/rpc/cache" "github.com/TicketsBot/GoPanel/rpc/cache"
"github.com/TicketsBot/GoPanel/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rxdn/gdl/objects/user" "github.com/rxdn/gdl/objects/user"
"net/http"
"time" "time"
) )
@ -28,19 +29,19 @@ type (
} }
) )
func GetTickets(ctx *gin.Context) { func GetTickets(c *gin.Context) {
userId := ctx.Keys["userid"].(uint64) userId := c.Keys["userid"].(uint64)
guildId := ctx.Keys["guildid"].(uint64) guildId := c.Keys["guildid"].(uint64)
tickets, err := database.Client.Tickets.GetGuildOpenTicketsWithMetadata(ctx, guildId) tickets, err := database.Client.Tickets.GetGuildOpenTicketsWithMetadata(c, guildId)
if err != nil { if err != nil {
ctx.JSON(500, utils.ErrorJson(err)) _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err))
return return
} }
panels, err := database.Client.Panel.GetByGuild(ctx, guildId) panels, err := database.Client.Panel.GetByGuild(c, guildId)
if err != nil { if err != nil {
ctx.JSON(500, utils.ErrorJson(err)) _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err))
return return
} }
@ -59,9 +60,9 @@ func GetTickets(ctx *gin.Context) {
} }
} }
users, err := cache.Instance.GetUsers(ctx, userIds) users, err := cache.Instance.GetUsers(c, userIds)
if err != nil { if err != nil {
ctx.JSON(500, utils.ErrorJson(err)) _ = c.AbortWithError(http.StatusInternalServerError, app.NewServerError(err))
return return
} }
@ -78,7 +79,7 @@ func GetTickets(ctx *gin.Context) {
} }
} }
ctx.JSON(200, listTicketsResponse{ c.JSON(200, listTicketsResponse{
Tickets: data, Tickets: data,
PanelTitles: panelTitles, PanelTitles: panelTitles,
ResolvedUsers: users, ResolvedUsers: users,