mirror of https://github.com/jlelse/GoBlog
Add support for undeleting posts within 7 days (#3)
parent
86625cc8fe
commit
337392112b
27 changed files with 482 additions and 76 deletions
@ -0,0 +1,32 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"log" |
||||
"time" |
||||
|
||||
"github.com/araddon/dateparse" |
||||
) |
||||
|
||||
func (a *goBlog) initPostsDeleter() { |
||||
a.hourlyHooks = append(a.hourlyHooks, func() { |
||||
a.checkDeletedPosts() |
||||
}) |
||||
} |
||||
|
||||
func (a *goBlog) checkDeletedPosts() { |
||||
// Get all posts with `deleted` parameter and a deleted status
|
||||
postsToDelete, err := a.getPosts(&postsRequestConfig{ |
||||
statusse: []postStatus{statusPublishedDeleted, statusDraftDeleted, statusPrivateDeleted, statusUnlistedDeleted, statusScheduledDeleted}, |
||||
parameter: "deleted", |
||||
}) |
||||
if err != nil { |
||||
log.Println("Error getting deleted posts:", err) |
||||
return |
||||
} |
||||
for _, post := range postsToDelete { |
||||
// Check if post is deleted for more than 7 days
|
||||
if deleted, err := dateparse.ParseLocal(post.firstParameter("deleted")); err == nil && deleted.Add(time.Hour*24*7).Before(time.Now()) { |
||||
a.deletePost(post.Path) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,61 @@ |
||||
package main |
||||
|
||||
import ( |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/stretchr/testify/require" |
||||
) |
||||
|
||||
func Test_checkDeletedPosts(t *testing.T) { |
||||
app := &goBlog{ |
||||
cfg: createDefaultTestConfig(t), |
||||
} |
||||
_ = app.initConfig() |
||||
_ = app.initDatabase(false) |
||||
app.initComponents(false) |
||||
|
||||
// Create a post
|
||||
app.createPost(&post{ |
||||
Content: "Test", |
||||
Status: statusPublished, |
||||
Path: "/testpost", |
||||
Section: "posts", |
||||
}) |
||||
|
||||
// Check if post count is 1
|
||||
count, err := app.db.countPosts(&postsRequestConfig{}) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 1, count) |
||||
|
||||
// Run deleter
|
||||
app.checkDeletedPosts() |
||||
|
||||
// Check if post count is still 1
|
||||
count, err = app.db.countPosts(&postsRequestConfig{}) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 1, count) |
||||
|
||||
// Delete the post
|
||||
err = app.deletePost("/testpost") |
||||
require.NoError(t, err) |
||||
|
||||
// Run deleter
|
||||
app.checkDeletedPosts() |
||||
|
||||
// Check if post count is still 1
|
||||
count, err = app.db.countPosts(&postsRequestConfig{}) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 1, count) |
||||
|
||||
// Set deleted time to more than 7 days ago
|
||||
app.db.replacePostParam("/testpost", "deleted", []string{time.Now().Add(-time.Hour * 24 * 8).Format(time.RFC3339)}) |
||||
|
||||
// Run deleter
|
||||
app.checkDeletedPosts() |
||||
|
||||
// Check if post count is 0
|
||||
count, err = app.db.countPosts(&postsRequestConfig{}) |
||||
require.NoError(t, err) |
||||
require.Equal(t, 0, count) |
||||
} |
Loading…
Reference in new issue