2024-02-03 07:20:32 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-08-17 17:06:52 +00:00
|
|
|
"fmt"
|
2024-11-02 05:41:51 +00:00
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/gap"
|
|
|
|
"git.solsynth.dev/hypernet/interactive/pkg/internal/models"
|
2024-10-31 14:41:32 +00:00
|
|
|
"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
|
|
|
)
|
|
|
|
|
2024-10-31 14:41: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
|
|
|
|
}
|
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-12-08 13:04:13 +00:00
|
|
|
func NotifyPosterAccount(pub models.Publisher, post models.Post, title, body, topic string, subtitle ...string) error {
|
2024-10-31 14:41:32 +00:00
|
|
|
if pub.AccountID == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2024-02-08 10:47:29 +00:00
|
|
|
|
2024-10-31 14:41:32 +00:00
|
|
|
if len(subtitle) == 0 {
|
|
|
|
subtitle = append(subtitle, "")
|
2024-06-22 09:29:53 +00:00
|
|
|
}
|
2024-10-31 14:41:32 +00:00
|
|
|
|
|
|
|
err := authkit.NotifyUser(gap.Nx, uint64(*pub.AccountID), pushkit.Notification{
|
2024-12-08 13:04:13 +00:00
|
|
|
Topic: topic,
|
2024-10-31 14:41:32 +00:00
|
|
|
Title: title,
|
|
|
|
Subtitle: subtitle[0],
|
|
|
|
Body: body,
|
|
|
|
Priority: 4,
|
|
|
|
Metadata: map[string]any{
|
|
|
|
"related_post": TruncatePostContent(post),
|
2024-12-08 13:04:13 +00:00
|
|
|
"avatar": pub.Avatar,
|
2024-07-16 02:53:02 +00:00
|
|
|
},
|
2024-02-21 14:58:51 +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 {
|
2024-10-31 14:41:32 +00:00
|
|
|
log.Debug().Uint("uid", pub.ID).Msg("Notified account.")
|
2024-04-26 16:13:47 +00:00
|
|
|
}
|
2024-02-08 10:47:29 +00:00
|
|
|
|
2024-02-21 14:58:51 +00:00
|
|
|
return err
|
2024-02-08 10:47:29 +00:00
|
|
|
}
|