diff options
Diffstat (limited to 'internal/api/client/admin')
-rw-r--r-- | internal/api/client/admin/admin.go | 17 | ||||
-rw-r--r-- | internal/api/client/admin/domainblockcreate.go | 70 | ||||
-rw-r--r-- | internal/api/client/admin/domainblockdelete.go | 47 | ||||
-rw-r--r-- | internal/api/client/admin/domainblockget.go | 60 | ||||
-rw-r--r-- | internal/api/client/admin/domainblocksget.go | 54 |
5 files changed, 246 insertions, 2 deletions
diff --git a/internal/api/client/admin/admin.go b/internal/api/client/admin/admin.go index b33813a7d..b8b94be76 100644 --- a/internal/api/client/admin/admin.go +++ b/internal/api/client/admin/admin.go @@ -29,10 +29,19 @@ import ( ) const ( - // BasePath is the base API path for this module + // BasePath is the base API path for this module. BasePath = "/api/v1/admin" - // EmojiPath is used for posting/deleting custom emojis + // EmojiPath is used for posting/deleting custom emojis. 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 + + // ExportQueryKey is for requesting a public export of some data. + ExportQueryKey = "export" + // IDKey specifies the ID of a single item being interacted with. + IDKey = "id" ) // Module implements the ClientAPIModule interface for admin-related actions (reports, emojis, etc) @@ -54,5 +63,9 @@ func New(config *config.Config, processor processing.Processor, log *logrus.Logg // Route attaches all routes from this module to the given router 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) return nil } diff --git a/internal/api/client/admin/domainblockcreate.go b/internal/api/client/admin/domainblockcreate.go new file mode 100644 index 000000000..5d3df58de --- /dev/null +++ b/internal/api/client/admin/domainblockcreate.go @@ -0,0 +1,70 @@ +package admin + +import ( + "errors" + "fmt" + "net/http" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/oauth" +) + +// DomainBlocksPOSTHandler deals with the creation of a new domain block. +func (m *Module) DomainBlocksPOSTHandler(c *gin.Context) { + l := m.log.WithFields(logrus.Fields{ + "func": "DomainBlocksPOSTHandler", + "request_uri": c.Request.RequestURI, + "user_agent": c.Request.UserAgent(), + "origin_ip": c.ClientIP(), + }) + + // make sure we're authed with an admin account + authed, err := oauth.Authed(c, true, true, true, true) + if err != nil { + l.Debugf("couldn't auth: %s", err) + c.JSON(http.StatusForbidden, gin.H{"error": err.Error()}) + return + } + if !authed.User.Admin { + l.Debugf("user %s not an admin", authed.User.ID) + c.JSON(http.StatusForbidden, gin.H{"error": "not an admin"}) + return + } + + // extract the media create form from the request context + l.Tracef("parsing request form: %+v", c.Request.Form) + form := &model.DomainBlockCreateRequest{} + if err := c.ShouldBind(form); err != nil { + l.Debugf("error parsing form %+v: %s", c.Request.Form, err) + c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("could not parse form: %s", err)}) + return + } + + // 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 { + 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 + } + + 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") + } + + return nil +} diff --git a/internal/api/client/admin/domainblockdelete.go b/internal/api/client/admin/domainblockdelete.go new file mode 100644 index 000000000..d8f4586f9 --- /dev/null +++ b/internal/api/client/admin/domainblockdelete.go @@ -0,0 +1,47 @@ +package admin + +import ( + "net/http" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + "github.com/superseriousbusiness/gotosocial/internal/oauth" +) + +// DomainBlockDELETEHandler deals with the delete of an existing domain block. +func (m *Module) DomainBlockDELETEHandler(c *gin.Context) { + l := m.log.WithFields(logrus.Fields{ + "func": "DomainBlockDELETEHandler", + "request_uri": c.Request.RequestURI, + "user_agent": c.Request.UserAgent(), + "origin_ip": c.ClientIP(), + }) + + // make sure we're authed with an admin account + authed, err := oauth.Authed(c, true, true, true, true) + if err != nil { + l.Debugf("couldn't auth: %s", err) + c.JSON(http.StatusForbidden, gin.H{"error": err.Error()}) + return + } + if !authed.User.Admin { + l.Debugf("user %s not an admin", authed.User.ID) + c.JSON(http.StatusForbidden, gin.H{"error": "not an admin"}) + return + } + + domainBlockID := c.Param(IDKey) + if domainBlockID == "" { + c.JSON(http.StatusBadRequest, gin.H{"error": "no domain block id provided"}) + return + } + + domainBlock, errWithCode := m.processor.AdminDomainBlockDelete(authed, domainBlockID) + if errWithCode != nil { + l.Debugf("error deleting domain block: %s", errWithCode.Error()) + c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) + return + } + + c.JSON(http.StatusOK, domainBlock) +} diff --git a/internal/api/client/admin/domainblockget.go b/internal/api/client/admin/domainblockget.go new file mode 100644 index 000000000..009794f8a --- /dev/null +++ b/internal/api/client/admin/domainblockget.go @@ -0,0 +1,60 @@ +package admin + +import ( + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + "github.com/superseriousbusiness/gotosocial/internal/oauth" +) + +// DomainBlockGETHandler returns one existing domain block, identified by its id. +func (m *Module) DomainBlockGETHandler(c *gin.Context) { + l := m.log.WithFields(logrus.Fields{ + "func": "DomainBlockGETHandler", + "request_uri": c.Request.RequestURI, + "user_agent": c.Request.UserAgent(), + "origin_ip": c.ClientIP(), + }) + + // make sure we're authed with an admin account + authed, err := oauth.Authed(c, true, true, true, true) + if err != nil { + l.Debugf("couldn't auth: %s", err) + c.JSON(http.StatusForbidden, gin.H{"error": err.Error()}) + return + } + if !authed.User.Admin { + l.Debugf("user %s not an admin", authed.User.ID) + c.JSON(http.StatusForbidden, gin.H{"error": "not an admin"}) + return + } + + domainBlockID := c.Param(IDKey) + if domainBlockID == "" { + c.JSON(http.StatusBadRequest, gin.H{"error": "no domain block id provided"}) + return + } + + export := false + exportString := c.Query(ExportQueryKey) + if exportString != "" { + i, err := strconv.ParseBool(exportString) + if err != nil { + l.Debugf("error parsing export string: %s", err) + c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse export query param"}) + return + } + export = i + } + + domainBlock, err := m.processor.AdminDomainBlockGet(authed, domainBlockID, export) + if err != nil { + l.Debugf("error getting domain block: %s", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, domainBlock) +} diff --git a/internal/api/client/admin/domainblocksget.go b/internal/api/client/admin/domainblocksget.go new file mode 100644 index 000000000..1e873a302 --- /dev/null +++ b/internal/api/client/admin/domainblocksget.go @@ -0,0 +1,54 @@ +package admin + +import ( + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + "github.com/sirupsen/logrus" + "github.com/superseriousbusiness/gotosocial/internal/oauth" +) + +// DomainBlocksGETHandler returns a list of all existing domain blocks. +func (m *Module) DomainBlocksGETHandler(c *gin.Context) { + l := m.log.WithFields(logrus.Fields{ + "func": "DomainBlocksGETHandler", + "request_uri": c.Request.RequestURI, + "user_agent": c.Request.UserAgent(), + "origin_ip": c.ClientIP(), + }) + + // make sure we're authed with an admin account + authed, err := oauth.Authed(c, true, true, true, true) + if err != nil { + l.Debugf("couldn't auth: %s", err) + c.JSON(http.StatusForbidden, gin.H{"error": err.Error()}) + return + } + if !authed.User.Admin { + l.Debugf("user %s not an admin", authed.User.ID) + c.JSON(http.StatusForbidden, gin.H{"error": "not an admin"}) + return + } + + export := false + exportString := c.Query(ExportQueryKey) + if exportString != "" { + i, err := strconv.ParseBool(exportString) + if err != nil { + l.Debugf("error parsing export string: %s", err) + c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse export query param"}) + return + } + export = i + } + + domainBlocks, err := m.processor.AdminDomainBlocksGet(authed, export) + if err != nil { + l.Debugf("error getting domain blocks: %s", err) + c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) + return + } + + c.JSON(http.StatusOK, domainBlocks) +} |