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

WIP: introducing ics endpoint for project calendars, using api_token for test purposes

Jonas Jabaric730ce7b1e20
diff --git a/Gemfile.lock b/Gemfile.lock
index 97209217843..7931cb4e3c6 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -72,6 +72,7 @@ PATH
remote: modules/calendar
specs:
openproject-calendar (1.0.0)
+ icalendar (~> 2.8.0)
PATH
remote: modules/costs
diff --git a/modules/calendar/app/controllers/calendar/calendars_controller.rb b/modules/calendar/app/controllers/calendar/calendars_controller.rb
index cb0d7df5b8b..e9b920f091c 100644
--- a/modules/calendar/app/controllers/calendar/calendars_controller.rb
+++ b/modules/calendar/app/controllers/calendar/calendars_controller.rb
@@ -28,8 +28,8 @@
module ::Calendar
class CalendarsController < ApplicationController
- before_action :find_optional_project
- before_action :authorize
+ before_action :find_optional_project, except: %i[ical]
+ before_action :authorize, except: %i[ical]
before_action :find_calendar, only: %i[destroy]
menu_item :calendar_view
@@ -42,6 +42,25 @@ module ::Calendar
render layout: 'angular/angular'
end
+ def ical
+ begin
+ call = ::Calendar::IcalResponseService.new().call(
+ ical_token: params[:ical_token],
+ query_id: params[:id]
+ )
+ rescue ActiveRecord::RecordNotFound
+ render_404
+ return
+ end
+
+ if call.present? && call.success?
+ render plain: call.result
+ else
+ render_404
+ end
+
+ end
+
def destroy
if @view.destroy
flash[:notice] = t(:notice_successful_delete)
diff --git a/modules/calendar/app/services/calendar/create_ical_service.rb b/modules/calendar/app/services/calendar/create_ical_service.rb
new file mode 100644
index 00000000000..9fcadfa6e4d
--- /dev/null
+++ b/modules/calendar/app/services/calendar/create_ical_service.rb
@@ -0,0 +1,113 @@
+#-- 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 'icalendar'
+
+module Calendar
+ class CreateIcalService < ::BaseServices::BaseCallable
+ include OpenProject::StaticRouting::UrlHelpers
+
+ def perform(work_packages:)
+ ical_string = create_ical_string(work_packages)
+
+ ServiceResult.success(result: ical_string)
+ end
+
+ protected
+
+ def create_ical_string(work_packages)
+ calendar = Icalendar::Calendar.new
+
+ calendar.prodid = "-//OpenProject GmbH//OpenProject Core Project//EN"
+ calendar.ip_method = "PUBLISH"
+ calendar.x_wr_calname = "OpenProject Calendar"
+
+ work_packages&.each do |work_package|
+ next if work_package.due_date.nil?
+
+ event = Icalendar::Event.new
+ event.uid = "#{work_package.id}@#{host}"
+ event.summary = work_package.name
+ event.dtstart = Icalendar::Values::Date.new(work_package.start_date)
+ event.dtend = Icalendar::Values::Date.new(work_package.due_date + 1.day)
+ event.location = work_package_url(work_package)
+ event.description = description_value(work_package)
+
+ calendar.add_event(event)
+ end
+
+ calendar.to_ical
+ end
+
+ def work_package_url work_package
+ url_for(
+ controller: :work_packages,
+ action: :show,
+ id: work_package.id,
+ only_path: false,
+ protocol: protocol,
+ host: host
+ )
+ end
+
+ # implementation taken from application_mailer
+ def host
+ if OpenProject::Configuration.rails_relative_url_root.blank?
+ Setting.host_name
+ else
+ Setting.host_name.to_s.gsub(%r{/.*\z}, '')
+ end
+ end
+
+ # implementation taken from application_mailer
+ def protocol
+ Setting.protocol
+ end
+
+ # # url_for wants to access the controller method, which we do not have in our service class.
+ # # see: http://stackoverflow.com/questions/3659455/is-there-a-new-syntax-for-url-for-in-rails-3
+ # def controller
+ # nil
+ # end
+
+ def description_value work_package
+ # TODO: Check if n+1 queries are caused by following code
+ project = work_package.project.name
+ type = "🟩 #{work_package.type.name}"
+ status = work_package.status.name
+ assignee = work_package.assigned_to
+ priority = "🟢 #{work_package.priority.name}"
+ description = "Work package description available at: #{work_package_url(work_package)}"
+
+ [
+ project, type, status, assignee, priority, description
+ ].join("\n")
+ end
+
+ end
+end
diff --git a/modules/calendar/app/services/calendar/ical_response_service.rb b/modules/calendar/app/services/calendar/ical_response_service.rb
new file mode 100644
index 00000000000..c0fab83a03a
--- /dev/null
+++ b/modules/calendar/app/services/calendar/ical_response_service.rb
@@ -0,0 +1,95 @@
+#-- 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.
+#++
+
+
+module Calendar
+ class IcalResponseService < ::BaseServices::BaseCallable
+
+ def perform(ical_token:, query_id:)
+ # STEP 1 -> TODO: simple time based caching -> recreate ics only every x minute(s)
+ ical_string = resolve_from_cache(ical_token, query_id)
+
+ if ical_string.present?
+ ServiceResult.success(result: ical_string)
+ return
+ end
+
+ # if not resolved from cache, proceed:
+
+ # STEP 2 -> Resolve user by token
+ user = resolve_user_by_token(ical_token)
+
+ # STEP 3 -> Resolve work_packages and authorize user
+ work_packages = resolve_work_packages(user, query_id)
+
+ # STEP 4 -> Resolve work_packages and authorize user
+ ical_string = create_ical_string(work_packages)
+
+ if ical_string.present?
+ ServiceResult.success(result: ical_string)
+ else
+ ServiceResult.failure
+ end
+ end
+
+ protected
+
+ def resolve_from_cache(ical_token, query_id)
+ # not implemented yet
+ end
+
+ def resolve_user_by_token(ical_token)
+ call = ::Calendar::ResolveIcalUserService.new().call(
+ ical_token: ical_token
+ )
+ user = call.result if call.success?
+
+ user
+ end
+
+ def resolve_work_packages(user, query_id)
+ call = ::Calendar::ResolveWorkPackagesService.new().call(
+ user: user,
+ query_id: query_id
+ )
+ work_packages = call.result if call.success?
+
+ work_packages
+ end
+
+ def create_ical_string(work_packages)
+ call = ::Calendar::CreateIcalService.new().call(
+ work_packages: work_packages
+ )
+ ical_string = call.result if call.success?
+
+ ical_string
+ end
+
+ end
+end
diff --git a/modules/calendar/app/services/calendar/resolve_ical_user_service.rb b/modules/calendar/app/services/calendar/resolve_ical_user_service.rb
new file mode 100644
index 00000000000..17f01ec577b
--- /dev/null
+++ b/modules/calendar/app/services/calendar/resolve_ical_user_service.rb
@@ -0,0 +1,44 @@
+#-- 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.
+#++
+
+module Calendar
+ class ResolveIcalUserService < ::BaseServices::BaseCallable
+
+ def perform(ical_token:)
+ # TODO: do it properly with new ICAL token
+ token = Token::API.find_by_plaintext_value(ical_token)
+ if token.present?
+ user = token.user
+ ServiceResult.success(result: user)
+ else
+ raise ActiveRecord::RecordNotFound
+ end
+ end
+
+ end
+end
diff --git a/modules/calendar/app/services/calendar/resolve_work_packages_service.rb b/modules/calendar/app/services/calendar/resolve_work_packages_service.rb
new file mode 100644
index 00000000000..4cf256d77bd
--- /dev/null
+++ b/modules/calendar/app/services/calendar/resolve_work_packages_service.rb
@@ -0,0 +1,62 @@
+#-- 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.
+#++
+
+module Calendar
+ class ResolveWorkPackagesService < ::BaseServices::BaseCallable
+
+ def perform(user:, query_id:)
+ query = resolve_query(user, query_id)
+ work_packages = resolve_work_packages(query)
+
+ if work_packages.present?
+ ServiceResult.success(result: work_packages)
+ else
+ ServiceResult.failure
+ end
+ end
+
+ protected
+
+ def resolve_query(user, query_id)
+ Query
+ .visible(user) # authorization
+ .find(query_id)
+ end
+
+ def resolve_work_packages(query)
+ unless(
+ query.nil? ||
+ query.results.nil? ||
+ query.results.work_packages.nil?
+ )
+ work_packages = query.results.work_packages
+ end
+ end
+
+ end
+end
diff --git a/modules/calendar/config/routes.rb b/modules/calendar/config/routes.rb
index ef116593810..c4a09221a2e 100644
--- a/modules/calendar/config/routes.rb
+++ b/modules/calendar/config/routes.rb
@@ -5,6 +5,8 @@ OpenProject::Application.routes.draw do
only: %i[index destroy],
as: :calendars do
get '/new' => 'calendar/calendars#show', on: :collection, as: 'new'
+ # TODO: discuss if other controller should be used
+ get '/ics' => 'calendar/calendars#ical', on: :member, as: 'ical'
get '(/*state)' => 'calendar/calendars#show', on: :member, as: ''
end
end
diff --git a/modules/calendar/openproject-calendar.gemspec b/modules/calendar/openproject-calendar.gemspec
index e068f24351b..abf38ebc867 100644
--- a/modules/calendar/openproject-calendar.gemspec
+++ b/modules/calendar/openproject-calendar.gemspec
@@ -8,5 +8,6 @@ Gem::Specification.new do |s|
s.license = 'GPLv3'
s.files = Dir['{app,config,db,lib}/**/*']
+ s.add_dependency 'icalendar', '~> 2.8.0'
s.metadata['rubygems_mfa_required'] = 'true'
end