diff --git a/app/http/endpoints/manage/ticketclose.go b/app/http/endpoints/manage/ticketclose.go new file mode 100644 index 0000000..9fdd953 --- /dev/null +++ b/app/http/endpoints/manage/ticketclose.go @@ -0,0 +1,79 @@ +package manage + +import ( + "fmt" + "github.com/TicketsBot/GoPanel/cache" + "github.com/TicketsBot/GoPanel/config" + "github.com/TicketsBot/GoPanel/database/table" + "github.com/TicketsBot/GoPanel/utils" + "github.com/TicketsBot/GoPanel/utils/discord/objects" + "github.com/gin-gonic/contrib/sessions" + "github.com/gin-gonic/gin" + "strconv" +) + +func TicketCloseHandler(ctx *gin.Context) { + store := sessions.Default(ctx) + if store == nil { + return + } + defer store.Save() + + if utils.IsLoggedIn(store) { + userIdStr := store.Get("userid").(string) + userId, err := utils.GetUserId(store) + if err != nil { + ctx.String(500, err.Error()) + return + } + + // Verify the guild exists + guildIdStr := ctx.Param("id") + guildId, err := strconv.ParseInt(guildIdStr, 10, 64) + if err != nil { + ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 404 Page + return + } + + // Get object for selected guild + var guild objects.Guild + for _, g := range table.GetGuilds(userIdStr) { + if g.Id == guildIdStr { + guild = g + break + } + } + + // Verify the user has permissions to be here + if !utils.Contains(config.Conf.Admins, userIdStr) && !guild.Owner && !table.IsAdmin(guildId, userId) { + ctx.Redirect(302, config.Conf.Server.BaseUrl) // TODO: 403 Page + return + } + + // Get CSRF token + csrfCorrect := ctx.PostForm("csrf") == store.Get("csrf").(string) + if !csrfCorrect { + ctx.Redirect(302, "/") + return + } + + // Get the UUID + uuid := ctx.Param("uuid") + + // Verify that tbe ticket exists + ticketChan := make(chan table.Ticket) + go table.GetTicket(uuid, ticketChan) + ticket := <-ticketChan + + if ticket.Uuid == "" { + ctx.Redirect(302, fmt.Sprintf("/manage/%d/tickets/view/%s?sucess=false", guildId, uuid)) + return + } + + go cache.Client.PublishTicketClose(ticket.Uuid, userId, "") // TODO: Add option for reason + + ctx.Redirect(302, fmt.Sprintf("/manage/%d/tickets", guildId)) + } else { + ctx.Redirect(302, "/login") + } +} diff --git a/cache/ticketclose.go b/cache/ticketclose.go new file mode 100644 index 0000000..e0287da --- /dev/null +++ b/cache/ticketclose.go @@ -0,0 +1,28 @@ +package cache + +import ( + "encoding/json" + "github.com/apex/log" +) + +type TicketCloseMessage struct { + Uuid string + User int64 + Reason string +} + +func (c *RedisClient) PublishTicketClose(ticket string, userId int64, reason string) { + settings := TicketCloseMessage{ + Uuid: ticket, + User: userId, + Reason: reason, + } + + encoded, err := json.Marshal(settings); if err != nil { + log.Error(err.Error()) + return + } + + c.Publish("tickets:close", string(encoded)) +} +