lists.go (3431B)
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 account 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/gtscontext" 28 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 29 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 30 "github.com/superseriousbusiness/gotosocial/internal/log" 31 ) 32 33 var noLists = make([]*apimodel.List, 0) 34 35 // ListsGet returns all lists owned by requestingAccount, which contain a follow for targetAccountID. 36 func (p *Processor) ListsGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetAccountID string) ([]*apimodel.List, gtserror.WithCode) { 37 targetAccount, err := p.state.DB.GetAccountByID(ctx, targetAccountID) 38 if err != nil { 39 if errors.Is(err, db.ErrNoEntries) { 40 return nil, gtserror.NewErrorNotFound(errors.New("account not found")) 41 } 42 return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err)) 43 } 44 45 visible, err := p.filter.AccountVisible(ctx, requestingAccount, targetAccount) 46 if err != nil { 47 return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err)) 48 } 49 50 if !visible { 51 return nil, gtserror.NewErrorNotFound(errors.New("account not found")) 52 } 53 54 // Requester has to follow targetAccount 55 // for them to be in any of their lists. 56 follow, err := p.state.DB.GetFollow( 57 // Don't populate follow. 58 gtscontext.SetBarebones(ctx), 59 requestingAccount.ID, 60 targetAccountID, 61 ) 62 if err != nil && !errors.Is(err, db.ErrNoEntries) { 63 return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err)) 64 } 65 66 if follow == nil { 67 return noLists, nil // by definition we know they're in no lists 68 } 69 70 listEntries, err := p.state.DB.GetListEntriesForFollowID( 71 // Don't populate entries. 72 gtscontext.SetBarebones(ctx), 73 follow.ID, 74 ) 75 if err != nil && !errors.Is(err, db.ErrNoEntries) { 76 return nil, gtserror.NewErrorInternalError(fmt.Errorf("db error: %w", err)) 77 } 78 79 count := len(listEntries) 80 if count == 0 { 81 return noLists, nil 82 } 83 84 apiLists := make([]*apimodel.List, 0, count) 85 for _, listEntry := range listEntries { 86 list, err := p.state.DB.GetListByID( 87 // Don't populate list. 88 gtscontext.SetBarebones(ctx), 89 listEntry.ListID, 90 ) 91 92 if err != nil { 93 log.Debugf(ctx, "skipping list %s due to error %q", listEntry.ListID, err) 94 continue 95 } 96 97 apiList, err := p.tc.ListToAPIList(ctx, list) 98 if err != nil { 99 log.Debugf(ctx, "skipping list %s due to error %q", listEntry.ListID, err) 100 continue 101 } 102 103 apiLists = append(apiLists, apiList) 104 } 105 106 return apiLists, nil 107 }