]> BookStack Code Mirror - bookstack/commitdiff
Added filtering to activity lists. Fixes #14.
authorDan Brown <redacted>
Sun, 30 Aug 2015 10:47:58 +0000 (11:47 +0100)
committerDan Brown <redacted>
Sun, 30 Aug 2015 10:47:58 +0000 (11:47 +0100)
app/Activity.php
app/Book.php
app/Entity.php
app/Http/routes.php
app/Services/ActivityService.php
resources/lang/en/activities.php
resources/views/books/show.blade.php

index 64dd2351816052181b8064298a319f5ad510aa14..bbbe5e89311ae6d37b21a3197a2a5b1d02f1fc0c 100644 (file)
@@ -5,22 +5,31 @@ namespace Oxbow;
 use Illuminate\Database\Eloquent\Model;
 
 /**
- * @property string key
- * @property \User user
+ * @property string  key
+ * @property \User   user
  * @property \Entity entity
- * @property string extra
+ * @property string  extra
  */
 class Activity extends Model
 {
+
+    /**
+     * Get the entity for this activity.
+     * @return bool
+     */
     public function entity()
     {
-        if($this->entity_id) {
+        if ($this->entity_id) {
             return $this->morphTo('entity')->first();
         } else {
             return false;
         }
     }
 
+    /**
+     * Get the user this activity relates to.
+     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
+     */
     public function user()
     {
         return $this->belongsTo('Oxbow\User');
@@ -35,4 +44,13 @@ class Activity extends Model
         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];
+    }
+
 }
index c4e691431c5e7a6a288eff184e25404d7dcfdf95..8a4be213f5c73681a1a8a522880cff689283ebd3 100644 (file)
@@ -37,15 +37,4 @@ class Book extends Entity
         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();
-    }
-
 }
index a09ba2e45554fc1eb9574d4b4994c2d5b9d215af..2e047cbdb90daed28558d5f41fd9d35d71e19e64 100644 (file)
@@ -44,15 +44,4 @@ class Entity extends Model
         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();
-    }
-
 }
index 63b0d5262f4de2c032a733610bf864b50af2808a..eb945c72f64b8220e477ec4301367c9072942833 100644 (file)
@@ -1,21 +1,6 @@
 <?php
 
-/*
-|--------------------------------------------------------------------------
-| 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...
+// Authenticated routes...
 Route::group(['middleware' => 'auth'], function () {
 
     Route::group(['prefix' => 'books'], function () {
@@ -40,6 +25,7 @@ Route::group(['middleware' => 'auth'], function () {
         Route::get('/{bookSlug}/page/{pageSlug}/delete', 'PageController@showDelete');
         Route::put('/{bookSlug}/page/{pageSlug}', 'PageController@update');
         Route::delete('/{bookSlug}/page/{pageSlug}', 'PageController@destroy');
+
         //Revisions
         Route::get('/{bookSlug}/page/{pageSlug}/revisions', 'PageController@showRevisions');
         Route::get('/{bookSlug}/page/{pageSlug}/revisions/{revId}', 'PageController@showRevision');
index 1691f06a10344407204b492c5f0684eb3cfa728b..a4259bf12dfbbb41c0d364fb5404e5f71454feff 100644 (file)
@@ -23,16 +23,16 @@ class ActivityService
     /**
      * Add activity data to database.
      * @param Entity $entity
-     * @param $activityKey
-     * @param int $bookId
-     * @param bool $extra
+     * @param        $activityKey
+     * @param int    $bookId
+     * @param bool   $extra
      */
     public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
     {
         $this->activity->user_id = $this->user->id;
         $this->activity->book_id = $bookId;
         $this->activity->key = strtolower($activityKey);
-        if($extra !== false) {
+        if ($extra !== false) {
             $this->activity->extra = $extra;
         }
         $entity->activity()->save($this->activity);
@@ -41,8 +41,8 @@ class ActivityService
 
     /**
      * Adds a activity history with a message & without binding to a entitiy.
-     * @param $activityKey
-     * @param int $bookId
+     * @param            $activityKey
+     * @param int        $bookId
      * @param bool|false $extra
      */
     public function addMessage($activityKey, $bookId = 0, $extra = false)
@@ -50,7 +50,7 @@ class ActivityService
         $this->activity->user_id = $this->user->id;
         $this->activity->book_id = $bookId;
         $this->activity->key = strtolower($activityKey);
-        if($extra !== false) {
+        if ($extra !== false) {
             $this->activity->extra = $extra;
         }
         $this->activity->save();
@@ -68,7 +68,7 @@ class ActivityService
     public function removeEntity(Entity $entity)
     {
         $activities = $entity->activity;
-        foreach($activities as $activity) {
+        foreach ($activities as $activity) {
             $activity->extra = $entity->name;
             $activity->entity_id = 0;
             $activity->entity_type = null;
@@ -85,7 +85,45 @@ class ActivityService
     public function latest($count = 20, $page = 0)
     {
         return $this->activity->orderBy('created_at', 'desc')
+            ->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;
     }
 
     /**
index ea18d6693fd772d63b80aa8939406b56b6111bde..7a3bcd7602e7f967f0057310c29e59f9d535ebc3 100644 (file)
@@ -8,31 +8,31 @@ return [
      */
 
     // Pages
-    'page_create' => 'created page',
-    'page_create_notification' => 'Page Successfully Created',
-    'page_update' => 'updated page',
-    'page_update_notification' => 'Page Successfully Updated',
-    'page_delete' => 'deleted page',
-    'page_delete_notification' => 'Page Successfully Created',
-    'page_restore' => 'restored page',
-    'page_restore_notification' => 'Page Successfully Restored',
+    'page_create'                 => 'created page',
+    'page_create_notification'    => 'Page Successfully Created',
+    'page_update'                 => 'updated page',
+    'page_update_notification'    => 'Page Successfully Updated',
+    'page_delete'                 => 'deleted page',
+    'page_delete_notification'    => 'Page Successfully Created',
+    'page_restore'                => 'restored page',
+    'page_restore_notification'   => 'Page Successfully Restored',
 
     // Chapters
-    'chapter_create' => 'created chapter',
+    'chapter_create'              => 'created chapter',
     'chapter_create_notification' => 'Chapter Successfully Created',
-    'chapter_update' => 'updated chapter',
+    'chapter_update'              => 'updated chapter',
     'chapter_update_notification' => 'Chapter Successfully Updated',
-    'chapter_delete' => 'deleted chapter',
+    'chapter_delete'              => 'deleted chapter',
     'chapter_delete_notification' => 'Chapter Successfully Deleted',
 
     // Books
-    'book_create' => 'created book',
-    'book_create_notification' => 'Book Successfully Created',
-    'book_update' => 'updated book',
-    'book_update_notification' => 'Book Successfully Updated',
-    'book_delete' => 'deleted book',
-    'book_delete_notification' => 'Book Successfully Deleted',
-    'book_sort'   => 'sorted book',
-    'book_sort_notification' => 'Book Successfully Re-sorted',
+    'book_create'                 => 'created book',
+    'book_create_notification'    => 'Book Successfully Created',
+    'book_update'                 => 'updated book',
+    'book_update_notification'    => 'Book Successfully Updated',
+    'book_delete'                 => 'deleted book',
+    'book_delete_notification'    => 'Book Successfully Deleted',
+    'book_sort'                   => 'sorted book',
+    'book_sort_notification'      => 'Book Successfully Re-sorted',
 
 ];
\ No newline at end of file
index 9b9e00401b22ff274bb098ecdd649256efb510aa..4f007e2439e04f58c8f626344b26a1bab31c1c41 100644 (file)
@@ -73,7 +73,7 @@
         <div class="col-md-3 col-md-offset-1">
             <div class="margin-top large"><br></div>
             <h3>Recent Activity</h3>
-            @include('partials/activity-list', ['activity' => $book->recentActivity()])
+            @include('partials/activity-list', ['activity' => Activity::entityActivity($book, 20, 0)])
         </div>
     </div>