diff options
| author | 2021-07-05 13:23:03 +0200 | |
|---|---|---|
| committer | 2021-07-05 13:23:03 +0200 | |
| commit | d389e7b150df6ecd215c7b661b294ea153ad0103 (patch) | |
| tree | 8739e3103cb5130875d903cc7fc72fd9db3b8434 /internal/api/client | |
| parent | Fix 404 contact (#74) (diff) | |
| download | gotosocial-d389e7b150df6ecd215c7b661b294ea153ad0103.tar.xz | |
Domain block (#76)
* start work on admin domain blocking
* move stuff around + further work on domain blocks
* move + restructure processor
* prep work for deleting account
* tidy
* go fmt
* formatting
* domain blocking more work
* check domain blocks way earlier on
* progress on delete account
* delete more stuff when an account is gone
* and more...
* domain blocky block block
* get individual domain block, delete a block
Diffstat (limited to 'internal/api/client')
| -rw-r--r-- | internal/api/client/account/accountupdate_test.go | 2 | ||||
| -rw-r--r-- | internal/api/client/account/statuses.go | 6 | ||||
| -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 | ||||
| -rw-r--r-- | internal/api/client/fileserver/servefile_test.go | 2 | ||||
| -rw-r--r-- | internal/api/client/instance/instancepatch.go | 1 | ||||
| -rw-r--r-- | internal/api/client/media/mediacreate_test.go | 2 | ||||
| -rw-r--r-- | internal/api/client/status/statusboost_test.go | 2 | ||||
| -rw-r--r-- | internal/api/client/status/statuscreate_test.go | 2 | ||||
| -rw-r--r-- | internal/api/client/status/statusfave_test.go | 2 | ||||
| -rw-r--r-- | internal/api/client/status/statusfavedby_test.go | 2 | ||||
| -rw-r--r-- | internal/api/client/status/statusget_test.go | 2 | ||||
| -rw-r--r-- | internal/api/client/status/statusunfave_test.go | 2 | 
16 files changed, 259 insertions, 14 deletions
diff --git a/internal/api/client/account/accountupdate_test.go b/internal/api/client/account/accountupdate_test.go index ba7faa794..341b865ff 100644 --- a/internal/api/client/account/accountupdate_test.go +++ b/internal/api/client/account/accountupdate_test.go @@ -53,7 +53,7 @@ func (suite *AccountUpdateTestSuite) SetupTest() {  	suite.db = testrig.NewTestDB()  	suite.storage = testrig.NewTestStorage()  	suite.log = testrig.NewTestLog() -	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil))) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil)), suite.storage)  	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)  	suite.accountModule = account.New(suite.config, suite.processor, suite.log).(*account.Module)  	testrig.StandardDBSetup(suite.db) diff --git a/internal/api/client/account/statuses.go b/internal/api/client/account/statuses.go index f03a942f3..c92e85cee 100644 --- a/internal/api/client/account/statuses.go +++ b/internal/api/client/account/statuses.go @@ -82,7 +82,7 @@ func (m *Module) AccountStatusesGETHandler(c *gin.Context) {  		maxID = maxIDString  	} -	pinned := false +	pinnedOnly := false  	pinnedString := c.Query(PinnedKey)  	if pinnedString != "" {  		i, err := strconv.ParseBool(pinnedString) @@ -91,7 +91,7 @@ func (m *Module) AccountStatusesGETHandler(c *gin.Context) {  			c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse pinned query param"})  			return  		} -		pinned = i +		pinnedOnly = i  	}  	mediaOnly := false @@ -106,7 +106,7 @@ func (m *Module) AccountStatusesGETHandler(c *gin.Context) {  		mediaOnly = i  	} -	statuses, errWithCode := m.processor.AccountStatusesGet(authed, targetAcctID, limit, excludeReplies, maxID, pinned, mediaOnly) +	statuses, errWithCode := m.processor.AccountStatusesGet(authed, targetAcctID, limit, excludeReplies, maxID, pinnedOnly, mediaOnly)  	if errWithCode != nil {  		l.Debugf("error from processor account statuses get: %s", errWithCode)  		c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) 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) +} diff --git a/internal/api/client/fileserver/servefile_test.go b/internal/api/client/fileserver/servefile_test.go index 2646da24d..cb503facb 100644 --- a/internal/api/client/fileserver/servefile_test.go +++ b/internal/api/client/fileserver/servefile_test.go @@ -78,7 +78,7 @@ func (suite *ServeFileTestSuite) SetupSuite() {  	suite.db = testrig.NewTestDB()  	suite.log = testrig.NewTestLog()  	suite.storage = testrig.NewTestStorage() -	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil))) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil)), suite.storage)  	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)  	suite.tc = testrig.NewTestTypeConverter(suite.db)  	suite.mediaHandler = testrig.NewTestMediaHandler(suite.db, suite.storage) diff --git a/internal/api/client/instance/instancepatch.go b/internal/api/client/instance/instancepatch.go index ace7674c0..250e836be 100644 --- a/internal/api/client/instance/instancepatch.go +++ b/internal/api/client/instance/instancepatch.go @@ -8,6 +8,7 @@ import (  	"github.com/superseriousbusiness/gotosocial/internal/oauth"  ) +// InstanceUpdatePATCHHandler allows an admin to update the instance information served at /api/v1/instance  func (m *Module) InstanceUpdatePATCHHandler(c *gin.Context) {  	l := m.log.WithField("func", "InstanceUpdatePATCHHandler")  	authed, err := oauth.Authed(c, true, true, true, true) diff --git a/internal/api/client/media/mediacreate_test.go b/internal/api/client/media/mediacreate_test.go index bed588017..89a77a729 100644 --- a/internal/api/client/media/mediacreate_test.go +++ b/internal/api/client/media/mediacreate_test.go @@ -84,7 +84,7 @@ func (suite *MediaCreateTestSuite) SetupSuite() {  	suite.tc = testrig.NewTestTypeConverter(suite.db)  	suite.mediaHandler = testrig.NewTestMediaHandler(suite.db, suite.storage)  	suite.oauthServer = testrig.NewTestOauthServer(suite.db) -	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil))) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil)), suite.storage)  	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)  	// setup module being tested diff --git a/internal/api/client/status/statusboost_test.go b/internal/api/client/status/statusboost_test.go index 24ed8c361..9400aeddc 100644 --- a/internal/api/client/status/statusboost_test.go +++ b/internal/api/client/status/statusboost_test.go @@ -52,7 +52,7 @@ func (suite *StatusBoostTestSuite) SetupTest() {  	suite.db = testrig.NewTestDB()  	suite.storage = testrig.NewTestStorage()  	suite.log = testrig.NewTestLog() -	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil))) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil)), suite.storage)  	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)  	suite.statusModule = status.New(suite.config, suite.processor, suite.log).(*status.Module)  	testrig.StandardDBSetup(suite.db) diff --git a/internal/api/client/status/statuscreate_test.go b/internal/api/client/status/statuscreate_test.go index a78374fe8..b19323869 100644 --- a/internal/api/client/status/statuscreate_test.go +++ b/internal/api/client/status/statuscreate_test.go @@ -57,7 +57,7 @@ func (suite *StatusCreateTestSuite) SetupTest() {  	suite.db = testrig.NewTestDB()  	suite.storage = testrig.NewTestStorage()  	suite.log = testrig.NewTestLog() -	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil))) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil)), suite.storage)  	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)  	suite.statusModule = status.New(suite.config, suite.processor, suite.log).(*status.Module)  	testrig.StandardDBSetup(suite.db) diff --git a/internal/api/client/status/statusfave_test.go b/internal/api/client/status/statusfave_test.go index 2f779baed..b1cafc2fb 100644 --- a/internal/api/client/status/statusfave_test.go +++ b/internal/api/client/status/statusfave_test.go @@ -55,7 +55,7 @@ func (suite *StatusFaveTestSuite) SetupTest() {  	suite.db = testrig.NewTestDB()  	suite.storage = testrig.NewTestStorage()  	suite.log = testrig.NewTestLog() -	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil))) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil)), suite.storage)  	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)  	suite.statusModule = status.New(suite.config, suite.processor, suite.log).(*status.Module)  	testrig.StandardDBSetup(suite.db) diff --git a/internal/api/client/status/statusfavedby_test.go b/internal/api/client/status/statusfavedby_test.go index 7b72df7bc..b6e1591e0 100644 --- a/internal/api/client/status/statusfavedby_test.go +++ b/internal/api/client/status/statusfavedby_test.go @@ -55,7 +55,7 @@ func (suite *StatusFavedByTestSuite) SetupTest() {  	suite.db = testrig.NewTestDB()  	suite.storage = testrig.NewTestStorage()  	suite.log = testrig.NewTestLog() -	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil))) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil)), suite.storage)  	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)  	suite.statusModule = status.New(suite.config, suite.processor, suite.log).(*status.Module)  	testrig.StandardDBSetup(suite.db) diff --git a/internal/api/client/status/statusget_test.go b/internal/api/client/status/statusget_test.go index b31acebca..1bbf48a91 100644 --- a/internal/api/client/status/statusget_test.go +++ b/internal/api/client/status/statusget_test.go @@ -45,7 +45,7 @@ func (suite *StatusGetTestSuite) SetupTest() {  	suite.db = testrig.NewTestDB()  	suite.storage = testrig.NewTestStorage()  	suite.log = testrig.NewTestLog() -	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil))) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil)), suite.storage)  	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)  	suite.statusModule = status.New(suite.config, suite.processor, suite.log).(*status.Module)  	testrig.StandardDBSetup(suite.db) diff --git a/internal/api/client/status/statusunfave_test.go b/internal/api/client/status/statusunfave_test.go index 44b1dd3a6..36144c5ce 100644 --- a/internal/api/client/status/statusunfave_test.go +++ b/internal/api/client/status/statusunfave_test.go @@ -55,7 +55,7 @@ func (suite *StatusUnfaveTestSuite) SetupTest() {  	suite.db = testrig.NewTestDB()  	suite.storage = testrig.NewTestStorage()  	suite.log = testrig.NewTestLog() -	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil))) +	suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil)), suite.storage)  	suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)  	suite.statusModule = status.New(suite.config, suite.processor, suite.log).(*status.Module)  	testrig.StandardDBSetup(suite.db)  | 
