mirror of
https://github.com/lkddi/nexusphp.git
synced 2026-04-16 14:00:56 +08:00
[api] add notifications
This commit is contained in:
@@ -61,7 +61,7 @@ class CommentController extends Controller
|
||||
];
|
||||
$data = array_filter($data);
|
||||
foreach ($allTypes as $type) {
|
||||
if ($data['type'] == $type && empty($data[$type . "_id"])) {
|
||||
if ($data['type'] == $type && empty($data[$type])) {
|
||||
throw new \InvalidArgumentException("require {$type}_id");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ class MessageController extends Controller
|
||||
public function show($id)
|
||||
{
|
||||
$message = Message::query()->with(['send_user'])->findOrFail($id);
|
||||
$message->update(['unread' => 'no']);
|
||||
$resource = new MessageResource($message);
|
||||
$resource->additional([
|
||||
'page_title' => nexus_trans('message.show.page_title'),
|
||||
|
||||
@@ -5,8 +5,11 @@ namespace App\Http\Controllers;
|
||||
use App\Http\Resources\NewsResource;
|
||||
use App\Models\News;
|
||||
use App\Repositories\NewsRepository;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Nexus\Database\NexusDB;
|
||||
|
||||
class NewsController extends Controller
|
||||
{
|
||||
@@ -95,17 +98,21 @@ class NewsController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @todo update the unread cache
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function latest()
|
||||
{
|
||||
$user = Auth::user();
|
||||
$result = News::query()->orderBy('id', 'desc')->first();
|
||||
$resource = new NewsResource($result);
|
||||
$resource->additional([
|
||||
'site_info' => site_info(),
|
||||
]);
|
||||
/**
|
||||
* Visiting the home page is the same as viewing the latest news
|
||||
* @see functions.php line 2590
|
||||
*/
|
||||
$user->update(['last_home' => Carbon::now()]);
|
||||
return $this->success($resource);
|
||||
}
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ class RewardController extends Controller
|
||||
]);
|
||||
$result = $this->repository->store($request->torrent_id, $request->value, Auth::user());
|
||||
$resource = new RewardResource($result);
|
||||
return $this->success($resource);
|
||||
return $this->success($resource, '赠魔成功!');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,10 +3,13 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Resources\ThankResource;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Thank;
|
||||
use App\Models\Torrent;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class ThankController extends Controller
|
||||
{
|
||||
@@ -39,17 +42,48 @@ class ThankController extends Controller
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
$user = Auth::user();
|
||||
$request->validate(['torrent_id' => 'required']);
|
||||
$torrentId = $request->torrent_id;
|
||||
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
|
||||
$torrent->checkIsNormal();
|
||||
$user = Auth::user();
|
||||
$torrentOwner = User::query()->findOrFail($torrent->owner);
|
||||
if ($user->id == $torrentOwner->id) {
|
||||
throw new \LogicException("you can't thank to yourself");
|
||||
}
|
||||
$torrentOwner->checkIsNormal();
|
||||
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]);
|
||||
|
||||
$result = DB::transaction(function () use ($user, $torrentOwner, $torrent) {
|
||||
$thank = $user->thank_torrent_logs()->create(['torrentid' => $torrent->id]);
|
||||
$sayThanksBonus = Setting::get('bonus.saythanks');
|
||||
$receiveThanksBonus = Setting::get('bonus.receivethanks');
|
||||
if ($sayThanksBonus > 0) {
|
||||
$affectedRows = User::query()
|
||||
->where('id', $user->id)
|
||||
->where('seedbonus', $user->seedbonus)
|
||||
->increment('seedbonus', $sayThanksBonus);
|
||||
if ($affectedRows != 1) {
|
||||
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
|
||||
throw new \RuntimeException("increment user bonus fail.");
|
||||
}
|
||||
}
|
||||
if ($receiveThanksBonus > 0) {
|
||||
$affectedRows = User::query()
|
||||
->where('id', $torrentOwner->id)
|
||||
->where('seedbonus', $torrentOwner->seedbonus)
|
||||
->increment('seedbonus', $receiveThanksBonus);
|
||||
if ($affectedRows != 1) {
|
||||
do_log("affectedRows: $affectedRows, query: " . last_query(), 'error');
|
||||
throw new \RuntimeException("increment owner bonus fail.");
|
||||
}
|
||||
}
|
||||
return $thank;
|
||||
});
|
||||
$resource = new ThankResource($result);
|
||||
return $this->success($resource);
|
||||
return $this->success($resource, '说谢谢成功!');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Repositories\ToolRepository;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class ToolController extends Controller
|
||||
{
|
||||
@@ -14,4 +15,11 @@ class ToolController extends Controller
|
||||
$this->repository = $repository;
|
||||
}
|
||||
|
||||
public function notifications(): array
|
||||
{
|
||||
$user = Auth::user();
|
||||
$result = $this->repository->getNotificationCount($user);
|
||||
return $this->success($result);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user