Added post globals
This commit is contained in:
parent
2bb2d1f743
commit
d3015627b3
11 changed files with 105 additions and 4 deletions
2
TODO
2
TODO
|
@ -28,8 +28,6 @@ everything related to posts:
|
|||
- link to iqdb and/or tineye
|
||||
|
||||
- auto feature
|
||||
- number of posts
|
||||
- total size of posts
|
||||
|
||||
- random post
|
||||
- regard safety settings
|
||||
|
|
|
@ -3,6 +3,10 @@
|
|||
}
|
||||
#home h1 {
|
||||
margin-top: 0;
|
||||
margin-bottom: 0;
|
||||
}
|
||||
#home h1+p {
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
#home .post {
|
||||
|
|
|
@ -13,6 +13,7 @@ App.Presenters.HomePresenter = function(
|
|||
var $el = jQuery('#content');
|
||||
var homeTemplate;
|
||||
var postContentTemplate;
|
||||
var globals;
|
||||
var post;
|
||||
|
||||
function init(args, loaded) {
|
||||
|
@ -22,15 +23,18 @@ App.Presenters.HomePresenter = function(
|
|||
promise.waitAll(
|
||||
util.promiseTemplate('home'),
|
||||
util.promiseTemplate('post-content'),
|
||||
api.get('/globals'),
|
||||
api.get('/posts/featured'))
|
||||
.then(function(
|
||||
homeTemplateHtml,
|
||||
postContentTemplateHtml,
|
||||
response) {
|
||||
globalsResponse,
|
||||
featuredPostResponse) {
|
||||
homeTemplate = _.template(homeTemplateHtml);
|
||||
postContentTemplate = _.template(postContentTemplateHtml);
|
||||
|
||||
post = response.json;
|
||||
globals = globalsResponse.json;
|
||||
post = featuredPostResponse.json;
|
||||
render();
|
||||
loaded();
|
||||
|
||||
|
@ -43,8 +47,10 @@ App.Presenters.HomePresenter = function(
|
|||
$el.html(homeTemplate({
|
||||
post: post,
|
||||
postContentTemplate: postContentTemplate,
|
||||
globals: globals,
|
||||
title: topNavigationPresenter.getBaseTitle(),
|
||||
formatRelativeTime: util.formatRelativeTime,
|
||||
formatFileSize: util.formatFileSize,
|
||||
}));
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
<div id="home">
|
||||
<h1><%= title %></h1>
|
||||
<p>
|
||||
<small>Serving <%= globals.postCount %> posts (<%= formatFileSize(globals.postSize) %>)</small>
|
||||
</p>
|
||||
|
||||
<% if (post) { %>
|
||||
<div class="post">
|
||||
|
|
8
scripts/cron-globals.php
Normal file
8
scripts/cron-globals.php
Normal file
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
require_once(__DIR__
|
||||
. DIRECTORY_SEPARATOR . '..'
|
||||
. DIRECTORY_SEPARATOR . 'src'
|
||||
. DIRECTORY_SEPARATOR . 'AutoLoader.php');
|
||||
|
||||
$postService = Szurubooru\Injector::get(\Szurubooru\Services\PostService::class);
|
||||
$postService->updatePostGlobals();
|
29
src/Controllers/GlobalParamController.php
Normal file
29
src/Controllers/GlobalParamController.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
namespace Szurubooru\Controllers;
|
||||
|
||||
final class GlobalParamController extends AbstractController
|
||||
{
|
||||
private $globalParamDao;
|
||||
|
||||
public function __construct(
|
||||
\Szurubooru\Dao\GlobalParamDao $globalParamDao)
|
||||
{
|
||||
$this->globalParamDao = $globalParamDao;
|
||||
}
|
||||
|
||||
public function registerRoutes(\Szurubooru\Router $router)
|
||||
{
|
||||
$router->get('/api/globals', [$this, 'getGlobals']);
|
||||
}
|
||||
|
||||
public function getGlobals()
|
||||
{
|
||||
$globals = $this->globalParamDao->findAll();
|
||||
$return = [];
|
||||
foreach ($globals as $global)
|
||||
{
|
||||
$return[$global->getKey()] = $global->getValue();
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
}
|
|
@ -26,6 +26,17 @@ class PostDao extends AbstractDao implements ICrudDao
|
|||
$this->thumbnailService = $thumbnailService;
|
||||
}
|
||||
|
||||
public function getCount()
|
||||
{
|
||||
return count($this->fpdo->from($this->tableName));
|
||||
}
|
||||
|
||||
public function getTotalFileSize()
|
||||
{
|
||||
$query = $this->fpdo->from($this->tableName)->select('SUM(originalFileSize) AS __sum');
|
||||
return intval(iterator_to_array($query)[0]['__sum']);
|
||||
}
|
||||
|
||||
public function findByName($name)
|
||||
{
|
||||
return $this->findOneBy('name', $name);
|
||||
|
|
|
@ -4,6 +4,8 @@ namespace Szurubooru\Entities;
|
|||
class GlobalParam extends Entity
|
||||
{
|
||||
const KEY_FEATURED_POST = 'featuredPost';
|
||||
const KEY_POST_SIZE = 'postSize';
|
||||
const KEY_POST_COUNT = 'postCount';
|
||||
|
||||
private $key;
|
||||
private $value;
|
||||
|
|
|
@ -229,6 +229,23 @@ class PostService
|
|||
$this->transactionManager->commit($transactionFunc);
|
||||
}
|
||||
|
||||
public function updatePostGlobals()
|
||||
{
|
||||
$transactionFunc = function()
|
||||
{
|
||||
$countParam = new \Szurubooru\Entities\GlobalParam();
|
||||
$countParam->setKey(\Szurubooru\Entities\GlobalParam::KEY_POST_COUNT);
|
||||
$countParam->setValue($this->postDao->getCount());
|
||||
$this->globalParamDao->save($countParam);
|
||||
|
||||
$fileSizeParam = new \Szurubooru\Entities\GlobalParam();
|
||||
$fileSizeParam->setKey(\Szurubooru\Entities\GlobalParam::KEY_POST_SIZE);
|
||||
$fileSizeParam->setValue($this->postDao->getTotalFileSize());
|
||||
$this->globalParamDao->save($fileSizeParam);
|
||||
};
|
||||
$this->transactionManager->commit($transactionFunc);
|
||||
}
|
||||
|
||||
private function assertNoPostWithThisContentChecksum(\Szurubooru\Entities\Post $parent)
|
||||
{
|
||||
$checksumToCheck = $parent->getContentChecksum();
|
||||
|
|
|
@ -33,6 +33,7 @@ return [
|
|||
$container->get(\Szurubooru\Controllers\UserAvatarController::class),
|
||||
$container->get(\Szurubooru\Controllers\PostController::class),
|
||||
$container->get(\Szurubooru\Controllers\PostContentController::class),
|
||||
$container->get(\Szurubooru\Controllers\GlobalParamController::class),
|
||||
];
|
||||
}),
|
||||
];
|
||||
|
|
|
@ -60,6 +60,28 @@ final class PostDaoTest extends \Szurubooru\Tests\AbstractDatabaseTestCase
|
|||
];
|
||||
|
||||
$this->assertEntitiesEqual($expected, $actual);
|
||||
$this->assertEquals(count($expected), $postDao->getCount());
|
||||
}
|
||||
|
||||
public function testGettingTotalFileSize()
|
||||
{
|
||||
$postDao = $this->getPostDao();
|
||||
|
||||
$post1 = $this->getPost();
|
||||
$post2 = $this->getPost();
|
||||
$post3 = $this->getPost();
|
||||
$post1->setOriginalFileSize(1249812);
|
||||
$post2->setOriginalFileSize(128);
|
||||
$post3->setOriginalFileSize(null);
|
||||
$postDao->save($post1);
|
||||
$postDao->save($post2);
|
||||
$postDao->save($post3);
|
||||
$expectedFileSize =
|
||||
$post1->getOriginalFileSize() +
|
||||
$post2->getOriginalFileSize() +
|
||||
$post3->getOriginalFileSize();
|
||||
$this->assertGreaterThan(0, $expectedFileSize);
|
||||
$this->assertEquals($expectedFileSize, $postDao->getTotalFileSize());
|
||||
}
|
||||
|
||||
public function testGettingById()
|
||||
|
|
Loading…
Reference in a new issue