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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Derive comment attachment claims from rich text content
Kabiru Mwenja5bf56b9ffba7
diff --git a/app/contracts/work_packages/activities_tab/comment_attachments_claims_contract.rb b/app/contracts/work_packages/activities_tab/comment_attachments_claims_contract.rbnew file mode 100644index 00000000000..0a16fe37d02--- /dev/null+++ b/app/contracts/work_packages/activities_tab/comment_attachments_claims_contract.rb@@ -0,0 +1,33 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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.+#++++class WorkPackages::ActivitiesTab::CommentAttachmentsClaimsContract < ModelContract+ include ::Attachments::ValidateReplacements+enddiff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex ccd240b741a..04143ffcf08 100644--- a/app/controllers/work_packages/activities_tab_controller.rb+++ b/app/controllers/work_packages/activities_tab_controller.rb@@ -328,10 +328,9 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef claim_journal_attachments_for(journal)- return if (attachment_ids = journal_params[:attachment_ids]).blank?-- journal.attachments = Attachment.where(author: User.current, id: attachment_ids, container: nil)- journal.save+ WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService+ .new(user: User.current, model: journal)+ .callenddef generate_time_based_update_streams(last_update_timestamp)diff --git a/app/services/work_packages/activities_tab/comment_attachments_claims/claims_service.rb b/app/services/work_packages/activities_tab/comment_attachments_claims/claims_service.rbnew file mode 100644index 00000000000..57bf9f51dbd--- /dev/null+++ b/app/services/work_packages/activities_tab/comment_attachments_claims/claims_service.rb@@ -0,0 +1,51 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 WorkPackages+ module ActivitiesTab+ module CommentAttachmentsClaims+ class ClaimsService < BaseServices::Update+ def persist(service_result)+ if service_result.result.attachments_replacements.present?+ service_result.result.attachments = service_result.result.attachments_replacements+ end++ super+ end++ private++ def default_contract_class+ WorkPackages::ActivitiesTab::CommentAttachmentsClaimsContract+ end+ end+ end+ end+enddiff --git a/app/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service.rb b/app/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service.rbnew file mode 100644index 00000000000..5bfa98b5d29--- /dev/null+++ b/app/services/work_packages/activities_tab/comment_attachments_claims/set_attributes_service.rb@@ -0,0 +1,67 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 WorkPackages+ module ActivitiesTab+ module CommentAttachmentsClaims+ class SetAttributesService < ::BaseServices::SetAttributes+ include ::Attachments::SetReplacements++ def perform(params = {})+ super(+ params.reverse_merge(+ attachment_ids: collect_attachment_ids_from_notes+ )+ )+ end++ private++ def collect_attachment_ids_from_notes+ return [] if model.notes.blank?++ parser.css("img.op-uc-image").filter_map do |img|+ src = img["src"]+ next if src.blank?++ # Extract the attachment ID from the src URL+ # Example: "/api/v3/attachments/30381/content" -> "30381"+ match = src.match(%r{/attachments/(\d+)/content})+ match[1] if match+ end+ end++ def parser+ @parser ||= Nokogiri::HTML.fragment(model.notes)+ end+ end+ end+ end+enddiff --git a/spec/services/work_packages/activities_tab/comment_attachments_claims/claims_service_spec.rb b/spec/services/work_packages/activities_tab/comment_attachments_claims/claims_service_spec.rbnew file mode 100644index 00000000000..1d00a5a50d5--- /dev/null+++ b/spec/services/work_packages/activities_tab/comment_attachments_claims/claims_service_spec.rb@@ -0,0 +1,103 @@+# frozen_string_literal: true++#-- copyright+# OpenProject is an open source project management software.+# Copyright (C) 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 "spec_helper"+require "services/base_services/behaves_like_update_service"++RSpec.describe WorkPackages::ActivitiesTab::CommentAttachmentsClaims::ClaimsService do+ shared_let(:user) { create(:user) }+ shared_let(:work_package) { create(:work_package, author: user) }++ it_behaves_like "BaseServices update service" do+ let(:model_instance) { build_stubbed(:work_package_journal, journable: work_package, version: 2, notes: "") }+ let(:set_attributes_class) { WorkPackages::ActivitiesTab::CommentAttachmentsClaims::SetAttributesService }+ let(:contract_class) { WorkPackages::ActivitiesTab::CommentAttachmentsClaimsContract }+ end++ describe "#call" do+ context "when the journal has no notes" do+ let(:journal_without_notes) { create(:work_package_journal, journable: work_package, version: 2, notes: "") }++ subject(:attachment_claims_service) do+ described_class.new(+ user:,+ model: journal_without_notes+ )+ end++ it "does not claim any attachments" do+ claim_result = attachment_claims_service.call+ expect(claim_result).to be_success++ expect(journal_without_notes.reload.attachments).to be_empty+ end+ end++ context "when the journal has notes with attachments" do+ shared_let(:attachment1) { create(:attachment, author: user, container: nil) }+ shared_let(:attachment2) { create(:attachment, author: user, container: nil) }+ shared_let(:attachment3) { create(:attachment, author: user, container: nil) }++ let(:journal_with_attachments) { create(:work_package_journal, journable: work_package, version: 3, notes:) }++ let(:notes) do+ <<~HTML+ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment1.id}/content">++ First attachment++ <br>++ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment2.id}/content">++ Second attachment++ <img class="op-uc-image op-uc-image_inline" src="/api/v3/attachments/#{attachment3.id}/content">++ Third attachment+ HTML+ end++ subject(:attachment_claims_service) do+ described_class.new(+ user:,+ model: journal_with_attachments+ )+ end++ it "claims the attachments" do+ claim_result = attachment_claims_service.call+ expect(claim_result).to be_success++ expect(journal_with_attachments.reload.attachments).to contain_exactly(attachment1, attachment2, attachment3)+ end+ end+ end+end