2018-05-27 22:40:42 +08:00
|
|
|
package main
|
|
|
|
|
2018-06-03 23:27:08 +08:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
)
|
2018-05-27 22:40:42 +08:00
|
|
|
|
|
|
|
func domainDelete(domain string) error {
|
|
|
|
if domain == "" {
|
|
|
|
return errorMissingField
|
|
|
|
}
|
|
|
|
|
|
|
|
statement := `
|
2019-12-29 12:53:45 +08:00
|
|
|
DELETE FROM domains
|
2018-05-27 22:40:42 +08:00
|
|
|
WHERE domain = $1;
|
|
|
|
`
|
|
|
|
_, err := db.Exec(statement, domain)
|
|
|
|
if err != nil {
|
|
|
|
return errorNoSuchDomain
|
|
|
|
}
|
|
|
|
|
|
|
|
statement = `
|
|
|
|
DELETE FROM views
|
|
|
|
WHERE views.domain = $1;
|
|
|
|
`
|
|
|
|
_, err = db.Exec(statement, domain)
|
|
|
|
if err != nil {
|
2019-12-29 12:53:45 +08:00
|
|
|
logger.Errorf("cannot delete domain from views: %v", err)
|
2018-05-27 22:40:42 +08:00
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
2018-06-03 23:27:08 +08:00
|
|
|
statement = `
|
|
|
|
DELETE FROM moderators
|
|
|
|
WHERE moderators.domain = $1;
|
|
|
|
`
|
|
|
|
_, err = db.Exec(statement, domain)
|
|
|
|
if err != nil {
|
2019-12-29 12:53:45 +08:00
|
|
|
logger.Errorf("cannot delete domain from moderators: %v", err)
|
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
statement = `
|
|
|
|
DELETE FROM ssotokens
|
|
|
|
WHERE ssotokens.domain = $1;
|
|
|
|
`
|
|
|
|
_, err = db.Exec(statement, domain)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("cannot delete domain from ssotokens: %v", err)
|
2018-06-03 23:27:08 +08:00
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
2019-04-14 09:02:40 +08:00
|
|
|
// comments, votes, and pages are handled by domainClear
|
|
|
|
if err = domainClear(domain); err != nil {
|
|
|
|
logger.Errorf("cannot clear domain: %v", err)
|
2018-05-27 22:40:42 +08:00
|
|
|
return errorInternal
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-03 23:27:08 +08:00
|
|
|
|
|
|
|
func domainDeleteHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
type request struct {
|
2018-06-20 11:29:55 +08:00
|
|
|
OwnerToken *string `json:"ownerToken"`
|
2018-06-20 11:50:11 +08:00
|
|
|
Domain *string `json:"domain"`
|
2018-06-03 23:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var x request
|
2018-07-24 14:58:43 +08:00
|
|
|
if err := bodyUnmarshal(r, &x); err != nil {
|
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-06-03 23:27:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-06-20 11:29:55 +08:00
|
|
|
o, err := ownerGetByOwnerToken(*x.OwnerToken)
|
2018-06-03 23:27:08 +08:00
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-06-03 23:27:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:00:45 +08:00
|
|
|
domain := domainStrip(*x.Domain)
|
2018-06-03 23:27:08 +08:00
|
|
|
isOwner, err := domainOwnershipVerify(o.OwnerHex, domain)
|
|
|
|
if err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-06-03 23:27:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isOwner {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": errorNotAuthorised.Error()})
|
2018-06-03 23:27:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = domainDelete(*x.Domain); err != nil {
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": false, "message": err.Error()})
|
2018-06-03 23:27:08 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-24 14:58:43 +08:00
|
|
|
bodyMarshal(w, response{"success": true})
|
2018-06-03 23:27:08 +08:00
|
|
|
}
|