2024-02-06 04:28:12 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-03-20 12:56:43 +00:00
|
|
|
"git.solsynth.dev/hydrogen/identity/pkg/models"
|
|
|
|
"git.solsynth.dev/hydrogen/identity/pkg/services"
|
2024-02-06 04:28:12 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func notifyUser(c *fiber.Ctx) error {
|
|
|
|
var data struct {
|
2024-02-07 15:40:43 +00:00
|
|
|
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"`
|
2024-03-31 08:03:59 +00:00
|
|
|
IsRealtime bool `json:"is_realtime"`
|
2024-02-07 15:40:43 +00:00
|
|
|
UserID uint `json:"user_id" validate:"required"`
|
2024-02-06 04:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-03-31 05:04:48 +00:00
|
|
|
notification := models.Notification{
|
|
|
|
Subject: data.Subject,
|
2024-03-31 08:03:59 +00:00
|
|
|
Content: data.Content,
|
2024-03-31 05:04:48 +00:00
|
|
|
Links: data.Links,
|
|
|
|
IsImportant: data.IsImportant,
|
2024-03-31 08:03:59 +00:00
|
|
|
IsRealtime: data.IsRealtime,
|
2024-03-31 05:04:48 +00:00
|
|
|
ReadAt: nil,
|
|
|
|
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())
|
|
|
|
}
|
2024-02-06 04:28:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|