mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-24 03:57:22 +08:00
[api] reward + thanks
This commit is contained in:
@@ -61,7 +61,8 @@ class Handler extends ExceptionHandler
|
||||
|
||||
$this->renderable(function (NotFoundHttpException $e) {
|
||||
if ($e->getPrevious() && $e->getPrevious() instanceof ModelNotFoundException) {
|
||||
return response()->json(fail('No query result.', request()->all()), 404);
|
||||
$exception = $e->getPrevious();
|
||||
return response()->json(fail($exception->getMessage(), request()->all()), 404);
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -97,7 +98,12 @@ class Handler extends ExceptionHandler
|
||||
|
||||
protected function getHttpStatusCode(Throwable $e)
|
||||
{
|
||||
if ($e instanceof \InvalidArgumentException || $e instanceof NexusException) {
|
||||
if (
|
||||
$e instanceof NexusException
|
||||
|| $e instanceof \InvalidArgumentException
|
||||
|| $e instanceof \LogicException
|
||||
|| $e instanceof \RuntimeException
|
||||
) {
|
||||
return 200;
|
||||
}
|
||||
if ($this->isHttpException($e)) {
|
||||
|
||||
@@ -4,10 +4,20 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\CommentResource;
|
||||
use App\Models\Comment;
|
||||
use App\Repositories\CommentRepository;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class CommentController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(CommentRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
@@ -20,7 +30,6 @@ class CommentController extends Controller
|
||||
$comments = Comment::query()
|
||||
->with($with)
|
||||
->where('torrent', $torrentId)
|
||||
->whereHas('create_user')
|
||||
->paginate();
|
||||
$resource = CommentResource::collection($comments);
|
||||
$resource->additional([
|
||||
@@ -30,6 +39,35 @@ class CommentController extends Controller
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
private function prepareData(Request $request)
|
||||
{
|
||||
$allTypes = array_keys(Comment::TYPE_MAPS);
|
||||
$request->validate([
|
||||
'type' => ['required', Rule::in($allTypes)],
|
||||
'torrent_id' => 'nullable|integer',
|
||||
'text' => 'required',
|
||||
'offer_id' => 'nullable|integer',
|
||||
'request_id' => 'nullable|integer',
|
||||
'anonymous' => 'nullable',
|
||||
]);
|
||||
$data = [
|
||||
'type' => $request->type,
|
||||
'torrent' => $request->torrent_id,
|
||||
'text' => $request->text,
|
||||
'ori_text' => $request->text,
|
||||
'offer' => $request->offer_id,
|
||||
'request' => $request->request_id,
|
||||
'anonymous' => $request->anonymous,
|
||||
];
|
||||
$data = array_filter($data);
|
||||
foreach ($allTypes as $type) {
|
||||
if ($data['type'] == $type && empty($data[$type . "_id"])) {
|
||||
throw new \InvalidArgumentException("require {$type}_id");
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
@@ -38,7 +76,10 @@ class CommentController extends Controller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
$user = Auth::user();
|
||||
$comment = $this->repository->store($this->prepareData($request), $user);
|
||||
$resource = new CommentResource($comment);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -18,11 +18,14 @@ class MessageController extends Controller
|
||||
public function index(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$messages = Message::query()
|
||||
->where('receiver', $user->id)
|
||||
$query = $user->receive_messages()
|
||||
->with(['send_user'])
|
||||
->orderBy('id', 'desc')
|
||||
->paginate();
|
||||
->orderBy('id', 'desc');
|
||||
|
||||
if ($request->unread) {
|
||||
$query->where('unread', 'yes');
|
||||
}
|
||||
$messages = $query->paginate();
|
||||
$resource = MessageResource::collection($messages);
|
||||
return $this->success($resource);
|
||||
|
||||
@@ -49,6 +52,9 @@ class MessageController extends Controller
|
||||
{
|
||||
$message = Message::query()->with(['send_user'])->findOrFail($id);
|
||||
$resource = new MessageResource($message);
|
||||
$resource->additional([
|
||||
'page_title' => nexus_trans('message.show.page_title'),
|
||||
]);
|
||||
|
||||
return $this->success($resource);
|
||||
}
|
||||
@@ -75,4 +81,27 @@ class MessageController extends Controller
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
public function listUnread(Request $request): array
|
||||
{
|
||||
$user = Auth::user();
|
||||
$query = $user->receive_messages()
|
||||
->with(['send_user'])
|
||||
->orderBy('id', 'desc')
|
||||
->where('unread', 'yes');
|
||||
|
||||
$messages = $query->paginate();
|
||||
$resource = MessageResource::collection($messages);
|
||||
$resource->additional([
|
||||
'site_info' => site_info(),
|
||||
]);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
public function countUnread()
|
||||
{
|
||||
$user = Auth::user();
|
||||
$count = $user->receive_messages()->where('unread', 'yes')->count();
|
||||
return $this->success(['unread' => $count]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,4 +93,20 @@ class NewsController extends Controller
|
||||
$result = $this->repository->delete($id);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo update the unread cache
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function latest()
|
||||
{
|
||||
$result = News::query()->orderBy('id', 'desc')->first();
|
||||
$resource = new NewsResource($result);
|
||||
$resource->additional([
|
||||
'site_info' => site_info(),
|
||||
]);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,162 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\PollResource;
|
||||
use App\Models\Poll;
|
||||
use App\Repositories\PollRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class PollController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private function getRules(): array
|
||||
{
|
||||
return [
|
||||
'family_id' => 'required|numeric',
|
||||
'name' => 'required|string',
|
||||
'peer_id' => 'required|string',
|
||||
'agent' => 'required|string',
|
||||
'comment' => 'required|string',
|
||||
|
||||
];
|
||||
}
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$result = $this->repository->getList($request->all());
|
||||
$resource = PollResource::collection($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate($this->getRules());
|
||||
$result = $this->repository->store($request->all());
|
||||
$resource = new PollResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$result = Poll::query()->findOrFail($id);
|
||||
$resource = new PollResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$request->validate($this->getRules());
|
||||
$result = $this->repository->update($request->all(), $id);
|
||||
$resource = new PollResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
$result = $this->repository->delete($id);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo 弃权选项
|
||||
* @return array
|
||||
*/
|
||||
public function latest()
|
||||
{
|
||||
$user = Auth::user();
|
||||
$poll = Poll::query()->orderBy('id', 'desc')->first();
|
||||
$selection = null;
|
||||
$answerStats = [];
|
||||
if ($poll) {
|
||||
$baseAnswerQuery = $poll->answers()->where('selection', '<=', Poll::MAX_OPTION_INDEX);
|
||||
$poll->answers_count = (clone $baseAnswerQuery)->count();
|
||||
$answer = $poll->answers()->where('userid', $user->id)->first();
|
||||
$options = [];
|
||||
for ($i = 0; $i <= Poll::MAX_OPTION_INDEX; $i++) {
|
||||
$field = "option{$i}";
|
||||
$value = $poll->{$field};
|
||||
if ($value !== '') {
|
||||
$options[$i] = $value;
|
||||
}
|
||||
}
|
||||
if ($answer) {
|
||||
$selection = $answer->selection;
|
||||
} else {
|
||||
$options["255"] = "弃权(我想偷看结果!)";
|
||||
}
|
||||
$poll->options = $options;
|
||||
|
||||
$answerStats = (clone $baseAnswerQuery)
|
||||
->selectRaw("selection, count(*) as count")->groupBy("selection")
|
||||
->get()->pluck('count', 'selection')->toArray();
|
||||
foreach ($answerStats as $index => &$value) {
|
||||
$value = number_format(($value / $poll->answers_count) * 100, 2) . '%';
|
||||
}
|
||||
}
|
||||
|
||||
$resource = new PollResource($poll);
|
||||
$resource->additional([
|
||||
'selection' => $selection,
|
||||
'answer_stats' => $answerStats,
|
||||
'site_info' => site_info(),
|
||||
]);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
public function vote(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'poll_id' => 'required',
|
||||
'selection' => 'required|integer|min:0|max:255',
|
||||
]);
|
||||
$pollId = $request->poll_id;
|
||||
$selection = $request->selection;
|
||||
$user = Auth::user();
|
||||
$poll = Poll::query()->findOrFail($pollId);
|
||||
$data = [
|
||||
'userid' => $user->id,
|
||||
'selection' => $selection,
|
||||
];
|
||||
$answer = $poll->answers()->create($data);
|
||||
return $this->success($answer->toArray());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\RewardResource;
|
||||
use App\Http\Resources\PeerResource;
|
||||
use App\Http\Resources\SnatchResource;
|
||||
use App\Models\Peer;
|
||||
use App\Models\Snatch;
|
||||
use App\Repositories\RewardRepository;
|
||||
use App\Repositories\TorrentRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class RewardController extends Controller
|
||||
{
|
||||
private $repository;
|
||||
|
||||
public function __construct(RewardRepository $repository)
|
||||
{
|
||||
$this->repository = $repository;
|
||||
}
|
||||
/**
|
||||
* @param Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'torrent_id' => 'required',
|
||||
]);
|
||||
$result = $this->repository->getList($request->all());
|
||||
$resource = RewardResource::collection($result);
|
||||
$resource->additional([
|
||||
'page_title' => nexus_trans('reward.index.page_title'),
|
||||
]);
|
||||
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'torrent_id' => 'required',
|
||||
'value' => 'required',
|
||||
]);
|
||||
$result = $this->repository->store($request->torrent_id, $request->value, Auth::user());
|
||||
$resource = new RewardResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*
|
||||
* @param int $id
|
||||
* @return \Illuminate\Http\Response
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,9 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\ThankResource;
|
||||
use App\Models\Thank;
|
||||
use App\Models\Torrent;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ThankController extends Controller
|
||||
{
|
||||
@@ -37,7 +39,17 @@ class ThankController extends Controller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
//
|
||||
$request->validate(['torrent_id' => 'required']);
|
||||
$torrentId = $request->torrent_id;
|
||||
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
|
||||
$torrent->checkIsNormal();
|
||||
$user = Auth::user();
|
||||
if ($user->thank_torrent_logs()->where('torrentid', $torrentId)->exists()) {
|
||||
throw new \LogicException("you already thank this torrent");
|
||||
}
|
||||
$result = $user->thank_torrent_logs()->create(['torrentid' => $torrentId]);
|
||||
$resource = new ThankResource($result);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\RewardResource;
|
||||
use App\Http\Resources\TorrentResource;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
use App\Repositories\TorrentRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
@@ -51,16 +53,19 @@ class TorrentController extends Controller
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
|
||||
$result = $this->repository->getDetail($id, Auth::user());
|
||||
|
||||
$isBookmarked = Auth::user()->bookmarks()->where('torrentid', $id)->exists();
|
||||
/**
|
||||
* @var User
|
||||
*/
|
||||
$user = Auth::user();
|
||||
$result = $this->repository->getDetail($id, $user);
|
||||
$isBookmarked = $user->bookmarks()->where('torrentid', $id)->exists();
|
||||
|
||||
$resource = new TorrentResource($result);
|
||||
$resource->additional([
|
||||
'page_title' => nexus_trans('torrent.show.page_title'),
|
||||
'field_labels' => Torrent::getFieldLabels(),
|
||||
'is_bookmarked' => (int)$isBookmarked,
|
||||
'bonus_reward_values' => Torrent::BONUS_REWARD_VALUES,
|
||||
]);
|
||||
|
||||
return $this->success($resource);
|
||||
|
||||
@@ -19,6 +19,7 @@ class MessageResource extends JsonResource
|
||||
'subject' => $this->subject,
|
||||
'msg' => strip_all_tags($this->msg),
|
||||
'added_human' => $this->added->diffForHumans(),
|
||||
'added' => format_datetime($this->added),
|
||||
'send_user' => new UserResource($this->whenLoaded('send_user')),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -14,11 +14,13 @@ class NewsResource extends JsonResource
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$descriptionArr = format_description($this->body);
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'body' => $this->body,
|
||||
'added' => $this->added,
|
||||
'body' => $descriptionArr,
|
||||
'images' => get_image_from_description($descriptionArr),
|
||||
'added' => format_datetime($this->added, 'Y.m.d'),
|
||||
'user' => new UserResource($this->whenLoaded('user'))
|
||||
];
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class PollResource extends JsonResource
|
||||
{
|
||||
public $preserveKeys = true;
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
$out = [
|
||||
'id' => $this->id,
|
||||
'added' => format_datetime($this->added),
|
||||
'question' => $this->question,
|
||||
'answers_count' => $this->answers_count,
|
||||
'options' => $this->options,
|
||||
];
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Resources;
|
||||
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
class RewardResource extends JsonResource
|
||||
{
|
||||
/**
|
||||
* Transform the resource into an array.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @return array
|
||||
*/
|
||||
public function toArray($request)
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'user_id' => $this->userid,
|
||||
'torrent_id' => $this->torrentid,
|
||||
'value' => $this->value,
|
||||
'created_at' => format_datetime($this->created_at),
|
||||
'updated_at' => format_datetime($this->updated_at),
|
||||
'user' => new UserResource($this->whenLoaded('user'))
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,8 @@ class ThankResource extends JsonResource
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'torrent_id' => $this->torrentid,
|
||||
'user_id' => $this->userid,
|
||||
'user' => new UserResource($this->whenLoaded('user')),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ class TorrentResource extends JsonResource
|
||||
'user' => new UserResource($this->whenLoaded('user')),
|
||||
'basic_category' => new CategoryResource($this->whenLoaded('basic_category')),
|
||||
'tags' => TagResource::collection($this->whenLoaded('tags')),
|
||||
'thanks' => ThankResource::collection($this->whenLoaded('thanks')),
|
||||
'reward_logs' => RewardResource::collection($this->whenLoaded('reward_logs')),
|
||||
];
|
||||
$descriptionArr = format_description($this->descr);
|
||||
$out['cover'] = get_image_from_description($descriptionArr, true);
|
||||
@@ -63,6 +65,7 @@ class TorrentResource extends JsonResource
|
||||
|
||||
$out['thank_users_count'] = $this->thank_users_count;
|
||||
$out['peers_count'] = $this->peers_count;
|
||||
$out['reward_logs_count'] = $this->reward_logs_count;
|
||||
}
|
||||
// $out['upload_peers_count'] = $this->upload_peers_count;
|
||||
// $out['download_peers_count'] = $this->download_peers_count;
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class Comment extends NexusModel
|
||||
{
|
||||
protected $casts = [
|
||||
@@ -10,6 +12,43 @@ class Comment extends NexusModel
|
||||
'editdate' => 'datetime',
|
||||
];
|
||||
|
||||
protected $fillable = ['user', 'torrent', 'added', 'text', 'ori_text', 'editedby', 'editdate', 'offer', 'request', 'anonymous'];
|
||||
|
||||
const TYPE_TORRENT = 'torrent';
|
||||
const TYPE_REQUEST = 'request';
|
||||
const TYPE_OFFER = 'offer';
|
||||
|
||||
const TYPE_MAPS = [
|
||||
self::TYPE_TORRENT => [
|
||||
'model' => Torrent::class,
|
||||
'foreign_key' => 'torrent',
|
||||
'target_name_field' => 'name',
|
||||
'target_script' => 'details.php?id=%s'
|
||||
],
|
||||
self::TYPE_REQUEST => [
|
||||
'model' => Request::class,
|
||||
'foreign_key' => 'request',
|
||||
'target_name_field' => 'request',
|
||||
'target_script' => 'viewrequests.php?id=%s&req_details=1'
|
||||
],
|
||||
self::TYPE_OFFER => [
|
||||
'model' => Offer::class,
|
||||
'foreign_key' => 'offer',
|
||||
'target_name_field' => 'name',
|
||||
'target_script' => 'offers.php?id=%s&off_details=1'
|
||||
],
|
||||
];
|
||||
|
||||
public function scopeType(Builder $query, $type)
|
||||
{
|
||||
foreach (self::TYPE_MAPS as $key => $value) {
|
||||
if ($type != $key) {
|
||||
$query->where($value['foreign_key'], 0);
|
||||
}
|
||||
}
|
||||
return $query;
|
||||
}
|
||||
|
||||
public function related_torrent()
|
||||
{
|
||||
return $this->belongsTo(Torrent::class, 'torrent');
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
class Offer extends NexusModel
|
||||
{
|
||||
protected $fillable = ['userid', 'name', 'descr', 'comments', 'added'];
|
||||
|
||||
protected $casts = [
|
||||
'added' => 'datetime'
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'userid');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
class Poll extends NexusModel
|
||||
{
|
||||
protected $fillable = ['added', 'question', 'option0', 'option1', 'option2', 'option3', 'option4', 'option5'];
|
||||
|
||||
protected $casts = [
|
||||
'added' => 'datetime'
|
||||
];
|
||||
|
||||
const MAX_OPTION_INDEX = 19;
|
||||
|
||||
public function answers()
|
||||
{
|
||||
return $this->hasMany(PollAnswer::class, 'pollid');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
class PollAnswer extends NexusModel
|
||||
{
|
||||
protected $table = 'pollanswers';
|
||||
|
||||
protected $fillable = ['pollid', 'userid', 'selection',];
|
||||
|
||||
public function poll()
|
||||
{
|
||||
return $this->belongsTo(Poll::class, 'pollid');
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'userid');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
class Request extends NexusModel
|
||||
{
|
||||
protected $fillable = ['userid', 'request', 'descr', 'comments', 'hits', 'added'];
|
||||
|
||||
protected $casts = [
|
||||
'added' => 'datetime'
|
||||
];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'userid');
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
|
||||
class Reward extends NexusModel
|
||||
{
|
||||
protected $table = 'magic';
|
||||
|
||||
protected $fillable = ['torrentid', 'userid', 'value', ];
|
||||
|
||||
public $timestamps = true;
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'userid');
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,8 @@ namespace App\Models;
|
||||
|
||||
class Thank extends NexusModel
|
||||
{
|
||||
protected $fillable = ['torrentid', 'userid'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'userid');
|
||||
|
||||
+15
-35
@@ -28,6 +28,11 @@ class Torrent extends NexusModel
|
||||
'added' => 'datetime'
|
||||
];
|
||||
|
||||
public static $commentFields = [
|
||||
'id', 'name', 'added', 'visible', 'banned', 'owner', 'sp_state', 'pos_state', 'hr', 'picktype', 'picktime',
|
||||
'last_action', 'leechers', 'seeders', 'times_completed', 'views', 'size'
|
||||
];
|
||||
|
||||
public static $basicRelations = [
|
||||
'basic_category', 'basic_audio_codec', 'basic_codec', 'basic_media',
|
||||
'basic_source', 'basic_standard', 'basic_team',
|
||||
@@ -73,42 +78,9 @@ class Torrent extends NexusModel
|
||||
self::PROMOTION_ONE_THIRD_DOWN => ['text' => '30%', 'up_multiplier' => 1, 'down_multiplier' => 0.3],
|
||||
];
|
||||
|
||||
public function mappableAs(): array
|
||||
{
|
||||
return [
|
||||
'id' => 'long',
|
||||
'name' => [
|
||||
'type' => 'text',
|
||||
'analyzer' => 'ik_max_word',
|
||||
],
|
||||
'descr' => [
|
||||
'type' => 'text',
|
||||
'analyzer' => 'ik_max_word',
|
||||
],
|
||||
'owner' => 'long',
|
||||
'source' => 'long',
|
||||
'leechers' => 'long',
|
||||
'seeders' => 'long',
|
||||
'added' => 'date',
|
||||
'owner_info' => [
|
||||
const BONUS_REWARD_VALUES = [50, 100, 200, 500, 1000];
|
||||
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function toSearchableArray()
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'descr' => $this->descr,
|
||||
'owner' => $this->owner,
|
||||
'source' => $this->source,
|
||||
'leechers' => $this->leechers,
|
||||
'seeders' => $this->seeders,
|
||||
'added' => $this->added,
|
||||
];
|
||||
}
|
||||
|
||||
public function getSpStateRealTextAttribute()
|
||||
{
|
||||
@@ -185,7 +157,10 @@ class Torrent extends NexusModel
|
||||
|
||||
public static function getFieldLabels(): array
|
||||
{
|
||||
$fields = ['comments', 'times_completed', 'peers_count', 'thank_users_count', 'numfiles', 'bookmark_yes', 'bookmark_no'];
|
||||
$fields = [
|
||||
'comments', 'times_completed', 'peers_count', 'thank_users_count', 'numfiles', 'bookmark_yes', 'bookmark_no',
|
||||
'reward_yes', 'reward_no', 'reward_logs', 'download', 'thanks_yes', 'thanks_no'
|
||||
];
|
||||
$result = [];
|
||||
foreach($fields as $field) {
|
||||
$result[$field] = nexus_trans("torrent.show.{$field}_label");
|
||||
@@ -320,4 +295,9 @@ class Torrent extends NexusModel
|
||||
return $this->belongsToMany(Tag::class, 'torrent_tags', 'torrent_id', 'tag_id')
|
||||
->orderByRaw(sprintf("field(`tags`.`id`,%s)", TagRepository::getOrderByFieldIdString()));
|
||||
}
|
||||
|
||||
public function reward_logs(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Reward::class, 'torrentid');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -364,6 +364,21 @@ class User extends Authenticatable
|
||||
return $this->valid_medals()->where('user_medals.status', UserMedal::STATUS_WEARING);
|
||||
}
|
||||
|
||||
public function reward_torrent_logs(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Reward::class, 'userid');
|
||||
}
|
||||
|
||||
public function thank_torrent_logs(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(Thank::class, 'userid');
|
||||
}
|
||||
|
||||
public function poll_answers(): \Illuminate\Database\Eloquent\Relations\HasMany
|
||||
{
|
||||
return $this->hasMany(PollAnswer::class, 'userid');
|
||||
}
|
||||
|
||||
public function getAvatarAttribute($value)
|
||||
{
|
||||
if ($value) {
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Comment;
|
||||
use App\Models\Message;
|
||||
use App\Models\NexusModel;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
use Carbon\Carbon;
|
||||
use Hamcrest\Core\Set;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class CommentRepository extends BaseRepository
|
||||
{
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = Comment::query()->with(['create_user', 'update_user']);
|
||||
if (!empty($params['torrent_id'])) {
|
||||
$query->where('torrent', $params['torrent_id']);
|
||||
}
|
||||
if (!empty($params['offer_id'])) {
|
||||
$query->where('offer', $params['offer_id']);
|
||||
}
|
||||
if (!empty($params['request_id'])) {
|
||||
$query->where('request', $params['request_id']);
|
||||
}
|
||||
list($sortField, $sortType) = $this->getSortFieldAndType($params);
|
||||
$query->orderBy($sortField, $sortType);
|
||||
return $query->paginate();
|
||||
}
|
||||
|
||||
public function store(array $params, User $user)
|
||||
{
|
||||
$type = $params['type'];
|
||||
$modelName = Comment::TYPE_MAPS[$params['type']]['model'];
|
||||
/**
|
||||
* @var NexusModel $model
|
||||
*/
|
||||
$model = new $modelName;
|
||||
$target = $model->newQuery()->with('user')->find($params[$type]);
|
||||
return DB::transaction(function () use ($params, $user, $target) {
|
||||
$comment = $user->comments()->create($params);
|
||||
$commentCount = Comment::query()->type($params['type'])->count();
|
||||
$target->comments = $commentCount;
|
||||
$target->save();
|
||||
|
||||
$userUpdate = [
|
||||
'seedbonus' => DB::raw('seedbonus + ' . Setting::get('bonus.addcomment')),
|
||||
'last_comment' => Carbon::now(),
|
||||
];
|
||||
$user->update($userUpdate);
|
||||
|
||||
//message
|
||||
if ($target->user->commentpm == 'yes' && $user->id != $target->user->id) {
|
||||
$messageInfo = $this->getNoticeMessage($target, $params['type']);
|
||||
$insert = [
|
||||
'sender' => 0,
|
||||
'receiver' => $target->user->id,
|
||||
'subject' => $messageInfo['subject'],
|
||||
'msg' => $messageInfo['body'],
|
||||
'added' => Carbon::now()
|
||||
];
|
||||
Message::query()->insert($insert);
|
||||
NexusDB::cache_del('user_'.$target->user->id.'_unread_message_count');
|
||||
NexusDB::cache_del('user_'.$target->user->id.'_inbox_count');
|
||||
}
|
||||
|
||||
return $comment;
|
||||
});
|
||||
}
|
||||
|
||||
public function update(array $params, $id)
|
||||
{
|
||||
$model = Comment::query()->findOrFail($id);
|
||||
$model->update($params);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function getDetail($id)
|
||||
{
|
||||
$model = Comment::query()->findOrFail($id);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$model = Comment::query()->findOrFail($id);
|
||||
$result = $model->delete();
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function getNoticeMessage($target, $type): array
|
||||
{
|
||||
$allTrans = require_once base_path('lang/_target/lang_comment.php');
|
||||
$lang = $target->user->language->site_lang_folder ?? 'en';
|
||||
$trans = $allTrans[$lang];
|
||||
$subject = $trans['msg_new_comment'];
|
||||
$targetScript = Comment::TYPE_MAPS[$type]['target_script'];
|
||||
$targetNameField = Comment::TYPE_MAPS[$type]['target_name_field'];
|
||||
$body = sprintf(
|
||||
'%s [url="%s/%s"]%s[/url]',
|
||||
$trans[$type]['msg_torrent_receive_comment'],
|
||||
getSchemeAndHttpHost(),
|
||||
sprintf($targetScript, $target->id),
|
||||
$target->{$targetNameField}
|
||||
);
|
||||
return compact('subject', 'body');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Poll;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PollRepository extends BaseRepository
|
||||
{
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = Poll::query();
|
||||
list($sortField, $sortType) = $this->getSortFieldAndType($params);
|
||||
$query->orderBy($sortField, $sortType);
|
||||
return $query->paginate();
|
||||
}
|
||||
|
||||
public function store($torrentId, $value, User $user)
|
||||
{
|
||||
if ($user->seedbonus < $value) {
|
||||
throw new \LogicException("user bonus not enough.");
|
||||
}
|
||||
if ($user->reward_torrent_logs()->where('torrentid', $torrentId)->exists()) {
|
||||
throw new \LogicException("user already reward this torrent.");
|
||||
}
|
||||
$torrent = Torrent::query()->findOrFail($torrentId, ['owner']);
|
||||
$torrentOwner = User::query()->findOrFail($torrent->owner, ['id', 'seedbonus']);
|
||||
return DB::transaction(function () use ($torrentId, $value, $user, $torrentOwner) {
|
||||
$model = $user->reward_torrent_logs()->create([
|
||||
'torrentid' => $torrentId,
|
||||
'value' => $value,
|
||||
]);
|
||||
$affectedRows = $user->where('seedbonus', $user->seedbonus)->decrement('seedbonus', $value);
|
||||
if ($affectedRows != 1) {
|
||||
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
|
||||
throw new \RuntimeException("decrement user bonus fail.");
|
||||
}
|
||||
$affectedRows = $torrentOwner->where('seedbonus', $torrentOwner->seedbonus)->increment('seedbonus', $value);
|
||||
if ($affectedRows != 1) {
|
||||
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
|
||||
throw new \RuntimeException("increment owner bonus fail.");
|
||||
}
|
||||
return $model;
|
||||
});
|
||||
}
|
||||
|
||||
public function update(array $params, $id)
|
||||
{
|
||||
$model = Poll::query()->findOrFail($id);
|
||||
$model->update($params);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function getDetail($id)
|
||||
{
|
||||
$model = Poll::query()->findOrFail($id);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$model = Poll::query()->findOrFail($id);
|
||||
$result = $model->delete();
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function vote($selection, User $user)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
namespace App\Repositories;
|
||||
|
||||
use App\Models\Reward;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
use Google\Service\ToolResults\StepLabelsEntry;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class RewardRepository extends BaseRepository
|
||||
{
|
||||
public function getList(array $params)
|
||||
{
|
||||
$query = Reward::query()->with(['user']);
|
||||
if (!empty($params['torrent_id'])) {
|
||||
$query->where('torrentid', $params['torrent_id']);
|
||||
}
|
||||
list($sortField, $sortType) = $this->getSortFieldAndType($params);
|
||||
$query->orderBy($sortField, $sortType);
|
||||
return $query->paginate();
|
||||
}
|
||||
|
||||
public function store($torrentId, $value, User $user)
|
||||
{
|
||||
if ($user->seedbonus < $value) {
|
||||
throw new \LogicException("your bonus not enough.");
|
||||
}
|
||||
if ($user->reward_torrent_logs()->where('torrentid', $torrentId)->exists()) {
|
||||
throw new \LogicException("you already reward this torrent.");
|
||||
}
|
||||
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
|
||||
$torrent->checkIsNormal();
|
||||
$torrentOwner = User::query()->findOrFail($torrent->owner, ['id', 'seedbonus']);
|
||||
if ($user->id == $torrentOwner->id) {
|
||||
throw new \LogicException("you can't reward to yourself.");
|
||||
}
|
||||
return DB::transaction(function () use ($torrentId, $value, $user, $torrentOwner) {
|
||||
$model = $user->reward_torrent_logs()->create([
|
||||
'torrentid' => $torrentId,
|
||||
'value' => $value,
|
||||
]);
|
||||
$affectedRows = User::query()
|
||||
->where('id', $user->id)
|
||||
->where('seedbonus', $user->seedbonus)
|
||||
->decrement('seedbonus', $value);
|
||||
if ($affectedRows != 1) {
|
||||
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
|
||||
throw new \RuntimeException("decrement user bonus fail.");
|
||||
}
|
||||
$affectedRows = User::query()
|
||||
->where('id', $torrentOwner->id)
|
||||
->where('seedbonus', $torrentOwner->seedbonus)
|
||||
->increment('seedbonus', $value);
|
||||
if ($affectedRows != 1) {
|
||||
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
|
||||
throw new \RuntimeException("increment owner bonus fail.");
|
||||
}
|
||||
return $model;
|
||||
});
|
||||
}
|
||||
|
||||
public function update(array $params, $id)
|
||||
{
|
||||
$model = Reward::query()->findOrFail($id);
|
||||
$model->update($params);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function getDetail($id)
|
||||
{
|
||||
$model = Reward::query()->findOrFail($id);
|
||||
return $model;
|
||||
}
|
||||
|
||||
public function delete($id)
|
||||
{
|
||||
$model = Reward::query()->findOrFail($id);
|
||||
$result = $model->delete();
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -93,8 +93,12 @@ class TorrentRepository extends BaseRepository
|
||||
|
||||
public function getDetail($id, User $user)
|
||||
{
|
||||
$with = ['user', 'basic_audio_codec', 'basic_category', 'basic_codec', 'basic_media', 'basic_source', 'basic_standard', 'basic_team'];
|
||||
$result = Torrent::query()->with($with)->withCount(['peers', 'thank_users'])->visible()->findOrFail($id);
|
||||
$with = [
|
||||
'user', 'basic_audio_codec', 'basic_category', 'basic_codec', 'basic_media', 'basic_source', 'basic_standard', 'basic_team',
|
||||
'thanks' => function ($query) use ($user) {$query->where('userid', $user->id);},
|
||||
'reward_logs' => function ($query) use ($user) {$query->where('userid', $user->id);},
|
||||
];
|
||||
$result = Torrent::query()->with($with)->withCount(['peers', 'thank_users', 'reward_logs'])->visible()->findOrFail($id);
|
||||
$result->download_url = $this->getDownloadUrl($id, $user->toArray());
|
||||
return $result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user