get.go (2989B)
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 status 19 20 import ( 21 "context" 22 "sort" 23 24 apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model" 25 "github.com/superseriousbusiness/gotosocial/internal/gtserror" 26 "github.com/superseriousbusiness/gotosocial/internal/gtsmodel" 27 ) 28 29 // Get gets the given status, taking account of privacy settings and blocks etc. 30 func (p *Processor) Get(ctx context.Context, requestingAccount *gtsmodel.Account, targetStatusID string) (*apimodel.Status, gtserror.WithCode) { 31 targetStatus, errWithCode := p.getVisibleStatus(ctx, requestingAccount, targetStatusID) 32 if errWithCode != nil { 33 return nil, errWithCode 34 } 35 36 return p.apiStatus(ctx, targetStatus, requestingAccount) 37 } 38 39 // ContextGet returns the context (previous and following posts) from the given status ID. 40 func (p *Processor) ContextGet(ctx context.Context, requestingAccount *gtsmodel.Account, targetStatusID string) (*apimodel.Context, gtserror.WithCode) { 41 targetStatus, errWithCode := p.getVisibleStatus(ctx, requestingAccount, targetStatusID) 42 if errWithCode != nil { 43 return nil, errWithCode 44 } 45 46 context := &apimodel.Context{ 47 Ancestors: []apimodel.Status{}, 48 Descendants: []apimodel.Status{}, 49 } 50 51 parents, err := p.state.DB.GetStatusParents(ctx, targetStatus, false) 52 if err != nil { 53 return nil, gtserror.NewErrorInternalError(err) 54 } 55 56 for _, status := range parents { 57 if v, err := p.filter.StatusVisible(ctx, requestingAccount, status); err == nil && v { 58 apiStatus, err := p.tc.StatusToAPIStatus(ctx, status, requestingAccount) 59 if err == nil { 60 context.Ancestors = append(context.Ancestors, *apiStatus) 61 } 62 } 63 } 64 65 sort.Slice(context.Ancestors, func(i int, j int) bool { 66 return context.Ancestors[i].ID < context.Ancestors[j].ID 67 }) 68 69 children, err := p.state.DB.GetStatusChildren(ctx, targetStatus, false, "") 70 if err != nil { 71 return nil, gtserror.NewErrorInternalError(err) 72 } 73 74 for _, status := range children { 75 if v, err := p.filter.StatusVisible(ctx, requestingAccount, status); err == nil && v { 76 apiStatus, err := p.tc.StatusToAPIStatus(ctx, status, requestingAccount) 77 if err == nil { 78 context.Descendants = append(context.Descendants, *apiStatus) 79 } 80 } 81 } 82 83 return context, nil 84 }