2024-06-22 05:04:21 +00:00
|
|
|
package api
|
2024-02-01 07:58:28 +00:00
|
|
|
|
|
|
|
import (
|
2024-10-31 12:38:50 +00:00
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/authkit/models"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/database"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/http/exts"
|
|
|
|
"git.solsynth.dev/hypernet/passport/pkg/internal/services"
|
2024-02-01 07:58:28 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2024-10-13 05:00:51 +00:00
|
|
|
"github.com/samber/lo"
|
2024-10-14 14:23:51 +00:00
|
|
|
"strconv"
|
2024-10-13 05:00:51 +00:00
|
|
|
"time"
|
2024-02-01 07:58:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func getNotifications(c *fiber.Ctx) error {
|
|
|
|
take := c.QueryInt("take", 0)
|
|
|
|
offset := c.QueryInt("offset", 0)
|
|
|
|
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-06-22 05:04:21 +00:00
|
|
|
|
2024-07-15 16:05:09 +00:00
|
|
|
tx := database.C.Where(&models.Notification{AccountID: user.ID}).Model(&models.Notification{})
|
2024-02-28 15:30:29 +00:00
|
|
|
|
2024-02-01 07:58:28 +00:00
|
|
|
var count int64
|
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-10-13 05:00:51 +00:00
|
|
|
var notifications []models.Notification
|
2024-02-28 15:30:29 +00:00
|
|
|
if err := tx.
|
2024-02-01 07:58:28 +00:00
|
|
|
Limit(take).
|
|
|
|
Offset(offset).
|
2024-10-16 13:07:53 +00:00
|
|
|
Order("read_at DESC, created_at DESC").
|
2024-02-01 07:58:28 +00:00
|
|
|
Find(¬ifications).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
"data": notifications,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-09 15:24:12 +00:00
|
|
|
func getNotificationCount(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
user := c.Locals("user").(models.Account)
|
|
|
|
|
|
|
|
tx := database.C.Where(&models.Notification{AccountID: user.ID}).Model(&models.Notification{})
|
|
|
|
|
|
|
|
var count int64
|
|
|
|
if err := tx.
|
|
|
|
Count(&count).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(fiber.Map{
|
|
|
|
"count": count,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-02-01 07:58:28 +00:00
|
|
|
func markNotificationRead(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-02-01 07:58:28 +00:00
|
|
|
id, _ := c.ParamsInt("notificationId", 0)
|
|
|
|
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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{
|
2024-07-15 16:02:28 +00:00
|
|
|
BaseModel: models.BaseModel{ID: uint(id)},
|
2024-07-15 16:05:09 +00:00
|
|
|
AccountID: 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-10-13 05:00:51 +00:00
|
|
|
notify.ReadAt = lo.ToPtr(time.Now())
|
|
|
|
|
|
|
|
if err := database.C.Save(¬ify).Error; err != nil {
|
2024-04-28 11:59:27 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else {
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "notifications.mark.read", strconv.Itoa(int(notify.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-04-28 11:59:27 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func markNotificationReadBatch(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-04-28 11:59:27 +00:00
|
|
|
|
|
|
|
var data struct {
|
|
|
|
MessageIDs []uint `json:"messages"`
|
|
|
|
}
|
|
|
|
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.BindAndValidate(c, &data); err != nil {
|
2024-04-28 11:59:27 +00:00
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := database.C.Model(&models.Notification{}).
|
2024-07-23 14:04:52 +00:00
|
|
|
Where("account_id = ? AND id IN ?", user.ID, data.MessageIDs).
|
2024-10-13 05:00:51 +00:00
|
|
|
Updates(&models.Notification{ReadAt: lo.ToPtr(time.Now())}).Error; err != nil {
|
2024-02-01 07:58:28 +00:00
|
|
|
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
|
|
|
} else {
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "notifications.markAll.read", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-02-01 07:58:28 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 15:15:16 +00:00
|
|
|
|
2024-10-13 04:53:47 +00:00
|
|
|
func getNotifySubscriber(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-10-13 04:53:47 +00:00
|
|
|
|
|
|
|
var subscribers []models.NotificationSubscriber
|
|
|
|
if err := database.C.Where(&models.NotificationSubscriber{
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).Find(&subscribers).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(subscribers)
|
|
|
|
}
|
|
|
|
|
2024-02-07 15:15:16 +00:00
|
|
|
func addNotifySubscriber(c *fiber.Ctx) error {
|
2024-06-22 05:04:21 +00:00
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-02-07 15:15:16 +00:00
|
|
|
|
|
|
|
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-06-22 05:04:21 +00:00
|
|
|
if err := exts.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(
|
2024-10-30 16:17:53 +00:00
|
|
|
user,
|
2024-02-07 15:15:16 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "notifications.subscribe.push", data.DeviceID, c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-02-07 15:15:16 +00:00
|
|
|
return c.JSON(subscriber)
|
|
|
|
}
|
2024-10-13 04:53:47 +00:00
|
|
|
|
|
|
|
func removeNotifySubscriber(c *fiber.Ctx) error {
|
|
|
|
if err := exts.EnsureAuthenticated(c); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-30 16:17:53 +00:00
|
|
|
user := c.Locals("user").(models.Account)
|
2024-10-13 04:53:47 +00:00
|
|
|
|
|
|
|
device := c.Params("deviceId")
|
|
|
|
|
|
|
|
if err := database.C.Where(&models.NotificationSubscriber{
|
|
|
|
DeviceID: device,
|
|
|
|
AccountID: user.ID,
|
|
|
|
}).Delete(&models.NotificationSubscriber{}).Error; err != nil {
|
|
|
|
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
|
|
|
}
|
|
|
|
|
2024-10-14 14:23:51 +00:00
|
|
|
services.AddEvent(user.ID, "notifications.unsubscribe.push", device, c.IP(), c.Get(fiber.HeaderUserAgent))
|
2024-10-13 04:53:47 +00:00
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|