Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: purge notifications older than 1 year (#811)

Guilherme Oenningadf9ee2f548a
diff --git a/app/cmd/server.go b/app/cmd/server.go
index c58f31c2f..d25be59c6 100644
--- a/app/cmd/server.go
+++ b/app/cmd/server.go
@@ -7,6 +7,7 @@ import (
"syscall"
"github.com/getfider/fider/app/models"
+ "github.com/getfider/fider/app/models/cmd"
"github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/pkg/bus"
"github.com/getfider/fider/app/pkg/env"
@@ -38,6 +39,8 @@ func RunServer(settings *models.SystemSettings) int {
})
}
+ bus.Publish(ctx, &cmd.PurgeExpiredNotifications{})
+
e := routes(web.New(settings))
go e.Start(":" + env.Config.Port)
diff --git a/app/models/cmd/notification.go b/app/models/cmd/notification.go
index e484d5058..3abd37545 100644
--- a/app/models/cmd/notification.go
+++ b/app/models/cmd/notification.go
@@ -4,6 +4,10 @@ import "github.com/getfider/fider/app/models"
type MarkAllNotificationsAsRead struct{}
+type PurgeExpiredNotifications struct {
+ NumOfDeletedNotifications int
+}
+
type MarkNotificationAsRead struct {
ID int
}
diff --git a/app/services/sqlstore/postgres/notification.go b/app/services/sqlstore/postgres/notification.go
index 31bc64d9c..ed6eca145 100644
--- a/app/services/sqlstore/postgres/notification.go
+++ b/app/services/sqlstore/postgres/notification.go
@@ -7,13 +7,40 @@ import (
"github.com/getfider/fider/app/models"
"github.com/getfider/fider/app/models/cmd"
+ "github.com/getfider/fider/app/models/dto"
"github.com/getfider/fider/app/models/enum"
"github.com/getfider/fider/app/models/query"
"github.com/getfider/fider/app/pkg/dbx"
"github.com/getfider/fider/app/pkg/errors"
+ "github.com/getfider/fider/app/pkg/log"
"github.com/lib/pq"
)
+func purgeExpiredNotifications(ctx context.Context, c *cmd.PurgeExpiredNotifications) error {
+ log.Debug(ctx, "deleting notifications more than 1 year old")
+
+ trx, err := dbx.BeginTx(ctx)
+ if err != nil {
+ return errors.Wrap(err, "failed to open transaction")
+ }
+
+ count, err := trx.Execute("DELETE FROM notifications WHERE CREATED_AT <= NOW() - INTERVAL '365 days'")
+ if err != nil {
+ return errors.Wrap(err, "failed to delete expired notifications")
+ }
+
+ log.Debugf(ctx, "a total of @{RowsDeleted} notifications were deleted", dto.Props{
+ "RowsDeleted": count,
+ })
+
+ if err = trx.Commit(); err != nil {
+ return errors.Wrap(err, "failed commit transaction")
+ }
+
+ c.NumOfDeletedNotifications = int(count)
+ return nil
+}
+
func markAllNotificationsAsRead(ctx context.Context, c *cmd.MarkAllNotificationsAsRead) error {
return using(ctx, func(trx *dbx.Trx, tenant *models.Tenant, user *models.User) error {
if user == nil {
diff --git a/app/services/sqlstore/postgres/notifications_test.go b/app/services/sqlstore/postgres/notifications_test.go
index 38290af47..cd59b1775 100644
--- a/app/services/sqlstore/postgres/notifications_test.go
+++ b/app/services/sqlstore/postgres/notifications_test.go
@@ -1,6 +1,7 @@
package postgres_test
import (
+ "context"
"testing"
"time"
@@ -153,3 +154,30 @@ func TestNotificationStorage_GetNotificationByID_OtherUser(t *testing.T) {
Expect(errors.Cause(err)).Equals(app.ErrNotFound)
Expect(q.Result).IsNil()
}
+
+func TestNotificationStorage_PurgeExpiredNotifications(t *testing.T) {
+ SetupDatabaseTest(t)
+ defer TeardownDatabaseTest()
+ defer ResetDatabase()
+
+ newPost := &cmd.AddNewPost{Title: "Title", Description: "Description"}
+ err := bus.Dispatch(jonSnowCtx, newPost)
+ Expect(err).IsNil()
+
+ addNotification1 := &cmd.AddNewNotification{User: jonSnow, Title: "Hello World 1", Link: "http://www.google.com", PostID: newPost.Result.ID}
+ addNotification2 := &cmd.AddNewNotification{User: jonSnow, Title: "Hello World 2", Link: "http://www.google.com", PostID: newPost.Result.ID}
+ addNotification3 := &cmd.AddNewNotification{User: jonSnow, Title: "Hello World 3", Link: "http://www.microsoft.com", PostID: newPost.Result.ID}
+ addNotification4 := &cmd.AddNewNotification{User: jonSnow, Title: "Hello World 4", Link: "http://www.microsoft.com", PostID: newPost.Result.ID}
+ err = bus.Dispatch(aryaStarkCtx, addNotification1, addNotification2, addNotification3, addNotification4)
+ Expect(err).IsNil()
+
+ rows, err := trx.Execute("UPDATE notifications SET created_at = NOW() - INTERVAL '2 years' WHERE link = 'http://www.microsoft.com'")
+ Expect(err).IsNil()
+ Expect(rows).Equals(int64(2))
+
+ trx.MustCommit()
+
+ purgeCommand := &cmd.PurgeExpiredNotifications{}
+ bus.Publish(context.Background(), purgeCommand)
+ Expect(purgeCommand.NumOfDeletedNotifications).Equals(2)
+}
diff --git a/app/services/sqlstore/postgres/postgres.go b/app/services/sqlstore/postgres/postgres.go
index 058a18ac8..12b553cdb 100644
--- a/app/services/sqlstore/postgres/postgres.go
+++ b/app/services/sqlstore/postgres/postgres.go
@@ -31,6 +31,8 @@ func (s Service) Enabled() bool {
func (s Service) Init() {
bus.AddHandler(storeEvent)
+ bus.AddListener(purgeExpiredNotifications)
+
bus.AddHandler(markAllNotificationsAsRead)
bus.AddHandler(markNotificationAsRead)
bus.AddHandler(countUnreadNotifications)
diff --git a/app/services/sqlstore/postgres/setup_test.go b/app/services/sqlstore/postgres/setup_test.go
index 50985ceda..74738d0dc 100644
--- a/app/services/sqlstore/postgres/setup_test.go
+++ b/app/services/sqlstore/postgres/setup_test.go
@@ -80,12 +80,16 @@ func SetupDatabaseTest(t *testing.T) context.Context {
return trxCtx
}
+func ResetDatabase() {
+ dbx.Seed()
+}
+
func TeardownDatabaseTest() {
trx.MustRollback()
}
func TestMain(m *testing.M) {
- dbx.Seed()
+ ResetDatabase()
code := m.Run()
os.Exit(code)