Skip to content

Commit

Permalink
Add badge in sidebar to indicate unread council messages
Browse files Browse the repository at this point in the history
  • Loading branch information
WaveHack committed Jun 13, 2019
1 parent 3e53183 commit 9a2e17a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/). This project uses its own versioning system.

## [Unreleased]
### Added
- Added unread count badge to the council page menu item in the sidebar to indicate new messages since your last council visit

### Fixed
- Fixed unit OP/DP on military training page to show with including certain bonuses

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddCouncilLastReadColumnToDominions extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('dominions', function (Blueprint $table) {
$table->dateTime('council_last_read')
->nullable()
->after('building_dock');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('dominions', function (Blueprint $table) {
$table->dropColumn('council_last_read');
});
}
}
2 changes: 1 addition & 1 deletion app/resources/views/partials/main-sidebar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
<li class="{{ Route::is('dominion.espionage') ? 'active' : null }}"><a href="{{ route('dominion.espionage') }}"><i class="fa fa-user-secret fa-fw"></i> <span>Espionage</span></a></li>

<li class="header">COMMS</li>
<li class="{{ Route::is('dominion.council*') ? 'active' : null }}"><a href="{{ route('dominion.council') }}"><i class="fa fa-group ra-fw"></i> <span>The Council</span></a></li>
<li class="{{ Route::is('dominion.council*') ? 'active' : null }}"><a href="{{ route('dominion.council') }}"><i class="fa fa-group ra-fw"></i> <span>The Council</span> {!! $councilUnreadCount > 0 ? ('<span class="pull-right-container"><small class="label pull-right bg-green">' . $councilUnreadCount . '</small></span>') : null !!}</a></li>
<li class="{{ Route::is('dominion.op-center*') ? 'active' : null }}"><a href="{{ route('dominion.op-center') }}"><i class="fa fa-globe ra-fw"></i> <span>Op Center</span></a></li>

<li class="header">REALM</li>
Expand Down
13 changes: 11 additions & 2 deletions src/Http/Controllers/Dominion/CouncilController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
namespace OpenDominion\Http\Controllers\Dominion;

use Exception;
use Illuminate\Support\Facades\DB;
use OpenDominion\Http\Requests\Dominion\Council\CreatePostRequest;
use OpenDominion\Http\Requests\Dominion\Council\CreateThreadRequest;
use OpenDominion\Models\Council;
use OpenDominion\Services\Analytics\AnalyticsEvent;
use OpenDominion\Models\Dominion;
use OpenDominion\Services\CouncilService;
use RuntimeException;

Expand All @@ -16,6 +15,7 @@ class CouncilController extends AbstractDominionController
public function getIndex()
{
$dominion = $this->getSelectedDominion();
$this->updateDominionCouncilLastRead($dominion);
$councilService = app(CouncilService::class);

return view('pages.dominion.council.index', [
Expand Down Expand Up @@ -69,6 +69,9 @@ public function getThread(Council\Thread $thread)
{
$this->guardAgainstCrossRealm($thread);

$dominion = $this->getSelectedDominion();
$this->updateDominionCouncilLastRead($dominion);

$thread->load(['dominion.user', 'posts.dominion.user']);

return view('pages.dominion.council.thread', compact(
Expand Down Expand Up @@ -112,4 +115,10 @@ protected function guardAgainstCrossRealm(Council\Thread $thread)
throw new RuntimeException('No permission to view thread'); // todo: modelnotfoundexception?
}
}

protected function updateDominionCouncilLastRead(Dominion $dominion): void
{
$dominion->council_last_read = now();
$dominion->save();
}
}
1 change: 1 addition & 0 deletions src/Models/Dominion.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
* @property int $building_shrine
* @property int $building_barracks
* @property int $building_dock
* @property \Illuminate\Support\Carbon|null $council_last_read
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int|null $pack_id
Expand Down
35 changes: 35 additions & 0 deletions src/Providers/ComposerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Illuminate\Contracts\View\View;
use OpenDominion\Calculators\NetworthCalculator;
use OpenDominion\Helpers\NotificationHelper;
use OpenDominion\Models\Council\Post;
use OpenDominion\Models\Council\Thread;
use OpenDominion\Models\Dominion;
use OpenDominion\Services\Dominion\SelectorService;

class ComposerServiceProvider extends AbstractServiceProvider
Expand All @@ -21,6 +24,38 @@ public function boot()
$view->with('selectorService', app(SelectorService::class));
});

view()->composer('partials.main-sidebar', function (View $view) {
$selectorService = app(SelectorService::class);

if (!$selectorService->hasUserSelectedDominion()) {
return;
}

/** @var Dominion $dominion */
$dominion = $selectorService->getUserSelectedDominion();

$lastRead = $dominion->council_last_read;

$councilUnreadCount = $dominion->realm
->councilThreads()
->with('posts')
->get()
->map(static function (Thread $thread) use ($lastRead) {
$unreadCount = $thread->posts->filter(static function (Post $post) use ($lastRead) {
return $post->created_at > $lastRead;
})->count();

if ($thread->created_at > $lastRead) {
$unreadCount++;
}

return $unreadCount;
})
->sum();

$view->with('councilUnreadCount', $councilUnreadCount);
});

view()->composer('partials.main-footer', function (View $view) {
$version = (Cache::has('version-html') ? Cache::get('version-html') : 'unknown');
$view->with('version', $version);
Expand Down

0 comments on commit 9a2e17a

Please sign in to comment.