mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-13 13:03:20 +00:00
PHRAS-3442_optimize-list-notifications_4.1-bis
WIP : pushed to run all tests on circle move "session/notifications" controller to "user/notifications" use twig to render notifs + dropdown + dlg fixed some tests still todo : mark "read"
This commit is contained in:
@@ -9,12 +9,133 @@
|
||||
*/
|
||||
namespace Alchemy\Phrasea\Controller\User;
|
||||
|
||||
use Alchemy\Phrasea\Application\Helper\EntityManagerAware;
|
||||
use Alchemy\Phrasea\Controller\Controller;
|
||||
use Alchemy\Phrasea\Model\Repositories\BasketRepository;
|
||||
use Alchemy\Phrasea\Utilities\Stopwatch;
|
||||
use eventsmanager_broker;
|
||||
use Exception;
|
||||
use Symfony\Component\HttpFoundation\JsonResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Session\Session;
|
||||
|
||||
class UserNotificationController extends Controller
|
||||
{
|
||||
use EntityManagerAware;
|
||||
|
||||
/**
|
||||
* Check things to notify
|
||||
*
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function getNotifications(Request $request)
|
||||
{
|
||||
$stopwatch = new Stopwatch('notif');
|
||||
|
||||
if (!$request->isXmlHttpRequest()) {
|
||||
$this->app->abort(400);
|
||||
}
|
||||
|
||||
$ret = [
|
||||
'status' => 'unknown',
|
||||
'message' => '',
|
||||
'notifications' => [],
|
||||
'unread_basket_ids' => []
|
||||
];
|
||||
|
||||
$authenticator = $this->getAuthenticator();
|
||||
|
||||
if (!$authenticator->isAuthenticated()) {
|
||||
$ret['status'] = 'disconnected';
|
||||
|
||||
return $this->app->json($ret);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->getApplicationBox()->get_connection();
|
||||
}
|
||||
catch (Exception $e) {
|
||||
return $this->app->json($ret);
|
||||
}
|
||||
|
||||
$ret['status'] = 'ok';
|
||||
|
||||
$stopwatch->lap("start");
|
||||
|
||||
// get notifications from "notifications" table
|
||||
//
|
||||
|
||||
$offset = (int)$request->get('offset', 0);
|
||||
$limit = (int)$request->get('limit', 10);
|
||||
$what = (int)$request->get('what', eventsmanager_broker::UNREAD | eventsmanager_broker::READ);
|
||||
|
||||
$notifications = $this->getEventsManager()->get_notifications($offset, $limit, $what, $stopwatch);
|
||||
|
||||
$stopwatch->lap("get_notifications done");
|
||||
|
||||
// add html to each notif
|
||||
foreach ($notifications['notifications'] as $k => $v) {
|
||||
$notifications['notifications'][$k]['html'] = $this->render('prod/notification.html.twig', [
|
||||
'notification' => $v
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$ret['notifications'] = $notifications;
|
||||
|
||||
$stopwatch->lap("render done");
|
||||
|
||||
|
||||
// get unread baskets
|
||||
//
|
||||
|
||||
$baskets = $this->getBasketRepository()->findUnreadActiveByUser($authenticator->getUser());
|
||||
|
||||
$stopwatch->lap("baskets::findUnreadActiveByUser done");
|
||||
|
||||
foreach ($baskets as $basket) {
|
||||
$ret['unread_basket_ids'][] = $basket->getId();
|
||||
}
|
||||
|
||||
|
||||
// add message about maintenance
|
||||
//
|
||||
|
||||
if (in_array($this->getSession()->get('phraseanet.message'), ['1', null])) {
|
||||
if ($this->app['phraseanet.configuration']['main']['maintenance']) {
|
||||
$ret['message'] .= $this->app->trans('The application is going down for maintenance, please logout.');
|
||||
}
|
||||
|
||||
if ($this->getConf()->get(['registry', 'maintenance', 'enabled'], false)) {
|
||||
$ret['message'] .= strip_tags($this->getConf()->get(['registry', 'maintenance', 'message']));
|
||||
}
|
||||
}
|
||||
|
||||
$stopwatch->lap("end");
|
||||
$stopwatch->stop();
|
||||
|
||||
$response = new JsonResponse($ret);
|
||||
|
||||
// add specific timing debug
|
||||
$response->headers->set('Server-Timing', $stopwatch->getLapsesAsServerTimingHeader(), false);
|
||||
$response->setCharset('UTF-8');
|
||||
|
||||
// add general timing debug
|
||||
$duration = (microtime(true) - $request->server->get('REQUEST_TIME_FLOAT')) * 1000.0;
|
||||
$h = '_global;' . 'dur=' . $duration;
|
||||
$response->headers->set('Server-Timing', $h, false); // false : add header (don't replace)
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Set notifications as read
|
||||
*
|
||||
@@ -61,7 +182,7 @@ class UserNotificationController extends Controller
|
||||
*/
|
||||
|
||||
/**
|
||||
* @return \eventsmanager_broker
|
||||
* @return eventsmanager_broker
|
||||
*/
|
||||
/* remove in favor of existing /session/ route
|
||||
private function getEventsManager()
|
||||
@@ -69,4 +190,35 @@ class UserNotificationController extends Controller
|
||||
return $this->app['events-manager'];
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @return eventsmanager_broker
|
||||
*/
|
||||
private function getEventsManager()
|
||||
{
|
||||
return $this->app['events-manager'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BasketRepository
|
||||
*/
|
||||
private function getBasketRepository()
|
||||
{
|
||||
/** @var BasketRepository $ret */
|
||||
$ret = $this->getEntityManager()->getRepository('Phraseanet:Basket');
|
||||
|
||||
return $ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Session
|
||||
*/
|
||||
private function getSession()
|
||||
{
|
||||
return $this->app['session'];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user