WebSocket listen notification API

This commit is contained in:
2024-03-31 13:04:48 +08:00
parent 11377c378b
commit 7873bafa4f
14 changed files with 164 additions and 72 deletions

View File

@ -0,0 +1,13 @@
package services
import (
"git.solsynth.dev/hydrogen/identity/pkg/models"
"github.com/gofiber/contrib/websocket"
)
var WsConn = make(map[uint][]*websocket.Conn)
var WsNotifyQueue = make(map[uint][]byte)
func CheckOnline(user models.Account) bool {
return len(WsConn[user.ID]) > 0
}

View File

@ -2,6 +2,7 @@ package services
import (
"context"
jsoniter "github.com/json-iterator/go"
"firebase.google.com/go/messaging"
"git.solsynth.dev/hydrogen/identity/pkg/database"
@ -23,34 +24,27 @@ func AddNotifySubscriber(user models.Account, provider, device, ua string) (mode
return subscriber, err
}
func NewNotification(
user models.ThirdClient,
target models.Account,
subject, content string,
links []models.NotificationLink,
important bool,
) error {
notification := models.Notification{
Subject: subject,
Content: content,
Links: links,
IsImportant: important,
ReadAt: nil,
SenderID: &user.ID,
RecipientID: target.ID,
}
func NewNotification(notification models.Notification) error {
if err := database.C.Save(&notification).Error; err != nil {
return err
}
go func() {
err := PushNotification(notification)
log.Error().Err(err).Msg("Unexpected error occurred during the notification.")
}()
return nil
}
func PushNotification(notification models.Notification) error {
WsNotifyQueue[notification.RecipientID], _ = jsoniter.Marshal(notification)
var subscribers []models.NotificationSubscriber
if err := database.C.Where(&models.NotificationSubscriber{
AccountID: target.ID,
AccountID: notification.RecipientID,
}).Find(&subscribers).Error; err != nil {
// I don't know why cannot get subscribers list, but whatever, the notifications has created
log.Error().Err(err).Msg("Unexpected error occurred during the notification.")
return nil
return err
}
for _, subscriber := range subscribers {