From 80a8a317262d3fca2ef3f1bbaf982367d8752cae Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 15 May 2024 20:31:25 +0800 Subject: [PATCH] :bug: Bug fixes of won't accept reply id --- pkg/server/posts_api.go | 24 +++++++++++++++--------- pkg/services/posts.go | 18 ++++++++++++++++++ settings.toml | 2 +- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/pkg/server/posts_api.go b/pkg/server/posts_api.go index 7911c0e..e31f41c 100644 --- a/pkg/server/posts_api.go +++ b/pkg/server/posts_api.go @@ -83,7 +83,8 @@ func createPost(c *fiber.Ctx) error { Attachments []models.Attachment `json:"attachments" form:"attachments"` PublishedAt *time.Time `json:"published_at" form:"published_at"` RealmAlias string `json:"realm" form:"realm"` - RepostTo uint `json:"repost_to" form:"repost_to"` + ReplyTo *uint `json:"reply_to" form:"reply_to"` + RepostTo *uint `json:"repost_to" form:"repost_to"` } if err := BindAndValidate(c, &data); err != nil { @@ -102,15 +103,20 @@ func createPost(c *fiber.Ctx) error { Content: data.Content, } - var relatedCount int64 - if data.RepostTo > 0 { - if err := database.C.Where("id = ?", data.RepostTo). - Model(&models.Post{}).Count(&relatedCount).Error; err != nil { - return fiber.NewError(fiber.StatusBadRequest, err.Error()) - } else if relatedCount <= 0 { - return fiber.NewError(fiber.StatusNotFound, "related post was not found") + if data.ReplyTo != nil { + var replyTo models.Post + if err := database.C.Where("id = ?", data.ReplyTo).First(&replyTo).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("related post was not found: %v", err)) } else { - item.RepostID = &data.RepostTo + item.ReplyID = &replyTo.ID + } + } + if data.RepostTo != nil { + var repostTo models.Post + if err := database.C.Where("id = ?", data.RepostTo).First(&repostTo).Error; err != nil { + return fiber.NewError(fiber.StatusNotFound, fmt.Sprintf("related post was not found: %v", err)) + } else { + item.RepostID = &repostTo.ID } } diff --git a/pkg/services/posts.go b/pkg/services/posts.go index 5c4a832..c5f5859 100644 --- a/pkg/services/posts.go +++ b/pkg/services/posts.go @@ -56,6 +56,12 @@ func GetPostWithAlias(alias string, ignoreLimitation ...bool) (models.Post, erro 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 } @@ -74,6 +80,12 @@ func GetPost(id uint, ignoreLimitation ...bool) (models.Post, error) { 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 } @@ -146,6 +158,12 @@ func ListPost(tx *gorm.DB, take int, offset int, noReact ...bool) ([]models.Post 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 } diff --git a/settings.toml b/settings.toml index c20bba6..5fd8f29 100644 --- a/settings.toml +++ b/settings.toml @@ -17,7 +17,7 @@ print_routes = false client_id = "solarplaza" client_secret = "Z9k9AFTj^p" endpoint = "http://localhost:8444" -grpc_endpoint = "127.0.0.1:7444" +grpc_endpoint = "id.solsynth.dev:7444" [mailer] name = "Alphabot "