⬆️ Use faster way to check attachment it exists

This commit is contained in:
LittleSheep 2024-05-18 16:59:58 +08:00
parent 827423ae3f
commit 7d36755a72
6 changed files with 17 additions and 15 deletions

2
go.mod
View File

@ -3,7 +3,7 @@ module git.solsynth.dev/hydrogen/interactive
go 1.21.6 go 1.21.6
require ( require (
git.solsynth.dev/hydrogen/paperclip v0.0.0-20240517123600-145c5563a55e git.solsynth.dev/hydrogen/paperclip v0.0.0-20240518085442-715238074040
git.solsynth.dev/hydrogen/passport v0.0.0-20240517123434-ebef35a619f5 git.solsynth.dev/hydrogen/passport v0.0.0-20240517123434-ebef35a619f5
github.com/go-playground/validator/v10 v10.17.0 github.com/go-playground/validator/v10 v10.17.0
github.com/gofiber/fiber/v2 v2.52.4 github.com/gofiber/fiber/v2 v2.52.4

2
go.sum
View File

@ -1,5 +1,7 @@
git.solsynth.dev/hydrogen/paperclip v0.0.0-20240517123600-145c5563a55e h1:jQNErCjKl76zVO2+nkBvKJK5eEkOVhVXQh4FMm8G0Xc= git.solsynth.dev/hydrogen/paperclip v0.0.0-20240517123600-145c5563a55e h1:jQNErCjKl76zVO2+nkBvKJK5eEkOVhVXQh4FMm8G0Xc=
git.solsynth.dev/hydrogen/paperclip v0.0.0-20240517123600-145c5563a55e/go.mod h1:uTNEtJcNdgt7DhOgsewPaLQQ5kTN9H+tGNRT2CshHGs= git.solsynth.dev/hydrogen/paperclip v0.0.0-20240517123600-145c5563a55e/go.mod h1:uTNEtJcNdgt7DhOgsewPaLQQ5kTN9H+tGNRT2CshHGs=
git.solsynth.dev/hydrogen/paperclip v0.0.0-20240518085442-715238074040 h1:B/3gXFaxoLdD7icLu33OXrx166raEHtHdfhCFrPYgTE=
git.solsynth.dev/hydrogen/paperclip v0.0.0-20240518085442-715238074040/go.mod h1:uTNEtJcNdgt7DhOgsewPaLQQ5kTN9H+tGNRT2CshHGs=
git.solsynth.dev/hydrogen/passport v0.0.0-20240517123434-ebef35a619f5 h1:iEnty5+OHZiIaa27/e9qXfj7lmlhUVe8Oog/BJmLsRM= git.solsynth.dev/hydrogen/passport v0.0.0-20240517123434-ebef35a619f5 h1:iEnty5+OHZiIaa27/e9qXfj7lmlhUVe8Oog/BJmLsRM=
git.solsynth.dev/hydrogen/passport v0.0.0-20240517123434-ebef35a619f5/go.mod h1:mEcDEKashAh3jvoGDbNLefK+HgsJaMj4xEc6vkLZ+Zc= git.solsynth.dev/hydrogen/passport v0.0.0-20240517123434-ebef35a619f5/go.mod h1:mEcDEKashAh3jvoGDbNLefK+HgsJaMj4xEc6vkLZ+Zc=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=

View File

@ -36,7 +36,7 @@ type Post struct {
Author Account `json:"author"` Author Account `json:"author"`
// Dynamic Calculated Values // Dynamic Calculated Values
ReplyCount int64 `json:"comment_count"` ReplyCount int64 `json:"reply_count"`
ReactionCount int64 `json:"reaction_count"` ReactionCount int64 `json:"reaction_count"`
ReactionList map[string]int64 `json:"reaction_list" gorm:"-"` ReactionList map[string]int64 `json:"reaction_list" gorm:"-"`
} }

View File

@ -94,8 +94,8 @@ func createPost(c *fiber.Ctx) error {
} }
for _, attachment := range data.Attachments { for _, attachment := range data.Attachments {
if _, err := services.GetAttachmentByUUID(attachment); err != nil { if services.CheckAttachmentByUUIDExists(attachment, "i.attachment") {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %s not found: %v", attachment, err)) return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %s not found", attachment))
} }
} }
@ -170,8 +170,8 @@ func editPost(c *fiber.Ctx) error {
} }
for _, attachment := range data.Attachments { for _, attachment := range data.Attachments {
if _, err := services.GetAttachmentByUUID(attachment); err != nil { if services.CheckAttachmentByUUIDExists(attachment, "i.attachment") {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %s not found: %v", attachment, err)) return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %s not found", attachment))
} }
} }

View File

@ -18,3 +18,12 @@ func GetAttachmentByUUID(uuid string) (*pcpb.Attachment, error) {
Uuid: &uuid, Uuid: &uuid,
}) })
} }
func CheckAttachmentByUUIDExists(uuid string, usage string) bool {
_, err := grpc.Attachments.CheckAttachmentExists(context.Background(), &pcpb.AttachmentLookupRequest{
Uuid: &uuid,
Usage: &usage,
})
return err == nil
}

View File

@ -55,13 +55,10 @@ func GetPostWithAlias(alias string, ignoreLimitation ...bool) (models.Post, erro
if err := tx. if err := tx.
Where("alias = ?", alias). Where("alias = ?", alias).
Preload("Author"). Preload("Author").
Preload("Attachments").
Preload("ReplyTo"). Preload("ReplyTo").
Preload("ReplyTo.Author"). Preload("ReplyTo.Author").
Preload("ReplyTo.Attachments").
Preload("RepostTo"). Preload("RepostTo").
Preload("RepostTo.Author"). Preload("RepostTo.Author").
Preload("RepostTo.Attachments").
First(&item).Error; err != nil { First(&item).Error; err != nil {
return item, err return item, err
} }
@ -79,13 +76,10 @@ func GetPost(id uint, ignoreLimitation ...bool) (models.Post, error) {
if err := tx. if err := tx.
Where("id = ?", id). Where("id = ?", id).
Preload("Author"). Preload("Author").
Preload("Attachments").
Preload("ReplyTo"). Preload("ReplyTo").
Preload("ReplyTo.Author"). Preload("ReplyTo.Author").
Preload("ReplyTo.Attachments").
Preload("RepostTo"). Preload("RepostTo").
Preload("RepostTo.Author"). Preload("RepostTo.Author").
Preload("RepostTo.Attachments").
First(&item).Error; err != nil { First(&item).Error; err != nil {
return item, err return item, err
} }
@ -157,13 +151,10 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]models.Post
Limit(take).Offset(offset). Limit(take).Offset(offset).
Order("created_at DESC"). Order("created_at DESC").
Preload("Author"). Preload("Author").
Preload("Attachments").
Preload("ReplyTo"). Preload("ReplyTo").
Preload("ReplyTo.Author"). Preload("ReplyTo.Author").
Preload("ReplyTo.Attachments").
Preload("RepostTo"). Preload("RepostTo").
Preload("RepostTo.Author"). Preload("RepostTo.Author").
Preload("RepostTo.Attachments").
Find(&items).Error; err != nil { Find(&items).Error; err != nil {
return items, err return items, err
} }