From 9d9b2ac866fcecb13dedaaf8bff72dacc1734493 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 30 Jan 2025 22:29:03 +0800 Subject: [PATCH] :zap: Save post insight result in database --- pkg/internal/database/migrator.go | 1 + pkg/internal/http/api/insight_api.go | 20 +++----------------- pkg/internal/models/posts.go | 11 ++++++++++- pkg/internal/services/insight.go | 20 ++++++++++++++++---- pkg/internal/services/posts.go | 3 +++ 5 files changed, 33 insertions(+), 22 deletions(-) diff --git a/pkg/internal/database/migrator.go b/pkg/internal/database/migrator.go index 0930c3c..39429bb 100644 --- a/pkg/internal/database/migrator.go +++ b/pkg/internal/database/migrator.go @@ -10,6 +10,7 @@ var AutoMaintainRange = []any{ &models.Category{}, &models.Tag{}, &models.Post{}, + &models.PostInsight{}, &models.Subscription{}, } diff --git a/pkg/internal/http/api/insight_api.go b/pkg/internal/http/api/insight_api.go index 2cfcc72..23a92d8 100644 --- a/pkg/internal/http/api/insight_api.go +++ b/pkg/internal/http/api/insight_api.go @@ -1,18 +1,14 @@ package api import ( - "context" "strconv" "strings" - localCache "git.solsynth.dev/hypernet/interactive/pkg/internal/cache" "git.solsynth.dev/hypernet/interactive/pkg/internal/database" "git.solsynth.dev/hypernet/interactive/pkg/internal/models" "git.solsynth.dev/hypernet/interactive/pkg/internal/services" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" - "github.com/eko/gocache/lib/v4/cache" - "github.com/eko/gocache/lib/v4/marshaler" "github.com/gofiber/fiber/v2" ) @@ -51,19 +47,9 @@ func getPostInsight(c *fiber.Ctx) error { return fiber.NewError(fiber.StatusNotFound, err.Error()) } - cacheManager := cache.New[any](localCache.S) - marshal := marshaler.New(cacheManager) - contx := context.Background() - - var response string - if val, err := marshal.Get(contx, services.GetPostInsightCacheKey(item.ID), new(string)); err == nil { - response = *(val.(*string)) - } else { - response, err = services.GeneratePostInsights(item, user.ID) - if err != nil { - return fiber.NewError(fiber.StatusBadRequest, err.Error()) - } - marshal.Set(contx, services.GetPostInsightCacheKey(item.ID), response) + response, err := services.GeneratePostInsights(item, user.ID) + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) } return c.JSON(fiber.Map{ diff --git a/pkg/internal/models/posts.go b/pkg/internal/models/posts.go index b9e09fa..f862fa0 100644 --- a/pkg/internal/models/posts.go +++ b/pkg/internal/models/posts.go @@ -1,9 +1,10 @@ package models import ( + "time" + "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" - "time" "gorm.io/datatypes" ) @@ -79,3 +80,11 @@ type PostArticleBody struct { Content string `json:"content"` Attachments []string `json:"attachments"` } + +type PostInsight struct { + cruda.BaseModel + + Response string `json:"response"` + Post Post `json:"post"` + PostID uint `json:"post_id"` +} diff --git a/pkg/internal/services/insight.go b/pkg/internal/services/insight.go index 7e767ef..1c7e5d7 100644 --- a/pkg/internal/services/insight.go +++ b/pkg/internal/services/insight.go @@ -7,15 +7,18 @@ import ( "time" iproto "git.solsynth.dev/hypernet/insight/pkg/proto" + "git.solsynth.dev/hypernet/interactive/pkg/internal/database" "git.solsynth.dev/hypernet/interactive/pkg/internal/gap" "git.solsynth.dev/hypernet/interactive/pkg/internal/models" + "github.com/rs/zerolog/log" ) -func GetPostInsightCacheKey(postId uint) string { - return fmt.Sprintf("post-insight-%d", postId) -} - func GeneratePostInsights(post models.Post, user uint) (string, error) { + var insight models.PostInsight + if err := database.C.Where("post_id = ?", post.ID).First(&insight).Error; err == nil { + return insight.Response, nil + } + var compactBuilder []string if val, ok := post.Body["title"].(string); ok && len(val) > 0 { compactBuilder = append(compactBuilder, "Title: "+val) @@ -44,5 +47,14 @@ func GeneratePostInsights(post models.Post, user uint) (string, error) { return "", err } + insight = models.PostInsight{ + Response: resp.Response, + Post: post, + PostID: post.ID, + } + if err := database.C.Create(&insight).Error; err != nil { + log.Error().Err(err).Msg("Failed to create post insight result in database...") + } + return resp.Response, nil } diff --git a/pkg/internal/services/posts.go b/pkg/internal/services/posts.go index 2fc1d4c..454a638 100644 --- a/pkg/internal/services/posts.go +++ b/pkg/internal/services/posts.go @@ -558,6 +558,9 @@ func DeletePost(item models.Post) error { }), UserId: lo.ToPtr(uint64(*item.Publisher.AccountID)), }) + if err != nil { + log.Error().Err(err).Msg("An error occurred when deleting post attachment...") + } } return nil