Passport/pkg/services/notifications.go

79 lines
1.8 KiB
Go
Raw Normal View History

package services
import (
"reflect"
2024-03-20 12:56:43 +00:00
2024-04-13 05:48:19 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/database"
"git.solsynth.dev/hydrogen/passport/pkg/models"
"github.com/rs/zerolog/log"
)
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,
}); 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
}
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
}
func NewNotification(notification models.Notification) error {
if err := database.C.Save(&notification).Error; err != nil {
return err
}
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.")
}
}()
return nil
}
func PushNotification(notification models.Notification) error {
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
}
var subscribers []models.NotificationSubscriber
if err := database.C.Where(&models.NotificationSubscriber{
AccountID: notification.RecipientID,
}).Find(&subscribers).Error; err != nil {
return err
}
for _, subscriber := range subscribers {
switch subscriber.Provider {
2024-06-02 12:15:04 +00:00
case models.NotifySubscriberAPN:
}
}
return nil
}