summaryrefslogtreecommitdiff
path: root/internal/processing/account
diff options
context:
space:
mode:
authorLibravatar Tobi Smethurst <31960611+tsmethurst@users.noreply.github.com>2021-07-11 16:22:21 +0200
committerLibravatar GitHub <noreply@github.com>2021-07-11 16:22:21 +0200
commit846057f0d696fded87d105dec1245e9ba32763ce (patch)
tree9a4914c07bcf189a3eea0a2c091567c56cdf4963 /internal/processing/account
parentfavourites GET implementation (#95) (diff)
downloadgotosocial-846057f0d696fded87d105dec1245e9ba32763ce.tar.xz
Block/unblock (#96)
* remote + local block logic, incl. federation * improve blocking stuff * fiddle with display of blocked profiles * go fmt
Diffstat (limited to 'internal/processing/account')
-rw-r--r--internal/processing/account/account.go5
-rw-r--r--internal/processing/account/createblock.go155
-rw-r--r--internal/processing/account/get.go14
-rw-r--r--internal/processing/account/removeblock.go67
4 files changed, 239 insertions, 2 deletions
diff --git a/internal/processing/account/account.go b/internal/processing/account/account.go
index efdac5d3e..7b8910149 100644
--- a/internal/processing/account/account.go
+++ b/internal/processing/account/account.go
@@ -59,6 +59,11 @@ type Processor interface {
FollowCreate(requestingAccount *gtsmodel.Account, form *apimodel.AccountFollowRequest) (*apimodel.Relationship, gtserror.WithCode)
// FollowRemove handles the removal of a follow/follow request to an account, either remote or local.
FollowRemove(requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode)
+ // BlockCreate handles the creation of a block from requestingAccount to targetAccountID, either remote or local.
+ BlockCreate(requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode)
+ // BlockRemove handles the removal of a block from requestingAccount to targetAccountID, either remote or local.
+ BlockRemove(requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode)
+
// UpdateHeader does the dirty work of checking the header part of an account update form,
// parsing and checking the image, and doing the necessary updates in the database for this to become
// the account's new header image.
diff --git a/internal/processing/account/createblock.go b/internal/processing/account/createblock.go
new file mode 100644
index 000000000..79ce03805
--- /dev/null
+++ b/internal/processing/account/createblock.go
@@ -0,0 +1,155 @@
+/*
+ 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 <http://www.gnu.org/licenses/>.
+*/
+
+package account
+
+import (
+ "fmt"
+
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+ "github.com/superseriousbusiness/gotosocial/internal/id"
+ "github.com/superseriousbusiness/gotosocial/internal/util"
+)
+
+func (p *processor) BlockCreate(requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) {
+ // make sure the target account actually exists in our db
+ targetAcct := &gtsmodel.Account{}
+ if err := p.db.GetByID(targetAccountID, targetAcct); err != nil {
+ if _, ok := err.(db.ErrNoEntries); ok {
+ return nil, gtserror.NewErrorNotFound(fmt.Errorf("BlockCreate: account %s not found in the db: %s", targetAccountID, err))
+ }
+ }
+
+ // if requestingAccount already blocks target account, we don't need to do anything
+ block := &gtsmodel.Block{}
+ if err := p.db.GetWhere([]db.Where{
+ {Key: "account_id", Value: requestingAccount.ID},
+ {Key: "target_account_id", Value: targetAccountID},
+ }, block); err == nil {
+ // block already exists, just return relationship
+ return p.RelationshipGet(requestingAccount, targetAccountID)
+ }
+
+ // make the block
+ newBlockID, err := id.NewULID()
+ if err != nil {
+ return nil, gtserror.NewErrorInternalError(err)
+ }
+ block.ID = newBlockID
+ block.AccountID = requestingAccount.ID
+ block.Account = requestingAccount
+ block.TargetAccountID = targetAccountID
+ block.TargetAccount = targetAcct
+ block.URI = util.GenerateURIForBlock(requestingAccount.Username, p.config.Protocol, p.config.Host, newBlockID)
+
+ // whack it in the database
+ if err := p.db.Put(block); err != nil {
+ return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockCreate: error creating block in db: %s", err))
+ }
+
+ // clear any follows or follow requests from the blocked account to the target account -- this is a simple delete
+ if err := p.db.DeleteWhere([]db.Where{
+ {Key: "account_id", Value: targetAccountID},
+ {Key: "target_account_id", Value: requestingAccount.ID},
+ }, &gtsmodel.Follow{}); err != nil {
+ return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockCreate: error removing follow in db: %s", err))
+ }
+ if err := p.db.DeleteWhere([]db.Where{
+ {Key: "account_id", Value: targetAccountID},
+ {Key: "target_account_id", Value: requestingAccount.ID},
+ }, &gtsmodel.FollowRequest{}); err != nil {
+ return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockCreate: error removing follow in db: %s", err))
+ }
+
+ // clear any follows or follow requests from the requesting account to the target account --
+ // this might require federation so we need to pass some messages around
+
+ // check if a follow request exists from the requesting account to the target account, and remove it if it does (storing the URI for later)
+ var frChanged bool
+ var frURI string
+ fr := &gtsmodel.FollowRequest{}
+ if err := p.db.GetWhere([]db.Where{
+ {Key: "account_id", Value: requestingAccount.ID},
+ {Key: "target_account_id", Value: targetAccountID},
+ }, fr); err == nil {
+ frURI = fr.URI
+ if err := p.db.DeleteByID(fr.ID, fr); err != nil {
+ return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockCreate: error removing follow request from db: %s", err))
+ }
+ frChanged = true
+ }
+
+ // now do the same thing for any existing follow
+ var fChanged bool
+ var fURI string
+ f := &gtsmodel.Follow{}
+ if err := p.db.GetWhere([]db.Where{
+ {Key: "account_id", Value: requestingAccount.ID},
+ {Key: "target_account_id", Value: targetAccountID},
+ }, f); err == nil {
+ fURI = f.URI
+ if err := p.db.DeleteByID(f.ID, f); err != nil {
+ return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockCreate: error removing follow from db: %s", err))
+ }
+ fChanged = true
+ }
+
+ // follow request status changed so send the UNDO activity to the channel for async processing
+ if frChanged {
+ p.fromClientAPI <- gtsmodel.FromClientAPI{
+ APObjectType: gtsmodel.ActivityStreamsFollow,
+ APActivityType: gtsmodel.ActivityStreamsUndo,
+ GTSModel: &gtsmodel.Follow{
+ AccountID: requestingAccount.ID,
+ TargetAccountID: targetAccountID,
+ URI: frURI,
+ },
+ OriginAccount: requestingAccount,
+ TargetAccount: targetAcct,
+ }
+ }
+
+ // follow status changed so send the UNDO activity to the channel for async processing
+ if fChanged {
+ p.fromClientAPI <- gtsmodel.FromClientAPI{
+ APObjectType: gtsmodel.ActivityStreamsFollow,
+ APActivityType: gtsmodel.ActivityStreamsUndo,
+ GTSModel: &gtsmodel.Follow{
+ AccountID: requestingAccount.ID,
+ TargetAccountID: targetAccountID,
+ URI: fURI,
+ },
+ OriginAccount: requestingAccount,
+ TargetAccount: targetAcct,
+ }
+ }
+
+ // handle the rest of the block process asynchronously
+ p.fromClientAPI <- gtsmodel.FromClientAPI{
+ APObjectType: gtsmodel.ActivityStreamsBlock,
+ APActivityType: gtsmodel.ActivityStreamsCreate,
+ GTSModel: block,
+ OriginAccount: requestingAccount,
+ TargetAccount: targetAcct,
+ }
+
+ return p.RelationshipGet(requestingAccount, targetAccountID)
+}
diff --git a/internal/processing/account/get.go b/internal/processing/account/get.go
index aba1ed14a..b937ace5b 100644
--- a/internal/processing/account/get.go
+++ b/internal/processing/account/get.go
@@ -45,9 +45,19 @@ func (p *processor) Get(requestingAccount *gtsmodel.Account, targetAccountID str
p.log.WithField("func", "AccountGet").Debugf("dereferencing account: %s", err)
}
- var mastoAccount *apimodel.Account
+ var blocked bool
var err error
- if requestingAccount != nil && targetAccount.ID == requestingAccount.ID {
+ if requestingAccount != nil {
+ blocked, err = p.db.Blocked(requestingAccount.ID, targetAccountID)
+ if err != nil {
+ return nil, fmt.Errorf("error checking account block: %s", err)
+ }
+ }
+
+ var mastoAccount *apimodel.Account
+ if blocked {
+ mastoAccount, err = p.tc.AccountToMastoBlocked(targetAccount)
+ } else if requestingAccount != nil && targetAccount.ID == requestingAccount.ID {
mastoAccount, err = p.tc.AccountToMastoSensitive(targetAccount)
} else {
mastoAccount, err = p.tc.AccountToMastoPublic(targetAccount)
diff --git a/internal/processing/account/removeblock.go b/internal/processing/account/removeblock.go
new file mode 100644
index 000000000..03b0c6750
--- /dev/null
+++ b/internal/processing/account/removeblock.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 <http://www.gnu.org/licenses/>.
+*/
+
+package account
+
+import (
+ "fmt"
+
+ apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
+ "github.com/superseriousbusiness/gotosocial/internal/db"
+ "github.com/superseriousbusiness/gotosocial/internal/gtserror"
+ "github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
+)
+
+func (p *processor) BlockRemove(requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) {
+ // make sure the target account actually exists in our db
+ targetAcct := &gtsmodel.Account{}
+ if err := p.db.GetByID(targetAccountID, targetAcct); err != nil {
+ if _, ok := err.(db.ErrNoEntries); ok {
+ return nil, gtserror.NewErrorNotFound(fmt.Errorf("BlockRemove: account %s not found in the db: %s", targetAccountID, err))
+ }
+ }
+
+ // check if a block exists, and remove it if it does (storing the URI for later)
+ var blockChanged bool
+ block := &gtsmodel.Block{}
+ if err := p.db.GetWhere([]db.Where{
+ {Key: "account_id", Value: requestingAccount.ID},
+ {Key: "target_account_id", Value: targetAccountID},
+ }, block); err == nil {
+ block.Account = requestingAccount
+ block.TargetAccount = targetAcct
+ if err := p.db.DeleteByID(block.ID, &gtsmodel.Block{}); err != nil {
+ return nil, gtserror.NewErrorInternalError(fmt.Errorf("BlockRemove: error removing block from db: %s", err))
+ }
+ blockChanged = true
+ }
+
+ // block status changed so send the UNDO activity to the channel for async processing
+ if blockChanged {
+ p.fromClientAPI <- gtsmodel.FromClientAPI{
+ APObjectType: gtsmodel.ActivityStreamsBlock,
+ APActivityType: gtsmodel.ActivityStreamsUndo,
+ GTSModel: block,
+ OriginAccount: requestingAccount,
+ TargetAccount: targetAcct,
+ }
+ }
+
+ // return whatever relationship results from all this
+ return p.RelationshipGet(requestingAccount, targetAccountID)
+}