From 1f8a4475e93f94c446cb3847b1f94bcff117f5d1 Mon Sep 17 00:00:00 2001 From: Martin Zurowietz Date: Tue, 13 Sep 2016 10:36:58 +0200 Subject: [PATCH 1/9] Implement annotation sessions for transect label filter endpoint References BiodataMiningGroup/dias-transects#12 --- .../Api/TransectsAnnotationsController.php | 16 +++- src/Jobs/GenerateAnnotationPatch.php | 6 +- ...sApiTransectsAnnotationsControllerTest.php | 86 +++++++++++++++++++ 3 files changed, 102 insertions(+), 6 deletions(-) diff --git a/src/Http/Controllers/Api/TransectsAnnotationsController.php b/src/Http/Controllers/Api/TransectsAnnotationsController.php index b5410b9b..0b813a82 100644 --- a/src/Http/Controllers/Api/TransectsAnnotationsController.php +++ b/src/Http/Controllers/Api/TransectsAnnotationsController.php @@ -5,6 +5,7 @@ use Dias\Transect; use Dias\Annotation; use Illuminate\Http\Request; +use Illuminate\Contracts\Auth\Guard; use Dias\Http\Controllers\Api\Controller; class TransectsAnnotationsController extends Controller @@ -22,25 +23,34 @@ class TransectsAnnotationsController extends Controller * @apiDescription Returns a list of annotation IDs * * @param Request $request + * @param Guard $auth * @param int $tid Transect ID * @param int $lid Label ID * @return \Illuminate\Http\Response */ - public function filter(Request $request, $tid, $lid) + public function filter(Request $request, Guard $auth, $tid, $lid) { $transect = Transect::findOrFail($tid); $this->authorize('access', $transect); $this->validate($request, ['take' => 'integer']); $take = $request->input('take'); - return Annotation::join('annotation_labels', 'annotations.id', '=', 'annotation_labels.annotation_id') + $session = $transect->activeAnnotationSession; + + if ($session) { + $query = $session->annotations($auth->user()); + } else { + $query = Annotation::query(); + } + + return $query->join('annotation_labels', 'annotations.id', '=', 'annotation_labels.annotation_id') ->whereIn('annotations.image_id', function ($query) use ($tid) { $query->select('id') ->from('images') ->where('transect_id', $tid); }) ->where('annotation_labels.label_id', $lid) - ->when($take !== null, function ($query) use ($take) { + ->when(!is_null($take), function ($query) use ($take) { return $query->orderBy('annotations.created_at', 'desc') ->take($take); }) diff --git a/src/Jobs/GenerateAnnotationPatch.php b/src/Jobs/GenerateAnnotationPatch.php index 990b783f..3c14f232 100644 --- a/src/Jobs/GenerateAnnotationPatch.php +++ b/src/Jobs/GenerateAnnotationPatch.php @@ -2,13 +2,13 @@ namespace Dias\Modules\Ate\Jobs; -use Dias\Annotation; +use File; use Dias\Shape; use Dias\Jobs\Job; +use Dias\Annotation; +use InterventionImage as IImage; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; -use InterventionImage as IImage; -use File; class GenerateAnnotationPatch extends Job implements ShouldQueue { diff --git a/tests/Http/Controllers/Api/AteModuleHttpControllersApiTransectsAnnotationsControllerTest.php b/tests/Http/Controllers/Api/AteModuleHttpControllersApiTransectsAnnotationsControllerTest.php index c3eec815..33c1ac91 100644 --- a/tests/Http/Controllers/Api/AteModuleHttpControllersApiTransectsAnnotationsControllerTest.php +++ b/tests/Http/Controllers/Api/AteModuleHttpControllersApiTransectsAnnotationsControllerTest.php @@ -1,5 +1,6 @@ assertResponseOk(); $this->seeJsonEquals($expect3); } + + public function testFilterAnnotationSession() + { + $id = $this->transect()->id; + $image = ImageTest::create(['transect_id' => $id]); + + $a1 = AnnotationTest::create([ + 'image_id' => $image->id, + 'created_at' => Carbon::yesterday(), + ]); + + $a2 = AnnotationTest::create([ + 'image_id' => $image->id, + 'created_at' => Carbon::today(), + ]); + + $a3 = AnnotationTest::create([ + 'image_id' => $image->id, + 'created_at' => Carbon::yesterday(), + ]); + + $l1 = AnnotationLabelTest::create([ + 'annotation_id' => $a1->id, + 'user_id' => $this->editor()->id, + ]); + + $l2 = AnnotationLabelTest::create([ + 'annotation_id' => $a2->id, + 'label_id' => $l1->label_id, + 'user_id' => $this->editor()->id, + ]); + + $l3 = AnnotationLabelTest::create([ + 'annotation_id' => $a3->id, + 'label_id' => $l1->label_id, + 'user_id' => $this->admin()->id, + ]); + + $this->beEditor(); + + // test hide own + $session = AnnotationSessionTest::create([ + 'transect_id' => $id, + 'starts_at' => Carbon::today(), + 'ends_at' => Carbon::tomorrow(), + 'hide_own_annotations' => true, + 'hide_other_users_annotations' => false, + ]); + + $expect = [$a2->id, $a3->id]; + if ($this->isSqlite()) { + $expect = array_map('strval', $expect); + } + + $this->get("/api/v1/transects/{$id}/annotations/filter/label/{$l1->label_id}"); + $this->assertResponseOk(); + $this->seeJsonEquals($expect); + + // test hide other + $session->hide_own_annotations = false; + $session->hide_other_users_annotations = true; + $session->save(); + + $expect = [$a1->id, $a2->id]; + if ($this->isSqlite()) { + $expect = array_map('strval', $expect); + } + + $this->get("/api/v1/transects/{$id}/annotations/filter/label/{$l1->label_id}"); + $this->assertResponseOk(); + $this->seeJsonEquals($expect); + + // test hide both + $session->hide_own_annotations = true; + $session->save(); + + $expect = [$a2->id]; + if ($this->isSqlite()) { + $expect = array_map('strval', $expect); + } + + $this->get("/api/v1/transects/{$id}/annotations/filter/label/{$l1->label_id}"); + $this->assertResponseOk(); + $this->seeJsonEquals($expect); + } } From d46f23eaa8ad3228f8c14de82f42726159008516 Mon Sep 17 00:00:00 2001 From: Martin Zurowietz Date: Tue, 13 Sep 2016 10:57:52 +0200 Subject: [PATCH 2/9] Simplify project annotation filter endpoint --- .../Api/ProjectsAnnotationsController.php | 15 ++++++--------- ...ollersApiProjectsAnnotationsControllerTest.php | 1 - 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/Http/Controllers/Api/ProjectsAnnotationsController.php b/src/Http/Controllers/Api/ProjectsAnnotationsController.php index f606fba2..0949a489 100644 --- a/src/Http/Controllers/Api/ProjectsAnnotationsController.php +++ b/src/Http/Controllers/Api/ProjectsAnnotationsController.php @@ -35,17 +35,14 @@ public function filter(Request $request, $pid, $lid) $take = $request->input('take'); return Annotation::join('annotation_labels', 'annotations.id', '=', 'annotation_labels.annotation_id') - ->whereIn('annotations.image_id', function ($query) use ($pid) { - $query->select('id') - ->from('images') - ->whereIn('transect_id', function ($query) use ($pid) { - $query->select('transect_id') - ->from('project_transect') - ->where('project_id', $pid); - }); + ->join('images', 'annotations.image_id', '=', 'images.id') + ->whereIn('images.transect_id', function ($query) use ($pid) { + $query->select('transect_id') + ->from('project_transect') + ->where('project_id', $pid); }) ->where('annotation_labels.label_id', $lid) - ->when($take !== null, function ($query) use ($take) { + ->when(!is_null($take), function ($query) use ($take) { return $query->orderBy('annotations.created_at', 'desc') ->take($take); }) diff --git a/tests/Http/Controllers/Api/AteModuleHttpControllersApiProjectsAnnotationsControllerTest.php b/tests/Http/Controllers/Api/AteModuleHttpControllersApiProjectsAnnotationsControllerTest.php index 4f9e81b6..4c3c8ffe 100644 --- a/tests/Http/Controllers/Api/AteModuleHttpControllersApiProjectsAnnotationsControllerTest.php +++ b/tests/Http/Controllers/Api/AteModuleHttpControllersApiProjectsAnnotationsControllerTest.php @@ -1,6 +1,5 @@ Date: Tue, 13 Sep 2016 13:19:56 +0200 Subject: [PATCH 3/9] Use query scope for annotation session restrictions --- src/Http/Controllers/Api/TransectsAnnotationsController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/Controllers/Api/TransectsAnnotationsController.php b/src/Http/Controllers/Api/TransectsAnnotationsController.php index 0b813a82..fb315391 100644 --- a/src/Http/Controllers/Api/TransectsAnnotationsController.php +++ b/src/Http/Controllers/Api/TransectsAnnotationsController.php @@ -38,7 +38,7 @@ public function filter(Request $request, Guard $auth, $tid, $lid) $session = $transect->activeAnnotationSession; if ($session) { - $query = $session->annotations($auth->user()); + $query = Annotation::allowedBySession($session, $auth->user()); } else { $query = Annotation::query(); } From dbed58b968b9be107a42aa1a023f0f118733b4b2 Mon Sep 17 00:00:00 2001 From: Martin Zurowietz Date: Tue, 20 Sep 2016 09:21:11 +0200 Subject: [PATCH 4/9] Improve APIdoc for endpoints affected by annotation sessions --- src/Http/Controllers/Api/AteController.php | 2 +- src/Http/Controllers/Api/TransectsAnnotationsController.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Http/Controllers/Api/AteController.php b/src/Http/Controllers/Api/AteController.php index c73604db..13dd5b6c 100644 --- a/src/Http/Controllers/Api/AteController.php +++ b/src/Http/Controllers/Api/AteController.php @@ -26,7 +26,7 @@ class AteController extends Controller * @apiName ShowAnnotationPatch * @apiParam {Number} id The annotation ID. * @apiPermission projectMember - * @apiDescription Responds with an image file + * @apiDescription Responds with an image file. If there is an active annotation session, access to annotations hidden by the session is denied. * * @param int $id * @return \Illuminate\Http\Response diff --git a/src/Http/Controllers/Api/TransectsAnnotationsController.php b/src/Http/Controllers/Api/TransectsAnnotationsController.php index fb315391..9bd0530a 100644 --- a/src/Http/Controllers/Api/TransectsAnnotationsController.php +++ b/src/Http/Controllers/Api/TransectsAnnotationsController.php @@ -20,7 +20,7 @@ class TransectsAnnotationsController extends Controller * @apiParam {Number} lit The Label ID * @apiParam (Optional arguments) {Number} take Number of annotations to return. If this parameter is present, the most recent annotations will be returned first. Default is unlimited and unordered. * @apiPermission projectMember - * @apiDescription Returns a list of annotation IDs + * @apiDescription Returns a list of annotation IDs. If there is an active annotation session, images with annotations hidden by the session are not returned. * * @param Request $request * @param Guard $auth From 38cbb78a5d5b3820b20093111c52d977e2a92a6d Mon Sep 17 00:00:00 2001 From: Martin Zurowietz Date: Fri, 23 Sep 2016 10:19:52 +0200 Subject: [PATCH 5/9] Add annotation session indicator to navbar --- src/resources/views/index.blade.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/src/resources/views/index.blade.php b/src/resources/views/index.blade.php index ebf00a5e..bb9853e5 100644 --- a/src/resources/views/index.blade.php +++ b/src/resources/views/index.blade.php @@ -19,20 +19,7 @@ @section('navbar') @endsection From dc8e975a76fa804fc581d82299d116b22db01b4d Mon Sep 17 00:00:00 2001 From: Martin Zurowietz Date: Wed, 28 Sep 2016 09:26:47 +0200 Subject: [PATCH 6/9] Fix link style of transect Ate breadcrumbs --- src/resources/views/index.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/resources/views/index.blade.php b/src/resources/views/index.blade.php index bb9853e5..78ea601c 100644 --- a/src/resources/views/index.blade.php +++ b/src/resources/views/index.blade.php @@ -19,7 +19,7 @@ @section('navbar') @endsection From 67abf2f0ff5dca5694df13c62d45082cd37fc742 Mon Sep 17 00:00:00 2001 From: Martin Zurowietz Date: Wed, 28 Sep 2016 09:27:20 +0200 Subject: [PATCH 7/9] Use projects show toolbar for link to project Ate view --- src/AteServiceProvider.php | 2 +- src/resources/views/projectsShow.blade.php | 8 -------- src/resources/views/projectsShowToolbar.blade.php | 1 + 3 files changed, 2 insertions(+), 9 deletions(-) delete mode 100644 src/resources/views/projectsShow.blade.php create mode 100644 src/resources/views/projectsShowToolbar.blade.php diff --git a/src/AteServiceProvider.php b/src/AteServiceProvider.php index c10a88b1..3176bac5 100644 --- a/src/AteServiceProvider.php +++ b/src/AteServiceProvider.php @@ -45,7 +45,7 @@ public function boot(Modules $modules, Router $router) $modules->addMixin('ate', 'annotationsStyles'); $modules->addMixin('ate', 'annotationsSidebar'); $modules->addMixin('ate', 'annotationsSettings'); - $modules->addMixin('ate', 'projectsShow'); + $modules->addMixin('ate', 'projectsShowToolbar'); } /** diff --git a/src/resources/views/projectsShow.blade.php b/src/resources/views/projectsShow.blade.php deleted file mode 100644 index 92192138..00000000 --- a/src/resources/views/projectsShow.blade.php +++ /dev/null @@ -1,8 +0,0 @@ -
-
- ATE -
- -
diff --git a/src/resources/views/projectsShowToolbar.blade.php b/src/resources/views/projectsShowToolbar.blade.php new file mode 100644 index 00000000..f3004066 --- /dev/null +++ b/src/resources/views/projectsShowToolbar.blade.php @@ -0,0 +1 @@ + ATE re-evaluation From c0762cb26818096cea43466493dba7e5061428d8 Mon Sep 17 00:00:00 2001 From: Martin Zurowietz Date: Thu, 13 Oct 2016 15:40:40 +0200 Subject: [PATCH 8/9] Implement a/s users for transect label filter enpoint --- .../Api/TransectsAnnotationsController.php | 5 +++-- ...rollersApiTransectsAnnotationsControllerTest.php | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/Http/Controllers/Api/TransectsAnnotationsController.php b/src/Http/Controllers/Api/TransectsAnnotationsController.php index 9bd0530a..4d3686e2 100644 --- a/src/Http/Controllers/Api/TransectsAnnotationsController.php +++ b/src/Http/Controllers/Api/TransectsAnnotationsController.php @@ -35,10 +35,11 @@ public function filter(Request $request, Guard $auth, $tid, $lid) $this->validate($request, ['take' => 'integer']); $take = $request->input('take'); - $session = $transect->activeAnnotationSession; + $user = $auth->user(); + $session = $transect->getActiveAnnotationSession($user); if ($session) { - $query = Annotation::allowedBySession($session, $auth->user()); + $query = Annotation::allowedBySession($session, $user); } else { $query = Annotation::query(); } diff --git a/tests/Http/Controllers/Api/AteModuleHttpControllersApiTransectsAnnotationsControllerTest.php b/tests/Http/Controllers/Api/AteModuleHttpControllersApiTransectsAnnotationsControllerTest.php index 33c1ac91..2d96d513 100644 --- a/tests/Http/Controllers/Api/AteModuleHttpControllersApiTransectsAnnotationsControllerTest.php +++ b/tests/Http/Controllers/Api/AteModuleHttpControllersApiTransectsAnnotationsControllerTest.php @@ -101,6 +101,8 @@ public function testFilterAnnotationSession() 'hide_other_users_annotations' => false, ]); + $session->users()->attach($this->editor()); + $expect = [$a2->id, $a3->id]; if ($this->isSqlite()) { $expect = array_map('strval', $expect); @@ -136,5 +138,16 @@ public function testFilterAnnotationSession() $this->get("/api/v1/transects/{$id}/annotations/filter/label/{$l1->label_id}"); $this->assertResponseOk(); $this->seeJsonEquals($expect); + + $session->users()->detach($this->editor()); + + $expect = [$a1->id, $a2->id, $a3->id]; + if ($this->isSqlite()) { + $expect = array_map('strval', $expect); + } + + $this->get("/api/v1/transects/{$id}/annotations/filter/label/{$l1->label_id}"); + $this->assertResponseOk(); + $this->seeJsonEquals($expect); } } From 96567cb465d727d622c5ce1dc5cbb2cdaed485c0 Mon Sep 17 00:00:00 2001 From: Martin Zurowietz Date: Thu, 20 Oct 2016 15:11:17 +0200 Subject: [PATCH 9/9] Update required version of dias/transects --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 6dcf0733..e8054ed9 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "dias/ate", "require": { - "dias/transects": "~1.0", + "dias/transects": "~1.1", "intervention/image": "~2.1" }, "require-dev": {