Skip to content

Commit

Permalink
delete empty pads in purge command
Browse files Browse the repository at this point in the history
  • Loading branch information
0x46616c6b committed Apr 15, 2021
1 parent 7071128 commit a8a81aa
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Flags:

The command checks every Pad if the last edited date is older than the defined limit. Older Pads will be deleted.

Pads without any changes (revisions) will be deleted.
Pads without a suffix will be deleted after 30 days of inactivity.
Pads with the suffix "-temp" will be deleted after 24 hours of inactivity.
Pads with the suffix "-keep" will be deleted after 365 days of inactivity.
Expand Down
1 change: 1 addition & 0 deletions cmd/purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var (
longDescription = `
The command checks every Pad if the last edited date is older than the defined limit. Older Pads will be deleted.
Pads without any changes (revisions) will be deleted.
Pads without a suffix will be deleted after 30 days of inactivity.
Pads with the suffix "-temp" will be deleted after 24 hours of inactivity.
Pads with the suffix "-keep" will be deleted after 365 days of inactivity.
Expand Down
28 changes: 28 additions & 0 deletions pkg/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,34 @@ func (ep *Etherpad) CopyPad(sourceID, destinationID string, force bool) error {
return nil
}

// GetRevisionsCount returns the number of revisions of this pad.
// See: https://etherpad.org/doc/v1.8.4/#index_getrevisionscount_padid
func (ep *Etherpad) GetRevisionsCount(padID string) (int, error) {
params := map[string]interface{}{"padID": padID}
res, err := ep.sendRequest("getRevisionsCount", params)
if err != nil {
return 0, err
}
defer res.Body.Close()

var body struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
Revisions int `json:"revisions"`
}
}
if err = json.NewDecoder(res.Body).Decode(&body); err != nil {
return 0, err
}

if body.Code != 0 {
return 0, fmt.Errorf("error: %s (code: %d)", body.Message, body.Code)
}

return body.Data.Revisions, nil
}

func (ep *Etherpad) sendRequest(path string, params map[string]interface{}) (*http.Response, error) {
uri, err := url.Parse(fmt.Sprintf("%s/api/%s/%s", ep.url, ep.apiVersion, path))
if err != nil {
Expand Down
32 changes: 32 additions & 0 deletions pkg/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,35 @@ func TestEtherpad_CopyPad_NotFound(t *testing.T) {
err := etherpad.CopyPad("pad1", "pad2", false)
assert.NotNil(t, err)
}

func TestEtherpad_GetRevisionsCount_Successful(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code": 0, "message":"ok", "data": {"revisions": 31}}`))
}))
defer ts.Close()

etherpad := NewEtherpadClient(ts.URL, etherpadApiKey)
etherpad.Client = ts.Client()

rev, err := etherpad.GetRevisionsCount("pad")
assert.Nil(t, err)
assert.Equal(t, 31, rev)
}

func TestEtherpad_GetRevisionsCount_NotFound(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(`{"code": 1, "message":"padID does not exist", "data": null}`))
}))
defer ts.Close()

etherpad := NewEtherpadClient(ts.URL, etherpadApiKey)
etherpad.Client = ts.Client()

rev, err := etherpad.GetRevisionsCount("pad")
assert.NotNil(t, err)
assert.Equal(t, 0, rev)
}
12 changes: 9 additions & 3 deletions pkg/purge/purger.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,18 +66,24 @@ func (p *Purger) worker(pads chan string, out chan int) {
for pad := range pads {
log.WithField("pad", pad).Debug("Process Pad")

revisions, err := p.Etherpad.GetRevisionsCount(pad)
if err != nil {
log.WithError(err).WithField("pad", pad).Error("failed to get last edited time")
continue
}

lastEdited, err := p.Etherpad.GetLastEdited(pad)
if err != nil {
log.WithError(err).Error("")
return
continue
}

deletable := lastEdited.Before(time.Now().Add(padDuration(pad)))
deletable := lastEdited.Before(time.Now().Add(padDuration(pad))) || revisions == 0
if !deletable {
continue
}

log.WithField("pad", pad).WithField("lastEdited", lastEdited).Info("Delete Pad")
log.WithFields(log.Fields{"pad": pad, "lastEdited": lastEdited, "revisions": revisions}).Info("Delete Pad")
if p.DryRun {
continue
}
Expand Down

0 comments on commit a8a81aa

Please sign in to comment.