⚡ Save post insight result in database
This commit is contained in:
parent
39ef6f9e0a
commit
9d9b2ac866
@ -10,6 +10,7 @@ var AutoMaintainRange = []any{
|
||||
&models.Category{},
|
||||
&models.Tag{},
|
||||
&models.Post{},
|
||||
&models.PostInsight{},
|
||||
&models.Subscription{},
|
||||
}
|
||||
|
||||
|
@ -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,20 +47,10 @@ 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)
|
||||
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{
|
||||
"response": response,
|
||||
|
@ -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"`
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
func GeneratePostInsights(post models.Post, user uint) (string, error) {
|
||||
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
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user