diff --git a/api/domain_clear.go b/api/domain_clear.go new file mode 100644 index 0000000..25e3c84 --- /dev/null +++ b/api/domain_clear.go @@ -0,0 +1,82 @@ +package main + +import ( + "net/http" +) + +func domainClear(domain string) error { + if domain == "" { + return errorMissingField + } + + statement := ` + DELETE FROM votes + USING comments + WHERE comments.commentHex = votes.commentHex AND comments.domain = $1; + ` + _, err := db.Exec(statement, domain) + if err != nil { + logger.Errorf("cannot delete votes: %v", err) + return errorInternal + } + + statement = ` + DELETE FROM comments + WHERE comments.domain = $1; + ` + _, err = db.Exec(statement, domain) + if err != nil { + logger.Errorf(statement, domain) + return errorInternal + } + + statement = ` + DELETE FROM pages + WHERE pages.domain = $1; + ` + _, err = db.Exec(statement, domain) + if err != nil { + logger.Errorf(statement, domain) + return errorInternal + } + + return nil +} + +func domainClearHandler(w http.ResponseWriter, r *http.Request) { + type request struct { + OwnerToken *string `json:"ownerToken"` + Domain *string `json:"domain"` + } + + var x request + if err := bodyUnmarshal(r, &x); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) + return + } + + o, err := ownerGetByOwnerToken(*x.OwnerToken) + if err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) + return + } + + domain := domainStrip(*x.Domain) + isOwner, err := domainOwnershipVerify(o.OwnerHex, domain) + if err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) + return + } + + if !isOwner { + bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()}) + return + } + + if err = domainClear(*x.Domain); err != nil { + bodyMarshal(w, response{"success": false, "message": err.Error()}) + return + } + + bodyMarshal(w, response{"success": true}) +} diff --git a/api/domain_delete.go b/api/domain_delete.go index 24864bd..c9ede5d 100644 --- a/api/domain_delete.go +++ b/api/domain_delete.go @@ -19,17 +19,6 @@ func domainDelete(domain string) error { return errorNoSuchDomain } - statement = ` - DELETE FROM votes - USING comments - WHERE comments.commentHex = votes.commentHex AND comments.domain = $1; - ` - _, err = db.Exec(statement, domain) - if err != nil { - logger.Errorf("cannot delete votes: %v", err) - return errorInternal - } - statement = ` DELETE FROM views WHERE views.domain = $1; @@ -50,23 +39,9 @@ func domainDelete(domain string) error { return errorInternal } - statement = ` - DELETE FROM comments - WHERE comments.domain = $1; - ` - _, err = db.Exec(statement, domain) - if err != nil { - logger.Errorf(statement, domain) - return errorInternal - } - - statement = ` - DELETE FROM pages - WHERE pages.domain = $1; - ` - _, err = db.Exec(statement, domain) - if err != nil { - logger.Errorf(statement, domain) + // comments, votes, and pages are handled by domainClear + if err = domainClear(domain); err != nil { + logger.Errorf("cannot clear domain: %v", err) return errorInternal } diff --git a/api/router_api.go b/api/router_api.go index eaeab95..a6b4963 100644 --- a/api/router_api.go +++ b/api/router_api.go @@ -14,6 +14,7 @@ func apiRouterInit(router *mux.Router) error { router.HandleFunc("/api/domain/new", domainNewHandler).Methods("POST") router.HandleFunc("/api/domain/delete", domainDeleteHandler).Methods("POST") + router.HandleFunc("/api/domain/clear", domainClearHandler).Methods("POST") router.HandleFunc("/api/domain/list", domainListHandler).Methods("POST") router.HandleFunc("/api/domain/update", domainUpdateHandler).Methods("POST") router.HandleFunc("/api/domain/moderator/new", domainModeratorNewHandler).Methods("POST") diff --git a/frontend/dashboard.html b/frontend/dashboard.html index 80b6922..2277697 100644 --- a/frontend/dashboard.html +++ b/frontend/dashboard.html @@ -389,6 +389,20 @@ class="button green-button">Unfreeze +
+
+
+ Clear All Comments +
+
+ This will permanently delete all comments without affecting your settings. This may be useful if you want to clear all comments after testing Commento. Cannot be reversed. +
+
+
+ +
+
@@ -418,8 +432,8 @@ -
@@ -431,8 +445,21 @@ - +
+ + @@ -444,8 +471,8 @@ -
diff --git a/frontend/js/dashboard-danger.js b/frontend/js/dashboard-danger.js index 7a3df20..5ae9e6d 100644 --- a/frontend/js/dashboard-danger.js +++ b/frontend/js/dashboard-danger.js @@ -20,6 +20,18 @@ } + // Clears all comments in a domain. + global.domainClearHandler = function() { + var data = global.dashboard.$data; + + global.domainClear(data.domains[data.cd].domain, function(success) { + if (success) { + document.location = global.origin + "/dashboard"; + } + }); + } + + // Freezes a domain. global.domainFreezeHandler = function() { var data = global.dashboard.$data; diff --git a/frontend/js/dashboard-domain.js b/frontend/js/dashboard-domain.js index bf5bf52..d7a4404 100644 --- a/frontend/js/dashboard-domain.js +++ b/frontend/js/dashboard-domain.js @@ -149,4 +149,24 @@ }); } + + // Clears the comments in a domain. + global.domainClear = function(domain, callback) { + var json = { + "ownerToken": global.cookieGet("commentoOwnerToken"), + "domain": domain, + }; + + global.post(global.origin + "/api/domain/clear", json, function(resp) { + if (!resp.success) { + global.globalErrorShow(resp.message); + return; + } + + if (callback !== undefined) { + callback(resp.success); + } + }); + } + } (window.commento, document));