diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index b1fb9ed..fbadb31 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,10 +4,12 @@
-
+
-
+
+
+
@@ -154,7 +156,6 @@
-
@@ -179,7 +180,8 @@
-
+
+
true
diff --git a/pkg/internal/models/notifications.go b/pkg/internal/models/notifications.go
index 2e2d966..d9bad50 100644
--- a/pkg/internal/models/notifications.go
+++ b/pkg/internal/models/notifications.go
@@ -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:"-"`
}
diff --git a/pkg/internal/server/api/notifications_api.go b/pkg/internal/server/api/notifications_api.go
index 2d22a32..1ff58ae 100644
--- a/pkg/internal/server/api/notifications_api.go
+++ b/pkg/internal/server/api/notifications_api.go
@@ -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(¬ifications).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(¬ify).Error; err != nil {
+ notify.ReadAt = lo.ToPtr(time.Now())
+
+ if err := database.C.Save(¬ify).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)
diff --git a/pkg/internal/services/accounts.go b/pkg/internal/services/accounts.go
index 34d2240..ec9c7cf 100644
--- a/pkg/internal/services/accounts.go
+++ b/pkg/internal/services/accounts.go
@@ -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
}
diff --git a/pkg/internal/services/cleaner.go b/pkg/internal/services/cleaner.go
index bb199b8..4c084f1 100644
--- a/pkg/internal/services/cleaner.go
+++ b/pkg/internal/services/cleaner.go
@@ -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.")
}