Added filtering to activity lists. Fixes #14.

This commit is contained in:
Dan Brown 2015-08-30 11:47:58 +01:00
parent f37a886205
commit 8af012bc2a
7 changed files with 90 additions and 70 deletions

View File

@ -12,6 +12,11 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class Activity extends Model class Activity extends Model
{ {
/**
* Get the entity for this activity.
* @return bool
*/
public function entity() public function entity()
{ {
if ($this->entity_id) { if ($this->entity_id) {
@ -21,6 +26,10 @@ class Activity extends Model
} }
} }
/**
* Get the user this activity relates to.
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user() public function user()
{ {
return $this->belongsTo('Oxbow\User'); return $this->belongsTo('Oxbow\User');
@ -35,4 +44,13 @@ class Activity extends Model
return trans('activities.' . $this->key); return trans('activities.' . $this->key);
} }
/**
* Checks if another Activity matches the general information of another.
* @param $activityB
* @return bool
*/
public function isSimilarTo($activityB) {
return [$this->key, $this->entitiy_type, $this->entitiy_id] === [$activityB->key, $activityB->entitiy_type, $activityB->entitiy_id];
}
} }

View File

@ -37,15 +37,4 @@ class Book extends Entity
return $pages->sortBy('priority'); return $pages->sortBy('priority');
} }
/**
* Gets only the most recent activity for this book
* @param int $limit
* @param int $page
* @return mixed
*/
public function recentActivity($limit = 20, $page=0)
{
return $this->hasMany('Oxbow\Activity')->orderBy('created_at', 'desc')->skip($limit*$page)->take($limit)->get();
}
} }

View File

@ -44,15 +44,4 @@ class Entity extends Model
return $this->morphMany('Oxbow\Activity', 'entity')->orderBy('created_at', 'desc'); return $this->morphMany('Oxbow\Activity', 'entity')->orderBy('created_at', 'desc');
} }
/**
* Gets only the most recent activity
* @param int $limit
* @param int $page
* @return mixed
*/
public function recentActivity($limit = 20, $page=0)
{
return $this->activity()->skip($limit*$page)->take($limit)->get();
}
} }

View File

@ -1,21 +1,6 @@
<?php <?php
/* // Authenticated routes...
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
Route::get('/test', function () {
return Auth::user()->can('users-edit');
});
// Authentication routes...
Route::group(['middleware' => 'auth'], function () { Route::group(['middleware' => 'auth'], function () {
Route::group(['prefix' => 'books'], function () { Route::group(['prefix' => 'books'], function () {
@ -40,6 +25,7 @@ Route::group(['middleware' => 'auth'], function () {
Route::get('/{bookSlug}/page/{pageSlug}/delete', 'PageController@showDelete'); Route::get('/{bookSlug}/page/{pageSlug}/delete', 'PageController@showDelete');
Route::put('/{bookSlug}/page/{pageSlug}', 'PageController@update'); Route::put('/{bookSlug}/page/{pageSlug}', 'PageController@update');
Route::delete('/{bookSlug}/page/{pageSlug}', 'PageController@destroy'); Route::delete('/{bookSlug}/page/{pageSlug}', 'PageController@destroy');
//Revisions //Revisions
Route::get('/{bookSlug}/page/{pageSlug}/revisions', 'PageController@showRevisions'); Route::get('/{bookSlug}/page/{pageSlug}/revisions', 'PageController@showRevisions');
Route::get('/{bookSlug}/page/{pageSlug}/revisions/{revId}', 'PageController@showRevision'); Route::get('/{bookSlug}/page/{pageSlug}/revisions/{revId}', 'PageController@showRevision');

View File

@ -88,6 +88,44 @@ class ActivityService
->skip($count * $page)->take($count)->get(); ->skip($count * $page)->take($count)->get();
} }
/**
* Gets the latest activity for an entitiy, Filtering out similar
* items to prevent a message activity list.
* @param Entity $entity
* @param int $count
* @param int $page
* @return array
*/
function entityActivity($entity, $count = 20, $page = 0)
{
$activity = $entity->hasMany('Oxbow\Activity')->orderBy('created_at', 'desc')
->skip($count*$page)->take($count)->get();
return $this->filterSimilar($activity);
}
/**
* Filters out similar acitivity.
* @param Activity[] $activity
* @return array
*/
protected function filterSimilar($activity) {
$newActivity = [];
$previousItem = false;
foreach($activity as $activityItem) {
if($previousItem === false) {
$previousItem = $activityItem;
$newActivity[] = $activityItem;
continue;
}
if(!$activityItem->isSimilarTo($previousItem)) {
$newActivity[] = $activityItem;
}
$previousItem = $activityItem;
}
return $newActivity;
}
/** /**
* Flashes a notification message to the session if an appropriate message is available. * Flashes a notification message to the session if an appropriate message is available.
* @param $activityKey * @param $activityKey

View File

@ -73,7 +73,7 @@
<div class="col-md-3 col-md-offset-1"> <div class="col-md-3 col-md-offset-1">
<div class="margin-top large"><br></div> <div class="margin-top large"><br></div>
<h3>Recent Activity</h3> <h3>Recent Activity</h3>
@include('partials/activity-list', ['activity' => $book->recentActivity()]) @include('partials/activity-list', ['activity' => Activity::entityActivity($book, 20, 0)])
</div> </div>
</div> </div>