2024-02-06 04:28:12 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-06-06 14:48:43 +00:00
|
|
|
"context"
|
2024-07-14 16:01:17 +00:00
|
|
|
"fmt"
|
2024-07-10 09:38:39 +00:00
|
|
|
"reflect"
|
2024-07-17 06:04:55 +00:00
|
|
|
"sync"
|
2024-07-14 16:01:17 +00:00
|
|
|
"time"
|
2024-07-10 09:38:39 +00:00
|
|
|
|
2024-07-19 15:27:58 +00:00
|
|
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/gap"
|
|
|
|
|
2024-06-06 14:48:43 +00:00
|
|
|
"firebase.google.com/go/messaging"
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
2024-02-07 15:40:43 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-06-06 16:15:43 +00:00
|
|
|
"github.com/sideshow/apns2"
|
|
|
|
payload2 "github.com/sideshow/apns2/payload"
|
2024-06-07 12:50:27 +00:00
|
|
|
"github.com/spf13/viper"
|
2024-02-06 04:28:12 +00:00
|
|
|
)
|
|
|
|
|
2024-05-07 13:00:20 +00: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 15:15:16 +00:00
|
|
|
AccountID: user.ID,
|
2024-05-07 13:00:20 +00:00
|
|
|
}); err != nil {
|
|
|
|
subscriber = models.NotificationSubscriber{
|
|
|
|
UserAgent: ua,
|
|
|
|
Provider: provider,
|
|
|
|
DeviceID: id,
|
|
|
|
DeviceToken: tk,
|
|
|
|
AccountID: user.ID,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
prev = subscriber
|
2024-02-07 15:15:16 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 13:00:20 +00: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 15:15:16 +00:00
|
|
|
|
|
|
|
return subscriber, err
|
|
|
|
}
|
|
|
|
|
2024-07-03 15:07:59 +00:00
|
|
|
// NewNotification will create a notification and push via the push method it
|
2024-03-31 05:04:48 +00:00
|
|
|
func NewNotification(notification models.Notification) error {
|
2024-02-06 04:28:12 +00:00
|
|
|
if err := database.C.Save(¬ification).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-30 03:57:57 +00:00
|
|
|
if err := PushNotification(notification); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-31 05:04:48 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-17 06:04:55 +00:00
|
|
|
func NewNotificationBatch(notifications []models.Notification) error {
|
|
|
|
if err := database.C.CreateInBatches(notifications, 1000).Error; err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
PushNotificationBatch(notifications)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-14 16:01:17 +00:00
|
|
|
// PushNotification will push the notification whatever it exists record in the
|
2024-07-15 16:02:28 +00:00
|
|
|
// database Recommend pushing another goroutine when you need to push a lot of
|
2024-07-14 16:01:17 +00:00
|
|
|
// notifications And just use a block statement when you just push one
|
2024-07-15 16:02:28 +00:00
|
|
|
// notification.
|
|
|
|
// The time of creating a new subprocess is much more than push notification.
|
2024-03-31 05:04:48 +00:00
|
|
|
func PushNotification(notification models.Notification) error {
|
2024-07-14 16:01:17 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, err := proto.NewStreamControllerClient(gap.H.GetDealerGrpcConn()).PushStream(ctx, &proto.PushStreamRequest{
|
2024-07-15 16:05:09 +00:00
|
|
|
UserId: uint64(notification.AccountID),
|
2024-07-14 16:01:17 +00:00
|
|
|
Body: models.UnifiedCommand{
|
2024-05-09 15:35:13 +00:00
|
|
|
Action: "notifications.new",
|
2024-05-13 14:31:19 +00:00
|
|
|
Payload: notification,
|
2024-07-14 16:01:17 +00:00
|
|
|
}.Marshal(),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to push via websocket: %v", err)
|
2024-03-31 08:03:59 +00:00
|
|
|
}
|
2024-03-31 05:04:48 +00:00
|
|
|
|
2024-07-03 15:07:59 +00:00
|
|
|
// Skip push notification
|
2024-07-15 16:05:09 +00:00
|
|
|
if GetStatusDisturbable(notification.AccountID) != nil {
|
2024-06-26 12:05:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-06-06 14:48:43 +00:00
|
|
|
|
2024-02-07 15:40:43 +00:00
|
|
|
var subscribers []models.NotificationSubscriber
|
|
|
|
if err := database.C.Where(&models.NotificationSubscriber{
|
2024-07-15 16:05:09 +00:00
|
|
|
AccountID: notification.AccountID,
|
2024-02-07 15:40:43 +00:00
|
|
|
}).Find(&subscribers).Error; err != nil {
|
2024-03-31 05:04:48 +00:00
|
|
|
return err
|
2024-02-07 15:40:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, subscriber := range subscribers {
|
|
|
|
switch subscriber.Provider {
|
2024-06-06 14:48:43 +00:00
|
|
|
case models.NotifySubscriberFirebase:
|
|
|
|
if ExtFire != nil {
|
|
|
|
ctx := context.Background()
|
|
|
|
client, err := ExtFire.Messaging(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("An error occurred when creating FCM client...")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-07-19 15:27:58 +00:00
|
|
|
var image string
|
|
|
|
if notification.Picture != nil {
|
|
|
|
image = *notification.Picture
|
|
|
|
}
|
2024-06-06 14:48:43 +00:00
|
|
|
message := &messaging.Message{
|
|
|
|
Notification: &messaging.Notification{
|
2024-07-19 15:27:58 +00:00
|
|
|
Title: notification.Title,
|
|
|
|
Body: notification.Body,
|
|
|
|
ImageURL: image,
|
2024-06-06 14:48:43 +00:00
|
|
|
},
|
|
|
|
Token: subscriber.DeviceToken,
|
|
|
|
}
|
|
|
|
|
|
|
|
if response, err := client.Send(ctx, message); err != nil {
|
|
|
|
log.Warn().Err(err).Msg("An error occurred when notify subscriber via FCM...")
|
|
|
|
} else {
|
|
|
|
log.Debug().
|
|
|
|
Str("response", response).
|
|
|
|
Int("subscriber", int(subscriber.ID)).
|
|
|
|
Msg("Notified subscriber via FCM.")
|
|
|
|
}
|
|
|
|
}
|
2024-06-06 16:15:43 +00:00
|
|
|
case models.NotifySubscriberAPNs:
|
|
|
|
if ExtAPNS != nil {
|
2024-07-19 15:27:58 +00:00
|
|
|
data := payload2.
|
2024-06-06 16:15:43 +00:00
|
|
|
NewPayload().
|
2024-07-15 16:02:28 +00:00
|
|
|
AlertTitle(notification.Title).
|
|
|
|
AlertBody(notification.Body).
|
2024-06-08 10:18:28 +00:00
|
|
|
Sound("default").
|
2024-07-15 16:02:28 +00:00
|
|
|
Category(notification.Topic).
|
2024-07-19 15:27:58 +00:00
|
|
|
MutableContent()
|
|
|
|
if notification.Avatar != nil {
|
|
|
|
data = data.Custom("avatar_url", *notification.Avatar)
|
|
|
|
}
|
|
|
|
if notification.Picture != nil {
|
|
|
|
data = data.Custom("picture_url", *notification.Picture)
|
|
|
|
}
|
|
|
|
rawData, err := data.MarshalJSON()
|
2024-06-06 16:15:43 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("An error occurred when preparing to notify subscriber via APNs...")
|
|
|
|
}
|
|
|
|
payload := &apns2.Notification{
|
2024-06-07 12:50:27 +00:00
|
|
|
ApnsID: subscriber.DeviceID,
|
2024-06-06 16:15:43 +00:00
|
|
|
DeviceToken: subscriber.DeviceToken,
|
2024-06-07 12:50:27 +00:00
|
|
|
Topic: viper.GetString("apns_topic"),
|
2024-07-19 15:27:58 +00:00
|
|
|
Payload: rawData,
|
2024-06-06 16:15:43 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 12:24:32 +00:00
|
|
|
if resp, err := ExtAPNS.Push(payload); err != nil {
|
2024-06-06 16:15:43 +00:00
|
|
|
log.Warn().Err(err).Msg("An error occurred when notify subscriber via APNs...")
|
2024-06-07 12:24:32 +00:00
|
|
|
} else {
|
|
|
|
log.Debug().
|
|
|
|
Str("reason", resp.Reason).
|
|
|
|
Int("status", resp.StatusCode).
|
|
|
|
Int("subscriber", int(subscriber.ID)).
|
|
|
|
Msg("Notified subscriber via APNs.")
|
2024-06-06 16:15:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-07 15:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-06 04:28:12 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-17 06:04:55 +00:00
|
|
|
|
|
|
|
func PushNotificationBatch(notifications []models.Notification) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for _, notification := range notifications {
|
|
|
|
wg.Add(1)
|
|
|
|
item := notification
|
|
|
|
go func() {
|
|
|
|
_ = PushNotification(item)
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|