From 64d9f51f19f1b25ab65d27ff88c659c5c9167c57 Mon Sep 17 00:00:00 2001 From: Sotirios Pupakis Date: Sun, 7 Sep 2025 05:13:06 +0200 Subject: [PATCH] discord embed changes --- config.yaml | 1 + controllers/adminController.go | 2 + controllers/authController.go | 9 ++++ controllers/indexerController.go | 82 +++++++++++--------------------- initializers/initialize.go | 3 ++ main.go | 7 +++ models/discordhook.go | 7 +++ 7 files changed, 58 insertions(+), 53 deletions(-) create mode 100644 controllers/adminController.go diff --git a/config.yaml b/config.yaml index fcce8eb..9c14f16 100644 --- a/config.yaml +++ b/config.yaml @@ -3,3 +3,4 @@ ip: "localhost" secret: "secret" db: "crawler.db" discordWebhook: "https://discord.com/api/webhooks/1413673169792925787/bfbCJ8wiFeKsYkLGCp1U8sr5jOOWDcF0NP9DsHuGXy8NjrfKOncYWJbPnROophTXr-kH" +adminSecret: "123456789" \ No newline at end of file diff --git a/controllers/adminController.go b/controllers/adminController.go new file mode 100644 index 0000000..083d62a --- /dev/null +++ b/controllers/adminController.go @@ -0,0 +1,2 @@ +package controllers + diff --git a/controllers/authController.go b/controllers/authController.go index 748c3c3..7a20636 100644 --- a/controllers/authController.go +++ b/controllers/authController.go @@ -15,3 +15,12 @@ func CheckAllowed(c *gin.Context) { return } } + +func CheckAdmin(c *gin.Context) { + adminSecret := c.GetHeader("X-ADMIN-KEY") + if adminSecret != initializers.ADMIN_SECRET { + c.JSON(http.StatusForbidden, gin.H{"error": "Forbidden"}) + c.Abort() + return + } +} diff --git a/controllers/indexerController.go b/controllers/indexerController.go index 7f7c545..1adc480 100644 --- a/controllers/indexerController.go +++ b/controllers/indexerController.go @@ -12,8 +12,6 @@ import ( "gitea.tbdevent.eu/TBD/reforger_crawler_main/models" "github.com/gin-gonic/gin" "gorm.io/gorm" - - discordwebhook "github.com/bensch777/discord-webhook-golang" ) func GetNextToBeIndexed(c *gin.Context) { @@ -85,20 +83,26 @@ func SaveIndexingResult(c *gin.Context) { return } + ret = initializers.DB.CreateInBatches(&result.Files, 100) + if ret.Error != nil { + c.JSON(500, gin.H{"error": ret.Error.Error()}) + return + } + addon.CurrentVersionNumber = result.CurrentVersion addon.IsBeingIndexed = false addon.ToBeIndexed = false - initializers.DB.Save(&addon) - - // Add new files with versionID - for _, file := range result.Files { - initializers.DB.Create(&file) + ret = initializers.DB.Save(&addon) + if ret.Error != nil { + c.JSON(500, gin.H{"error": ret.Error.Error()}) + return } if initializers.DiscordWebhook != "" { - text := "Indexing of addon " + addon.ID + " (" + addon.Name + ") was successful." + text := "Indexing of addon " + addon.Name + " (" + addon.ID + ") was successful." colour := 2228479 nbr := strconv.Itoa(len(result.Files)) + size := fmt.Sprintf("%.2f MB", float64(addon.CurrentVersionSize)/1_000_000) addonsToBeIndexed := int64(0) addonsCount := int64(0) @@ -120,6 +124,23 @@ func SaveIndexingResult(c *gin.Context) { Image: models.CustomImage{ URL: addon.Preview, }, + Fields: []models.CustomEmbedField{ + { + Name: "Size", + Value: size, + Inline: true, + }, + { + Name: "Current Version", + Value: addon.CurrentVersionNumber, + Inline: true, + }, + // { + // Name: "Indexed by", + // Value: c.ClientIP(), + // Inline: false, + // }, + }, } myHook := models.CustomHook{ @@ -131,41 +152,6 @@ func SaveIndexingResult(c *gin.Context) { if err != nil { fmt.Println("Error sending webhook:", err) } - - /*embed := discordwebhook.Embed{ - Title: text, - Thumbnail: discordwebhook.Thumbnail{ - Url: addon.Preview, - }, - Description: txt, - Color: colour, - Timestamp: time.Now(), - Fields: []discordwebhook.Field{ - discordwebhook.Field{ - Name: "Name", - Value: addon.Name, - Inline: true, - }, - discordwebhook.Field{ - Name: "GUID", - Value: addon.ID, - Inline: true, - }, - discordwebhook.Field{ - Name: "Version", - Value: addon.CurrentVersionNumber, - Inline: false, - }, - discordwebhook.Field{ - Name: "Files Indexed", - Value: nbr, - Inline: false, - }, - }, - - } - - sendEmbed(initializers.DiscordWebhook, embed)*/ } c.JSON(200, gin.H{"status": "success"}) @@ -183,16 +169,6 @@ func checkIndexingTimeout() { } } -func sendEmbed(webhookUrl string, embed discordwebhook.Embed) { - hook := discordwebhook.Hook{ - Username: "Reforger Crawler", - Embeds: []discordwebhook.Embed{embed}, - } - - payload, _ := json.Marshal(hook) - discordwebhook.ExecuteWebhook(webhookUrl, payload) -} - func SendCustomWebhook(webhookURL string, hook models.CustomHook) error { payload, err := json.Marshal(hook) if err != nil { diff --git a/initializers/initialize.go b/initializers/initialize.go index 6cb691a..36840d1 100644 --- a/initializers/initialize.go +++ b/initializers/initialize.go @@ -16,6 +16,7 @@ var IP string var SECRET string var DB_NAME string var DiscordWebhook string +var ADMIN_SECRET string func ConnectToDB() { db, err := gorm.Open(sqlite.Open(DB_NAME), &gorm.Config{}) @@ -34,6 +35,7 @@ type Configuration struct { Secret string `yaml:"secret"` DB string `yaml:"db"` DiscordWebhook string `yaml:"discordWebhook"` + ADMIN_SECRET string `yaml:"adminSecret"` } func Load() { @@ -55,4 +57,5 @@ file, err := os.ReadFile("config.yaml") SECRET = configuration.Secret DB_NAME = configuration.DB DiscordWebhook = configuration.DiscordWebhook + ADMIN_SECRET = configuration.ADMIN_SECRET } diff --git a/main.go b/main.go index ec0d221..0da4beb 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,13 @@ func main() { back.POST("/submitAddon", controllers.CheckAllowed, controllers.SaveIndexingResult) } + admin := r.Group("/admin") + { + admin.GET("/duplicates", controllers.CheckAdmin) + admin.POST("/setPriority", controllers.CheckAdmin) + admin.POST("/setToBeIndexed", controllers.CheckAdmin) + } + r.Run(initializers.IP + ":" + initializers.PORT) } diff --git a/models/discordhook.go b/models/discordhook.go index a1c5d22..af50bf0 100644 --- a/models/discordhook.go +++ b/models/discordhook.go @@ -16,9 +16,16 @@ type CustomEmbed struct { Timestamp time.Time `json:"timestamp,omitempty"` Color int `json:"color,omitempty"` Image CustomImage `json:"image,omitempty"` // Your new field + Fields []CustomEmbedField `json:"fields,omitempty"` // You can add other fields like Footer, Author, Fields as needed. } +type CustomEmbedField struct { + Name string `json:"name,omitempty"` + Value string `json:"value,omitempty"` + Inline bool `json:"inline,omitempty"` +} + // CustomHook is the top-level structure for the webhook payload. type CustomHook struct { Username string `json:"username,omitempty"`