♻️ Improve notifications mark read system

This commit is contained in:
2024-10-13 13:00:51 +08:00
parent bee9799415
commit 6ea4850459
5 changed files with 22 additions and 9 deletions

View File

@ -2,6 +2,7 @@ package models
import (
"gorm.io/datatypes"
"time"
)
type Notification struct {
@ -19,6 +20,8 @@ type Notification struct {
Account Account `json:"account"`
AccountID uint `json:"account_id"`
ReadAt *time.Time `json:"read_at"`
IsRealtime bool `json:"is_realtime" gorm:"-"`
IsForcePush bool `json:"is_force_push" gorm:"-"`
}

View File

@ -6,6 +6,8 @@ import (
"git.solsynth.dev/hydrogen/passport/pkg/internal/server/exts"
"git.solsynth.dev/hydrogen/passport/pkg/internal/services"
"github.com/gofiber/fiber/v2"
"github.com/samber/lo"
"time"
)
func getNotifications(c *fiber.Ctx) error {
@ -20,15 +22,16 @@ func getNotifications(c *fiber.Ctx) error {
tx := database.C.Where(&models.Notification{AccountID: user.ID}).Model(&models.Notification{})
var count int64
var notifications []models.Notification
if err := tx.
Count(&count).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
var notifications []models.Notification
if err := tx.
Limit(take).
Offset(offset).
Order("CASE WHEN read_at IS NULL THEN 0 ELSE 1 END, created_at").
Find(&notifications).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
}
@ -58,7 +61,9 @@ func markNotificationRead(c *fiber.Ctx) error {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
if err := database.C.Delete(&notify).Error; err != nil {
notify.ReadAt = lo.ToPtr(time.Now())
if err := database.C.Save(&notify).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)
@ -81,7 +86,7 @@ func markNotificationReadBatch(c *fiber.Ctx) error {
if err := database.C.Model(&models.Notification{}).
Where("account_id = ? AND id IN ?", user.ID, data.MessageIDs).
Delete(&models.Notification{}).Error; err != nil {
Updates(&models.Notification{ReadAt: lo.ToPtr(time.Now())}).Error; err != nil {
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
} else {
return c.SendStatus(fiber.StatusOK)

View File

@ -322,8 +322,10 @@ func DeleteAccount(id uint) error {
}
func RecycleUnConfirmAccount() {
deadline := time.Now().Add(-24 * time.Hour)
var hitList []models.Account
if err := database.C.Where("confirmed_at IS NULL").Find(&hitList).Error; err != nil {
if err := database.C.Where("confirmed_at IS NULL AND created_at <= ?", deadline).Find(&hitList).Error; err != nil {
log.Error().Err(err).Msg("An error occurred while recycling accounts...")
return
}

View File

@ -20,7 +20,8 @@ func DoAutoDatabaseCleanup() {
}
deadline := time.Now().Add(-30 * 24 * time.Hour)
database.C.Unscoped().Where("created_at <= ?", deadline).Delete(&models.Notification{})
seenDeadline := time.Now().Add(-7 * 24 * time.Hour)
database.C.Unscoped().Where("created_at <= ? OR read_at <= ?", deadline, seenDeadline).Delete(&models.Notification{})
log.Debug().Int64("affected", count).Msg("Clean up entire database accomplished.")
}