From 04d1970dc0adfdd9a4724838bbe3d965b2ae6e79 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 8 Feb 2024 18:47:29 +0800 Subject: [PATCH] :sparkles: Reply will notify original poster --- pkg/services/accounts.go | 23 +++++++++++++++++++++++ pkg/services/posts.go | 22 ++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/pkg/services/accounts.go b/pkg/services/accounts.go index f9e9be8..7a4dd54 100644 --- a/pkg/services/accounts.go +++ b/pkg/services/accounts.go @@ -3,6 +3,9 @@ package services import ( "code.smartsheep.studio/hydrogen/interactive/pkg/database" "code.smartsheep.studio/hydrogen/interactive/pkg/models" + "fmt" + "github.com/gofiber/fiber/v2" + "github.com/spf13/viper" ) func FollowAccount(followerId, followingId uint) error { @@ -28,3 +31,23 @@ func GetAccountFollowed(user models.Account, target models.Account) (models.Acco Error return relationship, err == nil } + +func NotifyAccount(user models.Account, subject, content string, links ...fiber.Map) error { + agent := fiber.Post(viper.GetString("passport.endpoint") + "/api/dev/notify") + agent.JSON(fiber.Map{ + "client_id": viper.GetString("passport.client_id"), + "client_secret": viper.GetString("passport.client_secret"), + "subject": subject, + "content": content, + "links": links, + "user_id": user.ExternalID, + }) + + if status, body, errs := agent.Bytes(); len(errs) > 0 { + return errs[0] + } else if status != 200 { + return fmt.Errorf(string(body)) + } + + return nil +} diff --git a/pkg/services/posts.go b/pkg/services/posts.go index a7ee191..cfc4e6f 100644 --- a/pkg/services/posts.go +++ b/pkg/services/posts.go @@ -3,6 +3,8 @@ package services import ( "errors" "fmt" + "github.com/gofiber/fiber/v2" + "github.com/rs/zerolog/log" "time" "code.smartsheep.studio/hydrogen/interactive/pkg/database" @@ -174,6 +176,26 @@ func NewPost( return post, err } + if post.ReplyID != nil { + var op models.Post + if err := database.C.Where(&models.Post{ + BaseModel: models.BaseModel{ID: *post.ReplyID}, + }).Preload("Author").First(&op).Error; err == nil { + if op.Author.ID != user.ID { + postUrl := fmt.Sprintf("https://%s/posts/%d", viper.GetString("domain"), post.ID) + err := NotifyAccount( + op.Author, + fmt.Sprintf("%s replied you", user.Name), + fmt.Sprintf("%s replied your post. Check it out!", user.Name), + fiber.Map{"label": "Related post", "url": postUrl}, + ) + if err != nil { + log.Error().Err(err).Msg("An error occurred when notifying user...") + } + } + } + } + return post, nil }