Passport/pkg/internal/server/notify_api.go

61 lines
2.0 KiB
Go
Raw Normal View History

package server
import (
2024-04-13 05:48:19 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/models"
"git.solsynth.dev/hydrogen/passport/pkg/services"
2024-04-20 14:50:09 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/utils"
"github.com/gofiber/fiber/v2"
)
func notifyUser(c *fiber.Ctx) error {
var data struct {
ClientID string `json:"client_id" validate:"required"`
ClientSecret string `json:"client_secret" validate:"required"`
2024-06-07 12:05:56 +00:00
Type string `json:"type" validate:"required"`
Subject string `json:"subject" validate:"required,max=1024"`
2024-06-07 12:05:56 +00:00
Content string `json:"content" validate:"required,max=4096"`
Metadata map[string]any `json:"metadata"`
Links []models.NotificationLink `json:"links"`
2024-06-07 12:05:56 +00:00
IsForcePush bool `json:"is_force_push"`
2024-03-31 08:03:59 +00:00
IsRealtime bool `json:"is_realtime"`
UserID uint `json:"user_id" validate:"required"`
}
2024-04-20 14:50:09 +00:00
if err := utils.BindAndValidate(c, &data); err != nil {
return err
}
client, err := services.GetThirdClientWithSecret(data.ClientID, data.ClientSecret)
if err != nil {
return fiber.NewError(fiber.StatusForbidden, err.Error())
}
var user models.Account
if user, err = services.GetAccount(data.UserID); err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
notification := models.Notification{
2024-06-07 12:05:56 +00:00
Type: data.Type,
Subject: data.Subject,
2024-03-31 08:03:59 +00:00
Content: data.Content,
Links: data.Links,
2024-03-31 08:03:59 +00:00
IsRealtime: data.IsRealtime,
2024-06-07 12:05:56 +00:00
IsForcePush: data.IsForcePush,
RecipientID: user.ID,
SenderID: &client.ID,
}
2024-03-31 08:03:59 +00:00
if data.IsRealtime {
if err := services.PushNotification(notification); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
} else {
if err := services.NewNotification(notification); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
}
return c.SendStatus(fiber.StatusOK)
}