Files
ops-Gazelle/app/Json/UserTorrents.php
2025-09-10 13:01:53 +02:00

92 lines
3.3 KiB
PHP

<?php
namespace Gazelle\Json;
use Gazelle\Search\UserTorrent;
use Gazelle\Enum\UserTorrentSearch;
class UserTorrents extends \Gazelle\Json {
protected int $limit = 500;
protected int $offset = 0;
protected string $type = 'seeding';
public function __construct(
protected \Gazelle\User $user,
protected \Gazelle\User $viewer,
protected \Gazelle\Manager\Torrent $torMan = new \Gazelle\Manager\Torrent(),
) {}
public function setType(string $type): static {
$this->type = $type;
return $this;
}
public function setLimit(int $limit): static {
$this->limit = $limit;
return $this;
}
public function setOffset(int $offset): static {
$this->offset = $offset;
return $this;
}
public function payload(): array {
switch ($this->type) {
case 'downloaded':
$userTorrent = new UserTorrent($this->user, UserTorrentSearch::downloaded);
break;
case 'leeching':
$userTorrent = new UserTorrent($this->user, UserTorrentSearch::leeching);
break;
case 'seeding':
$userTorrent = new UserTorrent($this->user, UserTorrentSearch::seeding);
break;
case 'snatched':
$userTorrent = new UserTorrent($this->user, UserTorrentSearch::snatched);
break;
case 'snatched-unseeded':
$userTorrent = new UserTorrent($this->user, UserTorrentSearch::snatchedUnseeded);
break;
case 'uploaded':
$userTorrent = new UserTorrent($this->user, UserTorrentSearch::uploaded);
break;
case 'uploaded-unseeded':
$userTorrent = new UserTorrent($this->user, UserTorrentSearch::uploadedUnseeded);
break;
default:
json_error("bad type");
}
$userTorrent->setLimit($this->limit)->setOffset($this->offset);
return [
"$this->type" => array_reduce(
$userTorrent->idList(),
function ($acc, $id) {
$torrent = $this->torMan->findById($id);
if ($torrent) {
$item = [
'groupId' => $torrent->groupId(),
'torrentId' => $torrent->id(),
'name' => $torrent->group()->name(),
'torrentSize' => $torrent->size(),
];
if ($torrent->group()->hasArtistRole()) {
// artistId and artistName are returned mostly for compatibility with RED
// as people should probably just use `artists` array principally.
$item['artistId'] = $torrent->group()->artistRole()?->primaryId();
$item['artistName'] = $torrent->group()->artistName();
$item['artists'] = static::artistPayload($torrent->group());
}
$acc[] = $item;
}
return $acc;
},
[],
),
'total' => $userTorrent->total(),
];
}
}