diff --git a/README.md b/README.md index a724809..8cec0b7 100644 --- a/README.md +++ b/README.md @@ -126,26 +126,15 @@ the web app by using the admin controls on their profile page. templates, issue redirects, error pages, ... * `pkg/utility`: miscellaneous useful functions for the app. -## Cron API Endpoints +## Cron workers -In settings.json get or configure the CronAPIKey (a UUID4 value is good and -the app generates a fresh one by default). The following are the cron API -endpoints that you may want to configure to run periodic maintenance tasks -on the app, such as to remove orphaned comment photos. - -### GET /v1/comment-photos/remove-orphaned - -Query parameters: `apiKey` which is the CronAPIKey. - -This endpoint removes orphaned CommentPhotos (photo attachments to forum -posts). An orphaned photo is one that has no CommentID and was uploaded -older than 24 hours ago; e.g. a user uploaded a picture but then did not -complete the posting of their comment. - -Suggested crontab: +You can schedule the `nonshy vacuum` command in your crontab. This command +will check and clean up the database for things such as: orphaned comment +photos (where somebody uploaded a photo to post on the forum, but then didn't +finish creating their post). ```cron -0 2 * * * curl "http://localhost:8080/v1/comment-photos/remove-orphaned?apiKey=X" +0 2 * * * cd /home/nonshy/git/website && ./nonshy vacuum ``` ## License diff --git a/pkg/controller/api/orphaned_comment_photos.go b/pkg/controller/api/orphaned_comment_photos.go deleted file mode 100644 index c0547da..0000000 --- a/pkg/controller/api/orphaned_comment_photos.go +++ /dev/null @@ -1,72 +0,0 @@ -package api - -import ( - "net/http" - - "code.nonshy.com/nonshy/website/pkg/config" - "code.nonshy.com/nonshy/website/pkg/worker" -) - -// RemoveOrphanedCommentPhotos API. -// -// URL: /v1/comment-photos/remove-orphaned -// -// Query parameters: ?apiKey={CronAPIKey} -// -// This endpoint looks for CommentPhotos having a blank CommentID that were created older -// than 24 hours ago and removes them. Configure the "CronAPIKey" in your settings.json -// and pass it as the query parameter. -func RemoveOrphanedCommentPhotos() http.HandlerFunc { - // Response JSON schema. - type Response struct { - OK bool `json:"OK"` - Error string `json:"error,omitempty"` - Total int64 `json:"total"` - Removed int64 `json:"removed"` - } - - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - if r.Method != http.MethodGet { - SendJSON(w, http.StatusNotAcceptable, Response{ - Error: "GET method only", - }) - return - } - - // Get and validate the API key. - var ( - apiKey = r.FormValue("apiKey") - compare = config.Current.CronAPIKey - ) - - if compare == "" { - SendJSON(w, http.StatusInternalServerError, Response{ - OK: false, - Error: "app CronAPIKey is not configured", - }) - return - } else if apiKey == "" || apiKey != compare { - SendJSON(w, http.StatusInternalServerError, Response{ - OK: false, - Error: "invalid apiKey query parameter", - }) - return - } - - // Do the needful. - total, err := worker.VacuumOrphanedCommentPhotos(false) - if err != nil { - SendJSON(w, http.StatusInternalServerError, Response{ - Error: err.Error(), - }) - return - } - - // Send success response. - SendJSON(w, http.StatusOK, Response{ - OK: true, - Total: total, - Removed: total, - }) - }) -} diff --git a/pkg/models/comment_photo.go b/pkg/models/comment_photo.go index f4c52eb..0788223 100644 --- a/pkg/models/comment_photo.go +++ b/pkg/models/comment_photo.go @@ -134,7 +134,15 @@ func GetOrphanedCommentPhotos() ([]*CommentPhoto, int64, error) { ps = []*CommentPhoto{} ) - query := DB.Model(&CommentPhoto{}).Where("comment_id = 0 AND created_at < ?", cutoff) + query := DB.Model(&CommentPhoto{}).Where(` + (comment_id <> 0 AND NOT EXISTS ( + SELECT 1 FROM comments + WHERE comments.id = comment_photos.comment_id + )) + OR + (comment_id = 0 AND created_at < ?)`, + cutoff, + ) query.Count(&count) res := query.Limit(500).Find(&ps) if res.Error != nil { diff --git a/pkg/router/router.go b/pkg/router/router.go index 24b82fd..e23221e 100644 --- a/pkg/router/router.go +++ b/pkg/router/router.go @@ -121,7 +121,6 @@ func New() http.Handler { mux.Handle("POST /v1/notifications/read", middleware.LoginRequired(api.ReadNotification())) mux.Handle("POST /v1/notifications/delete", middleware.LoginRequired(api.ClearNotification())) mux.Handle("POST /v1/photos/mark-explicit", middleware.LoginRequired(api.MarkPhotoExplicit())) - mux.Handle("GET /v1/comment-photos/remove-orphaned", api.RemoveOrphanedCommentPhotos()) mux.Handle("POST /v1/barertc/report", barertc.Report()) mux.Handle("POST /v1/barertc/profile", barertc.Profile())