2021-06-13 16:42:28 +00:00
|
|
|
package status
|
|
|
|
|
|
|
|
import (
|
2021-06-17 16:02:33 +00:00
|
|
|
"fmt"
|
|
|
|
"sort"
|
|
|
|
|
2021-06-13 16:42:28 +00:00
|
|
|
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
|
2021-06-17 16:02:33 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/db"
|
2021-06-13 16:42:28 +00:00
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
|
|
|
|
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p *processor) Context(account *gtsmodel.Account, targetStatusID string) (*apimodel.Context, gtserror.WithCode) {
|
2021-06-17 16:02:33 +00:00
|
|
|
|
|
|
|
context := &apimodel.Context{
|
2021-06-13 16:42:28 +00:00
|
|
|
Ancestors: []apimodel.Status{},
|
|
|
|
Descendants: []apimodel.Status{},
|
2021-06-17 16:02:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
targetStatus := >smodel.Status{}
|
|
|
|
if err := p.db.GetByID(targetStatusID, targetStatus); err != nil {
|
|
|
|
if _, ok := err.(db.ErrNoEntries); ok {
|
|
|
|
return nil, gtserror.NewErrorNotFound(err)
|
|
|
|
}
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
visible, err := p.filter.StatusVisible(targetStatus, account)
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorNotFound(err)
|
|
|
|
}
|
|
|
|
if !visible {
|
|
|
|
return nil, gtserror.NewErrorForbidden(fmt.Errorf("account with id %s does not have permission to view status %s", account.ID, targetStatusID))
|
|
|
|
}
|
|
|
|
|
|
|
|
parents, err := p.db.StatusParents(targetStatus)
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, status := range parents {
|
|
|
|
if v, err := p.filter.StatusVisible(status, account); err == nil && v {
|
|
|
|
mastoStatus, err := p.tc.StatusToMasto(status, account)
|
|
|
|
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
|
|
|
|
})
|
|
|
|
|
|
|
|
children, err := p.db.StatusChildren(targetStatus)
|
|
|
|
if err != nil {
|
|
|
|
return nil, gtserror.NewErrorInternalError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, status := range children {
|
|
|
|
if v, err := p.filter.StatusVisible(status, account); err == nil && v {
|
|
|
|
mastoStatus, err := p.tc.StatusToMasto(status, account)
|
|
|
|
if err == nil {
|
|
|
|
context.Descendants = append(context.Descendants, *mastoStatus)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return context, nil
|
2021-06-13 16:42:28 +00:00
|
|
|
}
|