Subscribable notification

This commit is contained in:
2024-02-07 23:15:16 +08:00
parent cc2fa06c72
commit 775a3b8868
6 changed files with 70 additions and 15 deletions

View File

@ -3,6 +3,7 @@ package server
import (
"code.smartsheep.studio/hydrogen/passport/pkg/database"
"code.smartsheep.studio/hydrogen/passport/pkg/models"
"code.smartsheep.studio/hydrogen/passport/pkg/services"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"time"
@ -57,3 +58,28 @@ func markNotificationRead(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusOK)
}
}
func addNotifySubscriber(c *fiber.Ctx) error {
user := c.Locals("principal").(models.Account)
var data struct {
Provider string `json:"provider" validate:"required"`
DeviceID string `json:"device_id" validate:"required"`
}
if err := BindAndValidate(c, &data); err != nil {
return err
}
subscriber, err := services.AddNotifySubscriber(
user,
data.Provider,
data.DeviceID,
c.Get(fiber.HeaderUserAgent),
)
if err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.JSON(subscriber)
}

View File

@ -62,6 +62,7 @@ func NewServer() {
api.Get("/notifications", auth, getNotifications)
api.Put("/notifications/:notificationId/read", auth, markNotificationRead)
api.Post("/notification/subscribe", auth, addNotifySubscriber)
api.Get("/users/me", auth, getUserinfo)
api.Put("/users/me", auth, editUserinfo)