Compare commits

...

2 Commits

Author SHA1 Message Date
18a4321685 ♻️ Improved the notification subscriber API 2024-05-07 21:00:20 +08:00
fe27b0bf1c Use map to improve message delivery time 2024-05-07 20:54:01 +08:00
9 changed files with 6659 additions and 32 deletions

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,2 @@
#n:public
!<md> [41831, 0, null, null, -2147483648, -2147483648]

11
.idea/workspace.xml generated
View File

@ -4,10 +4,11 @@
<option name="autoReloadType" value="ALL" /> <option name="autoReloadType" value="ALL" />
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Dumb man make dumb mistake again"> <list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":zap: Use map to improve message delivery time">
<change beforePath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/dataSources/74bcf3ef-a2b9-435b-b9e5-f32902a33b25.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/realms.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/services/realms.go" afterDir="false" /> <change beforePath="$PROJECT_DIR$/pkg/models/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/models/notifications.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/server/notifications_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/server/notifications_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/services/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/services/notifications.go" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -149,7 +150,9 @@
<MESSAGE value=":bug: Bug fix on missing id in realm" /> <MESSAGE value=":bug: Bug fix on missing id in realm" />
<MESSAGE value=":bug: Bug fixes on realm missing member on creation" /> <MESSAGE value=":bug: Bug fixes on realm missing member on creation" />
<MESSAGE value=":bug: Dumb man make dumb mistake again" /> <MESSAGE value=":bug: Dumb man make dumb mistake again" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Dumb man make dumb mistake again" /> <MESSAGE value=":bug: Fix new realm owner missing permissions" />
<MESSAGE value=":zap: Use map to improve message delivery time" />
<option name="LAST_COMMIT_MESSAGE" value=":zap: Use map to improve message delivery time" />
</component> </component>
<component name="VgoProject"> <component name="VgoProject">
<settings-migrated>true</settings-migrated> <settings-migrated>true</settings-migrated>

View File

@ -34,5 +34,6 @@ type NotificationSubscriber struct {
UserAgent string `json:"user_agent"` UserAgent string `json:"user_agent"`
Provider string `json:"provider"` Provider string `json:"provider"`
DeviceID string `json:"device_id" gorm:"uniqueIndex"` DeviceID string `json:"device_id" gorm:"uniqueIndex"`
DeviceToken string `json:"device_token"`
AccountID uint `json:"account_id"` AccountID uint `json:"account_id"`
} }

View File

