From 3e4a5766c4a6c63849e81d19c32e31a585e2ecd0 Mon Sep 17 00:00:00 2001 From: xiaomlove Date: Wed, 30 Mar 2022 15:37:11 +0800 Subject: [PATCH] [api] reward + thanks --- app/Exceptions/Handler.php | 10 +- app/Http/Controllers/CommentController.php | 45 ++++- app/Http/Controllers/MessageController.php | 37 +++- app/Http/Controllers/NewsController.php | 16 ++ app/Http/Controllers/PollController.php | 162 ++++++++++++++++++ app/Http/Controllers/RewardController.php | 92 ++++++++++ app/Http/Controllers/ThankController.php | 14 +- app/Http/Controllers/TorrentController.php | 13 +- app/Http/Resources/MessageResource.php | 1 + app/Http/Resources/NewsResource.php | 6 +- app/Http/Resources/PollResource.php | 28 +++ app/Http/Resources/RewardResource.php | 27 +++ app/Http/Resources/ThankResource.php | 2 + app/Http/Resources/TorrentResource.php | 3 + app/Models/Comment.php | 39 +++++ app/Models/Offer.php | 19 ++ app/Models/Poll.php | 21 +++ app/Models/PollAnswer.php | 22 +++ app/Models/Request.php | 19 ++ app/Models/Reward.php | 18 ++ app/Models/Thank.php | 2 + app/Models/Torrent.php | 50 ++---- app/Models/User.php | 15 ++ app/Repositories/CommentRepository.php | 111 ++++++++++++ app/Repositories/PollRepository.php | 72 ++++++++ app/Repositories/RewardRepository.php | 81 +++++++++ app/Repositories/TorrentRepository.php | 8 +- config/cache.php | 6 +- .../2021_06_08_113437_create_thanks_table.php | 5 +- include/core.php | 7 +- include/globalfunctions.php | 10 ++ lang/_target/lang_comment.php | 1 + nexus/Database/NexusDB.php | 45 +++++ public/details.php | 2 +- resources/lang/zh_CN/message.php | 11 ++ resources/lang/zh_CN/reward.php | 11 ++ resources/lang/zh_CN/torrent.php | 8 +- routes/api.php | 8 + 38 files changed, 983 insertions(+), 64 deletions(-) create mode 100644 app/Http/Controllers/PollController.php create mode 100644 app/Http/Controllers/RewardController.php create mode 100644 app/Http/Resources/PollResource.php create mode 100644 app/Http/Resources/RewardResource.php create mode 100644 app/Models/Offer.php create mode 100644 app/Models/Poll.php create mode 100644 app/Models/PollAnswer.php create mode 100644 app/Models/Request.php create mode 100644 app/Models/Reward.php create mode 100644 app/Repositories/CommentRepository.php create mode 100644 app/Repositories/PollRepository.php create mode 100644 app/Repositories/RewardRepository.php create mode 100644 resources/lang/zh_CN/message.php create mode 100644 resources/lang/zh_CN/reward.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 1460d30d..dae8ae91 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -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)) { diff --git a/app/Http/Controllers/CommentController.php b/app/Http/Controllers/CommentController.php index d1f42218..8f63b456 100644 --- a/app/Http/Controllers/CommentController.php +++ b/app/Http/Controllers/CommentController.php @@ -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); } /** diff --git a/app/Http/Controllers/MessageController.php b/app/Http/Controllers/MessageController.php index 0ddb04a3..184ea1fd 100644 --- a/app/Http/Controllers/MessageController.php +++ b/app/Http/Controllers/MessageController.php @@ -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]); + } } diff --git a/app/Http/Controllers/NewsController.php b/app/Http/Controllers/NewsController.php index 501373b4..7961caee 100644 --- a/app/Http/Controllers/NewsController.php +++ b/app/Http/Controllers/NewsController.php @@ -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); + } + } diff --git a/app/Http/Controllers/PollController.php b/app/Http/Controllers/PollController.php new file mode 100644 index 00000000..8e4ed42e --- /dev/null +++ b/app/Http/Controllers/PollController.php @@ -0,0 +1,162 @@ + '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()); + } + +} diff --git a/app/Http/Controllers/RewardController.php b/app/Http/Controllers/RewardController.php new file mode 100644 index 00000000..4af1028c --- /dev/null +++ b/app/Http/Controllers/RewardController.php @@ -0,0 +1,92 @@ +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) + { + // + } +} diff --git a/app/Http/Controllers/ThankController.php b/app/Http/Controllers/ThankController.php index ba5e2916..a2d07ec6 100644 --- a/app/Http/Controllers/ThankController.php +++ b/app/Http/Controllers/ThankController.php @@ -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); } /** diff --git a/app/Http/Controllers/TorrentController.php b/app/Http/Controllers/TorrentController.php index bff69301..b6531bf4 100644 --- a/app/Http/Controllers/TorrentController.php +++ b/app/Http/Controllers/TorrentController.php @@ -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); diff --git a/app/Http/Resources/MessageResource.php b/app/Http/Resources/MessageResource.php index 730541fa..5693fdcd 100644 --- a/app/Http/Resources/MessageResource.php +++ b/app/Http/Resources/MessageResource.php @@ -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')), ]; } diff --git a/app/Http/Resources/NewsResource.php b/app/Http/Resources/NewsResource.php index ac5da67b..ec54040f 100644 --- a/app/Http/Resources/NewsResource.php +++ b/app/Http/Resources/NewsResource.php @@ -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')) ]; } diff --git a/app/Http/Resources/PollResource.php b/app/Http/Resources/PollResource.php new file mode 100644 index 00000000..2c7ae011 --- /dev/null +++ b/app/Http/Resources/PollResource.php @@ -0,0 +1,28 @@ + $this->id, + 'added' => format_datetime($this->added), + 'question' => $this->question, + 'answers_count' => $this->answers_count, + 'options' => $this->options, + ]; + + return $out; + } +} diff --git a/app/Http/Resources/RewardResource.php b/app/Http/Resources/RewardResource.php new file mode 100644 index 00000000..9f851c7f --- /dev/null +++ b/app/Http/Resources/RewardResource.php @@ -0,0 +1,27 @@ + $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')) + ]; + } +} diff --git a/app/Http/Resources/ThankResource.php b/app/Http/Resources/ThankResource.php index 9e0dae41..3ac52951 100644 --- a/app/Http/Resources/ThankResource.php +++ b/app/Http/Resources/ThankResource.php @@ -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')), ]; } diff --git a/app/Http/Resources/TorrentResource.php b/app/Http/Resources/TorrentResource.php index d2baf86d..435ff0b8 100644 --- a/app/Http/Resources/TorrentResource.php +++ b/app/Http/Resources/TorrentResource.php @@ -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; diff --git a/app/Models/Comment.php b/app/Models/Comment.php index b7df09b8..f560e4cd 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -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'); diff --git a/app/Models/Offer.php b/app/Models/Offer.php new file mode 100644 index 00000000..a8ead875 --- /dev/null +++ b/app/Models/Offer.php @@ -0,0 +1,19 @@ + 'datetime' + ]; + + public function user() + { + return $this->belongsTo(User::class, 'userid'); + } + +} diff --git a/app/Models/Poll.php b/app/Models/Poll.php new file mode 100644 index 00000000..cbe74b1a --- /dev/null +++ b/app/Models/Poll.php @@ -0,0 +1,21 @@ + 'datetime' + ]; + + const MAX_OPTION_INDEX = 19; + + public function answers() + { + return $this->hasMany(PollAnswer::class, 'pollid'); + } + +} diff --git a/app/Models/PollAnswer.php b/app/Models/PollAnswer.php new file mode 100644 index 00000000..dc55260c --- /dev/null +++ b/app/Models/PollAnswer.php @@ -0,0 +1,22 @@ +belongsTo(Poll::class, 'pollid'); + } + + public function user() + { + return $this->belongsTo(User::class, 'userid'); + } + +} diff --git a/app/Models/Request.php b/app/Models/Request.php new file mode 100644 index 00000000..4a690510 --- /dev/null +++ b/app/Models/Request.php @@ -0,0 +1,19 @@ + 'datetime' + ]; + + public function user() + { + return $this->belongsTo(User::class, 'userid'); + } + +} diff --git a/app/Models/Reward.php b/app/Models/Reward.php new file mode 100644 index 00000000..d3a85ab0 --- /dev/null +++ b/app/Models/Reward.php @@ -0,0 +1,18 @@ +belongsTo(User::class, 'userid'); + } +} diff --git a/app/Models/Thank.php b/app/Models/Thank.php index ec910325..bce01164 100644 --- a/app/Models/Thank.php +++ b/app/Models/Thank.php @@ -5,6 +5,8 @@ namespace App\Models; class Thank extends NexusModel { + protected $fillable = ['torrentid', 'userid']; + public function user() { return $this->belongsTo(User::class, 'userid'); diff --git a/app/Models/Torrent.php b/app/Models/Torrent.php index e3dacddd..d1d360e4 100644 --- a/app/Models/Torrent.php +++ b/app/Models/Torrent.php @@ -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'); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 19e60aa0..0164b007 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -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) { diff --git a/app/Repositories/CommentRepository.php b/app/Repositories/CommentRepository.php new file mode 100644 index 00000000..c2527c0a --- /dev/null +++ b/app/Repositories/CommentRepository.php @@ -0,0 +1,111 @@ +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'); + } +} diff --git a/app/Repositories/PollRepository.php b/app/Repositories/PollRepository.php new file mode 100644 index 00000000..c2ba1800 --- /dev/null +++ b/app/Repositories/PollRepository.php @@ -0,0 +1,72 @@ +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) + { + + } +} diff --git a/app/Repositories/RewardRepository.php b/app/Repositories/RewardRepository.php new file mode 100644 index 00000000..8f88c9fe --- /dev/null +++ b/app/Repositories/RewardRepository.php @@ -0,0 +1,81 @@ +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; + } +} diff --git a/app/Repositories/TorrentRepository.php b/app/Repositories/TorrentRepository.php index caf75f9c..30fc5811 100644 --- a/app/Repositories/TorrentRepository.php +++ b/app/Repositories/TorrentRepository.php @@ -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; } diff --git a/config/cache.php b/config/cache.php index e32a2fd3..1b684eb2 100644 --- a/config/cache.php +++ b/config/cache.php @@ -15,7 +15,7 @@ return [ | */ - 'default' => env('CACHE_DRIVER', 'file'), + 'default' => 'redis', /* |-------------------------------------------------------------------------- @@ -75,7 +75,7 @@ return [ 'redis' => [ 'driver' => 'redis', - 'connection' => 'cache', + 'connection' => 'default', 'lock_connection' => 'default', ], @@ -101,6 +101,6 @@ return [ | */ - 'prefix' => env('CACHE_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_cache'), + 'prefix' => '', ]; diff --git a/database/migrations/2021_06_08_113437_create_thanks_table.php b/database/migrations/2021_06_08_113437_create_thanks_table.php index 1e8dec14..36e768a9 100644 --- a/database/migrations/2021_06_08_113437_create_thanks_table.php +++ b/database/migrations/2021_06_08_113437_create_thanks_table.php @@ -19,9 +19,8 @@ class CreateThanksTable extends Migration Schema::create('thanks', function (Blueprint $table) { $table->increments('id'); $table->unsignedMediumInteger('torrentid')->default(0); - $table->unsignedMediumInteger('userid')->default(0); - $table->unique(['torrentid', 'id'], 'torrentid_id'); - $table->index(['torrentid', 'userid'], 'torrentid_userid'); + $table->unsignedMediumInteger('userid')->default(0)->index(); + $table->unique(['torrentid', 'userid']); }); } diff --git a/include/core.php b/include/core.php index b97a428c..74878cbb 100644 --- a/include/core.php +++ b/include/core.php @@ -16,14 +16,13 @@ require $rootpath . 'include/eloquent.php'; ini_set('date.timezone', nexus_config('nexus.timezone')); ini_set('error_reporting', E_ALL); ini_set('display_errors', 0); - +$Cache = new class_cache_redis(); //Load the caching class +$Cache->setLanguageFolderArray(get_langfolder_list()); +require $rootpath . 'include/config.php'; if (!in_array(nexus()->getScript(), ['announce', 'scrape'])) { require $rootpath . get_langfile_path("functions.php"); checkGuestVisit(); } -$Cache = new class_cache_redis(); //Load the caching class -$Cache->setLanguageFolderArray(get_langfolder_list()); -require $rootpath . 'include/config.php'; define('TIMENOW', time()); $USERUPDATESET = array(); diff --git a/include/globalfunctions.php b/include/globalfunctions.php index 26782ae0..0ddddf81 100644 --- a/include/globalfunctions.php +++ b/include/globalfunctions.php @@ -676,3 +676,13 @@ function nexus() { return \Nexus\Nexus::instance(); } + +function site_info() +{ + $setting = \App\Models\Setting::get('basic'); + $siteInfo = [ + 'site_name' => $setting['SITENAME'], + 'base_url' => getSchemeAndHttpHost(), + ]; + return $siteInfo; +} diff --git a/lang/_target/lang_comment.php b/lang/_target/lang_comment.php index a9b967da..8addbb38 100644 --- a/lang/_target/lang_comment.php +++ b/lang/_target/lang_comment.php @@ -34,4 +34,5 @@ $lang_comment_target = array ), ); +return $lang_comment_target; ?> diff --git a/nexus/Database/NexusDB.php b/nexus/Database/NexusDB.php index e04521f2..8b1f6f18 100644 --- a/nexus/Database/NexusDB.php +++ b/nexus/Database/NexusDB.php @@ -6,6 +6,7 @@ use Illuminate\Database\Capsule\Manager as Capsule; use Illuminate\Database\Query\Expression; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Redis; class NexusDB { @@ -286,6 +287,50 @@ class NexusDB } } + public static function cache_put($key, $value, $ttl = 3600) + { + if (IN_NEXUS) { + global $Cache; + return $Cache->cache_value($key, $value, $ttl); + } else { + return Cache::put($key, $value, $ttl); + } + } + + public static function cache_get($key) + { + if (IN_NEXUS) { + global $Cache; + return $Cache->get_value($key); + } else { + return Cache::get($key); + } + } + + public static function cache_del($key) + { + if (IN_NEXUS) { + global $Cache; + $Cache->delete_value($key, true); + } else { + Cache::forget($key); + $langList = get_langfolder_list(); + foreach ($langList as $lf) { + Cache::forget($lf . '_' . $key); + } + } + } + + public static function redis() + { + if (IN_NEXUS) { + global $Cache; + $Cache->getRedis(); + } else { + Redis::connection()->client(); + } + } + public static function getMysqlColumnInfo($table, $column) { static $driver; diff --git a/public/details.php b/public/details.php index 7b71b95a..67fd3432 100644 --- a/public/details.php +++ b/public/details.php @@ -413,7 +413,7 @@ echo ""; if(isset($magic_value_bonus)){ $bonus_array = $magic_value_bonus; }else{ - $bonus_array = '50 , 100 , 200 , 500, 1000'; + $bonus_array = implode(',', \App\Models\Torrent::BONUS_REWARD_VALUES); } echo '