Allow user view and remove notification subscriptions

This commit is contained in:
LittleSheep 2024-10-13 12:53:47 +08:00
parent 821e0c3e60
commit bee9799415
3 changed files with 45 additions and 5 deletions

View File

@ -4,9 +4,10 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":recycle: Single table to store auth preferences">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Trying to prevent send same notification to the same user in batch">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/grpc/notifier.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/grpc/notifier.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/index.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/notifications_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/notifications_api.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -153,7 +154,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Return affiliated to and automated by in userinfo grpc call" />
<MESSAGE value=":sparkles: Pagination bots api" />
<MESSAGE value=":bug: Fix query issue" />
<MESSAGE value=":bug: Fix compare perm node panic" />
@ -178,7 +178,8 @@
<MESSAGE value=":sparkles: Account deletion" />
<MESSAGE value=":sparkles: Auth config to limit auth steps" />
<MESSAGE value=":recycle: Single table to store auth preferences" />
<option name="LAST_COMMIT_MESSAGE" value=":recycle: Single table to store auth preferences" />
<MESSAGE value=":bug: Trying to prevent send same notification to the same user in batch" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Trying to prevent send same notification to the same user in batch" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

View File

@ -18,8 +18,13 @@ func MapAPIs(app *fiber.App, baseURL string) {
notify := api.Group("/notifications").Name("Notifications API")
{
notify.Get("/", getNotifications)
// Deprecated, use /subscription instead, will be removed in the future
notify.Post("/subscribe", addNotifySubscriber)
notify.Get("/", getNotifications)
notify.Get("/subscription", getNotifySubscriber)
notify.Post("/subscription", addNotifySubscriber)
notify.Delete("/subscription/:deviceId", removeNotifySubscriber)
notify.Put("/read", markNotificationReadBatch)
notify.Put("/read/:notificationId", markNotificationRead)
}

View File

@ -88,6 +88,22 @@ func markNotificationReadBatch(c *fiber.Ctx) error {
}
}
func getNotifySubscriber(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
var subscribers []models.NotificationSubscriber
if err := database.C.Where(&models.NotificationSubscriber{
AccountID: user.ID,
}).Find(&subscribers).Error; err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
return c.JSON(subscribers)
}
func addNotifySubscriber(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
@ -126,3 +142,21 @@ func addNotifySubscriber(c *fiber.Ctx) error {
return c.JSON(subscriber)
}
func removeNotifySubscriber(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
device := c.Params("deviceId")
if err := database.C.Where(&models.NotificationSubscriber{
DeviceID: device,
AccountID: user.ID,
}).Delete(&models.NotificationSubscriber{}).Error; err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}