Mark all notification as read api

This commit is contained in:
LittleSheep 2025-01-01 11:16:54 +08:00
parent 61976f6c47
commit fcd3b56f89
2 changed files with 23 additions and 3 deletions

View File

@ -25,6 +25,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
notify.Post("/subscription", addNotifySubscriber)
notify.Delete("/subscription/:deviceId", removeNotifySubscriber)
notify.Put("/read", markNotificationReadBatch)
notify.Put("/read/all", markNotificationAllRead)
notify.Put("/read/:notificationId", markNotificationRead)
}

View File

@ -1,14 +1,15 @@
package api
import (
"strconv"
"time"
"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"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"strconv"
"time"
)
func getNotifications(c *fiber.Ctx) error {
@ -110,11 +111,29 @@ func markNotificationReadBatch(c *fiber.Ctx) error {
Updates(&models.Notification{ReadAt: lo.ToPtr(time.Now())}).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
services.AddEvent(user.ID, "notifications.markAll.read", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
services.AddEvent(user.ID, "notifications.markBatch.read", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.SendStatus(fiber.StatusOK)
}
}
func markNotificationAllRead(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err
}
user := c.Locals("user").(models.Account)
if tx := database.C.Model(&models.Notification{}).
Where("account_id = ? AND read_at IS NULL", user.ID).
Updates(&models.Notification{ReadAt: lo.ToPtr(time.Now())}); tx.Error != nil {
return fiber.NewError(fiber.StatusInternalServerError, tx.Error.Error())
} else {
services.AddEvent(user.ID, "notifications.markAll.read", strconv.Itoa(int(user.ID)), c.IP(), c.Get(fiber.HeaderUserAgent))
return c.JSON(fiber.Map{
"count": tx.RowsAffected,
})
}
}
func getNotifySubscriber(c *fiber.Ctx) error {
if err := exts.EnsureAuthenticated(c); err != nil {
return err