Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

refactored ical url API

Jonas Jabari2705279c7ac1
diff --git a/lib/api/v3/queries/ical_url/query_ical_url_api.rb b/lib/api/v3/queries/ical_url/query_ical_url_api.rb
new file mode 100644
index 00000000000..0c9e3b8117e
--- /dev/null
+++ b/lib/api/v3/queries/ical_url/query_ical_url_api.rb
@@ -0,0 +1,76 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'api/v3/queries/ical_url/query_ical_url_representer'
+
+module API
+ module V3
+ module Queries
+ module ICalUrl
+ # API inflection rule prevents ICal inflection rule to be used in this context
+ class QueryIcalUrlAPI < ::API::OpenProjectAPI
+ namespace :ical_url do
+
+ # before do
+ # raise API::Errors::NotFound unless OpenProject::Configuration.ical_subscriptions_enabled?
+ # end
+
+ params do
+ requires :token_name, type: String, desc: 'The name which should be used for the ical token'
+ end
+
+ post do
+ authorize_by_policy(:share_via_ical)
+ # TODO: write test for this
+
+ # TODO: currently the generated URL points to controller action in calendar module
+ # correct approach? or should it be implemented as a API here?
+ call = ::Calendar::GenerateICalUrl.new.call(
+ user: current_user,
+ query_id: @query.id,
+ project_id: @query.project_id,
+ token_name: params[:token_name]
+ )
+
+ if call.failure?
+ fail ::API::Errors::ErrorBase.create_and_merge_errors(call.errors)
+ end
+
+ status 201
+
+ QueryICalUrlRepresenter.new(
+ OpenStruct.new(ical_url: call.result, query: @query),
+ current_user:
+ )
+ end
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/queries/ical_url/query_ical_url_representer.rb b/lib/api/v3/queries/ical_url/query_ical_url_representer.rb
new file mode 100644
index 00000000000..3b5afe5fe04
--- /dev/null
+++ b/lib/api/v3/queries/ical_url/query_ical_url_representer.rb
@@ -0,0 +1,68 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 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 COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'roar/decorator'
+require 'roar/json/hal'
+
+module API
+ module V3
+ module Queries
+ module ICalUrl
+ class QueryICalUrlRepresenter < ::API::Decorators::Single
+
+ link :self do
+ {
+ href: api_v3_paths.query_ical_url(represented.query.id),
+ method: :post
+ }
+ end
+
+ link :icalUrl do
+ # TODO: this is returning an absolute url
+ # --> valid approach to render it as href in a link?
+ {
+ href: represented.ical_url,
+ method: :get
+ }
+ end
+
+ link :query do
+ {
+ href: api_v3_paths.query(represented.query.id),
+ method: :get
+ }
+ end
+
+ def _type
+ 'QueryIcalUrl'
+ end
+ end
+ end
+ end
+ end
+end
diff --git a/lib/api/v3/queries/queries_api.rb b/lib/api/v3/queries/queries_api.rb
index 6368074df0f..af52182065e 100644
--- a/lib/api/v3/queries/queries_api.rb
+++ b/lib/api/v3/queries/queries_api.rb
@@ -159,57 +159,7 @@ module API
.mount
end
- namespace :ical_url do
- post do
-
- params do
- requires :token_name, type: String, desc: 'The name which should be used for the ical token'
- end
-
- authorize_by_policy(:share_via_ical)
-
- # TODO: use representer in order to properly generate following response
- # TODO: write test for this
-
- # currently the generated URL points to controller action in calendar module
- # correct approach? or should it be implemented as a API here?
- call = ::Calendar::GenerateICalUrl.new.call(
- user: current_user,
- query_id: @query.id,
- project_id: @query.project_id,
- token_name: params[:token_name]
- )
-
- if call.success?
- {
- "_type": "QueryICalUrl",
- "_links": {
- "self": {
- "href": api_v3_paths.query_ical_url(@query.id)
- },
- "query": {
- "href": api_v3_paths.query(@query.id)
- },
- "icalUrl": {
- "href": call.result,
- "format": "text/calendar"
- }
- }
- }
- else
- error!({
- "_type": "Error",
- "errorIdentifier": "urn:openproject-org:api:v3:errors:PropertyConstraintViolation",
- "message": call.message,
- "_embedded": {
- "details": {
- "attribute": "name"
- }
- }
- })
- end
- end
- end
+ mount API::V3::Queries::ICalUrl::QueryIcalUrlAPI
mount API::V3::Queries::Order::QueryOrderAPI
end
diff --git a/modules/calendar/app/models/ical_token_query_assignment.rb b/modules/calendar/app/models/ical_token_query_assignment.rb
index 0fb3b058db9..db69f3678a4 100644
--- a/modules/calendar/app/models/ical_token_query_assignment.rb
+++ b/modules/calendar/app/models/ical_token_query_assignment.rb
@@ -50,7 +50,13 @@ class ICalTokenQueryAssignment < ApplicationRecord
#
# when the ical_token and assignment are already created
# we can access the user_id of the ical_token directly instead
- ical_token&.user_id || user_id
+ result = ical_token&.user_id || user_id
+
+ if result.nil?
+ raise "No user_id given while saving the ical_token_query_assignment"
+ else
+ result
+ end
end
def unique_name_per_user_and_query
diff --git a/modules/calendar/app/services/calendar/generate_ical_url.rb b/modules/calendar/app/services/calendar/generate_ical_url.rb
index 32ecbbc0fae..7fbe0bd8eeb 100644
--- a/modules/calendar/app/services/calendar/generate_ical_url.rb
+++ b/modules/calendar/app/services/calendar/generate_ical_url.rb
@@ -30,22 +30,27 @@ module Calendar
class GenerateICalUrl < ::BaseServices::BaseCallable
def perform(user:, query_id:, project_id:, token_name:)
- begin
- new_ical_token = create_ical_token(user, query_id, token_name)
- new_ical_url = create_ical_url(query_id, project_id, new_ical_token)
+ new_ical_token = create_ical_token(user, query_id, token_name)
+
+ if new_ical_token.errors.any?
+ ServiceResult.failure(errors: new_ical_token.errors)
+ else
+ new_ical_token_value = new_ical_token.plain_value
+ new_ical_url = create_ical_url(query_id, project_id, new_ical_token_value)
ServiceResult.success(result: new_ical_url)
- rescue => e
- # in case a the query is not found or the ical_token could not be created
- # an exception is raised which needs to be passed as a failure here
- ServiceResult.failure(message: e.message)
end
end
protected
- def create_ical_token(user, query_id, token_name)
+ def create_ical_token(user, query_id, name)
query = Query.find(query_id)
- Token::ICal.create_and_return_value(user, query, token_name)
+
+ Token::ICal.create(user:,
+ ical_token_query_assignment_attributes: {
+ query:, name:, user_id: user.id
+ }
+ )
end
def create_ical_url(query_id, project_id, ical_token)