Added unread messages as global var

This commit is contained in:
Igor Scheller 2022-06-06 12:32:07 +02:00
parent 865873c099
commit 7acd34ba74
10 changed files with 19 additions and 71 deletions

View File

@ -72,7 +72,6 @@ $includeFiles = [
__DIR__ . '/../includes/pages/admin_shifts.php', __DIR__ . '/../includes/pages/admin_shifts.php',
__DIR__ . '/../includes/pages/admin_user.php', __DIR__ . '/../includes/pages/admin_user.php',
__DIR__ . '/../includes/pages/guest_login.php', __DIR__ . '/../includes/pages/guest_login.php',
__DIR__ . '/../includes/pages/user_messages.php',
__DIR__ . '/../includes/pages/user_myshifts.php', __DIR__ . '/../includes/pages/user_myshifts.php',
__DIR__ . '/../includes/pages/user_settings.php', __DIR__ . '/../includes/pages/user_settings.php',
__DIR__ . '/../includes/pages/user_shifts.php', __DIR__ . '/../includes/pages/user_shifts.php',

View File

@ -1,14 +0,0 @@
<?php
use Engelsystem\Controllers\MessagesController;
/**
* @return string
*/
function user_unread_messages()
{
$count = app()->make(MessagesController::class)
->numberOfUnreadMessages();
return $count > 0 ? ' <span class="badge bg-danger">' . $count . '</span>' : '';
}

View File

@ -44,7 +44,12 @@
{% endif %} {% endif %}
{% if is_user() and has_permission_to('user_messages') %} {% if is_user() and has_permission_to('user_messages') %}
{{ _self.toolbar_item(menuUserMessages(), url('messages'), 'messages', 'envelope') }} {{ _self.toolbar_item(
user_messages ? '<span class="badge bg-danger">' ~ user_messages ~ '</span>' : '',
url('messages'),
'messages',
'envelope'
) }}
{% endif %} {% endif %}
{{ menuUserHints() }} {{ menuUserHints() }}

View File

@ -202,17 +202,6 @@ class MessagesController extends BaseController
return $this->redirect->to('/messages/' . $otherUserId); return $this->redirect->to('/messages/' . $otherUserId);
} }
/**
* Returns the overall unread messages of the current user.
*/
public function numberOfUnreadMessages(): int
{
return $this->auth->user()
->messagesReceived()
->where('read', false)
->count();
}
/** /**
* The number of unread messages per conversation of the current user. * The number of unread messages per conversation of the current user.
* @return Collection of unread message amounts. Each object with key=other user, value=amount of unread messages * @return Collection of unread message amounts. Each object with key=other user, value=amount of unread messages

View File

@ -36,11 +36,16 @@ class Globals extends TwigExtension implements GlobalsInterface
{ {
$user = $this->auth->user(); $user = $this->auth->user();
$themes = config('themes'); $themes = config('themes');
$userMessages = null;
if ($user === null) { if ($user === null) {
$themeId = config('theme'); $themeId = config('theme');
} else { } else {
$themeId = $user->settings->theme; $themeId = $user->settings->theme;
$userMessages = $user
->messagesReceived()
->where('read', false)
->count();
} }
$query = $this->request->query->get('theme'); $query = $this->request->query->get('theme');
@ -55,10 +60,11 @@ class Globals extends TwigExtension implements GlobalsInterface
$theme = $themes[$themeId]; $theme = $themes[$themeId];
return [ return [
'user' => $user ?? [], 'user' => $user ?? [],
'request' => $this->request, 'user_messages' => $userMessages,
'themeId' => $themeId, 'request' => $this->request,
'theme' => $theme, 'themeId' => $themeId,
'theme' => $theme,
]; ];
} }
} }

View File

@ -28,7 +28,6 @@ class Legacy extends TwigExtension
return [ return [
new TwigFunction('menu', 'make_navigation', $isSafeHtml), new TwigFunction('menu', 'make_navigation', $isSafeHtml),
new TwigFunction('menuUserShiftState', 'User_shift_state_render', $isSafeHtml), new TwigFunction('menuUserShiftState', 'User_shift_state_render', $isSafeHtml),
new TwigFunction('menuUserMessages', 'user_unread_messages', $isSafeHtml),
new TwigFunction('menuUserHints', 'header_render_hints', $isSafeHtml), new TwigFunction('menuUserHints', 'header_render_hints', $isSafeHtml),
new TwigFunction('menuUserSubmenu', 'make_user_submenu', $isSafeHtml), new TwigFunction('menuUserSubmenu', 'make_user_submenu', $isSafeHtml),
new TwigFunction('page', [$this, 'getPage']), new TwigFunction('page', [$this, 'getPage']),

View File

@ -519,43 +519,6 @@ class MessagesControllerTest extends ControllerTest
$this->assertEquals(0, count(Message::whereId($msg->id)->get())); $this->assertEquals(0, count(Message::whereId($msg->id)->get()));
} }
/**
* @testdox NumberOfUnreadMessages: withNoMessages -> returns0
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessages
*/
public function testNumberOfUnreadMessagesWithNoMessagesReturns0()
{
$this->assertEquals(0, $this->controller->numberOfUnreadMessages());
}
/**
* @testdox NumberOfUnreadMessages: withMessagesNotToMe -> messagesNotToMeAreIgnored
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessages
*/
public function testNumberOfUnreadMessagesWithMessagesNotToMeMessagesNotToMeAreIgnored()
{
$userC = User::factory(['name' => 'c'])->create();
$this->createMessage($this->userA, $this->userB, 'a>b', $this->now);
$this->createMessage($this->userB, $userC, 'b>c', $this->now);
$this->assertEquals(0, $this->controller->numberOfUnreadMessages());
}
/**
* @testdox NumberOfUnreadMessages: withMessages -> returnsSumOfUnreadMessagesSentToMe
* @covers \Engelsystem\Controllers\MessagesController::numberOfUnreadMessages
*/
public function testNumberOfUnreadMessagesWithMessagesReturnsSumOfUnreadMessagesSentToMe()
{
$userC = User::factory(['name' => 'c'])->create();
$this->createMessage($this->userB, $this->userA, 'b>a1', $this->now);
$this->createMessage($this->userB, $this->userA, 'b>a2', $this->now);
$this->createMessage($userC, $this->userA, 'c>a', $this->now);
$this->assertEquals(3, $this->controller->numberOfUnreadMessages());
}
/** /**
* Setup environment * Setup environment
*/ */

