Merge branch '1.9' into php8

This commit is contained in:
xiaomlove
2026-01-07 01:31:11 +07:00
15 changed files with 91 additions and 42 deletions

View File

@@ -94,6 +94,7 @@ class BonusLogResource extends Resource
SelectFilter::make('business_type')
->options(BonusLogs::listStaticProps(Arr::except(BonusLogs::$businessTypes, BonusLogs::$businessTypeBonus), 'bonus-log.business_types', true))
->label(__('bonus-log.fields.business_type'))
->searchable(true)
,
// Tables\Filters\Filter::make('exclude_seeding_bonus')
// ->toggle()

View File

@@ -25,17 +25,8 @@ class CommentController extends Controller
*/
public function index(Request $request)
{
$torrentId = $request->torrent_id;
$with = ['create_user', 'update_user'];
$comments = Comment::query()
->with($with)
->where('torrent', $torrentId)
->paginate();
$comments = $this->repository->getList($request, Auth::user());
$resource = CommentResource::collection($comments);
// $resource->additional([
// 'page_title' => nexus_trans('comment.index.page_title'),
// ]);
return $this->success($resource);
}

View File

@@ -15,15 +15,13 @@ class CommentResource extends JsonResource
*/
public function toArray($request)
{
$descriptionArr = format_description($this->text);
return [
'id' => $this->id,
'description' => $descriptionArr,
'images' => get_image_from_description($descriptionArr),
'updated_at_human' => format_datetime($this->editdate),
'created_at_human' => $this->added->format('Y-m-d H:i'),
'text' => $this->text,
'updated_at' => format_datetime($this->editdate),
'created_at' => format_datetime($this->added),
'create_user' => new UserResource($this->whenLoaded('create_user')),
'update_user' => new UserResource($this->whenLoaded('update_user')),
'update_user' => $this->when($this->editedby > 0, new UserResource($this->whenLoaded('update_user'))),
];
}
}

View File

@@ -42,6 +42,7 @@ class BonusLogs extends NexusModel
const BUSINESS_TYPE_TASK_NOT_PASS_DEDUCT = 20;
const BUSINESS_TYPE_TASK_PASS_REWARD = 21;
const BUSINESS_TYPE_REWARD_TORRENT = 22;
const BUSINESS_TYPE_CLAIMED_UNREACHED = 23;
//获得类普通获得1000 起步
const BUSINESS_TYPE_ROLE_WORK_SALARY = 1000;
@@ -50,6 +51,7 @@ class BonusLogs extends NexusModel
const BUSINESS_TYPE_RECEIVE_GIFT = 1003;
const BUSINESS_TYPE_UPLOAD_TORRENT = 1004;
const BUSINESS_TYPE_TORRENT_BE_REWARD = 1005;
const BUSINESS_TYPE_CLAIMED_REACHED = 1006;
//获得类做种获得10000 起
const BUSINESS_TYPE_SEEDING_BASIC = 10000;
@@ -81,6 +83,7 @@ class BonusLogs extends NexusModel
self::BUSINESS_TYPE_TASK_NOT_PASS_DEDUCT => ['text' => 'Task failure penalty'],
self::BUSINESS_TYPE_TASK_PASS_REWARD => ['text' => 'Task success reward'],
self::BUSINESS_TYPE_REWARD_TORRENT => ['text' => 'Reward torrent'],
self::BUSINESS_TYPE_CLAIMED_UNREACHED => ['text' => 'Claimed torrent unreached'],
self::BUSINESS_TYPE_ROLE_WORK_SALARY => ['text' => 'Role work salary'],
self::BUSINESS_TYPE_TORRENT_BE_DOWNLOADED => ['text' => 'Torrent be downloaded'],
@@ -88,6 +91,7 @@ class BonusLogs extends NexusModel
self::BUSINESS_TYPE_RECEIVE_GIFT => ['text' => 'Receive gift'],
self::BUSINESS_TYPE_UPLOAD_TORRENT => ['text' => 'Upload torrent'],
self::BUSINESS_TYPE_TORRENT_BE_REWARD => ['text' => 'Torrent be reward'],
self::BUSINESS_TYPE_CLAIMED_REACHED => ['text' => 'Claimed torrent reached'],
self::BUSINESS_TYPE_SEEDING_BASIC => ['text' => 'Seeding basic'],
self::BUSINESS_TYPE_SEEDING_DONOR_ADDITION => ['text' => 'Seeding donor addition'],

View File

@@ -85,13 +85,23 @@ class TrackerUrl extends NexusModel
if ($redis->exists($notFoundFlagKey)) {
return false;
}
self::saveUrlCache();
$result = call_user_func_array([$redis, $command], $params);
if ($result !== false) {
return $result;
$lockKey = "$notFoundFlagKey:lock";
if (!$redis->set($lockKey, 1, ["nx", "ex" => 5])) {
return false;
}
try {
self::saveUrlCache();
$result = call_user_func_array([$redis, $command], $params);
if ($result !== false) {
return $result;
}
//只从 db 拉取一次,仍然没有即标记不存在, 有效期 15 分钟
$redis->setex($notFoundFlagKey, 900, date("Y-m-d H:i:s"));
} catch (\Throwable $throwable) {
do_log($throwable->getMessage(), 'error');
} finally {
$redis->del($lockKey);
}
//只从 db 拉取一次,仍然没有即标记不存在, 有效期 15 分钟
$redis->setex($notFoundFlagKey, 900, date("Y-m-d H:i:s"));
do_log(sprintf("redis command %s with args %s no result", $command, json_encode($params)), 'error');
return false;
}

