Save post insight result in database

This commit is contained in:
LittleSheep 2025-01-30 22:29:03 +08:00
parent 39ef6f9e0a
commit 9d9b2ac866
5 changed files with 33 additions and 22 deletions

View File

@ -10,6 +10,7 @@ var AutoMaintainRange = []any{
&models.Category{}, &models.Category{},
&models.Tag{}, &models.Tag{},
&models.Post{}, &models.Post{},
&models.PostInsight{},
&models.Subscription{}, &models.Subscription{},
} }

View File

@ -1,18 +1,14 @@
package api package api
import ( import (
"context"
"strconv" "strconv"
"strings" "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/database"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models" "git.solsynth.dev/hypernet/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/interactive/pkg/internal/services" "git.solsynth.dev/hypernet/interactive/pkg/internal/services"
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" 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" "github.com/gofiber/fiber/v2"
) )
@ -51,19 +47,9 @@ func getPostInsight(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} }
cacheManager := cache.New[any](localCache.S) response, err := services.GeneratePostInsights(item, user.ID)
marshal := marshaler.New(cacheManager) if err != nil {
contx := context.Background() return fiber.NewError(fiber.StatusBadRequest, err.Error())
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)
} }
return c.JSON(fiber.Map{ return c.JSON(fiber.Map{

View File

@ -1,9 +1,10 @@
package models package models
import ( import (
"time"
"git.solsynth.dev/hypernet/nexus/pkg/nex/cruda" "git.solsynth.dev/hypernet/nexus/pkg/nex/cruda"
authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models" authm "git.solsynth.dev/hypernet/passport/pkg/authkit/models"
"time"
"gorm.io/datatypes" "gorm.io/datatypes"
) )
@ -79,3 +80,11 @@ type PostArticleBody struct {
Content string `json:"content"` Content string `json:"content"`
Attachments []string `json:"attachments"` Attachments []string `json:"attachments"`
} }
type PostInsight struct {
cruda.BaseModel
Response string `json:"response"`
Post Post `json:"post"`
PostID uint `json:"post_id"`
}

View File

@ -7,15 +7,18 @@ import (
"time" "time"
iproto "git.solsynth.dev/hypernet/insight/pkg/proto" 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/gap"
"git.solsynth.dev/hypernet/interactive/pkg/internal/models" "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) { 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 var compactBuilder []string
if val, ok := post.Body["title"].(string); ok && len(val) > 0 { if val, ok := post.Body["title"].(string); ok && len(val) > 0 {
compactBuilder = append(compactBuilder, "Title: "+val) compactBuilder = append(compactBuilder, "Title: "+val)
@ -44,5 +47,14 @@ func GeneratePostInsights(post models.Post, user uint) (string, error) {
return "", err 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 return resp.Response, nil
} }

View File

@ -558,6 +558,9 @@ func DeletePost(item models.Post) error {
}), }),
UserId: lo.ToPtr(uint64(*item.Publisher.AccountID)), UserId: lo.ToPtr(uint64(*item.Publisher.AccountID)),
}) })
if err != nil {
log.Error().Err(err).Msg("An error occurred when deleting post attachment...")
}
} }
return nil return nil