[api] add notifications

This commit is contained in:
xiaomlove
2022-03-31 16:28:08 +08:00
parent 3e4a5766c4
commit f91cd6f20a
28 changed files with 274 additions and 44 deletions

View File

@@ -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");
}
}

View File

@@ -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'),

View File

@@ -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);
}

View File

@@ -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, '赠魔成功!');
}
/**

View File

@@ -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, '说谢谢成功!');
}
/**

View File

@@ -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);
}
}

View File

@@ -35,12 +35,14 @@ class TorrentResource extends JsonResource
'numfiles' => $this->numfiles,
'sp_state' => $this->sp_state,
'sp_state_real' => $this->sp_state_real,
'sp_state_real_text' => $this->spStateRealText,
'promotion_info' => $this->promotionInfo,
'hr' => $this->hr,
'pick_type' => $this->picktype,
'pick_time' => $this->picktime,
'pick_info' => $this->pickInfo,
'download_url' => $this->download_url,
'user' => new UserResource($this->whenLoaded('user')),
'anonymous' => $this->anonymous,
'basic_category' => new CategoryResource($this->whenLoaded('basic_category')),
'tags' => TagResource::collection($this->whenLoaded('tags')),
'thanks' => ThankResource::collection($this->whenLoaded('thanks')),

View File

@@ -39,11 +39,13 @@ class Comment extends NexusModel
],
];
public function scopeType(Builder $query, $type)
public function scopeType(Builder $query, string $type, int $typeValue)
{
foreach (self::TYPE_MAPS as $key => $value) {
if ($type != $key) {
$query->where($value['foreign_key'], 0);
} else {
$query->where($value['foreign_key'], $typeValue);
}
}
return $query;

View File

@@ -69,18 +69,77 @@ class Torrent extends NexusModel
const PROMOTION_ONE_THIRD_DOWN = 7;
public static array $promotionTypes = [
self::PROMOTION_NORMAL => ['text' => 'Normal', 'up_multiplier' => 1, 'down_multiplier' => 1],
self::PROMOTION_FREE => ['text' => 'Free', 'up_multiplier' => 1, 'down_multiplier' => 0],
self::PROMOTION_TWO_TIMES_UP => ['text' => '2X', 'up_multiplier' => 2, 'down_multiplier' => 1],
self::PROMOTION_FREE_TWO_TIMES_UP => ['text' => '2X Free', 'up_multiplier' => 2, 'down_multiplier' => 0],
self::PROMOTION_HALF_DOWN => ['text' => '50%', 'up_multiplier' => 1, 'down_multiplier' => 0.5],
self::PROMOTION_HALF_DOWN_TWO_TIMES_UP => ['text' => '2X 50%', 'up_multiplier' => 2, 'down_multiplier' => 0.5],
self::PROMOTION_ONE_THIRD_DOWN => ['text' => '30%', 'up_multiplier' => 1, 'down_multiplier' => 0.3],
self::PROMOTION_NORMAL => [
'text' => 'Normal',
'up_multiplier' => 1,
'down_multiplier' => 1,
'color' => ''
],
self::PROMOTION_FREE => [
'text' => 'Free',
'up_multiplier' => 1,
'down_multiplier' => 0,
'color' => 'linear-gradient(to right, rgba(0,52,206,0.5), rgba(0,52,206,1), rgba(0,52,206,0.5))'
],
self::PROMOTION_TWO_TIMES_UP => [
'text' => '2X',
'up_multiplier' => 2,
'down_multiplier' => 1,
'color' => 'linear-gradient(to right, rgba(0,153,0,0.5), rgba(0,153,0,1), rgba(0,153,0,0.5))'
],
self::PROMOTION_FREE_TWO_TIMES_UP => [
'text' => '2X Free',
'up_multiplier' => 2,
'down_multiplier' => 0,
'color' => 'linear-gradient(to right, rgba(0,153,0,1), rgba(0,52,206,1)'
],
self::PROMOTION_HALF_DOWN => [
'text' => '50%',
'up_multiplier' => 1,
'down_multiplier' => 0.5,
'color' => 'linear-gradient(to right, rgba(220,0,3,0.5), rgba(220,0,3,1), rgba(220,0,3,0.5))'
],
self::PROMOTION_HALF_DOWN_TWO_TIMES_UP => [
'text' => '2X 50%',
'up_multiplier' => 2,
'down_multiplier' => 0.5,
'color' => 'linear-gradient(to right, rgba(0,153,0,1), rgba(220,0,3,1)'
],
self::PROMOTION_ONE_THIRD_DOWN => [
'text' => '30%',
'up_multiplier' => 1,
'down_multiplier' => 0.3,
'color' => 'linear-gradient(to right, rgba(65,23,73,0.5), rgba(65,23,73,1), rgba(65,23,73,0.5))'
],
];
const PICK_NORMAL = 'normal';
const PICK_HOT = 'hot';
const PICK_CLASSIC = 'classic';
const PICK_RECOMMENDED = 'recommended';
public static array $pickTypes = [
self::PICK_NORMAL => ['text' => self::PICK_NORMAL, 'color' => ''],
self::PICK_HOT => ['text' => self::PICK_HOT, 'color' => '#e78d0f'],
self::PICK_CLASSIC => ['text' => self::PICK_CLASSIC, 'color' => '#77b300'],
self::PICK_RECOMMENDED => ['text' => self::PICK_RECOMMENDED, 'color' => '#820084'],
];
const BONUS_REWARD_VALUES = [50, 100, 200, 500, 1000];
public function getPickInfoAttribute()
{
$info = self::$pickTypes[$this->picktype] ?? null;
if ($info) {
$info['text'] = nexus_trans('torrent.pick_info.' . $this->picktype);
return $info;
}
}
public function getPromotionInfoAttribute()
{
return self::$promotionTypes[$this->sp_state_real] ?? null;
}
public function getSpStateRealTextAttribute()
{

View File

@@ -4,6 +4,7 @@ namespace App\Repositories;
use App\Exceptions\ClientNotAllowedException;
use App\Models\AgentAllow;
use App\Models\AgentDeny;
use Nexus\Database\NexusDB;
class AgentAllowRepository extends BaseRepository
{
@@ -72,10 +73,12 @@ class AgentAllowRepository extends BaseRepository
public function checkClient($peerId, $agent, $debug = false)
{
//check from high version to low version, if high version allow, stop!
$allows = AgentAllow::query()
->orderBy('peer_id_start', 'desc')
->orderBy('agent_start', 'desc')
->get();
$allows = NexusDB::remember("all_agent_allows", 600, function () {
return AgentAllow::query()
->orderBy('peer_id_start', 'desc')
->orderBy('agent_start', 'desc')
->get();
});
$agentAllowPassed = null;
$versionTooLowStr = '';
foreach ($allows as $agentAllow) {

View File

@@ -75,8 +75,8 @@ class AttendanceRepository extends BaseRepository
->where('uid', $uid)
->orderBy('id', 'desc');
if (!empty($date)) {
$query->where('added', '>=', Carbon::today())
->where('added', '<', Carbon::tomorrow());
$query->where('added', '>=', Carbon::parse($date)->startOfDay())
->where('added', '<=', Carbon::parse($date)->endOfDay());
}
return $query->first();
}

View File

@@ -41,8 +41,9 @@ class CommentRepository extends BaseRepository
$model = new $modelName;
$target = $model->newQuery()->with('user')->find($params[$type]);
return DB::transaction(function () use ($params, $user, $target) {
$params['added'] = Carbon::now();
$comment = $user->comments()->create($params);
$commentCount = Comment::query()->type($params['type'])->count();
$commentCount = Comment::query()->type($params['type'], $params[$params['type']])->count();
$target->comments = $commentCount;
$target->save();
@@ -60,7 +61,7 @@ class CommentRepository extends BaseRepository
'receiver' => $target->user->id,
'subject' => $messageInfo['subject'],
'msg' => $messageInfo['body'],
'added' => Carbon::now()
'added' => $params['added'],
];
Message::query()->insert($insert);
NexusDB::cache_del('user_'.$target->user->id.'_unread_message_count');
@@ -100,9 +101,8 @@ class CommentRepository extends BaseRepository
$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(),
'%s [url=%s]%s[/url]',
$trans['msg_torrent_receive_comment'],
sprintf($targetScript, $target->id),
$target->{$targetNameField}
);

View File

@@ -30,10 +30,11 @@ class RewardRepository extends BaseRepository
}
$torrent = Torrent::query()->findOrFail($torrentId, Torrent::$commentFields);
$torrent->checkIsNormal();
$torrentOwner = User::query()->findOrFail($torrent->owner, ['id', 'seedbonus']);
$torrentOwner = User::query()->findOrFail($torrent->owner);
if ($user->id == $torrentOwner->id) {
throw new \LogicException("you can't reward to yourself.");
}
$torrentOwner->checkIsNormal();
return DB::transaction(function () use ($torrentId, $value, $user, $torrentOwner) {
$model = $user->reward_torrent_logs()->create([
'torrentid' => $torrentId,

View File

@@ -1,7 +1,12 @@
<?php
namespace App\Repositories;
use App\Models\Message;
use App\Models\News;
use App\Models\Poll;
use App\Models\PollAnswer;
use App\Models\Setting;
use App\Models\User;
use Illuminate\Encryption\Encrypter;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
@@ -192,4 +197,28 @@ class ToolRepository extends BaseRepository
return false;
}
}
public function getNotificationCount(User $user): array
{
$result = [];
//attend or not
$attendRep = new AttendanceRepository();
$attendance = $attendRep->getAttendance($user->id, date('Ymd'));
$result['attendance'] = $attendance ? 0 : 1;
//unread news
$count = News::query()->where('added', '>', $user->last_home)->count();
$result['news'] = $count;
//unread messages
$count = Message::query()->where('receiver', $user->id)->where('unread', 'yes')->count();
$result['message'] = $count;
//un-vote poll
$total = Poll::query()->count();
$userVoteCount = PollAnswer::query()->where('userid', $user->id)->selectRaw('count(distinct(pollid)) as counts')->first()->counts;
$result['poll'] = $total - $userVoteCount;
return $result;
}
}

View File

@@ -24,6 +24,7 @@ use Carbon\Carbon;
use Hashids\Hashids;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use Nexus\Database\NexusDB;
class TorrentRepository extends BaseRepository
{
@@ -372,12 +373,14 @@ class TorrentRepository extends BaseRepository
private function getTrackerReportAuthKeySecret($id, $uid, $initializeIfNotExists = false)
{
$secret = TorrentSecret::query()
->where('uid', $uid)
->whereIn('torrent_id', [0, $id])
->orderBy('torrent_id', 'desc')
->orderBy('id', 'desc')
->first();
$secret = NexusDB::remember("tracker_report_authkey_secret:$id:$uid", 3600*24, function () use ($id, $uid) {
return TorrentSecret::query()
->where('uid', $uid)
->whereIn('torrent_id', [0, $id])
->orderBy('torrent_id', 'desc')
->orderBy('id', 'desc')
->first();
});
if ($secret) {
return $secret->secret;
}

View File

@@ -104,7 +104,7 @@ class TrackerRepository extends BaseRepository
$repDict = $this->generateFailedAnnounceResponse($exception->getMessage());
} catch (TrackerException $exception) {
$repDict = $this->generateFailedAnnounceResponse($exception->getMessage());
} catch (\Exception $exception) {
} catch (\Throwable $exception) {
//other system exception
do_log("[" . get_class($exception) . "] " . $exception->getMessage() . "\n" . $exception->getTraceAsString(), 'error');
$repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId());
@@ -806,7 +806,7 @@ class TrackerRepository extends BaseRepository
$repDict = $this->generateFailedAnnounceResponse($exception->getMessage());
} catch (TrackerException $exception) {
$repDict = $this->generateFailedAnnounceResponse($exception->getMessage());
} catch (\Exception $exception) {
} catch (\Throwable $exception) {
//other system exception
do_log("[" . get_class($exception) . "] " . $exception->getMessage() . "\n" . $exception->getTraceAsString(), 'error');
$repDict = $this->generateFailedAnnounceResponse("system error, report to sysop please, hint: " . nexus()->getRequestId());