2021-06-13 19:42:28 +03:00
|
|
|
package status
|
|
|
|
|
|
|
|
import (
|
2021-08-20 13:26:56 +03:00
|
|
|
"errors"
|
2021-06-17 19:02:33 +03:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2021-06-13 19:42:28 +03:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
)
|
|
|
|
|
2021-08-20 13:26:56 +03:00
|
|
|
func (p *processor) Context(requestingAccount *gtsmodel.Account, targetStatusID string) (*apimodel.Context, gtserror.WithCode) {
|
|
|
|
targetStatus, err := p.db.GetStatusByID(targetStatusID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorNotFound(fmt.Errorf("error fetching status %s: %s", targetStatusID, err))
|
2021-06-17 19:02:33 +03:00
|
|
|
}
|
2021-08-20 13:26:56 +03:00
|
|
|
if targetStatus.Account == nil {
|
|
|
|
return nil, gtserror.NewErrorNotFound(fmt.Errorf("no status owner for status %s", targetStatusID))
|
2021-06-17 19:02:33 +03:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:26:56 +03:00
|
|
|
visible, err := p.filter.StatusVisible(targetStatus, requestingAccount)
|
2021-06-17 19:02:33 +03:00
|
|
|
if err != nil {
|
2021-08-20 13:26:56 +03:00
|
|
|
return nil, gtserror.NewErrorNotFound(fmt.Errorf("error seeing if status %s is visible: %s", targetStatus.ID, err))
|
2021-06-17 19:02:33 +03:00
|
|
|
}
|
|
|
|
if !visible {
|
2021-08-20 13:26:56 +03:00
|
|
|
return nil, gtserror.NewErrorNotFound(errors.New("status is not visible"))
|
|
|
|
}
|
|
|
|
|
|
|
|
context := &apimodel.Context{
|
|
|
|
Ancestors: []apimodel.Status{},
|
|
|
|
Descendants: []apimodel.Status{},
|
2021-06-17 19:02:33 +03:00
|
|
|
}
|
|
|
|
|
2021-08-20 13:26:56 +03:00
|
|
|
parents, err := p.db.GetStatusParents(targetStatus, false)
|
2021-06-17 19:02:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, status := range parents {
|
2021-08-20 13:26:56 +03:00
|
|
|
if v, err := p.filter.StatusVisible(status, requestingAccount); err == nil && v {
|
|
|
|
mastoStatus, err := p.tc.StatusToMasto(status, requestingAccount)
|
2021-06-17 19:02:33 +03:00
|
|
|
if err == nil {
|
|
|
|
context.Ancestors = append(context.Ancestors, *mastoStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(context.Ancestors, func(i int, j int) bool {
|
|
|
|
return context.Ancestors[i].ID < context.Ancestors[j].ID
|
|
|
|
})
|
|
|
|
|
2021-08-20 13:26:56 +03:00
|
|
|
children, err := p.db.GetStatusChildren(targetStatus, false, "")
|
2021-06-17 19:02:33 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, status := range children {
|
2021-08-20 13:26:56 +03:00
|
|
|
if v, err := p.filter.StatusVisible(status, requestingAccount); err == nil && v {
|
|
|
|
mastoStatus, err := p.tc.StatusToMasto(status, requestingAccount)
|
2021-06-17 19:02:33 +03:00
|
|
|
if err == nil {
|
|
|
|
context.Descendants = append(context.Descendants, *mastoStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return context, nil
|
2021-06-13 19:42:28 +03:00
|
|
|
}
|