Passport/pkg/internal/http/admin/notify_api.go

121 lines
3.6 KiB
Go
Raw Normal View History

2024-06-30 03:32:01 +00:00
package admin
import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
2024-10-27 04:50:07 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/http/exts"
2024-06-30 03:32:01 +00:00
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"github.com/rs/zerolog/log"
)
func notifyAllUser(c *fiber.Ctx) error {
var data struct {
2024-10-26 16:06:23 +00:00
Topic string `json:"type" validate:"required"`
Title string `json:"subject" validate:"required,max=1024"`
Subtitle string `json:"subtitle" validate:"max=1024"`
Body string `json:"content" validate:"required,max=4096"`
Metadata map[string]any `json:"metadata"`
Priority int `json:"priority"`
IsRealtime bool `json:"is_realtime"`
2024-06-30 03:32:01 +00:00
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
if err := exts.EnsureGrantedPerm(c, "AdminNotifyAll", true); err != nil {
return err
}
2024-07-03 15:07:59 +00:00
operator := c.Locals("user").(models.Account)
2024-06-30 03:32:01 +00:00
var users []models.Account
if err := database.C.Find(&users).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
2024-07-03 15:07:59 +00:00
} else {
services.AddAuditRecord(operator, "notify.all", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{
"payload": data,
})
2024-06-30 03:32:01 +00:00
}
go func() {
for _, user := range users {
notification := models.Notification{
2024-10-26 16:06:23 +00:00
Topic: data.Topic,
Subtitle: data.Subtitle,
Title: data.Title,
Body: data.Body,
Metadata: data.Metadata,
Priority: data.Priority,
Account: user,
AccountID: user.ID,
2024-06-30 03:32:01 +00:00
}
if data.IsRealtime {
if err := services.PushNotification(notification); err != nil {
log.Error().Err(err).Uint("user", user.ID).Msg("Failed to push notification...")
}
} else {
if err := services.NewNotification(notification); err != nil {
log.Error().Err(err).Uint("user", user.ID).Msg("Failed to create notification...")
}
}
}
}()
return c.SendStatus(fiber.StatusOK)
}
2024-07-03 15:07:59 +00:00
func notifyOneUser(c *fiber.Ctx) error {
var data struct {
2024-10-26 16:06:23 +00:00
Topic string `json:"type" validate:"required"`
Title string `json:"subject" validate:"required,max=1024"`
Subtitle string `json:"subtitle" validate:"max=1024"`
Body string `json:"content" validate:"required,max=4096"`
Metadata map[string]any `json:"metadata"`
Priority int `json:"priority"`
IsRealtime bool `json:"is_realtime"`
UserID uint `json:"user_id" validate:"required"`
2024-07-03 15:07:59 +00:00
}
if err := exts.BindAndValidate(c, &data); err != nil {
return err
}
if err := exts.EnsureGrantedPerm(c, "AdminNotifyAll", true); err != nil {
return err
}
operator := c.Locals("user").(models.Account)
var user models.Account
if err := database.C.Where("id = ?", data.UserID).First(&user).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
services.AddAuditRecord(operator, "notify.one", c.IP(), c.Get(fiber.HeaderUserAgent), map[string]any{
"user_id": user.ID,
"payload": data,
})
}
notification := models.Notification{
2024-10-26 16:06:23 +00:00
Topic: data.Topic,
Subtitle: data.Subtitle,
Title: data.Title,
Body: data.Body,
Priority: data.Priority,
AccountID: user.ID,
2024-07-03 15:07:59 +00:00
}
if data.IsRealtime {
if err := services.PushNotification(notification); err != nil {
log.Error().Err(err).Uint("user", user.ID).Msg("Failed to push notification...")
}
} else {
if err := services.NewNotification(notification); err != nil {
log.Error().Err(err).Uint("user", user.ID).Msg("Failed to create notification...")
}
}
return c.SendStatus(fiber.StatusOK)
}