View File

@@ -6,6 +6,7 @@ use App\Exceptions\NexusException;
use App\Http\Middleware\Locale;
use App\Models\Traits\NexusActivityLogTrait;
use App\Repositories\ExamRepository;
use App\Repositories\TokenRepository;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Casts\Attribute;
@@ -635,10 +636,32 @@ class User extends Authenticatable implements FilamentUser, HasName
return is_null($this->original['notifs']) || str_contains($this->notifs, "[{$name}]");
}
public function tokenCan(string $ability)
public function tokenCan(string $ability): bool
{
$redis = NexusDB::redis();
return $redis->sismember(Setting::USER_TOKEN_PERMISSION_ALLOWED_CACHE_KRY, $ability)
$cacheKey = Setting::USER_TOKEN_PERMISSION_ALLOWED_CACHE_KRY;
if (!$redis->exists($cacheKey)) {
$lockKey = "$cacheKey:lock";
if ($redis->set($lockKey, 1, ['nx', 'ex' => 5])) {
try {
if (!$redis->exists($cacheKey)) {
$abilities = TokenRepository::listUserTokenPermissions(false);
do_log("load user token permissions: " . json_encode($abilities), 'alert');
if (!empty($abilities)) {
$redis->sadd($cacheKey, ...$abilities);
} else {
$redis->sadd($cacheKey, "__NO_USER_TOKEN_PERMISSION__");
$redis->expire($cacheKey, 900);
}
}
} catch (\Throwable $throwable) {
do_log($throwable->getMessage(), 'error');
} finally {
$redis->del($lockKey);
}
}
}
return $redis->sismember($cacheKey, $ability)
&& $this->accessToken && $this->accessToken->can($ability);
}

View File

@@ -2,6 +2,7 @@
namespace App\Repositories;
use App\Jobs\SettleClaim;
use App\Models\BonusLogs;
use App\Models\Claim;
use App\Models\Message;
use App\Models\Snatch;
@@ -260,16 +261,24 @@ class ClaimRepository extends BaseRepository
//Wrap with transaction
DB::transaction(function () use ($uid, $unReachedIdArr, $toUpdateIdArr, $bonusFinal, $totalDeduct, $uploadedCaseWhen, $seedTimeCaseWhen, $message, $now) {
//get latest
$oldBonus = User::query()->find($uid, ['seedbonus'])->seedbonus;
$delta = 0;
//Increase user bonus
User::query()->where('id', $uid)->increment('seedbonus', $bonusFinal);
do_log("Increase user bonus: $bonusFinal", 'alert');
$delta += $bonusFinal;
do_log("Increase user: $uid bonus: $bonusFinal", 'alert');
BonusLogs::add($uid, $oldBonus, $bonusFinal, $oldBonus + $bonusFinal, "", BonusLogs::BUSINESS_TYPE_CLAIMED_REACHED);
$oldBonus += $bonusFinal;
//Handle unreached
if (!empty($unReachedIdArr)) {
Claim::query()->whereIn('id', $unReachedIdArr)->delete();
User::query()->where('id', $uid)->decrement('seedbonus', $totalDeduct);
do_log("Deduct user bonus: $totalDeduct", 'alert');
$delta -= $totalDeduct;
do_log("Deduct user: $uid bonus: $totalDeduct", 'alert');
BonusLogs::add($uid, $oldBonus, $totalDeduct, $oldBonus - $totalDeduct, "", BonusLogs::BUSINESS_TYPE_CLAIMED_UNREACHED);
$oldBonus -= $totalDeduct;
}
User::query()->where('id', $uid)->increment('seedbonus', $delta);
//Update claim `last_settle_at` and init `seed_time_begin` & `uploaded_begin`
if (!empty($toUpdateIdArr)) {

View File

@@ -9,26 +9,27 @@ use App\Models\Torrent;
use App\Models\User;
use Carbon\Carbon;
use Hamcrest\Core\Set;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Nexus\Database\NexusDB;
class CommentRepository extends BaseRepository
{
public function getList(array $params)
public function getList(Request $request, Authenticatable $user)
{
$query = Comment::query()->with(['create_user', 'update_user']);
if (!empty($params['torrent_id'])) {
$query->where('torrent', $params['torrent_id']);
if (!empty($request->torrent_id)) {
$query->where('torrent', $request->torrent_id);
}
if (!empty($params['offer_id'])) {
$query->where('offer', $params['offer_id']);
if (!empty($request->offer_id)) {
$query->where('offer', $request->offer_id);
}
if (!empty($params['request_id'])) {
$query->where('request', $params['request_id']);
if (!empty($request->request_id)) {
$query->where('request', $request->request_id);
}
list($sortField, $sortType) = $this->getSortFieldAndType($params);
$query->orderBy($sortField, $sortType);
return $query->paginate();
$query->orderBy('id', 'desc');
return $query->paginate($this->getPerPageFromRequest($request));
}
public function store(array $params, User $user)