| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // 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/>. | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | package account | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-26 15:34:10 +02:00
										 |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/ap" | 
					
						
							|  |  |  | 	apimodel "code.superseriousbusiness.org/gotosocial/internal/api/model" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtserror" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/gtsmodel" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/id" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/log" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/messages" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/paging" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/uris" | 
					
						
							|  |  |  | 	"code.superseriousbusiness.org/gotosocial/internal/util" | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | // 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) { | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	targetAccount, existingBlock, errWithCode := p.getBlockTarget(ctx, requestingAccount, targetAccountID) | 
					
						
							|  |  |  | 	if errWithCode != nil { | 
					
						
							|  |  |  | 		return nil, errWithCode | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	if existingBlock != nil { | 
					
						
							|  |  |  | 		// Block already exists, nothing to do. | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 		return p.RelationshipGet(ctx, requestingAccount, targetAccountID) | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	// 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, | 
					
						
							| 
									
										
										
										
											2022-06-25 11:14:05 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	if err := p.state.DB.PutBlock(ctx, block); err != nil { | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 		err = fmt.Errorf("BlockCreate: error creating block in db: %w", err) | 
					
						
							|  |  |  | 		return nil, gtserror.NewErrorInternalError(err) | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	// 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) | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	// 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) | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	// Process block side effects (federation etc). | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 	msgs = append(msgs, &messages.FromClientAPI{ | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 		APObjectType:   ap.ActivityBlock, | 
					
						
							|  |  |  | 		APActivityType: ap.ActivityCreate, | 
					
						
							|  |  |  | 		GTSModel:       block, | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 		Origin:         requestingAccount, | 
					
						
							|  |  |  | 		Target:         targetAccount, | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Batch queue accreted client api messages. | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 	p.state.Workers.Client.Queue.Push(msgs...) | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	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 | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	if existingBlock == nil { | 
					
						
							|  |  |  | 		// Already not blocked, nothing to do. | 
					
						
							|  |  |  | 		return p.RelationshipGet(ctx, requestingAccount, targetAccountID) | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	// 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) | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	// Populate account fields for convenience. | 
					
						
							|  |  |  | 	existingBlock.Account = requestingAccount | 
					
						
							|  |  |  | 	existingBlock.TargetAccount = targetAccount | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// Process block removal side effects (federation etc). | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 	p.state.Workers.Client.Queue.Push(&messages.FromClientAPI{ | 
					
						
							| 
									
										
										
										
											2021-08-31 15:59:12 +02:00
										 |  |  | 		APObjectType:   ap.ActivityBlock, | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 		APActivityType: ap.ActivityUndo, | 
					
						
							|  |  |  | 		GTSModel:       existingBlock, | 
					
						
							| 
									
										
										
										
											2024-04-26 13:50:46 +01:00
										 |  |  | 		Origin:         requestingAccount, | 
					
						
							|  |  |  | 		Target:         targetAccount, | 
					
						
							| 
									
										
										
										
											2022-04-28 13:23:11 +01:00
										 |  |  | 	}) | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-25 15:34:33 +02:00
										 |  |  | 	return p.RelationshipGet(ctx, requestingAccount, targetAccountID) | 
					
						
							| 
									
										
										
										
											2021-07-11 16:22:21 +02:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | // 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 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-01 12:45:02 +00:00
										 |  |  | 	// Get the lowest and highest | 
					
						
							|  |  |  | 	// ID values, used for paging. | 
					
						
							|  |  |  | 	lo := blocks[count-1].ID | 
					
						
							|  |  |  | 	hi := blocks[0].ID | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 	items := make([]interface{}, 0, count) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, block := range blocks { | 
					
						
							|  |  |  | 		// Convert target account to frontend API model. (target will never be nil) | 
					
						
							| 
									
										
										
										
											2023-09-23 17:44:11 +01:00
										 |  |  | 		account, err := p.converter.AccountToAPIAccountBlocked(ctx, block.TargetAccount) | 
					
						
							| 
									
										
										
										
											2023-09-12 14:00:35 +01:00
										 |  |  | 		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) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return paging.PackageResponse(paging.ResponseParams{ | 
					
						
							|  |  |  | 		Items: items, | 
					
						
							|  |  |  | 		Path:  "/api/v1/blocks", | 
					
						
							|  |  |  | 		Next:  page.Next(lo, hi), | 
					
						
							|  |  |  | 		Prev:  page.Prev(lo, hi), | 
					
						
							|  |  |  | 	}), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 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. | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	targetAccount, err := p.state.DB.GetAccountByID(ctx, targetAccountID) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	if err != nil { | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 		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()) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	// Check if currently blocked. | 
					
						
							| 
									
										
										
										
											2023-03-01 18:26:53 +00:00
										 |  |  | 	block, err := p.state.DB.GetBlock(ctx, requestingAccount.ID, targetAccountID) | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	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) | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-20 19:10:08 +01:00
										 |  |  | 	return targetAccount, block, nil | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | } |