From 07256467d322d07a92100fdd7ac9ad08fbe7f5bf Mon Sep 17 00:00:00 2001 From: Dot-Rar Date: Thu, 5 Mar 2020 14:56:00 +0000 Subject: [PATCH] use webhooks --- app/http/endpoints/manage/webchatws.go | 42 +++++++++++++++++-- database/table/ticketwebhook.go | 31 ++++++++++++++ utils/discord/endpoints.go | 5 ++- .../endpoints/webhooks/executewebhook.go | 21 ++++++++++ utils/discord/objects/allowedmention.go | 7 ++++ utils/discord/objects/allowedmentiontype.go | 9 ++++ 6 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 database/table/ticketwebhook.go create mode 100644 utils/discord/endpoints/webhooks/executewebhook.go create mode 100644 utils/discord/objects/allowedmention.go create mode 100644 utils/discord/objects/allowedmentiontype.go diff --git a/app/http/endpoints/manage/webchatws.go b/app/http/endpoints/manage/webchatws.go index d7c4e65..677a9df 100644 --- a/app/http/endpoints/manage/webchatws.go +++ b/app/http/endpoints/manage/webchatws.go @@ -7,10 +7,13 @@ import ( "github.com/TicketsBot/GoPanel/utils" "github.com/TicketsBot/GoPanel/utils/discord" "github.com/TicketsBot/GoPanel/utils/discord/endpoints/channel" + "github.com/TicketsBot/GoPanel/utils/discord/endpoints/webhooks" "github.com/TicketsBot/GoPanel/utils/discord/objects" + "github.com/TicketsBot/TicketsGo/database" "github.com/gin-gonic/contrib/sessions" "github.com/gin-gonic/gin" "github.com/gorilla/websocket" + "net/http" "strconv" "sync" ) @@ -166,10 +169,41 @@ func WebChatWs(ctx *gin.Context) { content = content[0:1999] } - endpoint := channel.CreateMessage(int(ticket.Channel)) - err = endpoint.Request(store, &contentType, channel.CreateMessageBody{ - Content: content, - }, nil, nil) + // Preferably send via a webhook + webhookChan := make(chan *string) + go database.GetWebhookByUuid(ticket.Uuid, webhookChan) + webhook := <-webhookChan + + success := false + if webhook != nil { + endpoint := webhooks.ExecuteWebhook(*webhook) + + resChan := make(chan *http.Response) + err = endpoint.Request(store, &contentType, webhooks.ExecuteWebhookBody{ + Content: content, + Username: store.Get("name").(string), + AvatarUrl: store.Get("avatar").(string), + AllowedMentions: objects.AllowedMention{ + Parse: make([]objects.AllowedMentionType, 0), + Roles: make([]string, 0), + Users: make([]string, 0), + }, + }, nil, &resChan) + res := <-resChan + + if res.StatusCode == 200 { + success = true + } else { + go database.DeleteWebhookByUuid(ticket.Uuid) + } + } + + if !success { + endpoint := channel.CreateMessage(int(ticket.Channel)) + err = endpoint.Request(store, &contentType, channel.CreateMessageBody{ + Content: content, + }, nil, nil) + } } } } diff --git a/database/table/ticketwebhook.go b/database/table/ticketwebhook.go new file mode 100644 index 0000000..fa5a18e --- /dev/null +++ b/database/table/ticketwebhook.go @@ -0,0 +1,31 @@ +package table + +import "github.com/TicketsBot/GoPanel/database" + +type TicketWebhook struct { + Uuid string `gorm:"column:UUID;type:varchar(36);unique;primary_key"` + WebhookUrl string `gorm:"column:CDNURL;type:varchar(200)"` +} + +func (TicketWebhook) TableName() string { + return "webhooks" +} + +func (w *TicketWebhook) AddWebhook() { + database.Database.Create(w) +} + +func DeleteWebhookByUuid(uuid string) { + database.Database.Where(TicketWebhook{Uuid: uuid}).Delete(TicketWebhook{}) +} + +func GetWebhookByUuid(uuid string, res chan *string) { + var row TicketWebhook + database.Database.Where(TicketWebhook{Uuid: uuid}).Take(&row) + + if row.WebhookUrl == "" { + res <- nil + } else { + res <- &row.WebhookUrl + } +} diff --git a/utils/discord/endpoints.go b/utils/discord/endpoints.go index 7df507c..31a0231 100644 --- a/utils/discord/endpoints.go +++ b/utils/discord/endpoints.go @@ -23,6 +23,7 @@ const ( BEARER AuthorizationType = "Bearer" BOT AuthorizationType = "BOT" + NONE AuthorizationType = "NONE" ApplicationJson ContentType = "application/json" ApplicationFormUrlEncoded ContentType = "application/x-www-form-urlencoded" @@ -36,7 +37,7 @@ type Endpoint struct { Endpoint string } -func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, body interface{}, response interface{}, rawResponse *chan string) error { +func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, body interface{}, response interface{}, rawResponse *chan *http.Response) error { url := BASE_URL + e.Endpoint // Create req @@ -118,7 +119,7 @@ func (e *Endpoint) Request(store sessions.Session, contentType *ContentType, bod } if rawResponse != nil { - *rawResponse<-string(content) + *rawResponse<-res } return json.Unmarshal(content, response) diff --git a/utils/discord/endpoints/webhooks/executewebhook.go b/utils/discord/endpoints/webhooks/executewebhook.go new file mode 100644 index 0000000..cd5f113 --- /dev/null +++ b/utils/discord/endpoints/webhooks/executewebhook.go @@ -0,0 +1,21 @@ +package webhooks + +import ( + "github.com/TicketsBot/GoPanel/utils/discord" + "github.com/TicketsBot/GoPanel/utils/discord/objects" +) + +type ExecuteWebhookBody struct { + Content string `json:"content"` + Username string `json:"username"` + AvatarUrl string `json:"avatar_url"` + AllowedMentions objects.AllowedMention `json:"allowed_mentions"` +} + +func ExecuteWebhook(url string) discord.Endpoint { + return discord.Endpoint{ + RequestType: discord.POST, + AuthorizationType: discord.NONE, + Endpoint: url, + } +} diff --git a/utils/discord/objects/allowedmention.go b/utils/discord/objects/allowedmention.go new file mode 100644 index 0000000..4f1eb5c --- /dev/null +++ b/utils/discord/objects/allowedmention.go @@ -0,0 +1,7 @@ +package objects + +type AllowedMention struct { + Parse []AllowedMentionType + Roles []string + Users []string +} diff --git a/utils/discord/objects/allowedmentiontype.go b/utils/discord/objects/allowedmentiontype.go new file mode 100644 index 0000000..f50f029 --- /dev/null +++ b/utils/discord/objects/allowedmentiontype.go @@ -0,0 +1,9 @@ +package objects + +type AllowedMentionType string + +const( + EVERYONE AllowedMentionType = "everyone" + USERS AllowedMentionType = "users" + ROLES AllowedMentionType = "roles" +)