Passport/pkg/server/notify_api.go

50 lines
1.5 KiB
Go

package server
import (
"git.solsynth.dev/hydrogen/identity/pkg/models"
"git.solsynth.dev/hydrogen/identity/pkg/services"
"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"`
Subject string `json:"subject" validate:"required,max=1024"`
Content string `json:"content" validate:"required,max=3072"`
Links []models.NotificationLink `json:"links"`
IsImportant bool `json:"is_important"`
UserID uint `json:"user_id" validate:"required"`
}
if err := 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{
Subject: data.Subject,
Content: data.Subject,
Links: data.Links,
IsImportant: data.IsImportant,
ReadAt: nil,
RecipientID: user.ID,
SenderID: &client.ID,
}
if err := services.NewNotification(notification); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
}
return c.SendStatus(fiber.StatusOK)
}