gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

blocks.go (2926B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package processing
     19 
     20 import (
     21 	"context"
     22 	"fmt"
     23 	"net/url"
     24 
     25 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     26 	"github.com/superseriousbusiness/gotosocial/internal/config"
     27 	"github.com/superseriousbusiness/gotosocial/internal/db"
     28 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     29 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     30 )
     31 
     32 func (p *Processor) BlocksGet(ctx context.Context, authed *oauth.Auth, maxID string, sinceID string, limit int) (*apimodel.BlocksResponse, gtserror.WithCode) {
     33 	accounts, nextMaxID, prevMinID, err := p.state.DB.GetAccountBlocks(ctx, authed.Account.ID, maxID, sinceID, limit)
     34 	if err != nil {
     35 		if err == db.ErrNoEntries {
     36 			// there are just no entries
     37 			return &apimodel.BlocksResponse{
     38 				Accounts: []*apimodel.Account{},
     39 			}, nil
     40 		}
     41 		// there's an actual error
     42 		return nil, gtserror.NewErrorInternalError(err)
     43 	}
     44 
     45 	apiAccounts := []*apimodel.Account{}
     46 	for _, a := range accounts {
     47 		apiAccount, err := p.tc.AccountToAPIAccountBlocked(ctx, a)
     48 		if err != nil {
     49 			continue
     50 		}
     51 		apiAccounts = append(apiAccounts, apiAccount)
     52 	}
     53 
     54 	return p.packageBlocksResponse(apiAccounts, "/api/v1/blocks", nextMaxID, prevMinID, limit)
     55 }
     56 
     57 func (p *Processor) packageBlocksResponse(accounts []*apimodel.Account, path string, nextMaxID string, prevMinID string, limit int) (*apimodel.BlocksResponse, gtserror.WithCode) {
     58 	resp := &apimodel.BlocksResponse{
     59 		Accounts: []*apimodel.Account{},
     60 	}
     61 	resp.Accounts = accounts
     62 
     63 	// prepare the next and previous links
     64 	if len(accounts) != 0 {
     65 		protocol := config.GetProtocol()
     66 		host := config.GetHost()
     67 
     68 		nextLink := &url.URL{
     69 			Scheme:   protocol,
     70 			Host:     host,
     71 			Path:     path,
     72 			RawQuery: fmt.Sprintf("limit=%d&max_id=%s", limit, nextMaxID),
     73 		}
     74 		next := fmt.Sprintf("<%s>; rel=\"next\"", nextLink.String())
     75 
     76 		prevLink := &url.URL{
     77 			Scheme:   protocol,
     78 			Host:     host,
     79 			Path:     path,
     80 			RawQuery: fmt.Sprintf("limit=%d&min_id=%s", limit, prevMinID),
     81 		}
     82 		prev := fmt.Sprintf("<%s>; rel=\"prev\"", prevLink.String())
     83 		resp.LinkHeader = fmt.Sprintf("%s, %s", next, prev)
     84 	}
     85 
     86 	return resp, nil
     87 }