2024-02-06 04:28:12 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-06-06 14:48:43 +00:00
|
|
|
"context"
|
|
|
|
"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-06-06 16:15:43 +00:00
|
|
|
"reflect"
|
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-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-03-31 05:04:48 +00:00
|
|
|
go func() {
|
|
|
|
err := PushNotification(notification)
|
2024-03-31 08:03:59 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("Unexpected error occurred during the notification.")
|
|
|
|
}
|
2024-03-31 05:04:48 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func PushNotification(notification models.Notification) error {
|
2024-05-07 12:54:01 +00:00
|
|
|
for conn := range wsConn[notification.RecipientID] {
|
2024-05-09 15:35:13 +00:00
|
|
|
_ = conn.WriteMessage(1, models.UnifiedCommand{
|
|
|
|
Action: "notifications.new",
|
2024-05-13 14:31:19 +00:00
|
|
|
Payload: notification,
|
2024-05-09 15:35:13 +00:00
|
|
|
}.Marshal())
|
2024-03-31 08:03:59 +00:00
|
|
|
}
|
2024-03-31 05:04:48 +00:00
|
|
|
|
2024-06-08 12:11:58 +00:00
|
|
|
// TODO Detect the push notification is turned off (still push when IsForcePush is on)
|
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-03-31 05:04:48 +00:00
|
|
|
AccountID: notification.RecipientID,
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
message := &messaging.Message{
|
|
|
|
Notification: &messaging.Notification{
|
|
|
|
Title: notification.Subject,
|
|
|
|
Body: notification.Content,
|
|
|
|
},
|
|
|
|
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 {
|
|
|
|
data, err := payload2.
|
|
|
|
NewPayload().
|
|
|
|
AlertTitle(notification.Subject).
|
|
|
|
AlertBody(notification.Content).
|
2024-06-08 10:18:28 +00:00
|
|
|
Sound("default").
|
|
|
|
Category(notification.Type).
|
2024-06-06 16:15:43 +00:00
|
|
|
MarshalJSON()
|
|
|
|
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-06-06 16:15:43 +00:00
|
|
|
Payload: data,
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|