2024-02-06 04:28:12 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
2024-02-07 15:40:43 +00:00
|
|
|
"context"
|
2024-03-31 05:04:48 +00:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2024-03-20 12:56:43 +00:00
|
|
|
|
2024-02-07 15:40:43 +00:00
|
|
|
"firebase.google.com/go/messaging"
|
2024-03-20 12:56:43 +00:00
|
|
|
"git.solsynth.dev/hydrogen/identity/pkg/database"
|
|
|
|
"git.solsynth.dev/hydrogen/identity/pkg/external"
|
|
|
|
"git.solsynth.dev/hydrogen/identity/pkg/models"
|
2024-02-07 15:40:43 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2024-02-06 04:28:12 +00:00
|
|
|
)
|
|
|
|
|
2024-02-07 15:15:16 +00:00
|
|
|
func AddNotifySubscriber(user models.Account, provider, device, ua string) (models.NotificationSubscriber, error) {
|
|
|
|
subscriber := models.NotificationSubscriber{
|
|
|
|
UserAgent: ua,
|
|
|
|
Provider: provider,
|
2024-02-07 15:40:43 +00:00
|
|
|
DeviceID: device,
|
2024-02-07 15:15:16 +00:00
|
|
|
AccountID: user.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := database.C.Save(&subscriber).Error
|
|
|
|
|
|
|
|
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-03-31 08:03:59 +00:00
|
|
|
raw, _ := jsoniter.Marshal(notification)
|
|
|
|
for _, conn := range WsConn[notification.RecipientID] {
|
|
|
|
_ = conn.WriteMessage(1, raw)
|
|
|
|
}
|
2024-03-31 05:04:48 +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 {
|
|
|
|
case models.NotifySubscriberFirebase:
|
|
|
|
if external.Fire == nil {
|
|
|
|
// Didn't configure for firebase support
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
client, err := external.Fire.Messaging(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn().Err(err).Msg("An error occurred when getting firebase FCM client...")
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
message := &messaging.Message{
|
|
|
|
Notification: &messaging.Notification{
|
|
|
|
Title: notification.Subject,
|
|
|
|
Body: notification.Content,
|
|
|
|
},
|
|
|
|
Token: subscriber.DeviceID,
|
|
|
|
}
|
|
|
|
|
2024-02-08 04:35:52 +00:00
|
|
|
if response, err := client.Send(ctx, message); err != nil {
|
2024-02-07 15:40:43 +00:00
|
|
|
log.Warn().Err(err).Msg("An error occurred when notify subscriber though firebase FCM...")
|
2024-02-08 04:35:52 +00:00
|
|
|
} else {
|
|
|
|
log.Debug().
|
|
|
|
Str("response", response).
|
|
|
|
Int("subscriber", int(subscriber.ID)).
|
|
|
|
Msg("Notified to subscriber though firebase FCM.")
|
2024-02-07 15:40:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-06 04:28:12 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|