diff --git a/README.md b/README.md index f06cc1a..bb9dae2 100644 --- a/README.md +++ b/README.md @@ -146,7 +146,7 @@ Some rule or Docs May help you when you rebuild this project, Or you can join our chat group on Telegram -- [@ridpt](https://t.me/ridpt) | Name | Used As | Docs | -|:--|:--:|:--:|:--:| +|:--|:--:|:--:| | [MixPHP](https://github.com/mix-php/mix-framework/tree/v1) | Framework | https://www.kancloud.cn/onanying/mixphp1/379324 ( Chinese Version ) | | [siriusphp/validation](https://github.com/siriusphp/validation) | Validator | http://www.sirius.ro/php/sirius/validation/ | | [league/plates](https://github.com/thephpleague/plates) | Template system | http://platesphp.com/ | diff --git a/apps/config/http_base.php b/apps/config/http_base.php index 78a3b8b..5fbc074 100644 --- a/apps/config/http_base.php +++ b/apps/config/http_base.php @@ -36,12 +36,19 @@ 'rules' => [ 'GET tracker/{tracker_action}' => ['tracker','index'], 'GET captcha' => ['captcha', 'index'], + + // Auth By Passkey Route 'GET rss' => ['rss', 'index','middleware' => [ apps\middleware\AuthByPasskeyMiddleware::class ]], - 'api/v1/{controller}/{action}' => ['api/{controller}', '{action}', 'middleware' => [ - apps\middleware\ApiMiddleware::class + + // API version 1 + 'api/v1/{controller}/{action}' => ['api/v1/{controller}', '{action}', 'middleware' => [ + apps\middleware\ApiMiddleware::class, + apps\middleware\AuthByCookiesMiddleware::class ]], + + // Web view '{controller}/{action}' => ['{controller}', '{action}', 'middleware' => [ apps\middleware\AuthByCookiesMiddleware::class ]], diff --git a/apps/controllers/api/IndexController.php b/apps/controllers/api/IndexController.php deleted file mode 100644 index bbef352..0000000 --- a/apps/controllers/api/IndexController.php +++ /dev/null @@ -1,18 +0,0 @@ - "world"]; - } -} diff --git a/apps/controllers/api/v1/TorrentsController.php b/apps/controllers/api/v1/TorrentsController.php new file mode 100644 index 0000000..58d0577 --- /dev/null +++ b/apps/controllers/api/v1/TorrentsController.php @@ -0,0 +1,37 @@ +checkMethod('POST')) { + $bookmark = new TorrentsBookmarkForm(); + $bookmark->setData(app()->request->post()); + $success = $bookmark->validate(); + if (!$success) { + return [ + 'success' => false, + 'errors' => $bookmark->getErrors() + ]; + } else { + $ret = $bookmark->updateRecord(); + return array_merge( + ['success' => true], + $ret + ); + } + } else { + return $this->buildMethodFailMsg('POST'); + } + } +} diff --git a/apps/middleware/ApiMiddleware.php b/apps/middleware/ApiMiddleware.php index bedc290..6ad8dff 100644 --- a/apps/middleware/ApiMiddleware.php +++ b/apps/middleware/ApiMiddleware.php @@ -14,9 +14,17 @@ class ApiMiddleware public function handle($callable, \Closure $next) { app()->response->format = \Rid\Http\Response::FORMAT_JSON; + + // No cache for api response + app()->response->setHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT'); + app()->response->setHeader('Last-Modified', gmdate("D, d M Y H:i:s") . " GMT"); + app()->response->setHeader('Cache-Control', 'no-cache, must-revalidate'); + app()->response->setHeader('Pragma', 'no-cache'); + if (env("APP_DEBUG")) { app()->response->setHeader("access-control-allow-origin","*"); } + return $next(); } } diff --git a/apps/models/api/v1/form/TorrentsBookmarkForm.php b/apps/models/api/v1/form/TorrentsBookmarkForm.php new file mode 100644 index 0000000..7ac8796 --- /dev/null +++ b/apps/models/api/v1/form/TorrentsBookmarkForm.php @@ -0,0 +1,60 @@ + 'required | Integer' + ]; + } + + public static function callbackRules() { + return ['isExistTorrent']; + } + + protected function isExistTorrent() { + $torrent_exist = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `torrents` WHERE `id` = :tid')->bindParams([ + 'tid' => $this->tid + ])->queryScalar(); + if ($torrent_exist == 0) { + $this->buildCallbackFailMsg('Torrent', 'The torrent id ('. $this->tid. ') is not exist in our database'); + } + } + + public function updateRecord() { + $bookmark_exist = app()->pdo->createCommand('SELECT `id` FROM `bookmarks` WHERE `uid` = :uid AND `tid` = :tid ')->bindParams([ + 'uid' => app()->user->getId(), + 'tid' => $this->tid + ])->queryScalar() ?: 0; + if ($bookmark_exist > 0) { // Delete the exist record + app()->pdo->createCommand('DELETE FROM `bookmarks` WHERE `id` = :bid')->bindParams([ + 'bid' => $bookmark_exist + ])->execute(); + app()->redis->del('User:' . app()->user->getId() . ':bookmark_array'); + + return ['msg' => 'Delete Old Bookmark Success', 'result' => 'deleted']; + } else { // Add new record + app()->pdo->createCommand('INSERT INTO `bookmarks` (`uid`, `tid`) VALUES (:uid, :tid)')->bindParams([ + 'uid' => app()->user->getId(), + 'tid' => $this->tid + ])->execute(); + app()->redis->del('User:' . app()->user->getId() . ':bookmark_array'); + + return ['msg' => 'Add New Bookmark Success', 'result' => 'added']; + } + } +} diff --git a/apps/public/static/css/main.css b/apps/public/static/css/main.css index 692b973..b779b84 100644 --- a/apps/public/static/css/main.css +++ b/apps/public/static/css/main.css @@ -78,4 +78,5 @@ nav#menu .layui-nav-item{line-height:45px} .icon-leeching {color: red} -.torrent-title-block {margin: 15px} +.torrent-title-block{margin:15px} +.torrent-action-item{padding:3px} diff --git a/apps/public/static/js/main.js b/apps/public/static/js/main.js index ff89fdf..00e9849 100644 --- a/apps/public/static/js/main.js +++ b/apps/public/static/js/main.js @@ -1,16 +1,25 @@ ;layui.use(['layer', 'form','element','laypage','jquery'], function(){ - let $=layui.jquery; + let $ = layui.jquery; + let layer = layui.layer; + let api_point = '/api/v1'; - // Add favour action - $('.torrent-favour > i').click(function () { - let star = $(this); - // TODO Do ajax to api, if success then change the star - let old_is_stared = star.hasClass('fas'); - star.toggleClass('fas',!old_is_stared).toggleClass('far',old_is_stared); - new NoticeJs({ - text: 'Torrent add/remove from your favour', - position: 'bottomRight', - }).show(); - // TODO Notice user - }) + // Add/Remove favour action + $('.torrent-favour').click(function () { + let that = $(this); + let tid = that.attr('data-tid'); + let star = that.find(' > i'); + + $.post(api_point + '/torrents/bookmark', {'tid': tid}, function (res) { + if (res.success) { + let old_is_stared = star.hasClass('fas'); + star.toggleClass('fas', !old_is_stared).toggleClass('far', old_is_stared); + layer.msg(`Torrent(${tid}) ${res.result} from your favour successfully`, { + icon: 6, + offset: 'rb', + }); + } else { + layer.alert(res.errors.join(', '), {icon: 2}); + } + }); + }); }); diff --git a/apps/views/torrents/details.php b/apps/views/torrents/details.php index c8b3417..85cd528 100644 --- a/apps/views/torrents/details.php +++ b/apps/views/torrents/details.php @@ -42,19 +42,39 @@