♻️ Improve notifications mark read system

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

View File

@ -4,10 +4,12 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":bug: Trying to prevent send same notification to the same user in batch">
<list default="true" id="3fefb2c4-b6f9-466b-a523-53352e8d6f95" name="更改" comment=":sparkles: Allow user view and remove notification subscriptions">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/index.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/index.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/models/notifications.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/models/notifications.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/notifications_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/notifications_api.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/accounts.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/accounts.go" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/services/cleaner.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/cleaner.go" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -154,7 +156,6 @@
</option>
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value=":sparkles: Pagination bots api" />
<MESSAGE value=":bug: Fix query issue" />
<MESSAGE value=":bug: Fix compare perm node panic" />
<MESSAGE value=":bug: Fix compare perm node function" />
@ -179,7 +180,8 @@
<MESSAGE value=":sparkles: Auth config to limit auth steps" />
<MESSAGE value=":recycle: Single table to store auth preferences" />
<MESSAGE value=":bug: Trying to prevent send same notification to the same user in batch" />
<option name="LAST_COMMIT_MESSAGE" value=":bug: Trying to prevent send same notification to the same user in batch" />
<MESSAGE value=":sparkles: Allow user view and remove notification subscriptions" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Allow user view and remove notification subscriptions" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

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.")
}