@ -90,6 +90,7 @@ func addNotifySubscriber(c *fiber.Ctx) error {
var data struct { var data struct {
Provider string `json:"provider" validate:"required"` Provider string `json:"provider" validate:"required"`
DeviceToken string `json:"device_token" validate:"required"`
DeviceID string `json:"device_id" validate:"required"` DeviceID string `json:"device_id" validate:"required"`
} }
@ -100,6 +101,7 @@ func addNotifySubscriber(c *fiber.Ctx) error {
var count int64 var count int64
if err := database.C.Where(&models.NotificationSubscriber{ if err := database.C.Where(&models.NotificationSubscriber{
DeviceID: data.DeviceID, DeviceID: data.DeviceID,
DeviceToken: data.DeviceToken,
AccountID: user.ID, AccountID: user.ID,
}).Model(&models.NotificationSubscriber{}).Count(&count).Error; err != nil || count > 0 { }).Model(&models.NotificationSubscriber{}).Count(&count).Error; err != nil || count > 0 {
return c.SendStatus(fiber.StatusOK) return c.SendStatus(fiber.StatusOK)
@ -109,6 +111,7 @@ func addNotifySubscriber(c *fiber.Ctx) error {
user, user,
data.Provider, data.Provider,
data.DeviceID, data.DeviceID,
data.DeviceToken,
c.Get(fiber.HeaderUserAgent), c.Get(fiber.HeaderUserAgent),
) )
if err != nil { if err != nil {

View File

@ -4,14 +4,13 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/models" "git.solsynth.dev/hydrogen/passport/pkg/models"
"git.solsynth.dev/hydrogen/passport/pkg/services" "git.solsynth.dev/hydrogen/passport/pkg/services"
"github.com/gofiber/contrib/websocket" "github.com/gofiber/contrib/websocket"
"github.com/samber/lo"
) )
func listenNotifications(c *websocket.Conn) { func listenNotifications(c *websocket.Conn) {
user := c.Locals("principal").(models.Account) user := c.Locals("principal").(models.Account)
// Push connection // Push connection
services.WsConn[user.ID] = append(services.WsConn[user.ID], c) services.ClientRegister(user, c)
// Event loop // Event loop
var err error var err error
@ -22,7 +21,5 @@ func listenNotifications(c *websocket.Conn) {
} }
// Pop connection // Pop connection
services.WsConn[user.ID] = lo.Filter(services.WsConn[user.ID], func(item *websocket.Conn, idx int) bool { services.ClientUnregister(user, c)
return item != c
})
} }

View File

@ -5,13 +5,18 @@ import (
"github.com/gofiber/contrib/websocket" "github.com/gofiber/contrib/websocket"
) )
type WsPushRequest struct { var wsConn = make(map[uint]map[*websocket.Conn]bool)
Payload []byte
RecipientID uint func ClientRegister(user models.Account, conn *websocket.Conn) {
if wsConn[user.ID] == nil {
wsConn[user.ID] = make(map[*websocket.Conn]bool)
}
wsConn[user.ID][conn] = true
} }
var WsConn = make(map[uint][]*websocket.Conn) func ClientUnregister(user models.Account, conn *websocket.Conn) {
if wsConn[user.ID] == nil {
func CheckOnline(user models.Account) bool { wsConn[user.ID] = make(map[*websocket.Conn]bool)
return len(WsConn[user.ID]) > 0 }
delete(wsConn[user.ID], conn)
} }

View File

@ -3,6 +3,7 @@ package services
import ( import (
"context" "context"
jsoniter "github.com/json-iterator/go" jsoniter "github.com/json-iterator/go"
"reflect"
"firebase.google.com/go/messaging" "firebase.google.com/go/messaging"
"git.solsynth.dev/hydrogen/passport/pkg/database" "git.solsynth.dev/hydrogen/passport/pkg/database"
@ -11,15 +12,32 @@ import (
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
) )
func AddNotifySubscriber(user models.Account, provider, device, ua string) (models.NotificationSubscriber, error) { func AddNotifySubscriber(user models.Account, provider, id, tk, ua string) (models.NotificationSubscriber, error) {
subscriber := models.NotificationSubscriber{ var prev models.NotificationSubscriber
var subscriber models.NotificationSubscriber
if err := database.C.Where(&models.NotificationSubscriber{
DeviceID: id,
AccountID: user.ID,
}); err != nil {
subscriber = models.NotificationSubscriber{
UserAgent: ua, UserAgent: ua,
Provider: provider, Provider: provider,
DeviceID: device, DeviceID: id,
DeviceToken: tk,
AccountID: user.ID, AccountID: user.ID,
} }
} else {
prev = subscriber
}
err := database.C.Save(&subscriber).Error subscriber.UserAgent = ua
subscriber.Provider = provider
subscriber.DeviceToken = tk
var err error
if !reflect.DeepEqual(subscriber, prev) {
err = database.C.Save(&subscriber).Error
}
return subscriber, err return subscriber, err
} }
@ -41,7 +59,7 @@ func NewNotification(notification models.Notification) error {
func PushNotification(notification models.Notification) error { func PushNotification(notification models.Notification) error {
raw, _ := jsoniter.Marshal(notification) raw, _ := jsoniter.Marshal(notification)
for _, conn := range WsConn[notification.RecipientID] { for conn := range wsConn[notification.RecipientID] {
_ = conn.WriteMessage(1, raw) _ = conn.WriteMessage(1, raw)
} }
@ -72,7 +90,7 @@ func PushNotification(notification models.Notification) error {
Title: notification.Subject, Title: notification.Subject,
Body: notification.Content, Body: notification.Content,
}, },
Token: subscriber.DeviceID, Token: subscriber.DeviceToken,
} }
if response, err := client.Send(ctx, message); err != nil { if response, err := client.Send(ctx, message); err != nil {