mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-03 14:10:57 +08:00
UserResource add seeding leeching data + seed bonus per hour
This commit is contained in:
@@ -36,6 +36,9 @@ use Nexus\Database\NexusDB;
|
||||
|
||||
class UserRepository extends BaseRepository
|
||||
{
|
||||
private static array $allowIncludes = ['inviter', 'valid_medals'];
|
||||
private static array $allowIncludeFields = ['seeding_leeching_data'];
|
||||
private static array $allowIncludeCounts = [];
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = User::query();
|
||||
@@ -66,28 +69,34 @@ class UserRepository extends BaseRepository
|
||||
{
|
||||
//query this info default
|
||||
$query = User::query()->with([]);
|
||||
$allowIncludes = ['inviter', 'valid_medals'];
|
||||
$allowIncludeCounts = [];
|
||||
$allowIncludeFields = [];
|
||||
$apiQueryBuilder = ApiQueryBuilder::for(UserResource::NAME, $query)
|
||||
->allowIncludes($allowIncludes)
|
||||
->allowIncludeCounts($allowIncludeCounts)
|
||||
->allowIncludeFields($allowIncludeFields)
|
||||
->allowIncludes(self::$allowIncludes)
|
||||
->allowIncludeCounts(self::$allowIncludeCounts)
|
||||
->allowIncludeFields(self::$allowIncludeFields)
|
||||
;
|
||||
$query = $apiQueryBuilder->build();
|
||||
$user = $query->findOrFail($id);
|
||||
Gate::authorize('view', $user);
|
||||
return $this->appendIncludeFields($apiQueryBuilder, $currentUser, $user);
|
||||
$userList = $this->appendIncludeFields($apiQueryBuilder, $currentUser, [$user]);
|
||||
return $userList[0];
|
||||
}
|
||||
|
||||
private function appendIncludeFields(ApiQueryBuilder $apiQueryBuilder, Authenticatable $currentUser, User $user): User
|
||||
private function appendIncludeFields(ApiQueryBuilder $apiQueryBuilder, Authenticatable $currentUser, $userList)
|
||||
{
|
||||
// $id = $torrent->id;
|
||||
// if ($apiQueryBuilder->hasIncludeField('has_bookmarked')) {
|
||||
// $torrent->has_bookmarked = (int)$user->bookmarks()->where('torrentid', $id)->exists();;
|
||||
// }
|
||||
|
||||
return $user;
|
||||
$idArr = [];
|
||||
foreach ($userList as $user) {
|
||||
$idArr[] = $user->id;
|
||||
}
|
||||
if ($hasFieldSeedingData = $apiQueryBuilder->hasIncludeField('seeding_leeching_data')) {
|
||||
$seedingData = $this->listUserSeedingLeechingData($idArr);
|
||||
}
|
||||
foreach ($userList as $user) {
|
||||
$id = $user->id;
|
||||
if ($hasFieldSeedingData && isset($seedingData[$id])) {
|
||||
$user->seeding_leeching_data = $seedingData[$id];
|
||||
}
|
||||
}
|
||||
return $userList;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -814,4 +823,38 @@ class UserRepository extends BaseRepository
|
||||
return $loginLog;
|
||||
}
|
||||
|
||||
/**
|
||||
* get user seeding/leeching count and size
|
||||
*
|
||||
* @see calculate_seed_bonus()
|
||||
* @param array $userIdArr
|
||||
* @return array
|
||||
*/
|
||||
private function listUserSeedingLeechingData(array $userIdArr)
|
||||
{
|
||||
$minSize = get_setting('bonus.min_size', 0);
|
||||
$idStr = implode(",", $userIdArr);
|
||||
$sql = "select peers.userid, peers.seeder, torrents.size from torrents LEFT JOIN peers ON peers.torrent = torrents.id WHERE peers.userid in ($idStr) and torrents.size > $minSize group by peers.torrent, peers.peer_id, peers.userid, peers.seeder";
|
||||
$data = NexusDB::select($sql);
|
||||
$result = [];
|
||||
foreach ($data as $row) {
|
||||
if (!isset($result[$row['userid']])) {
|
||||
$result[$row['userid']] = [
|
||||
'seeding_count' => 0,
|
||||
'seeding_size' => 0,
|
||||
'leeching_count' => 0,
|
||||
'leeching_size' => 0,
|
||||
];
|
||||
}
|
||||
if ($row['seeder'] == 'yes') {
|
||||
$result[$row['userid']]['seeding_count'] += 1;
|
||||
$result[$row['userid']]['seeding_size'] += $row['size'];
|
||||
} else {
|
||||
$result[$row['userid']]['leeching_count'] += 1;
|
||||
$result[$row['userid']]['leeching_size'] += $row['size'];
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user