diff --git a/PROGRESS.md b/PROGRESS.md index 87b0f52c9..41e655a3e 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -41,9 +41,9 @@ * [ ] Blocks * [ ] /api/v1/blocks GET (See list of blocked accounts) * [ ] Domain Blocks - * [ ] /api/v1/domain_blocks GET (See list of domain blocks) - * [ ] /api/v1/domain_blocks POST (Create a domain block) - * [ ] /api/v1/domain_blocks DELETE (Remove a domain block) + * [x] /api/v1/domain_blocks GET (See list of domain blocks) + * [x] /api/v1/domain_blocks POST (Create a domain block) + * [x] /api/v1/domain_blocks DELETE (Remove a domain block) * [ ] Filters * [ ] /api/v1/filters GET (Get list of filters) * [ ] /api/v1/filters/:id GET (View a filter) @@ -134,7 +134,7 @@ * [x] /api/v2/search GET (Get search query results) * [ ] Instance * [x] /api/v1/instance GET (Get instance information) - * [ ] /api/v1/instance PATCH (Update instance information) + * [x] /api/v1/instance PATCH (Update instance information) * [ ] /api/v1/instance/peers GET (Get list of federated servers) * [ ] /api/v1/instance/activity GET (Instance activity over the last 3 months, binned weekly.) * [ ] Trends @@ -198,10 +198,10 @@ * [ ] App creation guide * [ ] Tooling * [ ] Database migration tool - * [ ] Admin CLI tool + * [x] Admin CLI tool * [ ] Build - * [ ] Docker containerization - * [ ] Dockerfile + * [x] Docker containerization + * [x] Dockerfile * [ ] docker-compose.yml * [ ] Tests * [ ] Unit/integration diff --git a/README.md b/README.md index fe67a8da5..26c45140f 100644 --- a/README.md +++ b/README.md @@ -26,9 +26,15 @@ A grab-bag of things that are already included or will be included in the projec ## Implementation Status -Things are moving on the project! As of June 2021 you can now: +Things are moving on the project! As of July 2021 you can now: + +### Admin * Build and deploy GoToSocial as a binary, with automatic LetsEncrypt certificate support built-in. +* Create, confirm, and promote users using self-documented CLI tool. + +### User + * Connect to the running instance via Tusky or Pinafore, using email address and password (stored encrypted). * Post/delete posts. * Reply/delete replies. @@ -44,7 +50,12 @@ Things are moving on the project! As of June 2021 you can now: * View local timeline. * View and scroll home timeline (with ~10ms latency hell yeah). * Stream new posts, notifications and deletes through a websockets connection via Pinafore. + +### Federation + * Federation support and interoperability with Mastodon and others. +* Domain blocking: create, update, delete, and export domain blocks. +* Domain blocking: import lists of domain blocks -- no more blocking domains one-by-one. In other words, a deployed GoToSocial instance is already pretty useable! 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 diff --git a/internal/db/pg/instance.go b/internal/db/pg/instance.go index 2de0c5366..208268ac6 100644 --- a/internal/db/pg/instance.go +++ b/internal/db/pg/instance.go @@ -42,8 +42,8 @@ func (ps *postgresService) GetDomainCountForInstance(domain string) (int, error) if domain == ps.config.Host { // if the domain is *this* domain, just count other instances it knows about - // TODO: exclude domains that are blocked or silenced - q = q.Where("domain != ?", domain) + // exclude domains that are blocked + q = q.Where("domain != ?", domain).Where("? IS NULL", pg.Ident("suspended_at")) } else { // TODO: implement federated domain counting properly for remote domains return 0, nil diff --git a/internal/processing/admin.go b/internal/processing/admin.go index 73ac32c7a..f4741a1b8 100644 --- a/internal/processing/admin.go +++ b/internal/processing/admin.go @@ -29,7 +29,11 @@ func (p *processor) AdminEmojiCreate(authed *oauth.Auth, form *apimodel.EmojiCre } func (p *processor) AdminDomainBlockCreate(authed *oauth.Auth, form *apimodel.DomainBlockCreateRequest) (*apimodel.DomainBlock, gtserror.WithCode) { - return p.adminProcessor.DomainBlockCreate(authed.Account, form) + return p.adminProcessor.DomainBlockCreate(authed.Account, form.Domain, form.Obfuscate, form.PublicComment, form.PrivateComment, "") +} + +func (p *processor) AdminDomainBlocksImport(authed *oauth.Auth, form *apimodel.DomainBlockCreateRequest) ([]*apimodel.DomainBlock, gtserror.WithCode) { + return p.adminProcessor.DomainBlocksImport(authed.Account, form.Domains) } func (p *processor) AdminDomainBlocksGet(authed *oauth.Auth, export bool) ([]*apimodel.DomainBlock, gtserror.WithCode) { diff --git a/internal/processing/admin/admin.go b/internal/processing/admin/admin.go index 8cb1d7f78..fd63d8a10 100644 --- a/internal/processing/admin/admin.go +++ b/internal/processing/admin/admin.go @@ -19,6 +19,8 @@ package admin import ( + "mime/multipart" + "github.com/sirupsen/logrus" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/config" @@ -31,7 +33,8 @@ import ( // Processor wraps a bunch of functions for processing admin actions. type Processor interface { - DomainBlockCreate(account *gtsmodel.Account, form *apimodel.DomainBlockCreateRequest) (*apimodel.DomainBlock, gtserror.WithCode) + DomainBlockCreate(account *gtsmodel.Account, domain string, obfuscate bool, publicComment string, privateComment string, subscriptionID string) (*apimodel.DomainBlock, gtserror.WithCode) + DomainBlocksImport(account *gtsmodel.Account, domains *multipart.FileHeader) ([]*apimodel.DomainBlock, gtserror.WithCode) DomainBlocksGet(account *gtsmodel.Account, export bool) ([]*apimodel.DomainBlock, gtserror.WithCode) DomainBlockGet(account *gtsmodel.Account, id string, export bool) (*apimodel.DomainBlock, gtserror.WithCode) DomainBlockDelete(account *gtsmodel.Account, id string) (*apimodel.DomainBlock, gtserror.WithCode) diff --git a/internal/processing/admin/createdomainblock.go b/internal/processing/admin/createdomainblock.go index 82a7cac4f..926ddf355 100644 --- a/internal/processing/admin/createdomainblock.go +++ b/internal/processing/admin/createdomainblock.go @@ -30,37 +30,37 @@ import ( "github.com/superseriousbusiness/gotosocial/internal/id" ) -func (p *processor) DomainBlockCreate(account *gtsmodel.Account, form *apimodel.DomainBlockCreateRequest) (*apimodel.DomainBlock, gtserror.WithCode) { +func (p *processor) DomainBlockCreate(account *gtsmodel.Account, domain string, obfuscate bool, publicComment string, privateComment string, subscriptionID string) (*apimodel.DomainBlock, gtserror.WithCode) { // first check if we already have a block -- if err == nil we already had a block so we can skip a whole lot of work domainBlock := >smodel.DomainBlock{} - err := p.db.GetWhere([]db.Where{{Key: "domain", Value: form.Domain, CaseInsensitive: true}}, domainBlock) + err := p.db.GetWhere([]db.Where{{Key: "domain", Value: domain, CaseInsensitive: true}}, domainBlock) if err != nil { if _, ok := err.(db.ErrNoEntries); !ok { // something went wrong in the DB - return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: db error checking for existence of domain block %s: %s", form.Domain, err)) + return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: db error checking for existence of domain block %s: %s", domain, err)) } // there's no block for this domain yet so create one // note: we take a new ulid from timestamp here in case we need to sort blocks blockID, err := id.NewULID() if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: error creating id for new domain block %s: %s", form.Domain, err)) + return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: error creating id for new domain block %s: %s", domain, err)) } domainBlock = >smodel.DomainBlock{ ID: blockID, - Domain: form.Domain, + Domain: domain, CreatedByAccountID: account.ID, - PrivateComment: form.PrivateComment, - PublicComment: form.PublicComment, - Obfuscate: form.Obfuscate, + PrivateComment: privateComment, + PublicComment: publicComment, + Obfuscate: obfuscate, } // put the new block in the database if err := p.db.Put(domainBlock); err != nil { if _, ok := err.(db.ErrAlreadyExists); !ok { // there's a real error creating the block - return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: db error putting new domain block %s: %s", form.Domain, err)) + return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: db error putting new domain block %s: %s", domain, err)) } } @@ -70,7 +70,7 @@ func (p *processor) DomainBlockCreate(account *gtsmodel.Account, form *apimodel. mastoDomainBlock, err := p.tc.DomainBlockToMasto(domainBlock, false) if err != nil { - return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: error converting domain block to frontend/masto representation %s: %s", form.Domain, err)) + return nil, gtserror.NewErrorInternalError(fmt.Errorf("DomainBlockCreate: error converting domain block to frontend/masto representation %s: %s", domain, err)) } return mastoDomainBlock, nil diff --git a/internal/processing/admin/deletedomainblock.go b/internal/processing/admin/deletedomainblock.go index 973344dc2..3eaf3368f 100644 --- a/internal/processing/admin/deletedomainblock.go +++ b/internal/processing/admin/deletedomainblock.go @@ -1,7 +1,26 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package admin import ( "fmt" + "time" apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" "github.com/superseriousbusiness/gotosocial/internal/db" @@ -32,5 +51,18 @@ func (p *processor) DomainBlockDelete(account *gtsmodel.Account, id string) (*ap return nil, gtserror.NewErrorInternalError(err) } + // remove the domain block reference from the instance, if we have an entry for it + i := >smodel.Instance{} + if err := p.db.GetWhere([]db.Where{ + {Key: "domain", Value: domainBlock.Domain, CaseInsensitive: true}, + {Key: "domain_block_id", Value: id}, + }, i); err == nil { + i.SuspendedAt = time.Time{} + i.DomainBlockID = "" + if err := p.db.UpdateByID(i.ID, i); err != nil { + return nil, gtserror.NewErrorInternalError(fmt.Errorf("couldn't update database entry for instance %s: %s", domainBlock.Domain, err)) + } + } + return mastoDomainBlock, nil } diff --git a/internal/processing/admin/getdomainblock.go b/internal/processing/admin/getdomainblock.go index 170d82e0b..7d1f9e2ab 100644 --- a/internal/processing/admin/getdomainblock.go +++ b/internal/processing/admin/getdomainblock.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package admin import ( diff --git a/internal/processing/admin/getdomainblocks.go b/internal/processing/admin/getdomainblocks.go index 57e2ca7af..5e2241412 100644 --- a/internal/processing/admin/getdomainblocks.go +++ b/internal/processing/admin/getdomainblocks.go @@ -1,3 +1,21 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package admin import ( diff --git a/internal/processing/admin/importdomainblocks.go b/internal/processing/admin/importdomainblocks.go new file mode 100644 index 000000000..ab171b712 --- /dev/null +++ b/internal/processing/admin/importdomainblocks.go @@ -0,0 +1,67 @@ +/* + GoToSocial + Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +package admin + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "io" + "mime/multipart" + + apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" + "github.com/superseriousbusiness/gotosocial/internal/gtserror" + "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" +) + +// DomainBlocksImport handles the import of a bunch of domain blocks at once, by calling the DomainBlockCreate function for each domain in the provided file. +func (p *processor) DomainBlocksImport(account *gtsmodel.Account, domains *multipart.FileHeader) ([]*apimodel.DomainBlock, gtserror.WithCode) { + + f, err := domains.Open() + if err != nil { + return nil, gtserror.NewErrorBadRequest(fmt.Errorf("DomainBlocksImport: error opening attachment: %s", err)) + } + buf := new(bytes.Buffer) + size, err := io.Copy(buf, f) + if err != nil { + return nil, gtserror.NewErrorBadRequest(fmt.Errorf("DomainBlocksImport: error reading attachment: %s", err)) + } + if size == 0 { + return nil, gtserror.NewErrorBadRequest(errors.New("DomainBlocksImport: could not read provided attachment: size 0 bytes")) + } + + d := []apimodel.DomainBlock{} + if err := json.Unmarshal(buf.Bytes(), &d); err != nil { + return nil, gtserror.NewErrorBadRequest(fmt.Errorf("DomainBlocksImport: could not read provided attachment: %s", err)) + } + + blocks := []*apimodel.DomainBlock{} + for _, d := range d { + block, err := p.DomainBlockCreate(account, d.Domain, false, d.PublicComment, "", "") + + if err != nil { + return nil, err + } + + blocks = append(blocks, block) + } + + return blocks, nil +} diff --git a/internal/processing/processor.go b/internal/processing/processor.go index d24897f61..302368411 100644 --- a/internal/processing/processor.go +++ b/internal/processing/processor.go @@ -87,6 +87,8 @@ type Processor interface { AdminEmojiCreate(authed *oauth.Auth, form *apimodel.EmojiCreateRequest) (*apimodel.Emoji, error) // AdminDomainBlockCreate handles the creation of a new domain block by an admin, using the given form. AdminDomainBlockCreate(authed *oauth.Auth, form *apimodel.DomainBlockCreateRequest) (*apimodel.DomainBlock, gtserror.WithCode) + // AdminDomainBlocksImport handles the import of multiple domain blocks by an admin, using the given form. + AdminDomainBlocksImport(authed *oauth.Auth, form *apimodel.DomainBlockCreateRequest) ([]*apimodel.DomainBlock, gtserror.WithCode) // AdminDomainBlocksGet returns a list of currently blocked domains. AdminDomainBlocksGet(authed *oauth.Auth, export bool) ([]*apimodel.DomainBlock, gtserror.WithCode) // AdminDomainBlockGet returns one domain block, specified by ID.