From f72b0216a5f4e1825970cf4b8c99aa1256f8e81a Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 25 Oct 2014 18:16:47 +0200 Subject: [PATCH] Fixed "featured by" showing post uploader --- public_html/js/Presenters/HomePresenter.js | 4 +- public_html/templates/home.tpl | 10 +-- src/Controllers/PostController.php | 35 ++++---- src/Entities/GlobalParam.php | 1 + src/Services/PostFeatureService.php | 92 ++++++++++++++++++++++ src/Services/PostService.php | 33 -------- 6 files changed, 122 insertions(+), 53 deletions(-) create mode 100644 src/Services/PostFeatureService.php diff --git a/public_html/js/Presenters/HomePresenter.js b/public_html/js/Presenters/HomePresenter.js index 792b581f..b8f7c9cf 100644 --- a/public_html/js/Presenters/HomePresenter.js +++ b/public_html/js/Presenters/HomePresenter.js @@ -32,7 +32,8 @@ App.Presenters.HomePresenter = function( templates.home = homeTemplate; globals = globalsResponse.json; - post = featuredPostResponse.json.id ? featuredPostResponse.json : null; + post = featuredPostResponse.json.post; + user = featuredPostResponse.json.user; render(); loaded(); @@ -49,6 +50,7 @@ App.Presenters.HomePresenter = function( function render() { $el.html(templates.home({ post: post, + user: user, globals: globals, title: topNavigationPresenter.getBaseTitle(), canViewUsers: auth.hasPrivilege(auth.privileges.viewUsers), diff --git a/public_html/templates/home.tpl b/public_html/templates/home.tpl index 2a9c2cad..49f9b3c1 100644 --- a/public_html/templates/home.tpl +++ b/public_html/templates/home.tpl @@ -31,17 +31,17 @@ featured by - <% var showLink = canViewUsers && post.user.name %> + <% var showLink = canViewUsers && user.name %> <% if (showLink) { %> - + <% } %> <%= post.user.name || 'Anonymous user' %> + src="/data/thumbnails/25x25/avatars/<%= user.name || '!' %>" + alt="<%= user.name || 'Anonymous user' %>"/> - <%= post.user.name || 'Anonymous user' %> + <%= user.name || 'Anonymous user' %> <% if (showLink) { %> diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index ef9cf41e..eeb76d37 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -3,6 +3,7 @@ namespace Szurubooru\Controllers; use Szurubooru\Config; use Szurubooru\Controllers\ViewProxies\PostViewProxy; use Szurubooru\Controllers\ViewProxies\SnapshotViewProxy; +use Szurubooru\Controllers\ViewProxies\UserViewProxy; use Szurubooru\Entities\Post; use Szurubooru\FormData\PostEditFormData; use Szurubooru\FormData\UploadFormData; @@ -11,6 +12,7 @@ use Szurubooru\Privilege; use Szurubooru\Router; use Szurubooru\SearchServices\Parsers\PostSearchParser; use Szurubooru\Services\AuthService; +use Szurubooru\Services\PostFeatureService; use Szurubooru\Services\PostService; use Szurubooru\Services\PrivilegeService; @@ -20,6 +22,7 @@ final class PostController extends AbstractController private $authService; private $privilegeService; private $postService; + private $postFeatureService; private $postSearchParser; private $inputReader; private $postViewProxy; @@ -30,8 +33,10 @@ final class PostController extends AbstractController AuthService $authService, PrivilegeService $privilegeService, PostService $postService, + PostFeatureService $postFeatureService, PostSearchParser $postSearchParser, InputReader $inputReader, + UserViewProxy $userViewProxy, PostViewProxy $postViewProxy, SnapshotViewProxy $snapshotViewProxy) { @@ -39,8 +44,10 @@ final class PostController extends AbstractController $this->authService = $authService; $this->privilegeService = $privilegeService; $this->postService = $postService; + $this->postFeatureService = $postFeatureService; $this->postSearchParser = $postSearchParser; $this->inputReader = $inputReader; + $this->userViewProxy = $userViewProxy; $this->postViewProxy = $postViewProxy; $this->snapshotViewProxy = $snapshotViewProxy; } @@ -49,6 +56,7 @@ final class PostController extends AbstractController { $router->post('/api/posts', [$this, 'createPost']); $router->get('/api/posts', [$this, 'getFiltered']); + $router->get('/api/posts/featured', [$this, 'getFeatured']); $router->get('/api/posts/:postNameOrId', [$this, 'getByNameOrId']); $router->get('/api/posts/:postNameOrId/history', [$this, 'getHistory']); $router->put('/api/posts/:postNameOrId', [$this, 'updatePost']); @@ -57,19 +65,26 @@ final class PostController extends AbstractController $router->put('/api/posts/:postNameOrId/feature', [$this, 'featurePost']); } + public function getFeatured() + { + $post = $this->postFeatureService->getFeaturedPost(); + $user = $this->postFeatureService->getFeaturedPostUser(); + return [ + 'user' => $this->userViewProxy->fromEntity($user), + 'post' => $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()), + ]; + } + public function getByNameOrId($postNameOrId) { - if ($postNameOrId !== 'featured') - $this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS); - - $post = $this->getByNameOrIdWithoutProxy($postNameOrId); + $post = $this->postService->getByNameOrId($postNameOrId); return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); } public function getHistory($postNameOrId) { $this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY); - $post = $this->getByNameOrIdWithoutProxy($postNameOrId); + $post = $this->getByNameOrId($postNameOrId); return ['data' => $this->snapshotViewProxy->fromArray($this->postService->getHistory($post))]; } @@ -137,15 +152,7 @@ final class PostController extends AbstractController public function featurePost($postNameOrId) { $post = $this->postService->getByNameOrId($postNameOrId); - $this->postService->featurePost($post); - } - - private function getByNameOrIdWithoutProxy($postNameOrId) - { - if ($postNameOrId === 'featured') - return $this->postService->getFeatured(); - else - return $this->postService->getByNameOrId($postNameOrId); + $this->postFeatureService->featurePost($post); } private function getFullFetchConfig() diff --git a/src/Entities/GlobalParam.php b/src/Entities/GlobalParam.php index 67bbbb85..3ad4eacd 100644 --- a/src/Entities/GlobalParam.php +++ b/src/Entities/GlobalParam.php @@ -3,6 +3,7 @@ namespace Szurubooru\Entities; final class GlobalParam extends Entity { + const KEY_FEATURED_POST_USER = 'featuredPostUser'; const KEY_FEATURED_POST = 'featuredPost'; const KEY_POST_SIZE = 'postSize'; const KEY_POST_COUNT = 'postCount'; diff --git a/src/Services/PostFeatureService.php b/src/Services/PostFeatureService.php new file mode 100644 index 00000000..b8ee6ab6 --- /dev/null +++ b/src/Services/PostFeatureService.php @@ -0,0 +1,92 @@ +transactionManager = $transactionManager; + $this->postDao = $postDao; + $this->userDao = $userDao; + $this->globalParamDao = $globalParamDao; + $this->authService = $authService; + $this->timeService = $timeService; + $this->historyService = $historyService; + } + + public function getFeaturedPost() + { + $transactionFunc = function() + { + $globalParam = $this->globalParamDao->findByKey(GlobalParam::KEY_FEATURED_POST); + if (!$globalParam) + return null; + return $this->postDao->findById($globalParam->getValue()); + }; + return $this->transactionManager->rollback($transactionFunc); + } + + public function getFeaturedPostUser() + { + $transactionFunc = function() + { + $globalParam = $this->globalParamDao->findByKey(GlobalParam::KEY_FEATURED_POST_USER); + if (!$globalParam) + return null; + return $this->userDao->findById($globalParam->getValue()); + }; + return $this->transactionManager->rollback($transactionFunc); + } + + public function featurePost(Post $post) + { + $transactionFunc = function() use ($post) + { + $previousFeaturedPost = $this->getFeaturedPost(); + + $post->setLastFeatureTime($this->timeService->getCurrentTime()); + $post->setFeatureCount($post->getFeatureCount() + 1); + $this->postDao->save($post); + + $globalParam = new GlobalParam(); + $globalParam->setKey(GlobalParam::KEY_FEATURED_POST); + $globalParam->setValue($post->getId()); + $this->globalParamDao->save($globalParam); + + $globalParam = new GlobalParam(); + $globalParam->setKey(GlobalParam::KEY_FEATURED_POST_USER); + $globalParam->setValue($this->authService->getLoggedInUser()->getId()); + $this->globalParamDao->save($globalParam); + + if ($previousFeaturedPost) + $this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($previousFeaturedPost)); + $this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($post)); + }; + $this->transactionManager->commit($transactionFunc); + } +} diff --git a/src/Services/PostService.php b/src/Services/PostService.php index 120325b5..487ea32e 100644 --- a/src/Services/PostService.php +++ b/src/Services/PostService.php @@ -103,18 +103,6 @@ class PostService return $this->transactionManager->rollback($transactionFunc); } - public function getFeatured() - { - $transactionFunc = function() - { - $globalParam = $this->globalParamDao->findByKey(GlobalParam::KEY_FEATURED_POST); - if (!$globalParam) - return null; - return $this->getByNameOrId($globalParam->getValue()); - }; - return $this->transactionManager->rollback($transactionFunc); - } - public function getHistory(Post $post) { $transactionFunc = function() use ($post) @@ -353,27 +341,6 @@ class PostService $this->transactionManager->commit($transactionFunc); } - public function featurePost(Post $post) - { - $transactionFunc = function() use ($post) - { - $previousFeaturedPost = $this->getFeatured(); - - $post->setLastFeatureTime($this->timeService->getCurrentTime()); - $post->setFeatureCount($post->getFeatureCount() + 1); - $this->postDao->save($post); - $globalParam = new GlobalParam(); - $globalParam->setKey(GlobalParam::KEY_FEATURED_POST); - $globalParam->setValue($post->getId()); - $this->globalParamDao->save($globalParam); - - if ($previousFeaturedPost) - $this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($previousFeaturedPost)); - $this->historyService->saveSnapshot($this->historyService->getPostChangeSnapshot($post)); - }; - $this->transactionManager->commit($transactionFunc); - } - public function updatePostGlobals() { $transactionFunc = function()