mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
Blocklist import (#77)
* first steps on importing blocklists * unblock domains properly
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sirupsen/logrus"
|
||||
@ -33,6 +34,18 @@ func (m *Module) DomainBlocksPOSTHandler(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
imp := false
|
||||
importString := c.Query(ImportQueryKey)
|
||||
if importString != "" {
|
||||
i, err := strconv.ParseBool(importString)
|
||||
if err != nil {
|
||||
l.Debugf("error parsing import string: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse import query param"})
|
||||
return
|
||||
}
|
||||
imp = i
|
||||
}
|
||||
|
||||
// extract the media create form from the request context
|
||||
l.Tracef("parsing request form: %+v", c.Request.Form)
|
||||
form := &model.DomainBlockCreateRequest{}
|
||||
@ -44,26 +57,43 @@ func (m *Module) DomainBlocksPOSTHandler(c *gin.Context) {
|
||||
|
||||
// Give the fields on the request form a first pass to make sure the request is superficially valid.
|
||||
l.Tracef("validating form %+v", form)
|
||||
if err := validateCreateDomainBlock(form); err != nil {
|
||||
if err := validateCreateDomainBlock(form, imp); err != nil {
|
||||
l.Debugf("error validating form: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
domainBlock, err := m.processor.AdminDomainBlockCreate(authed, form)
|
||||
if err != nil {
|
||||
l.Debugf("error creating domain block: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
if imp {
|
||||
// we're importing multiple blocks
|
||||
domainBlocks, err := m.processor.AdminDomainBlocksImport(authed, form)
|
||||
if err != nil {
|
||||
l.Debugf("error importing domain blocks: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, domainBlocks)
|
||||
} else {
|
||||
// we're just creating one block
|
||||
domainBlock, err := m.processor.AdminDomainBlockCreate(authed, form)
|
||||
if err != nil {
|
||||
l.Debugf("error creating domain block: %s", err)
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, domainBlock)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, domainBlock)
|
||||
}
|
||||
|
||||
func validateCreateDomainBlock(form *model.DomainBlockCreateRequest) error {
|
||||
// add some more validation here later if necessary
|
||||
if form.Domain == "" {
|
||||
return errors.New("empty domain provided")
|
||||
func validateCreateDomainBlock(form *model.DomainBlockCreateRequest, imp bool) error {
|
||||
if imp {
|
||||
if form.Domains.Size == 0 {
|
||||
return errors.New("import was specified but list of domains is empty")
|
||||
}
|
||||
} else {
|
||||
// add some more validation here later if necessary
|
||||
if form.Domain == "" {
|
||||
return errors.New("empty domain provided")
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
|
Reference in New Issue
Block a user