⬆️ Upgrade the Passport notify module
This commit is contained in:
@ -236,7 +236,7 @@ func reactPost(c *fiber.Ctx) error {
|
||||
reaction.PostID = &res.ID
|
||||
}
|
||||
|
||||
if positive, reaction, err := services.ReactPost(reaction); err != nil {
|
||||
if positive, reaction, err := services.ReactPost(user, reaction); err != nil {
|
||||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||||
} else {
|
||||
return c.Status(lo.Ternary(positive, fiber.StatusCreated, fiber.StatusNoContent)).JSON(reaction)
|
||||
|
@ -31,24 +31,25 @@ func GetAccountFriend(userId, relatedId uint, status int) (*proto.FriendshipResp
|
||||
})
|
||||
}
|
||||
|
||||
func NotifyAccount(user models.Account, subject, content string, realtime bool, links ...*proto.NotifyLink) error {
|
||||
func NotifyPosterAccount(user models.Account, subject, content string, links ...*proto.NotifyLink) error {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||
defer cancel()
|
||||
|
||||
_, err := grpc.Notify.NotifyUser(ctx, &proto.NotifyRequest{
|
||||
ClientId: viper.GetString("passport.client_id"),
|
||||
ClientSecret: viper.GetString("passport.client_secret"),
|
||||
Type: "notifications.interactive.feedback",
|
||||
Subject: subject,
|
||||
Content: content,
|
||||
Links: links,
|
||||
RecipientId: uint64(user.ExternalID),
|
||||
IsRealtime: realtime,
|
||||
IsImportant: false,
|
||||
IsRealtime: false,
|
||||
IsForcePush: true,
|
||||
})
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("An error occurred when notify account...")
|
||||
} else {
|
||||
log.Debug().Uint("external", user.ExternalID).Msg("Notified account.")
|
||||
log.Debug().Uint("eid", user.ExternalID).Msg("Notified account.")
|
||||
}
|
||||
|
||||
return err
|
||||
|
@ -267,27 +267,24 @@ func NewPost(user models.Account, item models.Post) (models.Post, error) {
|
||||
|
||||
// Notify the original poster its post has been replied
|
||||
if item.ReplyID != nil {
|
||||
go func() {
|
||||
var op models.Post
|
||||
if err := database.C.
|
||||
Where("id = ?", item.ReplyID).
|
||||
Preload("Author").
|
||||
First(&op).Error; err == nil {
|
||||
if op.Author.ID != user.ID {
|
||||
postUrl := fmt.Sprintf("https://%s/posts/%s", viper.GetString("domain"), item.Alias)
|
||||
err := NotifyAccount(
|
||||
op.Author,
|
||||
fmt.Sprintf("%s replied you", user.Nick),
|
||||
fmt.Sprintf("%s (%s) replied your post #%s.", user.Nick, user.Name, op.Alias),
|
||||
false,
|
||||
&proto.NotifyLink{Label: "Related post", Url: postUrl},
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
}
|
||||
var op models.Post
|
||||
if err := database.C.
|
||||
Where("id = ?", item.ReplyID).
|
||||
Preload("Author").
|
||||
First(&op).Error; err == nil {
|
||||
if op.Author.ID != user.ID {
|
||||
postUrl := fmt.Sprintf("https://%s/posts/%s", viper.GetString("domain"), item.Alias)
|
||||
err := NotifyPosterAccount(
|
||||
op.Author,
|
||||
fmt.Sprintf("%s replied you", user.Nick),
|
||||
fmt.Sprintf("%s (%s) replied your post #%s.", user.Nick, user.Name, op.Alias),
|
||||
&proto.NotifyLink{Label: "Related post", Url: postUrl},
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
return item, nil
|
||||
@ -308,9 +305,28 @@ func DeletePost(item models.Post) error {
|
||||
return database.C.Delete(&item).Error
|
||||
}
|
||||
|
||||
func ReactPost(reaction models.Reaction) (bool, models.Reaction, error) {
|
||||
func ReactPost(user models.Account, reaction models.Reaction) (bool, models.Reaction, error) {
|
||||
if err := database.C.Where(reaction).First(&reaction).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
var op models.Post
|
||||
if err := database.C.
|
||||
Where("id = ?", reaction.PostID).
|
||||
Preload("Author").
|
||||
First(&op).Error; err == nil {
|
||||
if op.Author.ID != user.ID {
|
||||
postUrl := fmt.Sprintf("https://%s/posts/%s", viper.GetString("domain"), op.Alias)
|
||||
err := NotifyPosterAccount(
|
||||
op.Author,
|
||||
fmt.Sprintf("%s reacted your post", user.Nick),
|
||||
fmt.Sprintf("%s (%s) reacted your post a %s", user.Nick, user.Name, reaction.Symbol),
|
||||
&proto.NotifyLink{Label: "Related post", Url: postUrl},
|
||||
)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when notifying user...")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true, reaction, database.C.Save(&reaction).Error
|
||||
} else {
|
||||
return true, reaction, err
|
||||
|
Reference in New Issue
Block a user