Compare commits
No commits in common. "18a4321685c81f0f7266d3fbe2b7dc085a12b87e" and "35f9580499a4a582627a98482551f01cda8bf64e" have entirely different histories.
18a4321685
...
35f9580499
6597
.idea/dataSources/723637bc-6ce3-4bbe-afb3-d88730c75a1b.xml
generated
6597
.idea/dataSources/723637bc-6ce3-4bbe-afb3-d88730c75a1b.xml
generated
File diff suppressed because it is too large
Load Diff
@ -1 +0,0 @@
|
||||
#n:passport
|
@ -1,2 +0,0 @@
|
||||
#n:public
|
||||
!<md> [41831, 0, null, null, -2147483648, -2147483648]
|
11
.idea/workspace.xml
generated
11
.idea/workspace.xml
generated
@ -4,11 +4,10 @@
|
||||
<option name="autoReloadType" value="ALL" />
|
||||
</component>
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":zap: Use map to improve message delivery time">
|
||||
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Dumb man make dumb mistake again">
|
||||
<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$/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" />
|
||||
<change beforePath="$PROJECT_DIR$/pkg/services/realms.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/services/realms.go" afterDir="false" />
|
||||
</list>
|
||||
<option name="SHOW_DIALOG" value="false" />
|
||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||
@ -150,9 +149,7 @@
|
||||
<MESSAGE value=":bug: Bug fix on missing id in realm" />
|
||||
<MESSAGE value=":bug: Bug fixes on realm missing member on creation" />
|
||||
<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" />
|
||||
<option name="LAST_COMMIT_MESSAGE" value=":bug: Dumb man make dumb mistake again" />
|
||||
</component>
|
||||
<component name="VgoProject">
|
||||
<settings-migrated>true</settings-migrated>
|
||||
|
@ -34,6 +34,5 @@ type NotificationSubscriber struct {
|
||||
UserAgent string `json:"user_agent"`
|
||||
Provider string `json:"provider"`
|
||||
DeviceID string `json:"device_id" gorm:"uniqueIndex"`
|
||||
DeviceToken string `json:"device_token"`
|
||||
AccountID uint `json:"account_id"`
|
||||
}
|
||||
|
@ -90,7 +90,6 @@ func addNotifySubscriber(c *fiber.Ctx) error {
|
||||
|
||||
var data struct {
|
||||
Provider string `json:"provider" validate:"required"`
|
||||
DeviceToken string `json:"device_token" validate:"required"`
|
||||
DeviceID string `json:"device_id" validate:"required"`
|
||||
}
|
||||
|
||||
@ -101,7 +100,6 @@ func addNotifySubscriber(c *fiber.Ctx) error {
|
||||
var count int64
|
||||
if err := database.C.Where(&models.NotificationSubscriber{
|
||||
DeviceID: data.DeviceID,
|
||||
DeviceToken: data.DeviceToken,
|
||||
AccountID: user.ID,
|
||||
}).Model(&models.NotificationSubscriber{}).Count(&count).Error; err != nil || count > 0 {
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
@ -111,7 +109,6 @@ func addNotifySubscriber(c *fiber.Ctx) error {
|
||||
user,
|
||||
data.Provider,
|
||||
data.DeviceID,
|
||||
data.DeviceToken,
|
||||
c.Get(fiber.HeaderUserAgent),
|
||||
)
|
||||
if err != nil {
|
||||
|
@ -4,13 +4,14 @@ import (
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/models"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/services"
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
func listenNotifications(c *websocket.Conn) {
|
||||
user := c.Locals("principal").(models.Account)
|
||||
|
||||
// Push connection
|
||||
services.ClientRegister(user, c)
|
||||
services.WsConn[user.ID] = append(services.WsConn[user.ID], c)
|
||||
|
||||
// Event loop
|
||||
var err error
|
||||
@ -21,5 +22,7 @@ func listenNotifications(c *websocket.Conn) {
|
||||
}
|
||||
|
||||
// Pop connection
|
||||
services.ClientUnregister(user, c)
|
||||
services.WsConn[user.ID] = lo.Filter(services.WsConn[user.ID], func(item *websocket.Conn, idx int) bool {
|
||||
return item != c
|
||||
})
|
||||
}
|
||||
|
@ -5,18 +5,13 @@ import (
|
||||
"github.com/gofiber/contrib/websocket"
|
||||
)
|
||||
|
||||
var wsConn = make(map[uint]map[*websocket.Conn]bool)
|
||||
|
||||
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
|
||||
type WsPushRequest struct {
|
||||
Payload []byte
|
||||
RecipientID uint
|
||||
}
|
||||
|
||||
func ClientUnregister(user models.Account, conn *websocket.Conn) {
|
||||
if wsConn[user.ID] == nil {
|
||||
wsConn[user.ID] = make(map[*websocket.Conn]bool)
|
||||
}
|
||||
delete(wsConn[user.ID], conn)
|
||||
var WsConn = make(map[uint][]*websocket.Conn)
|
||||
|
||||
func CheckOnline(user models.Account) bool {
|
||||
return len(WsConn[user.ID]) > 0
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package services
|
||||
import (
|
||||
"context"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"reflect"
|
||||
|
||||
"firebase.google.com/go/messaging"
|
||||
"git.solsynth.dev/hydrogen/passport/pkg/database"
|
||||
@ -12,32 +11,15 @@ import (
|
||||
"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,
|
||||
AccountID: user.ID,
|
||||
}); err != nil {
|
||||
subscriber = models.NotificationSubscriber{
|
||||
func AddNotifySubscriber(user models.Account, provider, device, ua string) (models.NotificationSubscriber, error) {
|
||||
subscriber := models.NotificationSubscriber{
|
||||
UserAgent: ua,
|
||||
Provider: provider,
|
||||
DeviceID: id,
|
||||
DeviceToken: tk,
|
||||
DeviceID: device,
|
||||
AccountID: user.ID,
|
||||
}
|
||||
} else {
|
||||
prev = subscriber
|
||||
}
|
||||
|
||||
subscriber.UserAgent = ua
|
||||
subscriber.Provider = provider
|
||||
subscriber.DeviceToken = tk
|
||||
|
||||
var err error
|
||||
if !reflect.DeepEqual(subscriber, prev) {
|
||||
err = database.C.Save(&subscriber).Error
|
||||
}
|
||||
err := database.C.Save(&subscriber).Error
|
||||
|
||||
return subscriber, err
|
||||
}
|
||||
@ -59,7 +41,7 @@ func NewNotification(notification models.Notification) error {
|
||||
|
||||
func PushNotification(notification models.Notification) error {
|
||||
raw, _ := jsoniter.Marshal(notification)
|
||||
for conn := range wsConn[notification.RecipientID] {
|
||||
for _, conn := range WsConn[notification.RecipientID] {
|
||||
_ = conn.WriteMessage(1, raw)
|
||||
}
|
||||
|
||||
@ -90,7 +72,7 @@ func PushNotification(notification models.Notification) error {
|
||||
Title: notification.Subject,
|
||||
Body: notification.Content,
|
||||
},
|
||||
Token: subscriber.DeviceToken,
|
||||
Token: subscriber.DeviceID,
|
||||
}
|
||||
|
||||
if response, err := client.Send(ctx, message); err != nil {
|
||||
|
Loading…
x
Reference in New Issue
Block a user