Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions app/Http/Controllers/API/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,37 @@
namespace App\Http\Controllers\API;

use App\Http\Resources\UserResource;
use App\Models\BonTransactions;
use App\Models\History;

class UserController extends BaseController
{
final public function show(): UserResource
{
$user = auth()->user();

$user->loadCount(['torrents', 'seedingTorrents', 'leechingTorrents']);

$user->history_stats = History::query()

Check failure on line 31 in app/Http/Controllers/API/UserController.php

View workflow job for this annotation

GitHub Actions / php 8.5 on ubuntu-24.04

Access to an undefined property App\Models\User::$history_stats.
->withTrashed()
->where('user_id', '=', $user->id)
->where('created_at', '>', $user->created_at)
->selectRaw('SUM(actual_uploaded) as upload_sum')
->selectRaw('SUM(uploaded) as credited_upload_sum')
->selectRaw('SUM(actual_downloaded) as download_sum')
->selectRaw('SUM(downloaded) as credited_download_sum')
->selectRaw('SUM(seedtime) as seedtime_sum')
->selectRaw('SUM(actual_downloaded > 0) as download_count')
->selectRaw('COUNT(*) as count')
->first();

$user->seeding_size = $user->seedingTorrents()->sum('size');

Check failure on line 44 in app/Http/Controllers/API/UserController.php

View workflow job for this annotation

GitHub Actions / php 8.5 on ubuntu-24.04

Access to an undefined property App\Models\User::$seeding_size.

$user->bonus_uploaded = BonTransactions::query()

Check failure on line 46 in app/Http/Controllers/API/UserController.php

View workflow job for this annotation

GitHub Actions / php 8.5 on ubuntu-24.04

Access to an undefined property App\Models\User::$bonus_uploaded.
->where('sender_id', '=', $user->id)
->whereRelation('exchange', 'upload', '=', true)
->sum('cost');

UserResource::withoutWrapping();

return new UserResource($user);
Expand Down
45 changes: 35 additions & 10 deletions app/Http/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,46 @@
* leeching: int,
* seedbonus: string,
* hit_and_runs: int,
* real_uploaded: int,
* real_downloaded: int,
* credited_uploaded: int,
* credited_downloaded: int,
* average_seedtime: int,
* seeding_size: int,
* fl_tokens: int,
* uploads_count: int,
* downloads_count: int,
* bonus_uploaded: int,
* }
*/
public function toArray(Request $request): array
{
$historyStats = $this->history_stats;

Check failure on line 55 in app/Http/Resources/UserResource.php

View workflow job for this annotation

GitHub Actions / php 8.5 on ubuntu-24.04

Access to an undefined property App\Http\Resources\UserResource::$history_stats.
$avgSeedtime = $historyStats?->count > 0
? (int) (($historyStats->seedtime_sum ?? 0) / $historyStats->count)
: 0;

return [
'username' => $this->username,
'group' => $this->group->name,
'uploaded' => str_replace("\u{00A0}", ' ', $this->formatted_uploaded),
'downloaded' => str_replace("\u{00A0}", ' ', $this->formatted_downloaded),
'ratio' => $this->formatted_ratio,
'buffer' => str_replace("\u{00A0}", ' ', $this->formatted_buffer),
'seeding' => \count($this->seedingTorrents),
'leeching' => \count($this->leechingTorrents),
'seedbonus' => $this->seedbonus,
'hit_and_runs' => $this->hitandruns,
'username' => $this->username,
'group' => $this->group->name,
'uploaded' => str_replace("\u{00A0}", ' ', $this->formatted_uploaded),
'downloaded' => str_replace("\u{00A0}", ' ', $this->formatted_downloaded),
'ratio' => $this->formatted_ratio,
'buffer' => str_replace("\u{00A0}", ' ', $this->formatted_buffer),
'seeding' => $this->seeding_torrents_count,
'leeching' => $this->leeching_torrents_count,
'seedbonus' => $this->seedbonus,
'hit_and_runs' => $this->hitandruns,
'real_uploaded' => (int) ($historyStats?->upload_sum ?? 0),
'real_downloaded' => (int) ($historyStats?->download_sum ?? 0),
'credited_uploaded' => (int) ($historyStats?->credited_upload_sum ?? 0),
'credited_downloaded' => (int) ($historyStats?->credited_download_sum ?? 0),
'average_seedtime' => $avgSeedtime,
'seeding_size' => (int) $this->seeding_size,

Check failure on line 76 in app/Http/Resources/UserResource.php

View workflow job for this annotation

GitHub Actions / php 8.5 on ubuntu-24.04

Access to an undefined property App\Http\Resources\UserResource::$seeding_size.
'fl_tokens' => $this->fl_tokens,
'uploads_count' => $this->torrents_count,
'downloads_count' => (int) ($historyStats?->download_count ?? 0),
'bonus_uploaded' => (int) $this->bonus_uploaded,

Check failure on line 80 in app/Http/Resources/UserResource.php

View workflow job for this annotation

GitHub Actions / php 8.5 on ubuntu-24.04

Access to an undefined property App\Http\Resources\UserResource::$bonus_uploaded.
];
}
}
36 changes: 35 additions & 1 deletion book/src/torrent_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,43 @@ Endpoint: GET `/api/user`

Response:
```json
{"username":"UNIT3D","group":"Owner","uploaded":"50 GiB","downloaded":"1 GiB","ratio":"50","buffer":"124 GiB","seeding":0,"leeching":0,"seedbonus":"0.00","hit_and_runs":0}
{
"username": "UNIT3D",
"group": "Owner",
"uploaded": "50 GiB",
"downloaded": "1 GiB",
"ratio": "50",
"buffer": "124 GiB",
"seeding": 0,
"leeching": 0,
"seedbonus": "0.00",
"hit_and_runs": 0,
"real_uploaded": 48318382080,
"real_downloaded": 1073741824,
"credited_uploaded": 53687091200,
"credited_downloaded": 1073741824,
"average_seedtime": 1339392,
"seeding_size": 2748779069440,
"fl_tokens": 5,
"uploads_count": 12,
"downloads_count": 87,
"bonus_uploaded": 5153960755
}
```

| Field | Type | Description |
|-------|------|-------------|
| `real_uploaded` | int | Actual bytes uploaded (without bonus multipliers) |
| `real_downloaded` | int | Actual bytes downloaded (without freeleech) |
| `credited_uploaded` | int | Credited bytes uploaded (with bonus multipliers applied) |
| `credited_downloaded` | int | Credited bytes downloaded (after freeleech applied) |
| `average_seedtime` | int | Average seed time per torrent in seconds |
| `seeding_size` | int | Total size in bytes of torrents currently seeding |
| `fl_tokens` | int | Available freeleech tokens |
| `uploads_count` | int | Number of torrents uploaded |
| `downloads_count` | int | Number of torrents downloaded |
| `bonus_uploaded` | int | Upload credit in bytes purchased with bonus points |

Example:
```
https://unit3d.site/api/user?api_token=YOURTOKENHERE
Expand Down
Loading