Reply will notify original poster

This commit is contained in:
LittleSheep 2024-02-08 18:47:29 +08:00
parent 834c9de463
commit 04d1970dc0
2 changed files with 45 additions and 0 deletions

View File

@ -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
}

View File

@ -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
}