Interactive/pkg/internal/services/accounts.go

58 lines
1.5 KiB
Go
Raw Normal View History

2024-02-03 07:20:32 +00:00
package services
import (
2024-08-17 17:06:52 +00:00
"fmt"
2024-07-23 08:12:19 +00:00
"git.solsynth.dev/hydrogen/interactive/pkg/internal/database"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/gap"
"git.solsynth.dev/hydrogen/interactive/pkg/internal/models"
"git.solsynth.dev/hypernet/passport/pkg/authkit"
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
2024-04-26 16:13:47 +00:00
"github.com/rs/zerolog/log"
2024-02-03 07:20:32 +00:00
)
func GetAccountWithID(id uint) (models.Publisher, error) {
var account models.Publisher
2024-09-16 16:12:09 +00:00
if err := database.C.Where("id = ?", id).First(&account).Error; err != nil {
return account, fmt.Errorf("unable to get account by id: %v", err)
}
return account, nil
}
func ModifyPosterVoteCount(user models.Publisher, isUpvote bool, delta int) error {
2024-07-23 08:12:19 +00:00
if isUpvote {
user.TotalUpvote += delta
} else {
user.TotalDownvote += delta
}
return database.C.Save(&user).Error
}
func NotifyPosterAccount(pub models.Publisher, post models.Post, title, body string, subtitle ...string) error {
if pub.AccountID == nil {
return nil
}
if len(subtitle) == 0 {
subtitle = append(subtitle, "")
}
err := authkit.NotifyUser(gap.Nx, uint64(*pub.AccountID), pushkit.Notification{
Topic: "interactive.feedback",
Title: title,
Subtitle: subtitle[0],
Body: body,
Priority: 4,
Metadata: map[string]any{
"related_post": TruncatePostContent(post),
2024-07-16 02:53:02 +00:00
},
})
2024-04-26 16:13:47 +00:00
if err != nil {
log.Warn().Err(err).Msg("An error occurred when notify account...")
} else {
log.Debug().Uint("uid", pub.ID).Msg("Notified account.")
2024-04-26 16:13:47 +00:00
}
return err
}