Allow user view and remove notification subscriptions

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

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)
}