mirror of
https://github.com/opf/openproject.git
synced 2026-06-14 03:30:14 +00:00
Derive comment attachment claims from rich text content
Claim inline attachments that are actually in use- leave any previosly uploaded but unclaimed attachemnts for clean up (later)- this way we don't clog up storage with unused image files
This commit is contained in:
@@ -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
|
||||
end
|
||||
@@ -328,10 +328,9 @@ class WorkPackages::ActivitiesTabController < ApplicationController
|
||||
end
|
||||
|
||||
def 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)
|
||||
.call
|
||||
end
|
||||
|
||||
def generate_time_based_update_streams(last_update_timestamp)
|
||||
|
||||
+51
@@ -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
|
||||
end
|
||||
+67
@@ -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
|
||||
end
|
||||
+103
@@ -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
|
||||
Reference in New Issue
Block a user