Fixed "featured by" showing post uploader

This commit is contained in:
Marcin Kurczewski 2014-10-25 18:16:47 +02:00
parent f7ff4e0a71
commit f72b0216a5
6 changed files with 122 additions and 53 deletions

View file

@ -32,7 +32,8 @@ App.Presenters.HomePresenter = function(
templates.home = homeTemplate; templates.home = homeTemplate;
globals = globalsResponse.json; globals = globalsResponse.json;
post = featuredPostResponse.json.id ? featuredPostResponse.json : null; post = featuredPostResponse.json.post;
user = featuredPostResponse.json.user;
render(); render();
loaded(); loaded();
@ -49,6 +50,7 @@ App.Presenters.HomePresenter = function(
function render() { function render() {
$el.html(templates.home({ $el.html(templates.home({
post: post, post: post,
user: user,
globals: globals, globals: globals,
title: topNavigationPresenter.getBaseTitle(), title: topNavigationPresenter.getBaseTitle(),
canViewUsers: auth.hasPrivilege(auth.privileges.viewUsers), canViewUsers: auth.hasPrivilege(auth.privileges.viewUsers),

View file

@ -31,17 +31,17 @@
<span class="right"> <span class="right">
featured by featured by
<% var showLink = canViewUsers && post.user.name %> <% var showLink = canViewUsers && user.name %>
<% if (showLink) { %> <% if (showLink) { %>
<a href="#/user/<%= post.user.name %>"> <a href="#/user/<%= user.name %>">
<% } %> <% } %>
<img width="25" height="25" class="author-avatar" <img width="25" height="25" class="author-avatar"
src="/data/thumbnails/25x25/avatars/<%= post.user.name || '!' %>" src="/data/thumbnails/25x25/avatars/<%= user.name || '!' %>"
alt="<%= post.user.name || 'Anonymous user' %>"/> alt="<%= user.name || 'Anonymous user' %>"/>
<%= post.user.name || 'Anonymous user' %> <%= user.name || 'Anonymous user' %>
<% if (showLink) { %> <% if (showLink) { %>
</a> </a>

View file

@ -3,6 +3,7 @@ namespace Szurubooru\Controllers;
use Szurubooru\Config; use Szurubooru\Config;
use Szurubooru\Controllers\ViewProxies\PostViewProxy; use Szurubooru\Controllers\ViewProxies\PostViewProxy;
use Szurubooru\Controllers\ViewProxies\SnapshotViewProxy; use Szurubooru\Controllers\ViewProxies\SnapshotViewProxy;
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
use Szurubooru\Entities\Post; use Szurubooru\Entities\Post;
use Szurubooru\FormData\PostEditFormData; use Szurubooru\FormData\PostEditFormData;
use Szurubooru\FormData\UploadFormData; use Szurubooru\FormData\UploadFormData;
@ -11,6 +12,7 @@ use Szurubooru\Privilege;
use Szurubooru\Router; use Szurubooru\Router;
use Szurubooru\SearchServices\Parsers\PostSearchParser; use Szurubooru\SearchServices\Parsers\PostSearchParser;
use Szurubooru\Services\AuthService; use Szurubooru\Services\AuthService;
use Szurubooru\Services\PostFeatureService;
use Szurubooru\Services\PostService; use Szurubooru\Services\PostService;
use Szurubooru\Services\PrivilegeService; use Szurubooru\Services\PrivilegeService;
@ -20,6 +22,7 @@ final class PostController extends AbstractController
private $authService; private $authService;
private $privilegeService; private $privilegeService;
private $postService; private $postService;
private $postFeatureService;
private $postSearchParser; private $postSearchParser;
private $inputReader; private $inputReader;
private $postViewProxy; private $postViewProxy;
@ -30,8 +33,10 @@ final class PostController extends AbstractController
AuthService $authService, AuthService $authService,
PrivilegeService $privilegeService, PrivilegeService $privilegeService,
PostService $postService, PostService $postService,
PostFeatureService $postFeatureService,
PostSearchParser $postSearchParser, PostSearchParser $postSearchParser,
InputReader $inputReader, InputReader $inputReader,
UserViewProxy $userViewProxy,
PostViewProxy $postViewProxy, PostViewProxy $postViewProxy,
SnapshotViewProxy $snapshotViewProxy) SnapshotViewProxy $snapshotViewProxy)
{ {
@ -39,8 +44,10 @@ final class PostController extends AbstractController
$this->authService = $authService; $this->authService = $authService;
$this->privilegeService = $privilegeService; $this->privilegeService = $privilegeService;
$this->postService = $postService; $this->postService = $postService;
$this->postFeatureService = $postFeatureService;
$this->postSearchParser = $postSearchParser; $this->postSearchParser = $postSearchParser;
$this->inputReader = $inputReader; $this->inputReader = $inputReader;
$this->userViewProxy = $userViewProxy;
$this->postViewProxy = $postViewProxy; $this->postViewProxy = $postViewProxy;
$this->snapshotViewProxy = $snapshotViewProxy; $this->snapshotViewProxy = $snapshotViewProxy;
} }
@ -49,6 +56,7 @@ final class PostController extends AbstractController
{ {
$router->post('/api/posts', [$this, 'createPost']); $router->post('/api/posts', [$this, 'createPost']);
$router->get('/api/posts', [$this, 'getFiltered']); $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', [$this, 'getByNameOrId']);
$router->get('/api/posts/:postNameOrId/history', [$this, 'getHistory']); $router->get('/api/posts/:postNameOrId/history', [$this, 'getHistory']);
$router->put('/api/posts/:postNameOrId', [$this, 'updatePost']); $router->put('/api/posts/:postNameOrId', [$this, 'updatePost']);
@ -57,19 +65,26 @@ final class PostController extends AbstractController
$router->put('/api/posts/:postNameOrId/feature', [$this, 'featurePost']); $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) public function getByNameOrId($postNameOrId)
{ {
if ($postNameOrId !== 'featured') $post = $this->postService->getByNameOrId($postNameOrId);
$this->privilegeService->assertPrivilege(Privilege::VIEW_POSTS);
$post = $this->getByNameOrIdWithoutProxy($postNameOrId);
return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig()); return $this->postViewProxy->fromEntity($post, $this->getFullFetchConfig());
} }
public function getHistory($postNameOrId) public function getHistory($postNameOrId)
{ {
$this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY); $this->privilegeService->assertPrivilege(Privilege::VIEW_HISTORY);
$post = $this->getByNameOrIdWithoutProxy($postNameOrId); $post = $this->getByNameOrId($postNameOrId);
return ['data' => $this->snapshotViewProxy->fromArray($this->postService->getHistory($post))]; return ['data' => $this->snapshotViewProxy->fromArray($this->postService->getHistory($post))];
} }
@ -137,15 +152,7 @@ final class PostController extends AbstractController
public function featurePost($postNameOrId) public function featurePost($postNameOrId)
{ {
$post = $this->postService->getByNameOrId($postNameOrId); $post = $this->postService->getByNameOrId($postNameOrId);
$this->postService->featurePost($post); $this->postFeatureService->featurePost($post);
}
private function getByNameOrIdWithoutProxy($postNameOrId)
{
if ($postNameOrId === 'featured')
return $this->postService->getFeatured();
else
return $this->postService->getByNameOrId($postNameOrId);
} }
private function getFullFetchConfig() private function getFullFetchConfig()

View file

@ -3,6 +3,7 @@ namespace Szurubooru\Entities;
final class GlobalParam extends Entity final class GlobalParam extends Entity
{ {
const KEY_FEATURED_POST_USER = 'featuredPostUser';
const KEY_FEATURED_POST = 'featuredPost'; const KEY_FEATURED_POST = 'featuredPost';
const KEY_POST_SIZE = 'postSize'; const KEY_POST_SIZE = 'postSize';
const KEY_POST_COUNT = 'postCount'; const KEY_POST_COUNT = 'postCount';

View file

@ -0,0 +1,92 @@
<?php
namespace Szurubooru\Services;
use Szurubooru\Dao\GlobalParamDao;
use Szurubooru\Dao\PostDao;
use Szurubooru\Dao\UserDao;
use Szurubooru\Dao\TransactionManager;
use Szurubooru\Entities\GlobalParam;
use Szurubooru\Entities\Post;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\HistoryService;
use Szurubooru\Services\TimeService;
use Szurubooru\Validator;
class PostFeatureService
{
private $transactionManager;
private $postDao;
private $userDao;
private $globalParamDao;
private $authService;
private $timeService;
private $historyService;
public function __construct(
TransactionManager $transactionManager,
PostDao $postDao,
UserDao $userDao,
GlobalParamDao $globalParamDao,
AuthService $authService,
TimeService $timeService,
HistoryService $historyService)
{
$this->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);
}
}

View file

@ -103,18 +103,6 @@ class PostService
return $this->transactionManager->rollback($transactionFunc); 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) public function getHistory(Post $post)
{ {
$transactionFunc = function() use ($post) $transactionFunc = function() use ($post)
@ -353,27 +341,6 @@ class PostService
$this->transactionManager->commit($transactionFunc); $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() public function updatePostGlobals()
{ {
$transactionFunc = function() $transactionFunc = function()