2024-02-01 07:58:28 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2024-06-17 14:21:34 +00:00
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/models"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
|
|
|
|
"git.solsynth.dev/hydrogen/passport/pkg/internal/utils"
|
2024-02-01 07:58:28 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getNotifications(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
2024-02-28 15:30:29 +00:00
|
|
|
tx := database.C.Where(&models.Notification{RecipientID: user.ID}).Model(&models.Notification{})
|
|
|
|
|
2024-02-01 07:58:28 +00:00
|
|
|
var count int64
|
|
|
|
var notifications []models.Notification
|
2024-02-28 15:30:29 +00:00
|
|
|
if err := tx.
|
2024-02-01 07:58:28 +00:00
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-02-28 15:30:29 +00:00
|
|
|
if err := tx.
|
2024-02-01 07:58:28 +00:00
|
|
|
Limit(take).
|
|
|
|
Offset(offset).
|
|
|
|
Find(¬ifications).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": notifications,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func markNotificationRead(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
id, _ := c.ParamsInt("notificationId", 0)
|
|
|
|
|
2024-04-28 11:59:27 +00:00
|
|
|
var notify models.Notification
|
2024-02-01 07:58:28 +00:00
|
|
|
if err := database.C.Where(&models.Notification{
|
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
|
|
|
RecipientID: user.ID,
|
2024-04-28 11:59:27 +00:00
|
|
|
}).First(¬ify).Error; err != nil {
|
2024-02-01 07:58:28 +00:00
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-06-07 12:05:56 +00:00
|
|
|
if err := database.C.Delete(¬ify).Error; err != nil {
|
2024-04-28 11:59:27 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func markNotificationReadBatch(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
|
|
|
MessageIDs []uint `json:"messages"`
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := utils.BindAndValidate(c, &data); err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.Model(&models.Notification{}).
|
|
|
|
Where("recipient_id = ? AND id IN ?", user.ID, data.MessageIDs).
|
2024-06-07 12:05:56 +00:00
|
|
|
Delete(&models.Notification{}).Error; err != nil {
|
2024-02-01 07:58:28 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else {
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 15:15:16 +00:00
|
|
|
|
|
|
|
func addNotifySubscriber(c *fiber.Ctx) error {
|
|
|
|
user := c.Locals("principal").(models.Account)
|
|
|
|
|
|
|
|
var data struct {
|
2024-05-07 13:00:20 +00:00
|
|
|
Provider string `json:"provider" validate:"required"`
|
|
|
|
DeviceToken string `json:"device_token" validate:"required"`
|
|
|
|
DeviceID string `json:"device_id" validate:"required"`
|
2024-02-07 15:15:16 +00:00
|
|
|
}
|
|
|
|
|
2024-04-20 14:50:09 +00:00
|
|
|
if err := utils.BindAndValidate(c, &data); err != nil {
|
2024-02-07 15:15:16 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-08 03:08:29 +00:00
|
|
|
var count int64
|
|
|
|
if err := database.C.Where(&models.NotificationSubscriber{
|
2024-05-07 13:00:20 +00:00
|
|
|
DeviceID: data.DeviceID,
|
|
|
|
DeviceToken: data.DeviceToken,
|
|
|
|
AccountID: user.ID,
|
2024-02-08 03:08:29 +00:00
|
|
|
}).Model(&models.NotificationSubscriber{}).Count(&count).Error; err != nil || count > 0 {
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
|
2024-02-07 15:15:16 +00:00
|
|
|
subscriber, err := services.AddNotifySubscriber(
|
|
|
|
user,
|
|
|
|
data.Provider,
|
|
|
|
data.DeviceID,
|
2024-05-07 13:00:20 +00:00
|
|
|
data.DeviceToken,
|
2024-02-07 15:15:16 +00:00
|
|
|
c.Get(fiber.HeaderUserAgent),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(subscriber)
|
|
|
|
}
|