forked from zammad/zammad
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes zammad#2644 - Including Knowledge Base Answers into Ticket Arti…
…cles doesn't attach Attachments
- Loading branch information
1 parent
3306de2
commit 880f1d5
Showing
14 changed files
with
226 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
module CanCloneAttachments | ||
extend ActiveSupport::Concern | ||
|
||
=begin | ||
clone existing attachments of article to the target object | ||
article_parent = Ticket::Article.find(123) | ||
article_new = Ticket::Article.find(456) | ||
attached_attachments = article_parent.clone_attachments(article_new.class.name, article_new.id, only_attached_attachments: true) | ||
inline_attachments = article_parent.clone_attachments(article_new.class.name, article_new.id, only_inline_attachments: true) | ||
returns | ||
[attachment1, attachment2, ...] | ||
=end | ||
|
||
def clone_attachments(object_type, object_id, options = {}) | ||
existing_attachments = Store.list( | ||
object: object_type, | ||
o_id: object_id, | ||
) | ||
|
||
is_html_content = false | ||
if content_type.present? && content_type =~ %r{text/html}i | ||
is_html_content = true | ||
end | ||
|
||
new_attachments = [] | ||
attachments.each do |new_attachment| | ||
next if new_attachment.preferences['content-alternative'] == true | ||
|
||
# only_attached_attachments mode is used by apply attached attachments to forwared article | ||
if options[:only_attached_attachments] == true | ||
if is_html_content == true | ||
|
||
content_id = new_attachment.preferences['Content-ID'] || new_attachment.preferences['content_id'] | ||
next if content_id.present? && body.present? && body.match?(/#{Regexp.quote(content_id)}/i) | ||
end | ||
end | ||
|
||
# only_inline_attachments mode is used when quoting HTML mail with #{article.body_as_html} | ||
if options[:only_inline_attachments] == true | ||
next if is_html_content == false | ||
next if body.blank? | ||
|
||
content_disposition = new_attachment.preferences['Content-Disposition'] || new_attachment.preferences['content_disposition'] | ||
next if content_disposition.present? && content_disposition !~ /inline/ | ||
|
||
content_id = new_attachment.preferences['Content-ID'] || new_attachment.preferences['content_id'] | ||
next if content_id.blank? | ||
next if !body.match?(/#{Regexp.quote(content_id)}/i) | ||
end | ||
|
||
already_added = false | ||
existing_attachments.each do |existing_attachment| | ||
next if existing_attachment.filename != new_attachment.filename || existing_attachment.size != new_attachment.size | ||
|
||
already_added = true | ||
break | ||
end | ||
next if already_added == true | ||
|
||
file = Store.add( | ||
object: object_type, | ||
o_id: object_id, | ||
data: new_attachment.content, | ||
filename: new_attachment.filename, | ||
preferences: new_attachment.preferences, | ||
) | ||
new_attachments.push file | ||
end | ||
|
||
new_attachments | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
app/policies/controllers/knowledge_base/answer/attachments_controller_policy.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
class Controllers::KnowledgeBase::Answer::AttachmentsControllerPolicy < Controllers::ApplicationControllerPolicy | ||
permit! :clone_to_form, to: 'knowledge_base.*' | ||
default_permit!('knowledge_base.editor') | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
spec/requests/knowledge_base/answer_attachments_cloning_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'KnowledgeBase answer attachments cloning', type: :request, authenticated_as: :agent_user do | ||
include_context 'basic Knowledge Base' do | ||
before do | ||
published_answer | ||
end | ||
end | ||
|
||
it 'copies to given UploadCache' do | ||
form_id = Random.rand(999..9999) | ||
endpoint = "/api/v1/knowledge_bases/#{knowledge_base.id}/answers/#{published_answer.id}/attachments/clone_to_form" | ||
params = { form_id: form_id } | ||
|
||
expect { post endpoint, params: params } | ||
.to change { Store.list(object: 'UploadCache', o_id: form_id).count } | ||
.from(0) | ||
.to(1) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.