⬆️ Use faster way to check attachment it exists

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

View File

@ -36,7 +36,7 @@ type Post struct {
Author Account `json:"author"`
// Dynamic Calculated Values
ReplyCount int64 `json:"comment_count"`
ReplyCount int64 `json:"reply_count"`
ReactionCount int64 `json:"reaction_count"`
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 {
if _, err := services.GetAttachmentByUUID(attachment); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %s not found: %v", attachment, err))
if services.CheckAttachmentByUUIDExists(attachment, "i.attachment") {
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 {
if _, err := services.GetAttachmentByUUID(attachment); err != nil {
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment %s not found: %v", attachment, err))
if services.CheckAttachmentByUUIDExists(attachment, "i.attachment") {
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,
})
}
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.
Where("alias = ?", alias).
Preload("Author").
Preload("Attachments").
Preload("ReplyTo").
Preload("ReplyTo.Author").
Preload("ReplyTo.Attachments").
Preload("RepostTo").
Preload("RepostTo.Author").
Preload("RepostTo.Attachments").
First(&item).Error; err != nil {
return item, err
}
@ -79,13 +76,10 @@ func GetPost(id uint, ignoreLimitation ...bool) (models.Post, error) {
if err := tx.
Where("id = ?", id).
Preload("Author").
Preload("Attachments").
Preload("ReplyTo").
Preload("ReplyTo.Author").
Preload("ReplyTo.Attachments").
Preload("RepostTo").
Preload("RepostTo.Author").
Preload("RepostTo.Attachments").
First(&item).Error; err != nil {
return item, err
}
@ -157,13 +151,10 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]models.Post
Limit(take).Offset(offset).
Order("created_at DESC").
Preload("Author").
Preload("Attachments").
Preload("ReplyTo").
Preload("ReplyTo.Author").
Preload("ReplyTo.Attachments").
Preload("RepostTo").
Preload("RepostTo.Author").
Preload("RepostTo.Attachments").
Find(&items).Error; err != nil {
return items, err
}