diff --git a/patches/delete-old-notifications/delete-old-notifications.go b/patches/delete-old-notifications/delete-old-notifications.go deleted file mode 100644 index 6c5064fc..00000000 --- a/patches/delete-old-notifications/delete-old-notifications.go +++ /dev/null @@ -1,34 +0,0 @@ -package main - -import ( - "fmt" - "time" - - "github.com/animenotifier/arn" - "github.com/fatih/color" -) - -const month = 30 * 24 * time.Hour - -func main() { - color.Yellow("Deleting private user data") - - defer arn.Node.Close() - defer color.Green("Finished.") - - for user := range arn.StreamUsers() { - color.Cyan(user.Nick) - - for _, notification := range user.Notifications().Notifications() { - if time.Since(notification.CreatedTime()) < 2*month { - continue - } - - fmt.Println(notification) - // notification.Delete() - } - - // Save in DB - // user.Save() - } -} diff --git a/patches/notification-delete-old/notification-delete-old.go b/patches/notification-delete-old/notification-delete-old.go index 04026e1c..97cffa5a 100644 --- a/patches/notification-delete-old/notification-delete-old.go +++ b/patches/notification-delete-old/notification-delete-old.go @@ -1,13 +1,11 @@ package main import ( - "fmt" - "github.com/animenotifier/arn" "github.com/fatih/color" ) -const maxNotificationsPerUser = 80 +const maxNotificationsPerUser = 30 func main() { color.Yellow("Deleting old notifications") @@ -15,11 +13,27 @@ func main() { defer color.Green("Finished") defer arn.Node.Close() + count := 0 + for user := range arn.StreamUsers() { - notificationCount := len(user.Notifications().Items) + notifications := user.Notifications() + notificationCount := len(notifications.Items) if notificationCount > maxNotificationsPerUser { - fmt.Println(user.Nick, notificationCount) + cut := len(notifications.Items) - maxNotificationsPerUser + deletedItems := notifications.Items[:cut] + newItems := notifications.Items[cut:] + + for _, notificationID := range deletedItems { + arn.DB.Delete("Notification", notificationID) + } + + notifications.Items = newItems + notifications.Save() + + count += len(deletedItems) } } + + color.Green("Deleted %d notifications", count) }