1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// 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 (
"context"
"errors"
"fmt"
"github.com/superseriousbusiness/gotosocial/internal/ap"
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/log"
"github.com/superseriousbusiness/gotosocial/internal/messages"
"github.com/superseriousbusiness/gotosocial/internal/paging"
"github.com/superseriousbusiness/gotosocial/internal/uris"
"github.com/superseriousbusiness/gotosocial/internal/util"
)
// BlockCreate handles the creation of a block from requestingAccount to targetAccountID, either remote or local.
func (p *Processor) BlockCreate(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) {
targetAccount, existingBlock, errWithCode := p.getBlockTarget(ctx, requestingAccount, targetAccountID)
if errWithCode != nil {
return nil, errWithCode
}
if existingBlock != nil {
// Block already exists, nothing to do.
return p.RelationshipGet(ctx, requestingAccount, targetAccountID)
}
// Create and store a new block.
blockID := id.NewULID()
blockURI := uris.GenerateURIForBlock(requestingAccount.Username, blockID)
block := >smodel.Block{
ID: blockID,
URI: blockURI,
AccountID: requestingAccount.ID,
Account: requestingAccount,
TargetAccountID: targetAccountID,
TargetAccount: targetAccount,
}
if err := p.state.DB.PutBlock(ctx, block); err != nil {
err = fmt.Errorf("BlockCreate: error creating block in db: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Ensure each account unfollows the other.
// We only care about processing unfollow side
// effects from requesting account -> target
// account, since requesting account is ours,
// and target account might not be.
msgs, err := p.unfollow(ctx, requestingAccount, targetAccount)
if err != nil {
err = fmt.Errorf("BlockCreate: error unfollowing: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Ensure unfollowed in other direction;
// ignore/don't process returned messages.
if _, err := p.unfollow(ctx, targetAccount, requestingAccount); err != nil {
err = fmt.Errorf("BlockCreate: error unfollowing: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Process block side effects (federation etc).
msgs = append(msgs, messages.FromClientAPI{
APObjectType: ap.ActivityBlock,
APActivityType: ap.ActivityCreate,
GTSModel: block,
OriginAccount: requestingAccount,
TargetAccount: targetAccount,
})
// Batch queue accreted client api messages.
p.state.Workers.EnqueueClientAPI(ctx, msgs...)
return p.RelationshipGet(ctx, requestingAccount, targetAccountID)
}
// BlockRemove handles the removal of a block from requestingAccount to targetAccountID, either remote or local.
func (p *Processor) BlockRemove(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*apimodel.Relationship, gtserror.WithCode) {
targetAccount, existingBlock, errWithCode := p.getBlockTarget(ctx, requestingAccount, targetAccountID)
if errWithCode != nil {
return nil, errWithCode
}
if existingBlock == nil {
// Already not blocked, nothing to do.
return p.RelationshipGet(ctx, requestingAccount, targetAccountID)
}
// We got a block, remove it from the db.
if err := p.state.DB.DeleteBlockByID(ctx, existingBlock.ID); err != nil {
err := fmt.Errorf("BlockRemove: error removing block from db: %w", err)
return nil, gtserror.NewErrorInternalError(err)
}
// Populate account fields for convenience.
existingBlock.Account = requestingAccount
existingBlock.TargetAccount = targetAccount
// Process block removal side effects (federation etc).
p.state.Workers.EnqueueClientAPI(ctx, messages.FromClientAPI{
APObjectType: ap.ActivityBlock,
APActivityType: ap.ActivityUndo,
GTSModel: existingBlock,
OriginAccount: requestingAccount,
TargetAccount: targetAccount,
})
return p.RelationshipGet(ctx, requestingAccount, targetAccountID)
}
// BlocksGet ...
func (p *Processor) BlocksGet(
ctx context.Context,
requestingAccount *gtsmodel.Account,
page *paging.Page,
) (*apimodel.PageableResponse, gtserror.WithCode) {
blocks, err := p.state.DB.GetAccountBlocks(ctx,
requestingAccount.ID,
page,
)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
return nil, gtserror.NewErrorInternalError(err)
}
// Check for empty response.
count := len(blocks)
if len(blocks) == 0 {
return util.EmptyPageableResponse(), nil
}
items := make([]interface{}, 0, count)
for _, block := range blocks {
// Convert target account to frontend API model. (target will never be nil)
account, err := p.converter.AccountToAPIAccountBlocked(ctx, block.TargetAccount)
if err != nil {
log.Errorf(ctx, "error converting account to public api account: %v", err)
continue
}
// Append target to return items.
items = append(items, account)
}
// Get the lowest and highest
// ID values, used for paging.
lo := blocks[count-1].ID
hi := blocks[0].ID
return paging.PackageResponse(paging.ResponseParams{
Items: items,
Path: "/api/v1/blocks",
Next: page.Next(lo, hi),
Prev: page.Prev(lo, hi),
}), nil
}
func (p *Processor) getBlockTarget(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) (*gtsmodel.Account, *gtsmodel.Block, gtserror.WithCode) {
// Account should not block or unblock itself.
if requestingAccount.ID == targetAccountID {
err := fmt.Errorf("getBlockTarget: account %s cannot block or unblock itself", requestingAccount.ID)
return nil, nil, gtserror.NewErrorNotAcceptable(err, err.Error())
}
// Ensure target account retrievable.
targetAccount, err := p.state.DB.GetAccountByID(ctx, targetAccountID)
if err != nil {
if !errors.Is(err, db.ErrNoEntries) {
// Real db error.
err = fmt.Errorf("getBlockTarget: db error looking for target account %s: %w", targetAccountID, err)
return nil, nil, gtserror.NewErrorInternalError(err)
}
// Account not found.
err = fmt.Errorf("getBlockTarget: target account %s not found in the db", targetAccountID)
return nil, nil, gtserror.NewErrorNotFound(err, err.Error())
}
// Check if currently blocked.
block, err := p.state.DB.GetBlock(ctx, requestingAccount.ID, targetAccountID)
if err != nil && !errors.Is(err, db.ErrNoEntries) {
err = fmt.Errorf("getBlockTarget: db error checking existing block: %w", err)
return nil, nil, gtserror.NewErrorInternalError(err)
}
return targetAccount, block, nil
}
|