diff --git a/TODO b/TODO index 7a10969a..4f7da6d7 100644 --- a/TODO +++ b/TODO @@ -3,7 +3,6 @@ first major release. everything related to posts: - single post view - - reduce requests to server - editing - ability to loop video posts - previous and next post (difficult) diff --git a/public_html/js/Presenters/PostPresenter.js b/public_html/js/Presenters/PostPresenter.js index c82e3b39..6ebc247b 100644 --- a/public_html/js/Presenters/PostPresenter.js +++ b/public_html/js/Presenters/PostPresenter.js @@ -84,24 +84,12 @@ App.Presenters.PostPresenter = function( function refreshPost() { return promise.make(function(resolve, reject) { - promise.waitAll( - api.get('/posts/' + postNameOrId), - api.get('/posts/' + postNameOrId + '/favorites'), - auth.isLoggedIn() ? - api.get('/posts/' + postNameOrId + '/score') : - null, - privileges.canViewHistory ? - api.get('/posts/' + postNameOrId + '/history') : - null) - .then(function( - postResponse, - postFavoritesResponse, - postScoreResponse, - postHistoryResponse) { + promise.waitAll(api.get('/posts/' + postNameOrId)) + .then(function(postResponse) { post = postResponse.json; - postScore = postScoreResponse && postScoreResponse.json && postScoreResponse.json.score; - postFavorites = postFavoritesResponse && postFavoritesResponse.json && postFavoritesResponse.json.data; - postHistory = postHistoryResponse && postHistoryResponse.json && postHistoryResponse.json.data; + postScore = postResponse.json.ownScore; + postFavorites = postResponse.json.favorites; + postHistory = postResponse.json.history; resolve(); }).fail(function(response) { showGenericError(response); diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 45c191f2..07cd9d27 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -131,6 +131,9 @@ final class PostController extends AbstractController \Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_RELATIONS => true, \Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_TAGS => true, \Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_USER => true, + \Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_HISTORY => true, + \Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_OWN_SCORE => true, + \Szurubooru\Controllers\ViewProxies\PostViewProxy::FETCH_FAVORITES => true, ]; } diff --git a/src/Controllers/ViewProxies/PostViewProxy.php b/src/Controllers/ViewProxies/PostViewProxy.php index c8bce8d7..c93c5c3a 100644 --- a/src/Controllers/ViewProxies/PostViewProxy.php +++ b/src/Controllers/ViewProxies/PostViewProxy.php @@ -6,51 +6,88 @@ class PostViewProxy extends AbstractViewProxy const FETCH_USER = 'fetchUser'; const FETCH_TAGS = 'fetchTags'; const FETCH_RELATIONS = 'fetchRelations'; + const FETCH_HISTORY = 'fetchHistory'; + const FETCH_OWN_SCORE = 'fetchOwnScore'; + const FETCH_FAVORITES = 'fetchFavorites'; + private $privilegeService; + private $authService; + private $historyService; + private $favoritesService; + private $postScoreService; private $tagViewProxy; private $userViewProxy; + private $snapshotViewProxy; public function __construct( + \Szurubooru\Services\PrivilegeService $privilegeService, + \Szurubooru\Services\AuthService $authService, + \Szurubooru\Services\HistoryService $historyService, + \Szurubooru\Services\FavoritesService $favoritesService, + \Szurubooru\Services\PostScoreService $postScoreService, TagViewProxy $tagViewProxy, - UserViewProxy $userViewProxy) + UserViewProxy $userViewProxy, + SnapshotViewProxy $snapshotViewProxy) { + $this->privilegeService = $privilegeService; + $this->authService = $authService; + $this->historyService = $historyService; + $this->favoritesService = $favoritesService; + $this->postScoreService = $postScoreService; $this->tagViewProxy = $tagViewProxy; $this->userViewProxy = $userViewProxy; + $this->snapshotViewProxy = $snapshotViewProxy; } public function fromEntity($post, $config = []) { $result = new \StdClass; - if ($post) + if (!$post) + return $result; + + $result->id = $post->getId(); + $result->idMarkdown = $post->getIdMarkdown(); + $result->name = $post->getName(); + $result->uploadTime = $post->getUploadTime(); + $result->lastEditTime = $post->getLastEditTime(); + $result->safety = \Szurubooru\Helpers\EnumHelper::postSafetyToString($post->getSafety()); + $result->contentType = \Szurubooru\Helpers\EnumHelper::postTypeToString($post->getContentType()); + $result->contentChecksum = $post->getContentChecksum(); + $result->contentMimeType = $post->getContentMimeType(); + $result->contentExtension = \Szurubooru\Helpers\MimeHelper::getExtension($post->getContentMimeType()); + $result->source = $post->getSource(); + $result->imageWidth = $post->getImageWidth(); + $result->imageHeight = $post->getImageHeight(); + $result->featureCount = $post->getFeatureCount(); + $result->lastFeatureTime = $post->getLastFeatureTime(); + $result->originalFileSize = $post->getOriginalFileSize(); + $result->favoriteCount = $post->getFavoriteCount(); + $result->score = $post->getScore(); + + if (!empty($config[self::FETCH_TAGS])) + $result->tags = $this->tagViewProxy->fromArray($post->getTags()); + + if (!empty($config[self::FETCH_USER])) + $result->user = $this->userViewProxy->fromEntity($post->getUser()); + + if (!empty($config[self::FETCH_RELATIONS])) + $result->relations = $this->fromArray($post->getRelatedPosts()); + + if (!empty($config[self::FETCH_HISTORY])) { - $result->id = $post->getId(); - $result->idMarkdown = $post->getIdMarkdown(); - $result->name = $post->getName(); - $result->uploadTime = $post->getUploadTime(); - $result->lastEditTime = $post->getLastEditTime(); - $result->safety = \Szurubooru\Helpers\EnumHelper::postSafetyToString($post->getSafety()); - $result->contentType = \Szurubooru\Helpers\EnumHelper::postTypeToString($post->getContentType()); - $result->contentChecksum = $post->getContentChecksum(); - $result->contentMimeType = $post->getContentMimeType(); - $result->contentExtension = \Szurubooru\Helpers\MimeHelper::getExtension($post->getContentMimeType()); - $result->source = $post->getSource(); - $result->imageWidth = $post->getImageWidth(); - $result->imageHeight = $post->getImageHeight(); - $result->featureCount = $post->getFeatureCount(); - $result->lastFeatureTime = $post->getLastFeatureTime(); - $result->originalFileSize = $post->getOriginalFileSize(); - $result->favoriteCount = $post->getFavoriteCount(); - $result->score = $post->getScore(); - - if (!empty($config[self::FETCH_TAGS])) - $result->tags = $this->tagViewProxy->fromArray($post->getTags()); - - if (!empty($config[self::FETCH_USER])) - $result->user = $this->userViewProxy->fromEntity($post->getUser()); - - if (!empty($config[self::FETCH_RELATIONS])) - $result->relations = $this->fromArray($post->getRelatedPosts()); + if ($this->privilegeService->hasPrivilege(\Szurubooru\Privilege::VIEW_HISTORY)) + $result->history = $this->snapshotViewProxy->fromArray($this->historyService->getPostHistory($post)); + else + $result->history = []; } + + if (!empty($config[self::FETCH_OWN_SCORE]) and $this->authService->isLoggedIn()) + $result->ownScore = $this->postScoreService->getScoreValue($this->authService->getLoggedInUser(), $post); + + if (!empty($config[self::FETCH_FAVORITES])) + $result->favorites = $this->userViewProxy->fromArray($this->favoritesService->getFavoriteUsers($post)); + + return $result; } } diff --git a/src/Controllers/ViewProxies/SnapshotViewProxy.php b/src/Controllers/ViewProxies/SnapshotViewProxy.php index 4a957f75..dc82f825 100644 --- a/src/Controllers/ViewProxies/SnapshotViewProxy.php +++ b/src/Controllers/ViewProxies/SnapshotViewProxy.php @@ -26,4 +26,3 @@ class SnapshotViewProxy extends AbstractViewProxy return $result; } } - diff --git a/src/Services/HistoryService.php b/src/Services/HistoryService.php index 4466db3e..3c81a489 100644 --- a/src/Services/HistoryService.php +++ b/src/Services/HistoryService.php @@ -35,6 +35,23 @@ class HistoryService return $this->transactionManager->rollback($transactionFunc); } + public function getPostHistory(\Szurubooru\Entities\Post $post) + { + $filter = new \Szurubooru\SearchServices\Filters\SnapshotFilter(); + + $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); + $requirement->setType(\Szurubooru\SearchServices\Filters\SnapshotFilter::REQUIREMENT_PRIMARY_KEY); + $requirement->setValue(new \Szurubooru\SearchServices\Requirements\RequirementSingleValue($post->getId())); + $filter->addRequirement($requirement); + + $requirement = new \Szurubooru\SearchServices\Requirements\Requirement(); + $requirement->setType(\Szurubooru\SearchServices\Filters\SnapshotFilter::REQUIREMENT_TYPE); + $requirement->setValue(new \Szurubooru\SearchServices\Requirements\RequirementSingleValue(\Szurubooru\Entities\Snapshot::TYPE_POST)); + $filter->addRequirement($requirement); + + return $this->getFiltered($filter)->getEntities(); + } + public function saveSnapshot(\Szurubooru\Entities\Snapshot $snapshot) { $transactionFunc = function() use ($snapshot) diff --git a/src/Services/PostScoreService.php b/src/Services/PostScoreService.php index d016c777..baa2dd3a 100644 --- a/src/Services/PostScoreService.php +++ b/src/Services/PostScoreService.php @@ -32,6 +32,14 @@ class PostScoreService return $this->transactionManager->rollback($transactionFunc); } + public function getScoreValue(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post) + { + $score = $this->getScore($user, $post); + if (!$score) + return 0; + return $score->getScore(); + } + public function setScore(\Szurubooru\Entities\User $user, \Szurubooru\Entities\Post $post, $scoreValue) { if ($scoreValue !== 1 and $scoreValue !== 0 and $scoreValue !== -1)