View File

@ -75,7 +75,7 @@ abstract class ExtensionTest extends TestCase
*/ */
protected function assertGlobalsExists($name, $value, $globals) protected function assertGlobalsExists($name, $value, $globals)
{ {
if (isset($globals[$name])) { if (array_key_exists($name, $globals)) {
$this->assertArraySubset([$name => $value], $globals); $this->assertArraySubset([$name => $value], $globals);
return; return;

View File

@ -51,6 +51,7 @@ class GlobalsTest extends ExtensionTest
// No user // No user
$globals = $extension->getGlobals(); $globals = $extension->getGlobals();
$this->assertGlobalsExists('user', [], $globals); $this->assertGlobalsExists('user', [], $globals);
$this->assertGlobalsExists('user_messages', null, $globals);
$this->assertGlobalsExists('request', $request, $globals); $this->assertGlobalsExists('request', $request, $globals);
$this->assertGlobalsExists('themeId', 23, $globals); $this->assertGlobalsExists('themeId', 23, $globals);
$this->assertGlobalsExists('theme', $theme2, $globals); $this->assertGlobalsExists('theme', $theme2, $globals);
@ -58,6 +59,7 @@ class GlobalsTest extends ExtensionTest
// User // User
$globals = $extension->getGlobals(); $globals = $extension->getGlobals();
$this->assertGlobalsExists('user', $user, $globals); $this->assertGlobalsExists('user', $user, $globals);
$this->assertGlobalsExists('user_messages', 0, $globals);
$this->assertGlobalsExists('themeId', 42, $globals); $this->assertGlobalsExists('themeId', 42, $globals);
$this->assertGlobalsExists('theme', $theme, $globals); $this->assertGlobalsExists('theme', $theme, $globals);

View File

@ -22,7 +22,6 @@ class LegacyTest extends ExtensionTest
$this->assertExtensionExists('menu', 'make_navigation', $functions, $isSafeHtml); $this->assertExtensionExists('menu', 'make_navigation', $functions, $isSafeHtml);
$this->assertExtensionExists('menuUserShiftState', 'User_shift_state_render', $functions, $isSafeHtml); $this->assertExtensionExists('menuUserShiftState', 'User_shift_state_render', $functions, $isSafeHtml);
$this->assertExtensionExists('menuUserMessages', 'user_unread_messages', $functions, $isSafeHtml);
$this->assertExtensionExists('menuUserHints', 'header_render_hints', $functions, $isSafeHtml); $this->assertExtensionExists('menuUserHints', 'header_render_hints', $functions, $isSafeHtml);
$this->assertExtensionExists('menuUserSubmenu', 'make_user_submenu', $functions, $isSafeHtml); $this->assertExtensionExists('menuUserSubmenu', 'make_user_submenu', $functions, $isSafeHtml);
$this->assertExtensionExists('page', [$extension, 'getPage'], $functions); $this->assertExtensionExists('page', [$extension, 'getPage'], $functions);