[admin] change uploaded + downloaded + bonus + invites

This commit is contained in:
xiaomlove
2022-05-10 20:12:11 +08:00
parent eccc182e33
commit 3b9125d236
43 changed files with 251 additions and 82 deletions
+13
View File
@@ -274,4 +274,17 @@ class UserController extends Controller
}
public function incrementDecrement(Request $request): array
{
$user = Auth::user();
$request->validate([
'uid' => 'required',
'action' => 'required',
'field' => 'required',
'value' => 'required|numeric',
]);
$result = $this->repository->incrementDecrement($user, $request->uid, $request->action, $request->field, $request->value, $request->reason);
return $this->success(['success' => $result]);
}
}
+1 -1
View File
@@ -25,6 +25,7 @@ class UserResource extends JsonResource
'class' => $this->class,
'class_text' => $this->class_text,
'avatar' => $this->avatar,
'invites' => $this->invites,
'uploaded' => $this->uploaded,
'uploaded_text' => mksize($this->uploaded),
'downloaded' => $this->downloaded,
@@ -44,7 +45,6 @@ class UserResource extends JsonResource
$out['seed_time'] = mkprettytime($this->seedtime);
$out['leech_time'] = mkprettytime($this->leechtime);
$out['share_ratio'] = get_share_ratio($this->uploaded, $this->downloaded);
$out['invites'] = $this->invites;
$out['comments_count'] = $this->comments_count;
$out['posts_count'] = $this->posts_count;
$out['torrents_count'] = $this->torrents_count;
+2 -2
View File
@@ -152,7 +152,7 @@ class User extends Authenticatable
public static $commonFields = [
'id', 'username', 'email', 'class', 'status', 'added', 'avatar',
'uploaded', 'downloaded', 'seedbonus', 'seedtime', 'leechtime',
'invited_by', 'enabled', 'seed_points', 'last_access'
'invited_by', 'enabled', 'seed_points', 'last_access', 'invites'
];
public static function getDefaultUserAttributes(): array
@@ -378,7 +378,7 @@ class User extends Authenticatable
}
public function updateWithModComment(array $update, $modComment)
public function updateWithModComment(array $update, $modComment): bool
{
if (!$this->exists) {
throw new \RuntimeException('This method only works when user exists!');
+3 -3
View File
@@ -697,7 +697,7 @@ class TrackerRepository extends BaseRepository
'downloaded_increment' => $realDownloaded,
'downloaded_increment_for_user' => $realDownloaded * $downRatio,
];
do_log("$log, result: " . json_encode($result), 'alert');
do_log("$log, result: " . json_encode($result), 'info');
return $result;
}
@@ -947,7 +947,7 @@ class TrackerRepository extends BaseRepository
$snatch->last_action = $nowStr;
$snatch->save();
do_log(last_query(), 'alert');
do_log(last_query(), 'info');
return $snatch;
}
@@ -1076,7 +1076,7 @@ class TrackerRepository extends BaseRepository
$user->update($update);
$log .= ", query: " . last_query();
}
do_log($log, 'alert');
do_log($log, 'info');
}
}
+42
View File
@@ -10,6 +10,7 @@ use App\Models\User;
use App\Models\UserBanLog;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Nexus\Database\NexusDB;
class UserRepository extends BaseRepository
{
@@ -206,6 +207,47 @@ class UserRepository extends BaseRepository
return $user->modcomment;
}
public function incrementDecrement(User $operator, $uid, $action, $field, $value, $reason = ''): bool
{
$fieldMap = [
'uploaded' => 'uploaded',
'downloaded' => 'downloaded',
'bonus' => 'seedbonus',
'invites' => 'invites',
];
if (!isset($fieldMap[$field])) {
throw new \InvalidArgumentException("Invalid field: $field, only support: " . implode(', ', array_keys($fieldMap)));
}
$sourceField = $fieldMap[$field];
$targetUser = User::query()->findOrFail($uid, User::$commonFields);
$old = $targetUser->{$sourceField};
if ($action == 'Increment') {
$new = $old + abs($value);
} elseif ($action == 'Decrement') {
$new = $old - abs($value);
} else {
throw new \InvalidArgumentException("Invalid action: $action.");
}
$modCommentText = sprintf(
"%s - %s change from %s to %s by %s, reason: %s.",
date('Y-m-d'), $field, $old, $new, $operator->username, $reason
);
do_log("user: $uid, $modCommentText");
$update = [
$sourceField => $new,
'modcomment' => NexusDB::raw("if(modcomment = '', '$modCommentText', concat_ws('\n', '$modCommentText', modcomment))"),
];
$affectedRows = User::query()
->where('id', $uid)
->where($sourceField, $old)
->update($update)
;
if ($affectedRows != 1) {
throw new \RuntimeException("Change fail, affected rows != 1($affectedRows)");
}
return true;
}
}