From 34467120c32e842765fc71495a1565fa78510fd3 Mon Sep 17 00:00:00 2001 From: ulferts Date: Mon, 9 Aug 2021 17:22:00 +0200 Subject: [PATCH] introduce resourceId and resourceType filter These filters in combination allow filtering notifications via the associated resource, e.g. a work package. When only using the id filter, due to the polymorphic association of the resource, it will not be guaranteed to only return the desired notifications. A different resource, e.g. a meeting might have the same id. But using the resourceType filter by itself also makes sense so that a client can fetch only notifications associated to a subset of resources, e.g. work packages. --- app/models/queries/notifications.rb | 26 ++++++------ .../filters/resource_id_filter.rb | 41 +++++++++++++++++++ .../filters/resource_type_filter.rb | 39 ++++++++++++++++++ .../v3/notifications/index_resource_spec.rb | 39 ++++++++++++++++-- 4 files changed, 129 insertions(+), 16 deletions(-) create mode 100644 app/models/queries/notifications/filters/resource_id_filter.rb create mode 100644 app/models/queries/notifications/filters/resource_type_filter.rb diff --git a/app/models/queries/notifications.rb b/app/models/queries/notifications.rb index 40143482e5..a7b7acdf97 100644 --- a/app/models/queries/notifications.rb +++ b/app/models/queries/notifications.rb @@ -29,18 +29,18 @@ #++ module Queries::Notifications - Queries::Register.filter Queries::Notifications::NotificationQuery, - Queries::Notifications::Filters::ReadIanFilter + [Queries::Notifications::Filters::ReadIanFilter, + Queries::Notifications::Filters::IdFilter, + Queries::Notifications::Filters::ResourceIdFilter, + Queries::Notifications::Filters::ResourceTypeFilter].each do |filter| + Queries::Register.filter Queries::Notifications::NotificationQuery, + filter + end - Queries::Register.filter Queries::Notifications::NotificationQuery, - Queries::Notifications::Filters::IdFilter - - Queries::Register.order Queries::Notifications::NotificationQuery, - Queries::Notifications::Orders::DefaultOrder - - Queries::Register.order Queries::Notifications::NotificationQuery, - Queries::Notifications::Orders::ReasonOrder - - Queries::Register.order Queries::Notifications::NotificationQuery, - Queries::Notifications::Orders::ReadIanOrder + [Queries::Notifications::Orders::DefaultOrder, + Queries::Notifications::Orders::ReasonOrder, + Queries::Notifications::Orders::ReadIanOrder].each do |order| + Queries::Register.order Queries::Notifications::NotificationQuery, + order + end end diff --git a/app/models/queries/notifications/filters/resource_id_filter.rb b/app/models/queries/notifications/filters/resource_id_filter.rb new file mode 100644 index 0000000000..80f369d733 --- /dev/null +++ b/app/models/queries/notifications/filters/resource_id_filter.rb @@ -0,0 +1,41 @@ +#-- encoding: UTF-8 + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) 2012-2021 the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See docs/COPYRIGHT.rdoc for more details. +#++ + +class Queries::Notifications::Filters::ResourceIdFilter < Queries::Notifications::Filters::NotificationFilter + def allowed_values + WorkPackage + .visible(User.current) + .pluck(:id, :id) + end + + def type + :list + end +end diff --git a/app/models/queries/notifications/filters/resource_type_filter.rb b/app/models/queries/notifications/filters/resource_type_filter.rb new file mode 100644 index 0000000000..f49164354b --- /dev/null +++ b/app/models/queries/notifications/filters/resource_type_filter.rb @@ -0,0 +1,39 @@ +#-- encoding: UTF-8 + +#-- copyright +# OpenProject is an open source project management software. +# Copyright (C) 2012-2021 the OpenProject GmbH +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License version 3. +# +# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows: +# Copyright (C) 2006-2013 Jean-Philippe Lang +# Copyright (C) 2010-2013 the ChiliProject Team +# +# This program is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License +# as published by the Free Software Foundation; either version 2 +# of the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +# +# See docs/COPYRIGHT.rdoc for more details. +#++ + +class Queries::Notifications::Filters::ResourceTypeFilter < Queries::Notifications::Filters::NotificationFilter + def allowed_values + [[WorkPackage.name, WorkPackage.name]] + end + + def type + :list + end +end diff --git a/spec/requests/api/v3/notifications/index_resource_spec.rb b/spec/requests/api/v3/notifications/index_resource_spec.rb index 3d7093da8e..9d1ac2f76d 100644 --- a/spec/requests/api/v3/notifications/index_resource_spec.rb +++ b/spec/requests/api/v3/notifications/index_resource_spec.rb @@ -37,9 +37,14 @@ describe ::API::V3::Notifications::NotificationsAPI, include API::V3::Utilities::PathHelper - shared_let(:recipient) { FactoryBot.create :user } - shared_let(:notification1) { FactoryBot.create :notification, recipient: recipient } - shared_let(:notification2) { FactoryBot.create :notification, recipient: recipient } + shared_let(:work_package) { FactoryBot.create :work_package } + shared_let(:recipient) do + FactoryBot.create :user, + member_in_project: work_package.project, + member_with_permissions: %i[view_work_packages] + end + shared_let(:notification1) { FactoryBot.create :notification, recipient: recipient, resource: work_package } + shared_let(:notification2) { FactoryBot.create :notification, recipient: recipient, resource: work_package } let(:notifications) { [notification1, notification2] } @@ -96,6 +101,34 @@ describe ::API::V3::Notifications::NotificationsAPI, end end end + + context 'with a resource filter' do + let(:notification3) { FactoryBot.create :notification, recipient: recipient } + let(:notifications) { [notification1, notification2, notification3] } + + let(:filters) do + [ + { + 'resourceId' => { + 'operator' => '=', + 'values' => [work_package.id.to_s] + } + }, + { + 'resourceType' => { + 'operator' => '=', + 'values' => [WorkPackage.name.to_s] + } + } + ] + end + + context 'with the filter being set to false' do + it_behaves_like 'API V3 collection response', 2, 2, 'Notification' do + let(:elements) { [notification2, notification1] } + end + end + end end describe 'admin user' do