api: log comment deleter and deletion date

Signed-off-by: Adhityaa Chandrasekar <adtac@adtac.in>
This commit is contained in:
Adhityaa Chandrasekar 2021-02-28 12:39:31 +05:30
parent 84bfd64e32
commit aaa44a0bee
5 changed files with 60 additions and 27 deletions

View File

@ -2,19 +2,26 @@ package main
import ( import (
"net/http" "net/http"
"time"
) )
func commentDelete(commentHex string) error { func commentDelete(commentHex string, deleterHex string) error {
if commentHex == "" { if commentHex == "" || deleterHex == "" {
return errorMissingField return errorMissingField
} }
statement := ` statement := `
UPDATE comments UPDATE comments
SET deleted = true, markdown = '[deleted]', html = '[deleted]', commenterHex = 'anonymous' SET
deleted = true,
markdown = '[deleted]',
html = '[deleted]',
commenterHex = 'anonymous',
deleterHex = $2,
deletionDate = $3
WHERE commentHex = $1; WHERE commentHex = $1;
` `
_, err := db.Exec(statement, commentHex) _, err := db.Exec(statement, commentHex, deleterHex, time.Now().UTC())
if err != nil { if err != nil {
// TODO: make sure this is the error is actually non-existant commentHex // TODO: make sure this is the error is actually non-existant commentHex
@ -65,7 +72,7 @@ func commentDeleteHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if err = commentDelete(*x.CommentHex); err != nil { if err = commentDelete(*x.CommentHex, c.CommenterHex); err != nil {
bodyMarshal(w, response{"success": false, "message": err.Error()}) bodyMarshal(w, response{"success": false, "message": err.Error()})
return return
} }

View File

@ -8,15 +8,16 @@ import (
func TestCommentDeleteBasics(t *testing.T) { func TestCommentDeleteBasics(t *testing.T) {
failTestOnError(t, setupTestEnv()) failTestOnError(t, setupTestEnv())
commentHex, _ := commentNew("temp-commenter-hex", "example.com", "/path.html", "root", "**foo**", "approved", time.Now().UTC()) commenterHex = "temp-commenter-hex"
commentNew("temp-commenter-hex", "example.com", "/path.html", commentHex, "**bar**", "approved", time.Now().UTC()) commentHex, _ := commentNew(commenterHex, "example.com", "/path.html", "root", "**foo**", "approved", time.Now().UTC())
commentNew(commenterHex, "example.com", "/path.html", commentHex, "**bar**", "approved", time.Now().UTC())
if err := commentDelete(commentHex); err != nil { if err := commentDelete(commentHex, commenterHex); err != nil {
t.Errorf("unexpected error deleting comment: %v", err) t.Errorf("unexpected error deleting comment: %v", err)
return return
} }
c, _, _ := commentList("temp-commenter-hex", "example.com", "/path.html", false) c, _, _ := commentList(commenterHex, "example.com", "/path.html", false)
if len(c) != 0 { if len(c) != 0 {
t.Errorf("expected no comments found %d comments", len(c)) t.Errorf("expected no comments found %d comments", len(c))
@ -27,7 +28,7 @@ func TestCommentDeleteBasics(t *testing.T) {
func TestCommentDeleteEmpty(t *testing.T) { func TestCommentDeleteEmpty(t *testing.T) {
failTestOnError(t, setupTestEnv()) failTestOnError(t, setupTestEnv())
if err := commentDelete(""); err == nil { if err := commentDelete("", "test-commenter-hex"); err == nil {
t.Errorf("expected error deleting comment with empty commentHex") t.Errorf("expected error deleting comment with empty commentHex")
return return
} }

View File

@ -7,18 +7,7 @@ import (
func emailModerateHandler(w http.ResponseWriter, r *http.Request) { func emailModerateHandler(w http.ResponseWriter, r *http.Request) {
unsubscribeSecretHex := r.FormValue("unsubscribeSecretHex") unsubscribeSecretHex := r.FormValue("unsubscribeSecretHex")
e, err := emailGetByUnsubscribeSecretHex(unsubscribeSecretHex)
if err != nil {
fmt.Fprintf(w, "error: %v", err.Error())
return
}
action := r.FormValue("action") action := r.FormValue("action")
if action != "delete" && action != "approve" {
fmt.Fprintf(w, "error: invalid action")
return
}
commentHex := r.FormValue("commentHex") commentHex := r.FormValue("commentHex")
if commentHex == "" { if commentHex == "" {
fmt.Fprintf(w, "error: invalid commentHex") fmt.Fprintf(w, "error: invalid commentHex")
@ -26,23 +15,35 @@ func emailModerateHandler(w http.ResponseWriter, r *http.Request) {
} }
statement := ` statement := `
SELECT domain SELECT domain, deleted
FROM comments FROM comments
WHERE commentHex = $1; WHERE commentHex = $1;
` `
row := db.QueryRow(statement, commentHex) row := db.QueryRow(statement, commentHex)
var domain string var domain string
if err = row.Scan(&domain); err != nil { var deleted bool
if err := row.Scan(&domain, &deleted); err != nil {
// TODO: is this the only error? // TODO: is this the only error?
fmt.Fprintf(w, "error: no such comment found (perhaps it has been deleted?)") fmt.Fprintf(w, "error: no such comment found (perhaps it has been deleted?)")
return return
} }
if deleted {
fmt.Fprintf(w, "error: that comment has already been deleted")
return
}
e, err := emailGetByUnsubscribeSecretHex(unsubscribeSecretHex)
if err != nil {
fmt.Fprintf(w, "error: %v", err.Error())
return
}
isModerator, err := isDomainModerator(domain, e.Email) isModerator, err := isDomainModerator(domain, e.Email)
if err != nil { if err != nil {
logger.Errorf("error checking if %s is a moderator: %v", e.Email, err) logger.Errorf("error checking if %s is a moderator: %v", e.Email, err)
fmt.Fprintf(w, "error checking if %s is a moderator: %v", e.Email, err) fmt.Fprintf(w, "error: %v", errorInternal)
return return
} }
@ -51,10 +52,31 @@ func emailModerateHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
if action == "approve" { // Do not use commenterGetByEmail here because we don't know which provider
// should be used. This was poor design on multiple fronts on my part, but
// let's deal with that later. For now, it suffices to match the
// deleter/approver with any account owned by the same email.
statement = `
SELECT commenterHex
FROM commenters
WHERE email = $1;
`
row = db.QueryRow(statement, e.Email)
var commenterHex string
if err = row.Scan(&commenterHex); err != nil {
logger.Errorf("cannot retrieve commenterHex by email %q: %v", e.Email, err)
fmt.Fprintf(w, "error: %v", errorInternal)
return
}
switch action {
case "approve":
err = commentApprove(commentHex) err = commentApprove(commentHex)
} else { case "delete":
err = commentDelete(commentHex) err = commentDelete(commentHex, commenterHex)
default:
err = errorInvalidAction
} }
if err != nil { if err != nil {

View File

@ -52,3 +52,4 @@ var errorCannotDeleteOwnerWithActiveDomains = errors.New("You cannot delete your
var errorNoSuchOwner = errors.New("No such owner.") var errorNoSuchOwner = errors.New("No such owner.")
var errorCannotUpdateOauthProfile = errors.New("You cannot update the profile of an external account managed by third-party log in. Please use the appropriate platform to update your details.") var errorCannotUpdateOauthProfile = errors.New("You cannot update the profile of an external account managed by third-party log in. Please use the appropriate platform to update your details.")
var errorUnsupportedCommentoImportVersion = errors.New("Unsupported Commento import format version.") var errorUnsupportedCommentoImportVersion = errors.New("Unsupported Commento import format version.")
var errorInvalidAction = errors.New("Invalid action.")

View File

@ -0,0 +1,2 @@
ALTER TABLE comments ADD deleterHex TEXT;
ALTER TABLE comments ADD deletionDate TIMESTAMP;