Skip to content

Conversation

@SlovakianCanon
Copy link

Summary

Extends the /api/user endpoint with additional statistics already available on the profile page:

  • real_uploaded / real_downloaded - actual transfer amounts
  • credited_uploaded / credited_downloaded - with bonus multipliers applied
  • average_seedtime - average seed time per torrent
  • seeding_size - total size of torrents currently seeding
  • fl_tokens - freeleech tokens
  • uploads_count / downloads_count - torrent counts
  • bonus_uploaded - upload purchased with bonus points

Changes

  • app/Http/Controllers/API/UserController.php - added history aggregation query
  • app/Http/Resources/UserResource.php - added new fields to response
  • book/src/torrent_api.md - updated API documentation

@what-the-diff
Copy link

what-the-diff bot commented Dec 29, 2025

PR Summary

  • Improvements to UserController.php

    • Integrates additional model and database interaction for better data handling.
    • Implements a new feature to collect information on user upload/download history and associated data.
    • Adds more user details to be returned to the client.
  • Updates to UserResource.php

    • Expands data handling in the constructor and toArray method, including newly calculated user statistics.
  • Updates to torrent_api.md

    • Illustrates the new response fields in the API example.
    • Provides a new table with detailed explanation of each new response field.

Copy link
Collaborator

@Roardom Roardom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good PR. I'm currently working on a more refined compartmentalized framework for api usage/keys and will consider this PR as soon as that framework is in place.

Comment on lines 43 to 46
$bonusUploaded = BonTransactions::query()
->where('sender_id', '=', $user->id)
->where('name', 'like', '%Upload%')
->sum('cost');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$bonusUploaded = BonTransactions::query()
->where('sender_id', '=', $user->id)
->where('name', 'like', '%Upload%')
->sum('cost');
$bonusUploaded = BonTransactions::query()
->where('sender_id', '=', $user->id)
->where('name', 'like', '%Upload%')
->sum('cost');

Wildcard searches shouldn't be permitted in the api.

Comment on lines 29 to 30
$history = DB::table('history')
->where('user_id', '=', $user->id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$history = DB::table('history')
->where('user_id', '=', $user->id)
$history = History::query()
->withTrashed()
->where('user_id', '=', $user->id)

Comment on lines 82 to 87
'real_uploaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes((int) ($this->history?->upload_sum ?? 0), 2)),
'real_downloaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes((int) ($this->history?->download_sum ?? 0), 2)),
'credited_uploaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes((int) ($this->history?->credited_upload_sum ?? 0), 2)),
'credited_downloaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes((int) ($this->history?->credited_download_sum ?? 0), 2)),
'average_seedtime' => StringHelper::timeElapsed($avgSeedtime),
'seeding_size' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes($this->seedingSize, 2)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be raw values, not human ones.

'fl_tokens' => $this->fl_tokens,
'uploads_count' => $this->uploadsCount,
'downloads_count' => (int) ($this->history?->download_count ?? 0),
'bonus_uploaded' => str_replace("\u{00A0}", ' ', StringHelper::formatBytes($this->bonusUploaded, 2)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Comment on lines 67 to 69
$avgSeedtime = $this->history?->count > 0
? (int) (($this->history->seedtime_sum ?? 0) / $this->history->count)
: 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should be able to use resource loading instead of manual db queries and passing them through: https://laravel.com/docs/12.x/eloquent-resources#conditional-relationships

- Use History::query()->withTrashed() instead of DB facade
- Replace wildcard LIKE with whereRelation('exchange', 'upload', true)
- Return raw integers instead of formatted strings
- Remove custom constructor, use standard resource pattern
@HDVinnie HDVinnie requested a review from Roardom January 9, 2026 00:59
Copy link
Collaborator

@Roardom Roardom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My review at this point is still the same:

This is a good PR. I'm currently working on a more refined compartmentalized framework for api usage/keys and will consider this PR as soon as that framework is in place.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants