2024-02-06 12:28:12 +08:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-06-06 22:48:43 +08:00
|
|
|
"context"
|
2024-07-15 00:01:17 +08:00
|
|
|
"fmt"
|
2024-10-24 00:13:16 +08:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/nex"
|
2024-10-27 00:06:23 +08:00
|
|
|
"git.solsynth.dev/hypernet/nexus/pkg/proto"
|
2024-10-31 20:38:50 +08:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
2024-10-27 00:06:23 +08:00
|
|
|
"git.solsynth.dev/hypernet/pusher/pkg/pushkit"
|
2024-09-17 14:50:05 +08:00
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-07-21 14:22:54 +08:00
|
|
|
"github.com/samber/lo"
|
2024-07-10 17:38:39 +08:00
|
|
|
|
2024-10-31 20:38:50 +08:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/gap"
|
2024-07-19 23:27:58 +08:00
|
|
|
|
2024-10-31 20:38:50 +08:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
2024-02-06 12:28:12 +08:00
|
|
|
)
|
|
|
|
|
2024-05-07 21:00:20 +08:00
|
|
|
func AddNotifySubscriber(user models.Account, provider, id, tk, ua string) (models.NotificationSubscriber, error) {
|
|
|
|
var prev models.NotificationSubscriber
|
|
|
|
var subscriber models.NotificationSubscriber
|
|
|
|
if err := database.C.Where(&models.NotificationSubscriber{
|
|
|
|
DeviceID: id,
|
2024-02-07 23:15:16 +08:00
|
|
|
AccountID: user.ID,
|
2024-05-07 21:00:20 +08:00
|
|
|
}); err != nil {
|
|
|
|
subscriber = models.NotificationSubscriber{
|
|
|
|
UserAgent: ua,
|
|
|
|
Provider: provider,
|
|
|
|
DeviceID: id,
|
|
|
|
DeviceToken: tk,
|
|
|
|
AccountID: user.ID,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prev = subscriber
|
2024-02-07 23:15:16 +08:00
|
|
|
}
|
|
|
|
|
2024-05-07 21:00:20 +08:00
|
|
|
subscriber.UserAgent = ua
|
|
|
|
subscriber.Provider = provider
|
|
|
|
subscriber.DeviceToken = tk
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if !reflect.DeepEqual(subscriber, prev) {
|
|
|
|
err = database.C.Save(&subscriber).Error
|
|
|
|
}
|
2024-02-07 23:15:16 +08:00
|
|
|
|
|
|
|
return subscriber, err
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:07:59 +08:00
|
|
|
// NewNotification will create a notification and push via the push method it
|
2024-10-24 00:13:16 +08:00
|
|
|
// Pleases provide the notification with the account field is not empty
|
2024-03-31 13:04:48 +08:00
|
|
|
func NewNotification(notification models.Notification) error {
|
2024-09-17 14:50:05 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-02-06 12:28:12 +08:00
|
|
|
if err := database.C.Save(¬ification).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-09-22 01:27:57 +08:00
|
|
|
if err := PushNotification(notification, true); err != nil {
|
2024-06-30 11:57:57 +08:00
|
|
|
return err
|
|
|
|
}
|
2024-03-31 13:04:48 +08:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-17 14:04:55 +08:00
|
|
|
func NewNotificationBatch(notifications []models.Notification) error {
|
2024-09-20 21:55:25 +08:00
|
|
|
if len(notifications) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
notifiable := CheckNotificationNotifiableBatch(lo.Map(notifications, func(item models.Notification, index int) models.Account {
|
|
|
|
return item.Account
|
|
|
|
}), notifications[0].Topic)
|
|
|
|
|
2024-09-21 22:54:54 +08:00
|
|
|
notifications = lo.Filter(notifications, func(item models.Notification, index int) bool {
|
|
|
|
return notifiable[index]
|
|
|
|
})
|
2024-09-20 21:55:25 +08:00
|
|
|
|
2024-07-17 14:04:55 +08:00
|
|
|
if err := database.C.CreateInBatches(notifications, 1000).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-09-21 22:54:54 +08:00
|
|
|
PushNotificationBatch(notifications, true)
|
2024-07-17 14:04:55 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-09-17 14:50:05 +08:00
|
|
|
// PushNotification will push a notification to the user, via websocket, firebase, or APNs
|
|
|
|
// Please provide the notification with the account field is not empty
|
2024-09-21 22:54:54 +08:00
|
|
|
func PushNotification(notification models.Notification, skipNotifiableCheck ...bool) error {
|
|
|
|
if len(skipNotifiableCheck) == 0 || !skipNotifiableCheck[0] {
|
|
|
|
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
|
|
|
|
}
|
2024-09-17 14:50:05 +08:00
|
|
|
}
|
|
|
|
|
2024-07-15 00:01:17 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
2024-10-27 00:06:23 +08:00
|
|
|
_, err := proto.NewStreamServiceClient(gap.Nx.GetNexusGrpcConn()).PushStream(ctx, &proto.PushStreamRequest{
|
2024-08-23 19:42:30 +08:00
|
|
|
UserId: lo.ToPtr(uint64(notification.AccountID)),
|
2024-10-24 00:13:16 +08:00
|
|
|
Body: nex.WebSocketPackage{
|
2024-05-09 23:35:13 +08:00
|
|
|
Action: "notifications.new",
|
2024-05-13 22:31:19 +08:00
|
|
|
Payload: notification,
|
2024-07-15 00:01:17 +08:00
|
|
|
}.Marshal(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to push via websocket: %v", err)
|
2024-03-31 16:03:59 +08:00
|
|
|
}
|
2024-03-31 13:04:48 +08:00
|
|
|
|
2024-07-03 23:07:59 +08:00
|
|
|
// Skip push notification
|
2024-07-16 00:05:09 +08:00
|
|
|
if GetStatusDisturbable(notification.AccountID) != nil {
|
2024-06-26 20:05:28 +08:00
|
|
|
return nil
|
|
|
|
}
|
2024-06-06 22:48:43 +08:00
|
|
|
|
2024-02-07 23:40:43 +08:00
|
|
|
var subscribers []models.NotificationSubscriber
|
|
|
|
if err := database.C.Where(&models.NotificationSubscriber{
|
2024-07-16 00:05:09 +08:00
|
|
|
AccountID: notification.AccountID,
|
2024-02-07 23:40:43 +08:00
|
|
|
}).Find(&subscribers).Error; err != nil {
|
2024-03-31 13:04:48 +08:00
|
|
|
return err
|
2024-02-07 23:40:43 +08:00
|
|
|
}
|
|
|
|
|
2024-07-21 14:22:54 +08:00
|
|
|
var providers []string
|
|
|
|
var tokens []string
|
2024-02-07 23:40:43 +08:00
|
|
|
for _, subscriber := range subscribers {
|
2024-07-21 14:22:54 +08:00
|
|
|
providers = append(providers, subscriber.Provider)
|
|
|
|
tokens = append(tokens, subscriber.DeviceToken)
|
2024-02-07 23:40:43 +08:00
|
|
|
}
|
2024-02-06 12:28:12 +08:00
|
|
|
|
2024-11-23 00:34:53 +08:00
|
|
|
log.Debug().Str("topic", notification.Topic).Any("uid", notification.AccountID).Msg("Pushing notify to user...")
|
|
|
|
|
2024-07-21 14:22:54 +08:00
|
|
|
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
2024-10-27 00:06:23 +08:00
|
|
|
err = gap.Px.PushNotifyBatch(pushkit.NotificationPushBatchRequest{
|
2024-10-31 21:26:25 +08:00
|
|
|
Providers: providers,
|
|
|
|
Tokens: tokens,
|
|
|
|
Notification: notification.EncodeToPushkit(),
|
2024-07-21 14:22:54 +08:00
|
|
|
})
|
2024-11-23 00:34:53 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Str("topic", notification.Topic).Msg("Failed to push notification to Pusher")
|
|
|
|
}
|
2024-07-21 14:22:54 +08:00
|
|
|
|
|
|
|
return err
|
2024-02-06 12:28:12 +08:00
|
|
|
}
|
2024-07-17 14:04:55 +08:00
|
|
|
|
2024-11-23 12:43:09 +08:00
|
|
|
// PushNotificationBatch will push a notification to the user
|
|
|
|
// The notification should be the same for all users except the account id field
|
|
|
|
// For the notification push, the method will only use the first notification as template
|
2024-09-21 22:54:54 +08:00
|
|
|
func PushNotificationBatch(notifications []models.Notification, skipNotifiableCheck ...bool) {
|
2024-09-17 14:50:05 +08:00
|
|
|
if len(notifications) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-09-21 22:54:54 +08:00
|
|
|
var accountIdx []uint
|
|
|
|
if len(skipNotifiableCheck) == 0 || !skipNotifiableCheck[0] {
|
|
|
|
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
|
|
|
|
},
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
accountIdx = lo.Map(
|
|
|
|
notifications,
|
|
|
|
func(item models.Notification, index int) uint {
|
|
|
|
return item.AccountID
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
2024-09-17 14:50:05 +08:00
|
|
|
|
2024-11-23 00:34:53 +08:00
|
|
|
log.Debug().Str("topic", notifications[0].Topic).Any("uid", accountIdx).Msg("Pushing notify to users...")
|
|
|
|
|
2024-09-17 14:50:05 +08:00
|
|
|
if len(accountIdx) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-21 14:22:54 +08:00
|
|
|
var subscribers []models.NotificationSubscriber
|
2024-11-23 12:43:09 +08:00
|
|
|
if err := database.C.Where("account_id IN ?", accountIdx).Find(&subscribers).Error; err != nil {
|
|
|
|
log.Error().Err(err).Msg("Failed to fetch subscribers, unable to push notifications")
|
|
|
|
}
|
2024-07-21 14:22:54 +08:00
|
|
|
|
2024-11-23 12:43:09 +08:00
|
|
|
var providers []string
|
|
|
|
var tokens []string
|
2024-10-27 00:06:23 +08:00
|
|
|
stream := proto.NewStreamServiceClient(gap.Nx.GetNexusGrpcConn())
|
2024-07-17 14:04:55 +08:00
|
|
|
for _, notification := range notifications {
|
2024-07-21 14:22:54 +08:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
_, _ = stream.PushStream(ctx, &proto.PushStreamRequest{
|
2024-08-23 19:42:30 +08:00
|
|
|
UserId: lo.ToPtr(uint64(notification.AccountID)),
|
2024-10-24 00:13:16 +08:00
|
|
|
Body: nex.WebSocketPackage{
|
2024-07-21 14:22:54 +08:00
|
|
|
Action: "notifications.new",
|
|
|
|
Payload: notification,
|
|
|
|
}.Marshal(),
|
|
|
|
})
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
// Skip push notification
|
|
|
|
if GetStatusDisturbable(notification.AccountID) != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-07-21 22:52:24 +08:00
|
|
|
for _, subscriber := range lo.Filter(subscribers, func(item models.NotificationSubscriber, index int) bool {
|
|
|
|
return item.AccountID == notification.AccountID
|
|
|
|
}) {
|
2024-07-21 14:22:54 +08:00
|
|
|
providers = append(providers, subscriber.Provider)
|
|
|
|
tokens = append(tokens, subscriber.DeviceToken)
|
|
|
|
}
|
2024-11-23 12:43:09 +08:00
|
|
|
}
|
2024-07-21 14:22:54 +08:00
|
|
|
|
2024-11-23 12:43:09 +08:00
|
|
|
if err := gap.Px.PushNotifyBatch(pushkit.NotificationPushBatchRequest{
|
|
|
|
Providers: providers,
|
|
|
|
Tokens: tokens,
|
|
|
|
Notification: notifications[0].EncodeToPushkit(),
|
|
|
|
}); err != nil {
|
|
|
|
log.Warn().Err(err).Str("topic", notifications[0].Topic).Msg("Failed to push notification to Pusher")
|
2024-07-17 14:04:55 +08:00
|
|
|
}
|
|
|
|
}
|