95 lines
2.5 KiB
Go
95 lines
2.5 KiB
Go
|
package resolver
|
||
|
|
||
|
// This file will be automatically regenerated based on the schema, any resolver implementations
|
||
|
// will be copied through when generating and any unknown code will be moved to the end.
|
||
|
// Code generated by github.com/99designs/gqlgen version v0.17.49
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"slices"
|
||
|
"strings"
|
||
|
|
||
|
graph "git.kmsign.ru/royalcat/tstor/src/delivery/graphql"
|
||
|
"git.kmsign.ru/royalcat/tstor/src/delivery/graphql/model"
|
||
|
"git.kmsign.ru/royalcat/tstor/src/sources/torrent"
|
||
|
)
|
||
|
|
||
|
// Torrents is the resolver for the torrents field.
|
||
|
func (r *torrentDaemonQueryResolver) Torrents(ctx context.Context, obj *model.TorrentDaemonQuery, filter *model.TorrentsFilter) ([]*model.Torrent, error) {
|
||
|
torrents, err := r.Service.ListTorrents(ctx)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
filterFuncs := []func(torrent *model.Torrent) bool{}
|
||
|
|
||
|
if filter != nil {
|
||
|
if filter.BytesCompleted != nil {
|
||
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
||
|
return filter.BytesCompleted.Include(torrent.BytesCompleted)
|
||
|
})
|
||
|
}
|
||
|
if filter.BytesMissing != nil {
|
||
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
||
|
return filter.BytesMissing.Include(torrent.BytesMissing)
|
||
|
})
|
||
|
}
|
||
|
if filter.PeersCount != nil {
|
||
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
||
|
return filter.PeersCount.Include(
|
||
|
int64(len(torrent.T.Torrent().PeerConns())),
|
||
|
)
|
||
|
})
|
||
|
}
|
||
|
if filter.Infohash != nil {
|
||
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
||
|
return filter.Infohash.Include(
|
||
|
torrent.Infohash,
|
||
|
)
|
||
|
})
|
||
|
}
|
||
|
if filter.Priority != nil {
|
||
|
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
|
||
|
return filter.Priority.Include(
|
||
|
torrent.Priority,
|
||
|
)
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
|
||
|
filterFunc := func(torrent *model.Torrent) bool {
|
||
|
for _, f := range filterFuncs {
|
||
|
if !f(torrent) {
|
||
|
return false
|
||
|
}
|
||
|
}
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
tr := []*model.Torrent{}
|
||
|
for _, t := range torrents {
|
||
|
d, err := model.MapTorrent(ctx, t)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
if !filterFunc(d) {
|
||
|
continue
|
||
|
}
|
||
|
tr = append(tr, d)
|
||
|
}
|
||
|
|
||
|
slices.SortStableFunc(torrents, func(t1, t2 *torrent.Controller) int {
|
||
|
return strings.Compare(t1.Name(), t2.Name())
|
||
|
})
|
||
|
|
||
|
return tr, nil
|
||
|
}
|
||
|
|
||
|
// TorrentDaemonQuery returns graph.TorrentDaemonQueryResolver implementation.
|
||
|
func (r *Resolver) TorrentDaemonQuery() graph.TorrentDaemonQueryResolver {
|
||
|
return &torrentDaemonQueryResolver{r}
|
||
|
}
|
||
|
|
||
|
type torrentDaemonQueryResolver struct{ *Resolver }
|