diff options
Diffstat (limited to 'internal/api')
-rw-r--r-- | internal/api/client/admin/admin.go | 10 | ||||
-rw-r--r-- | internal/api/client/admin/domainblockcreate.go | 54 | ||||
-rw-r--r-- | internal/api/model/domainblock.go | 10 |
3 files changed, 55 insertions, 19 deletions
diff --git a/internal/api/client/admin/admin.go b/internal/api/client/admin/admin.go index b8b94be76..eeec5196e 100644 --- a/internal/api/client/admin/admin.go +++ b/internal/api/client/admin/admin.go @@ -35,11 +35,13 @@ const ( EmojiPath = BasePath + "/custom_emojis" // DomainBlocksPath is used for posting domain blocks. DomainBlocksPath = BasePath + "/domain_blocks" - // DomainBlockPath is used for interacting with a single domain block. - DomainBlockPath = DomainBlocksPath + "/:" + IDKey + // DomainBlocksPathWithID is used for interacting with a single domain block. + DomainBlocksPathWithID = DomainBlocksPath + "/:" + IDKey // ExportQueryKey is for requesting a public export of some data. ExportQueryKey = "export" + // ImportQueryKey is for submitting an import of some data. + ImportQueryKey = "import" // IDKey specifies the ID of a single item being interacted with. IDKey = "id" ) @@ -65,7 +67,7 @@ func (m *Module) Route(r router.Router) error { r.AttachHandler(http.MethodPost, EmojiPath, m.emojiCreatePOSTHandler) r.AttachHandler(http.MethodPost, DomainBlocksPath, m.DomainBlocksPOSTHandler) r.AttachHandler(http.MethodGet, DomainBlocksPath, m.DomainBlocksGETHandler) - r.AttachHandler(http.MethodGet, DomainBlockPath, m.DomainBlockGETHandler) - r.AttachHandler(http.MethodDelete, DomainBlockPath, m.DomainBlockDELETEHandler) + r.AttachHandler(http.MethodGet, DomainBlocksPathWithID, m.DomainBlockGETHandler) + r.AttachHandler(http.MethodDelete, DomainBlocksPathWithID, m.DomainBlockDELETEHandler) return nil } diff --git a/internal/api/client/admin/domainblockcreate.go b/internal/api/client/admin/domainblockcreate.go index 5d3df58de..29436721c 100644 --- a/internal/api/client/admin/domainblockcreate.go +++ b/internal/api/client/admin/domainblockcreate.go @@ -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 diff --git a/internal/api/model/domainblock.go b/internal/api/model/domainblock.go index aaa28f34d..66d155ad7 100644 --- a/internal/api/model/domainblock.go +++ b/internal/api/model/domainblock.go @@ -18,13 +18,15 @@ package model +import "mime/multipart" + // DomainBlock represents a block on one domain type DomainBlock struct { ID string `json:"id,omitempty"` - Domain string `json:"domain"` + Domain string `form:"domain" json:"domain" validation:"required"` Obfuscate bool `json:"obfuscate,omitempty"` PrivateComment string `json:"private_comment,omitempty"` - PublicComment string `json:"public_comment,omitempty"` + PublicComment string `form:"public_comment" json:"public_comment,omitempty"` SubscriptionID string `json:"subscription_id,omitempty"` CreatedBy string `json:"created_by,omitempty"` CreatedAt string `json:"created_at,omitempty"` @@ -32,8 +34,10 @@ type DomainBlock struct { // DomainBlockCreateRequest is the form submitted as a POST to /api/v1/admin/domain_blocks to create a new block. type DomainBlockCreateRequest struct { + // A list of domains to block. Only used if import=true is specified. + Domains *multipart.FileHeader `form:"domains" json:"domains" xml:"domains"` // hostname/domain to block - Domain string `form:"domain" json:"domain" xml:"domain" validation:"required"` + Domain string `form:"domain" json:"domain" xml:"domain"` // whether the domain should be obfuscated when being displayed publicly Obfuscate bool `form:"obfuscate" json:"obfuscate" xml:"obfuscate"` // private comment for other admins on why the domain was blocked |