From e8b5b05755403a4a33df77b98ff51b9512f6a623 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 8 Feb 2024 11:08:29 +0800 Subject: [PATCH] :bug: Device no longer can register --- pkg/models/notifications.go | 2 +- pkg/server/notifications_api.go | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/models/notifications.go b/pkg/models/notifications.go index 08cf800..700d444 100644 --- a/pkg/models/notifications.go +++ b/pkg/models/notifications.go @@ -32,6 +32,6 @@ type NotificationSubscriber struct { UserAgent string `json:"user_agent"` Provider string `json:"provider"` - DeviceID string `json:"device_id"` + DeviceID string `json:"device_id" gorm:"uniqueIndex"` AccountID uint `json:"account_id"` } diff --git a/pkg/server/notifications_api.go b/pkg/server/notifications_api.go index 750c608..187de8d 100644 --- a/pkg/server/notifications_api.go +++ b/pkg/server/notifications_api.go @@ -1,5 +1,6 @@ package server +import "C" import ( "code.smartsheep.studio/hydrogen/passport/pkg/database" "code.smartsheep.studio/hydrogen/passport/pkg/models" @@ -71,6 +72,14 @@ func addNotifySubscriber(c *fiber.Ctx) error { return err } + var count int64 + if err := database.C.Where(&models.NotificationSubscriber{ + DeviceID: data.DeviceID, + AccountID: user.ID, + }).Model(&models.NotificationSubscriber{}).Count(&count).Error; err != nil || count > 0 { + return c.SendStatus(fiber.StatusOK) + } + subscriber, err := services.AddNotifySubscriber( user, data.Provider,