Implemented old notification deletion

This commit is contained in:
Eduard Urbach 2018-11-15 14:51:05 +09:00
parent 6dc98a695a
commit a4e3a5fbb7
2 changed files with 19 additions and 39 deletions

View File

@ -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()
}
}

View File

@ -1,13 +1,11 @@
package main package main
import ( import (
"fmt"
"github.com/animenotifier/arn" "github.com/animenotifier/arn"
"github.com/fatih/color" "github.com/fatih/color"
) )
const maxNotificationsPerUser = 80 const maxNotificationsPerUser = 30
func main() { func main() {
color.Yellow("Deleting old notifications") color.Yellow("Deleting old notifications")
@ -15,11 +13,27 @@ func main() {
defer color.Green("Finished") defer color.Green("Finished")
defer arn.Node.Close() defer arn.Node.Close()
count := 0
for user := range arn.StreamUsers() { for user := range arn.StreamUsers() {
notificationCount := len(user.Notifications().Items) notifications := user.Notifications()
notificationCount := len(notifications.Items)
if notificationCount > maxNotificationsPerUser { 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)
} }