Notification preferences

This commit is contained in:
2024-09-17 14:50:05 +08:00
parent df9fb0a92a
commit f287e295e5
9 changed files with 182 additions and 6 deletions

View File

@ -3,12 +3,14 @@ package services
import (
"context"
"fmt"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
jsoniter "github.com/json-iterator/go"
"github.com/samber/lo"
"reflect"
"time"
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
jsoniter "github.com/json-iterator/go"
"github.com/rs/zerolog/log"
"github.com/samber/lo"
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
@ -47,7 +49,13 @@ func AddNotifySubscriber(user models.Account, provider, id, tk, ua string) (mode
}
// NewNotification will create a notification and push via the push method it
// Please provide the notification with the account field is not empty
func NewNotification(notification models.Notification) error {
if ok := CheckNotificationNotifiable(notification.Account, notification.Topic); !ok {
log.Info().Str("topic", notification.Topic).Uint("uid", notification.AccountID).Msg("Notification dismissed by user...")
return nil
}
if err := database.C.Save(&notification).Error; err != nil {
return err
}
@ -67,7 +75,14 @@ func NewNotificationBatch(notifications []models.Notification) error {
return nil
}
// PushNotification will push a notification to the user, via websocket, firebase, or APNs
// Please provide the notification with the account field is not empty
func PushNotification(notification models.Notification) error {
if ok := CheckNotificationNotifiable(notification.Account, notification.Topic); !ok {
log.Info().Str("topic", notification.Topic).Uint("uid", notification.AccountID).Msg("Notification dismissed by user...")
return nil
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_, err := proto.NewStreamControllerClient(gap.H.GetDealerGrpcConn()).PushStream(ctx, &proto.PushStreamRequest{
@ -124,9 +139,26 @@ func PushNotification(notification models.Notification) error {
}
func PushNotificationBatch(notifications []models.Notification) {
accountIdx := lo.Map(notifications, func(item models.Notification, index int) uint {
return item.AccountID
})
if len(notifications) == 0 {
return
}
notifiable := CheckNotificationNotifiableBatch(lo.Map(notifications, func(item models.Notification, index int) models.Account {
return item.Account
}), notifications[0].Topic)
accountIdx := lo.Map(
lo.Filter(notifications, func(item models.Notification, index int) bool {
return notifiable[index]
}),
func(item models.Notification, index int) uint {
return item.AccountID
},
)
if len(accountIdx) == 0 {
return
}
var subscribers []models.NotificationSubscriber
database.C.Where("account_id IN ?", accountIdx).Find(&subscribers)

View File

@ -0,0 +1,78 @@
package services
import (
"errors"
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"github.com/samber/lo"
"gorm.io/datatypes"
"gorm.io/gorm"
)
func GetNotificationPreference(account models.Account) (models.PreferenceNotification, error) {
var notification models.PreferenceNotification
if err := database.C.Where("account_id = ?", account.ID).First(&notification).Error; err != nil {
return notification, err
}
return notification, nil
}
func UpdateNotificationPreference(account models.Account, config map[string]bool) (models.PreferenceNotification, error) {
var notification models.PreferenceNotification
var err error
if notification, err = GetNotificationPreference(account); err != nil {
notification = models.PreferenceNotification{
AccountID: account.ID,
Config: datatypes.JSONMap(
lo.MapValues(config, func(v bool, k string) any { return v }),
),
}
}
err = database.C.Save(&notification).Error
return notification, err
}
func CheckNotificationNotifiable(account models.Account, topic string) bool {
var notification models.PreferenceNotification
if err := database.C.Where("account_id = ?", account.ID).First(&notification).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return true
}
return false
}
if val, ok := notification.Config[topic]; ok {
if status, ok := val.(bool); !ok || status {
return true
} else if !status {
return false
}
}
return true
}
func CheckNotificationNotifiableBatch(accounts []models.Account, topic string) []bool {
var notifications []models.PreferenceNotification
if err := database.C.Where("account_id IN ?", accounts).Find(&notifications).Error; err != nil {
return lo.Map(accounts, func(item models.Account, index int) bool {
return false
})
}
var notifiable []bool
for _, notification := range notifications {
if val, ok := notification.Config[topic]; ok {
if status, ok := val.(bool); !ok || status {
notifiable = append(notifiable, true)
continue
} else if !status {
notifiable = append(notifiable, false)
continue
}
}
notifiable = append(notifiable, true)
}
return notifiable
}

View File

@ -113,6 +113,7 @@ func NewFriend(userA models.Account, userB models.Account, skipPending ...bool)
Title: "New Friend Request",
Subtitle: lo.ToPtr(fmt.Sprintf("New friend request from %s", userA.Name)),
Body: fmt.Sprintf("You got a new friend request from %s. Go to your account page and decide how to deal it.", userA.Nick),
Account: userB,
AccountID: userB.ID,
})
}
@ -149,6 +150,7 @@ func HandleFriend(userA models.Account, userB models.Account, isAccept bool) err
Title: "Friend Request Processed",
Subtitle: lo.ToPtr(fmt.Sprintf("Your friend request to %s has been processsed.", userA.Name)),
Body: fmt.Sprintf("Your relationship status with %s has been updated, go check it out!", userA.Nick),
Account: userB,
AccountID: userB.ID,
})
}