gtsocial-umbx

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

util.go (2848B)


      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 list
     19 
     20 import (
     21 	"context"
     22 	"errors"
     23 	"fmt"
     24 
     25 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     26 	"github.com/superseriousbusiness/gotosocial/internal/db"
     27 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     28 	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
     29 )
     30 
     31 // getList is a shortcut to get one list from the database and
     32 // check that it's owned by the given accountID. Will return
     33 // appropriate errors so caller doesn't need to bother.
     34 func (p *Processor) getList(ctx context.Context, accountID string, listID string) (*gtsmodel.List, gtserror.WithCode) {
     35 	list, err := p.state.DB.GetListByID(ctx, listID)
     36 	if err != nil {
     37 		if errors.Is(err, db.ErrNoEntries) {
     38 			// List doesn't seem to exist.
     39 			return nil, gtserror.NewErrorNotFound(err)
     40 		}
     41 		// Real database error.
     42 		return nil, gtserror.NewErrorInternalError(err)
     43 	}
     44 
     45 	if list.AccountID != accountID {
     46 		err = fmt.Errorf("list with id %s does not belong to account %s", list.ID, accountID)
     47 		return nil, gtserror.NewErrorNotFound(err)
     48 	}
     49 
     50 	return list, nil
     51 }
     52 
     53 // apiList is a shortcut to return the API version of the given
     54 // list, or return an appropriate error if conversion fails.
     55 func (p *Processor) apiList(ctx context.Context, list *gtsmodel.List) (*apimodel.List, gtserror.WithCode) {
     56 	apiList, err := p.tc.ListToAPIList(ctx, list)
     57 	if err != nil {
     58 		return nil, gtserror.NewErrorInternalError(fmt.Errorf("error converting list to api: %w", err))
     59 	}
     60 
     61 	return apiList, nil
     62 }
     63 
     64 // isInList check if thisID is equal to the result of thatID
     65 // for any entry in the given list.
     66 //
     67 // Will return the id of the listEntry if true, empty if false,
     68 // or an error if the result of thatID returns an error.
     69 func isInList(
     70 	list *gtsmodel.List,
     71 	thisID string,
     72 	getThatID func(listEntry *gtsmodel.ListEntry) (string, error),
     73 ) (string, error) {
     74 	for _, listEntry := range list.ListEntries {
     75 		thatID, err := getThatID(listEntry)
     76 		if err != nil {
     77 			return "", err
     78 		}
     79 
     80 		if thisID == thatID {
     81 			return listEntry.ID, nil
     82 		}
     83 	}
     84 	return "", nil
     85 }